Skip to content

Commit ed7a268

Browse files
committed
ldd ch04: setting dir - WIP
1 parent d442ec8 commit ed7a268

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

rust/kernel/proc.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,34 @@ pub trait ProcOperations {
112112

113113
/// TBD
114114
pub struct RustProcRegistration {
115-
parent: *mut bindings::proc_dir_entry,
116-
pub dir: *mut bindings::proc_dir_entry,
115+
dir: *mut bindings::proc_dir_entry,
117116
entry: *mut bindings::proc_dir_entry,
118117
_pin: PhantomPinned,
119118
}
120119

121120
impl RustProcRegistration {
122121
/// TBD
123-
pub fn new(parent: *mut bindings::proc_dir_entry) -> Self {
122+
pub fn new(dir: *mut bindings::proc_dir_entry) -> Self {
124123
Self {
125-
parent,
126-
dir: ptr::null_mut(),
124+
dir,
127125
entry: ptr::null_mut(),
128126
_pin: PhantomPinned,
129127
}
130128
}
131129

132130
/// TBD
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)) }?;
135-
Ok(())
131+
pub fn mkdir(
132+
name: &CString,
133+
parent: *mut bindings::proc_dir_entry,
134+
) -> Result<*mut bindings::proc_dir_entry> {
135+
// TODO: setting parent generated panic!!!
136+
//unsafe { from_err_ptr(bindings::proc_mkdir(name.as_char_ptr(), parent)) }
137+
unsafe {
138+
from_err_ptr(bindings::proc_mkdir(
139+
name.as_char_ptr(),
140+
core::ptr::null_mut(),
141+
))
142+
}
136143
}
137144

138145
/// TBD
@@ -173,10 +180,6 @@ unsafe impl Sync for RustProcRegistration {}
173180
impl Drop for RustProcRegistration {
174181
fn drop(&mut self) {
175182
unsafe {
176-
//let entry_name = CString::try_from_fmt(fmt!("{}", PROC_FS_NAME)).unwrap();
177-
//bindings::remove_proc_entry(entry_name.as_char_ptr(), self._parent);
178-
//let parent_name = CString::try_from_fmt(fmt!("{}", SUB_DIR_NAME)).unwrap();
179-
//bindings::remove_proc_entry(_SUB_DIR_NAME.as_char_ptr(), ptr::null_mut());
180183
bindings::proc_remove(self.entry);
181184
bindings::proc_remove(self.dir)
182185
}

samples/rust/rust_ldd04.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl ProcOperations for Token {
9797
}
9898

9999
struct RustProc {
100-
_reg: RustProcRegistration,
100+
_regs: Vec<RustProcRegistration>,
101101
}
102102

103103
impl kernel::Module for RustProc {
@@ -108,15 +108,19 @@ impl kernel::Module for RustProc {
108108
let filename: CString = CString::try_from_fmt(fmt!("proc_fs")).unwrap();
109109
let filename_mul = CString::try_from_fmt(fmt!("proc_fs_mul")).unwrap();
110110

111-
let mut reg = RustProcRegistration::new(core::ptr::null_mut());
112-
reg.mkdir(&dirname)?;
111+
let dir = RustProcRegistration::mkdir(&dirname, core::ptr::null_mut())?;
112+
113+
let mut regs = Vec::new();
114+
115+
let mut reg = RustProcRegistration::new(dir);
113116
reg.register::<Token>(&filename, None)?;
117+
regs.try_push(reg)?;
114118

115-
// set the same directory
116-
let mut reg = RustProcRegistration::new(reg.dir);
119+
let mut reg = RustProcRegistration::new(dir);
117120
reg.register::<Token>(&filename_mul, Some(3))?;
121+
regs.try_push(reg)?;
118122

119-
Ok(Self { _reg: reg })
123+
Ok(Self { _regs: regs })
120124
}
121125
}
122126

0 commit comments

Comments
 (0)