Skip to content

Commit caeb41d

Browse files
committed
ldd ch04: fix build errors of rust/kernel/proc.rs
1 parent 3e3b2df commit caeb41d

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

rust/kernel/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub mod mm;
5050
pub mod pages;
5151
pub mod prelude;
5252
pub mod print;
53+
pub mod proc;
5354
mod static_assert;
5455
#[doc(hidden)]
5556
pub mod std_vendor;
@@ -62,7 +63,6 @@ pub mod user_ptr;
6263
#[doc(hidden)]
6364
pub use bindings;
6465
pub use macros;
65-
pub use proc;
6666
pub use uapi;
6767

6868
#[doc(hidden)]

rust/kernel/proc.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use crate::{
1010
bindings,
11-
error::{from_kernel_result, Result},
11+
error::{from_err_ptr, from_result, Result},
1212
fmt,
1313
str::CString,
1414
};
@@ -23,19 +23,17 @@ impl<T: ProcOperations> ProcOperationsVtable<T> {
2323
_inode: *mut bindings::inode,
2424
_file: *mut bindings::file,
2525
) -> core::ffi::c_int {
26-
from_kernel_result! {
27-
T::proc_open(_inode, _file)
28-
}
26+
from_result(|| T::proc_open(_inode, _file))
2927
}
3028

3129
unsafe extern "C" fn proc_release(
3230
_inode: *mut bindings::inode,
3331
_file: *mut bindings::file,
3432
) -> core::ffi::c_int {
35-
from_kernel_result! {
33+
from_result(|| {
3634
let _ = T::proc_release(_inode, _file);
3735
Ok(0)
38-
}
36+
})
3937
}
4038

4139
unsafe extern "C" fn proc_read(
@@ -44,19 +42,15 @@ impl<T: ProcOperations> ProcOperationsVtable<T> {
4442
_size: usize,
4543
_ppos: *mut bindings::loff_t,
4644
) -> isize {
47-
from_kernel_result! {
48-
T::proc_read(_file, _buf, _size, _ppos)
49-
}
45+
from_result(|| T::proc_read(_file, _buf, _size, _ppos))
5046
}
5147

5248
unsafe extern "C" fn proc_lseek(
5349
_file: *mut bindings::file,
5450
_offset: bindings::loff_t,
5551
_whence: core::ffi::c_int,
5652
) -> bindings::loff_t {
57-
from_kernel_result! {
58-
T::proc_lseek(_file, _offset, _whence)
59-
}
53+
from_result(|| T::proc_lseek(_file, _offset, _whence))
6054
}
6155

6256
const VTABLE: bindings::proc_ops = bindings::proc_ops {
@@ -119,45 +113,40 @@ pub trait ProcOperations {
119113

120114
/// TBD
121115
pub struct RustProcRegistration {
122-
_parent: *mut bindings::proc_dir_entry,
123-
_entry: *mut bindings::proc_dir_entry,
116+
parent: *mut bindings::proc_dir_entry,
117+
dir: *mut bindings::proc_dir_entry,
118+
entry: *mut bindings::proc_dir_entry,
124119
_pin: PhantomPinned,
125120
}
126121

127122
impl RustProcRegistration {
128123
/// TBD
129-
pub fn new(parent: *mut bindings::proc_dir_entry) -> Self {
124+
pub fn new(parent: *mut bindings::proc_dir_entry, filename: CString) -> Self {
130125
Self {
131-
_parent: parent,
132-
_entry: ptr::null_mut(),
126+
parent,
127+
dir: ptr::null_mut(),
128+
entry: ptr::null_mut(),
133129
_pin: PhantomPinned,
134130
}
135131
}
136132

137-
pub fn mkdir(&self, name: &CString) -> Result<()> {
138-
// TODO: implement!
139-
let parent = bindings::proc_mkdir(
140-
CString::try_from_fmt(fmt!("{}", PROC_FS_NAME))?.as_char_ptr(),
141-
ptr::null_mut(),
142-
);
133+
pub fn mkdir(&mut self, name: &CString) -> Result<()> {
134+
self.dir = unsafe { from_err_ptr(bindings::proc_mkdir(name.as_char_ptr(), self.parent)) }?;
143135
Ok(())
144136
}
145137

146138
/// TBD
147-
pub fn register<T: ProcOperations<OpenData = ()>>(&self) -> Result<()> {
148-
let entry_name = CString::try_from_fmt(fmt!("{}", PROC_FS_NAME))?;
149-
139+
pub fn register<T: ProcOperations<OpenData = ()>>(&mut self, filename: &CString) -> Result<()> {
150140
let entry: *mut bindings::proc_dir_entry = unsafe {
151-
bindings::proc_create(
152-
entry_name.as_char_ptr(),
141+
from_err_ptr(bindings::proc_create(
142+
filename.as_char_ptr(),
153143
0o644,
154-
self._parent,
144+
self.dir,
155145
ProcOperationsVtable::<T>::build(),
156-
)
157-
};
158-
// TODO: How to check entry?
159-
if entry.is_null() {}
146+
))
147+
}?;
160148

149+
self.entry = entry;
161150
Ok(())
162151
}
163152
}
@@ -168,10 +157,12 @@ unsafe impl Sync for RustProcRegistration {}
168157
impl Drop for RustProcRegistration {
169158
fn drop(&mut self) {
170159
unsafe {
171-
let entry_name = CString::try_from_fmt(fmt!("{}", PROC_FS_NAME)).unwrap();
172-
bindings::remove_proc_entry(entry_name.as_char_ptr(), self._parent);
173-
let parent_name = CString::try_from_fmt(fmt!("{}", SUB_DIR_NAME)).unwrap();
174-
bindings::remove_proc_entry(_SUB_DIR_NAME.as_char_ptr(), ptr::null_mut());
160+
//let entry_name = CString::try_from_fmt(fmt!("{}", PROC_FS_NAME)).unwrap();
161+
//bindings::remove_proc_entry(entry_name.as_char_ptr(), self._parent);
162+
//let parent_name = CString::try_from_fmt(fmt!("{}", SUB_DIR_NAME)).unwrap();
163+
//bindings::remove_proc_entry(_SUB_DIR_NAME.as_char_ptr(), ptr::null_mut());
164+
bindings::proc_remove(self.entry);
165+
bindings::proc_remove(self.dir)
175166
}
176167
}
177168
}

samples/rust/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
55
obj-$(CONFIG_SAMPLE_RUST_FS) += rust_fs.o
66
obj-$(CONFIG_SAMPLE_RUST_LDD02) += rust_ldd02.o
77
obj-$(CONFIG_SAMPLE_RUST_LDD03) += rust_ldd03.o
8-
obj-$(CONFIG_SAMPLE_RUST_LDD04) += rust_proc.o
8+
obj-$(CONFIG_SAMPLE_RUST_LDD04) += rust_ldd04.o
99

1010
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/rust_proc.rs renamed to samples/rust/rust_ldd04.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl kernel::Module for RustProc {
106106
pr_info!("{} is loaded\n", name.to_str()?);
107107
pr_info!("proc_show={:#x}\n", proc_show as *mut ffi::c_void as usize);
108108

109-
let reg = RustProcRegistration::new(parent);
109+
let reg = RustProcRegistration::new(core::ptr::null_mut());
110110
reg.mkdir(parent_name)?;
111111
reg.register::<Token>()?;
112112

0 commit comments

Comments
 (0)