Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 3ad884e

Browse files
authored
fixes #263 -- remove printk indirection (#267)
1 parent d9a3d83 commit 3ad884e

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const INCLUDED_FUNCTIONS: &[&str] = &[
2323
"wait_for_random_bytes",
2424
"get_random_bytes",
2525
"rng_is_initialized",
26+
"printk",
2627
"add_device_randomness",
2728
];
2829
const INCLUDED_VARS: &[&str] = &[

src/helpers.c

-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
#include <linux/version.h>
55

66

7-
int printk_helper(const unsigned char *s, int len)
8-
{
9-
return printk(KERN_INFO "%.*s", len, (const char *)s);
10-
}
11-
127
void bug_helper(void)
138
{
149
BUG();

src/printk.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use core::cmp;
22
use core::fmt;
33

4+
use crate::bindings;
45
use crate::c_types::c_int;
56

6-
pub struct KernelConsole;
7-
8-
extern "C" {
9-
fn printk_helper(s: *const u8, len: c_int) -> c_int;
10-
}
11-
127
#[doc(hidden)]
138
pub fn printk(s: &[u8]) {
9+
// Don't copy the trailing NUL from `KERN_INFO`.
10+
let mut fmt_str = [0; bindings::KERN_INFO.len() - 1 + b"%.*s\0".len()];
11+
fmt_str[..bindings::KERN_INFO.len() - 1]
12+
.copy_from_slice(&bindings::KERN_INFO[..bindings::KERN_INFO.len() - 1]);
13+
fmt_str[bindings::KERN_INFO.len() - 1..].copy_from_slice(b"%.*s\0");
14+
1415
// TODO: I believe printk never fails
15-
unsafe { printk_helper(s.as_ptr(), s.len() as c_int) };
16+
unsafe { bindings::printk(fmt_str.as_ptr() as _, s.len() as c_int, s.as_ptr()) };
1617
}
1718

1819
// From kernel/print/printk.c

0 commit comments

Comments
 (0)