@@ -11,12 +11,11 @@ use core::ptr;
11
11
12
12
use kernel:: bindings;
13
13
use kernel:: prelude:: * ;
14
- use kernel:: {
15
- file:: { self , File } ,
16
- io_buffer:: { IoBufferReader , IoBufferWriter } ,
17
- str:: CString ,
18
- sync:: { Arc , ArcBorrow , Mutex , UniqueArc } ,
19
- } ;
14
+ use kernel:: str:: CString ;
15
+
16
+ static SUB_DIR_NAME : & ' static str = "rust_demo" ;
17
+ static PROC_FS_NAME : & ' static str = "rust_proc_fs" ;
18
+ static _PROC_FS_NAME_MUL: & ' static str = "rust_proc_fs_mul" ;
20
19
21
20
module ! {
22
21
type : RustProc ,
@@ -28,13 +27,52 @@ module! {
28
27
29
28
struct RustProc { }
30
29
30
+ impl RustProc {
31
+ unsafe extern "C" fn proc_open (
32
+ _inode : * mut bindings:: inode ,
33
+ _file : * mut bindings:: file ,
34
+ ) -> i32 {
35
+ 0 as i32
36
+ }
37
+
38
+ unsafe extern "C" fn proc_read (
39
+ _file : * mut bindings:: file ,
40
+ _buf : * mut core:: ffi:: c_char ,
41
+ _len : usize ,
42
+ _off : * mut bindings:: loff_t ,
43
+ ) -> isize {
44
+ 0 as isize
45
+ }
46
+ }
47
+
31
48
impl kernel:: Module for RustProc {
32
49
fn init ( name : & ' static CStr , _module : & ' static ThisModule ) -> Result < Self > {
33
50
pr_info ! ( "{} is loaded\n " , name. to_str( ) ?) ;
34
51
35
52
unsafe {
36
- let parent_dir = CString :: try_from_fmt ( fmt ! ( "rust_proc" ) ) ?;
37
- let proc_dir = bindings:: proc_mkdir ( parent_dir. as_char_ptr ( ) , ptr:: null_mut ( ) ) ;
53
+ let dir_name = CString :: try_from_fmt ( fmt ! ( "{}" , SUB_DIR_NAME ) ) ?;
54
+ let parent = bindings:: proc_mkdir ( dir_name. as_char_ptr ( ) , ptr:: null_mut ( ) ) ;
55
+
56
+ let proc_ops = bindings:: proc_ops {
57
+ proc_flags : 0 , // mandatory to prevent build error
58
+ proc_get_unmapped_area : None , // mandatory to prevent build error
59
+ proc_read_iter : None , // mandatory to prevent build error
60
+ proc_open : Some ( Self :: proc_open) ,
61
+ proc_read : Some ( Self :: proc_read) ,
62
+ proc_write : None ,
63
+ proc_lseek : None ,
64
+ proc_release : None ,
65
+ proc_poll : None ,
66
+ proc_ioctl : None ,
67
+ proc_mmap : None ,
68
+ } ;
69
+
70
+ bindings:: proc_create (
71
+ CString :: try_from_fmt ( fmt ! ( "{}" , PROC_FS_NAME ) ) ?. as_char_ptr ( ) ,
72
+ 0o644 ,
73
+ parent,
74
+ & proc_ops,
75
+ ) ;
38
76
}
39
77
40
78
Ok ( RustProc { } )
0 commit comments