-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprocfs.c
More file actions
78 lines (68 loc) · 2.1 KB
/
procfs.c
File metadata and controls
78 lines (68 loc) · 2.1 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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
static struct proc_dir_entry *example_dir, *first_file;
static int write_first(struct file *file,
const char __user *buffer,
unsigned long count,
void *data)
{
char * kernel_buf;
if (count < 0 || count > 1024)
return -EFAULT;
kernel_buf = kmalloc(count + 1, GFP_KERNEL);
if(copy_from_user(kernel_buf, buffer, count)) {
kfree(kernel_buf);
return -EFAULT;
}
kernel_buf[count] = '\0';
printk("write_first received data: %s\n", kernel_buf);
printk("first filename %s\n", file->f_dentry->d_iname);
printk("write_first data %s\n", (char *)data);
return count;
}
static int read_first(char *page, char **start,
off_t off, int count,
int *eof, void *data)
{
int offset = 0;
char * message = "hello world: ";
strcpy(page + offset, message);
offset += strlen(message);
memcpy(page + offset, data, strlen(data));
offset += strlen(data);
page[offset] = '\n';
offset += 1;
return offset;
}
static int __init procexample_module_init(void)
{
/* create a directory */
example_dir = proc_mkdir("example", NULL);
if(example_dir == NULL)
return -ENOMEM;
example_dir->owner = THIS_MODULE;
/* create a file */
first_file = create_proc_entry("first", 0644, example_dir);
if(first_file == NULL) {
remove_proc_entry("example", NULL);
return -ENOMEM;
}
first_file->data = kmalloc(strlen( "first file private data"), GFP_KERNEL);
strcpy(first_file->data, "first file private data");
first_file->read_proc = read_first;
first_file->write_proc = write_first;
first_file->owner = THIS_MODULE;
return 0;
}
static void __exit procexample_module_exit(void)
{
kfree(first_file->data);
remove_proc_entry("first", example_dir);
remove_proc_entry("example", NULL);
}
module_init(procexample_module_init);
module_exit(procexample_module_exit);
MODULE_LICENSE("GPL");