-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproc_fs.c
67 lines (57 loc) · 1.33 KB
/
proc_fs.c
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
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
#define BUFSIZE 100
char buf[BUFSIZE];
static struct proc_dir_entry *ent;
/* cat /proc/proc_fs */
static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos)
{
int len=0;
//char buf[BUFSIZE];
printk( KERN_DEBUG "read handler\n");
if(*ppos > 0 || count < BUFSIZE)
return 0;
if(copy_to_user(ubuf,buf,len))
return -EFAULT;
*ppos = len;
return len;
}
/* echo "test" > /proc/proc_fs */
static ssize_t mywrite(struct file *file, const char __user *ubuf,size_t count, loff_t *ppos)
{
printk( KERN_DEBUG "write handler\n");
int c;
//char buf[BUFSIZE];
if(*ppos > 0 || count > BUFSIZE)
return -EFAULT;
if(copy_from_user(buf,ubuf,count))
return -EFAULT;
c = strlen(buf);
*ppos = c;
return c;
}
static struct proc_ops myops =
{
.proc_read = myread,
.proc_write = mywrite,
.proc_lseek = noop_llseek,
};
static int hello_init(void)
{
pr_info("proc_fs: module loaded\n");
ent = proc_mkdir("smalinux_proc_fs", NULL);
ent = proc_create("proc_fs_1", 0660, ent, &myops);
return 0;
}
static void hello_exit(void)
{
proc_remove(ent);
pr_err("Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);