-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_proc.c
More file actions
79 lines (64 loc) · 1.33 KB
/
hello_proc.c
File metadata and controls
79 lines (64 loc) · 1.33 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include<linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
int len, temp;
static char *msg = 0;
static ssize_t
read_proc (struct file *filp, char __user * buf, size_t count, loff_t * offp)
{
if (count > temp)
{
count = temp;
}
temp = temp - count;
copy_to_user (buf, msg, count);
if (count == 0)
temp = len;
return count;
}
static ssize_t
write_proc (struct file *filp, const char __user * buf, size_t count,
loff_t * offp)
{
if (msg == 0 || count > 100)
{
printk (KERN_INFO " either msg is 0 or count >100\n");
}
// you have to move data from user space to kernel buffer
copy_from_user (msg, buf, count);
len = count;
temp = len;
return count;
}
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.read = read_proc,
.write = write_proc,
};
void
create_new_proc_entry (void)
{
proc_create ("userlist", 0666, NULL, &proc_fops);
msg = kmalloc (100 * sizeof (char), GFP_KERNEL);
if (msg == 0)
{
printk (KERN_INFO "why is msg 0 \n");
}
}
int
proc_init (void)
{
create_new_proc_entry ();
return 0;
}
void
proc_cleanup (void)
{
remove_proc_entry ("userlist", NULL);
}
MODULE_LICENSE ("GPL");
module_init (proc_init);
module_exit (proc_cleanup);