1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#define procfs_name "helloworld"
struct proc_dir_entry* Our_Proc_File;
struct proc_dir_entry proc_root;
int procfile_read(char* buf, char** buf_loc, off_t off, int buf_len, int* eof, void* data)
{
int ret;
printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);
if (off > 0)
ret = 0;
else
ret = sprintf(buf, "HelloWorld!\n");
return ret;
}
int init_module(void)
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, NULL);
//remove_proc_entry(procfs_name, &proc_root);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk(KERN_INFO "/proc/%s created\n", procfs_name);
return 0;
}
void cleanup_module(void)
{
remove_proc_entry(procfs_name, NULL);
//remove_proc_entry(procfs_name, &proc_root);
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Guillermo Ramos");
|