8
8
9
9
use crate :: {
10
10
bindings,
11
- error:: { from_kernel_result , Result } ,
11
+ error:: { from_err_ptr , from_result , Result } ,
12
12
fmt,
13
13
str:: CString ,
14
14
} ;
@@ -23,19 +23,17 @@ impl<T: ProcOperations> ProcOperationsVtable<T> {
23
23
_inode : * mut bindings:: inode ,
24
24
_file : * mut bindings:: file ,
25
25
) -> core:: ffi:: c_int {
26
- from_kernel_result ! {
27
- T :: proc_open( _inode, _file)
28
- }
26
+ from_result ( || T :: proc_open ( _inode, _file) )
29
27
}
30
28
31
29
unsafe extern "C" fn proc_release (
32
30
_inode : * mut bindings:: inode ,
33
31
_file : * mut bindings:: file ,
34
32
) -> core:: ffi:: c_int {
35
- from_kernel_result ! {
33
+ from_result ( || {
36
34
let _ = T :: proc_release ( _inode, _file) ;
37
35
Ok ( 0 )
38
- }
36
+ } )
39
37
}
40
38
41
39
unsafe extern "C" fn proc_read (
@@ -44,19 +42,15 @@ impl<T: ProcOperations> ProcOperationsVtable<T> {
44
42
_size : usize ,
45
43
_ppos : * mut bindings:: loff_t ,
46
44
) -> isize {
47
- from_kernel_result ! {
48
- T :: proc_read( _file, _buf, _size, _ppos)
49
- }
45
+ from_result ( || T :: proc_read ( _file, _buf, _size, _ppos) )
50
46
}
51
47
52
48
unsafe extern "C" fn proc_lseek (
53
49
_file : * mut bindings:: file ,
54
50
_offset : bindings:: loff_t ,
55
51
_whence : core:: ffi:: c_int ,
56
52
) -> bindings:: loff_t {
57
- from_kernel_result ! {
58
- T :: proc_lseek( _file, _offset, _whence)
59
- }
53
+ from_result ( || T :: proc_lseek ( _file, _offset, _whence) )
60
54
}
61
55
62
56
const VTABLE : bindings:: proc_ops = bindings:: proc_ops {
@@ -119,45 +113,40 @@ pub trait ProcOperations {
119
113
120
114
/// TBD
121
115
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 ,
124
119
_pin : PhantomPinned ,
125
120
}
126
121
127
122
impl RustProcRegistration {
128
123
/// 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 {
130
125
Self {
131
- _parent : parent,
132
- _entry : ptr:: null_mut ( ) ,
126
+ parent,
127
+ dir : ptr:: null_mut ( ) ,
128
+ entry : ptr:: null_mut ( ) ,
133
129
_pin : PhantomPinned ,
134
130
}
135
131
}
136
132
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 ) ) } ?;
143
135
Ok ( ( ) )
144
136
}
145
137
146
138
/// 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 < ( ) > {
150
140
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 ( ) ,
153
143
0o644 ,
154
- self . _parent ,
144
+ self . dir ,
155
145
ProcOperationsVtable :: < T > :: build ( ) ,
156
- )
157
- } ;
158
- // TODO: How to check entry?
159
- if entry. is_null ( ) { }
146
+ ) )
147
+ } ?;
160
148
149
+ self . entry = entry;
161
150
Ok ( ( ) )
162
151
}
163
152
}
@@ -168,10 +157,12 @@ unsafe impl Sync for RustProcRegistration {}
168
157
impl Drop for RustProcRegistration {
169
158
fn drop ( & mut self ) {
170
159
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 )
175
166
}
176
167
}
177
168
}
0 commit comments