-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscull.c
More file actions
81 lines (55 loc) · 1.42 KB
/
Copy pathscull.c
File metadata and controls
81 lines (55 loc) · 1.42 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
80
81
#include "scull.h"
void print_buffer(const char *buffer, size_t length)
{
size_t i;
char *bytes;
bytes = (char *)kmalloc((length+1)*sizeof(char), GFP_KERNEL);
if (bytes == NULL) {
printk(KERN_ERR "Failed to allocate memory\n");
return;
}
for (i = 0; i < length; i++) {
bytes[i] = buffer[i];
}
bytes[length] = '\0';
printk(KERN_INFO "Buffer contents: %s", bytes);
kfree(bytes);
}
int scull_open (struct inode *pinode, struct file *pfile)
{
printk(KERN_INFO "%s called\n", __FUNCTION__);
return 0;
}
ssize_t scull_read (struct file *pfile, char __user *buffer, size_t length, loff_t *offset)
{
printk(KERN_INFO "%s called\n", __FUNCTION__);
print_buffer(buffer, length);
return 0;
}
ssize_t scull_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
{
printk(KERN_INFO "%s called\n", __FUNCTION__);
print_buffer(buffer, length);
return length;
}
int scull_close(struct inode *pinode, struct file *pfile)
{
printk(KERN_INFO "%s called\n", __FUNCTION__);
return 0;
}
static int __init scull_init(void)
{
printk(KERN_INFO "%s called\n", __FUNCTION__);
// register char dev
register_chrdev(240, "scull", &scull_fops);
cdev_init(&scull_cdev, &scull_fops);
return 0;
}
static void __exit scull_exit(void)
{
printk(KERN_INFO "%s called\n", __FUNCTION__);
cdev_del(&scull_cdev);
unregister_chrdev(240, "scull");
}
module_init(scull_init);
module_exit(scull_exit);