-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhello.c
152 lines (119 loc) · 3.05 KB
/
hello.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("flygoast");
MODULE_DESCRIPTION("A Simple Character Device driver module");
static struct cdev cdev;
static dev_t devno;
static char tmp[128] = "hello from kernel!";
static int
hello_open(struct inode *inodep, struct file *filep)
{
printk(KERN_INFO "open\n");
return 0;
}
static int
hello_release(struct inode *inodep, struct file *filep)
{
printk(KERN_INFO "release\n");
return 0;
}
static ssize_t
hello_read(struct file *filep, char __user *buf, size_t count, loff_t *offset)
{
size_t avail;
printk(KERN_INFO "read\n");
avail = sizeof(tmp) - *offset;
if (count <= avail) {
if (copy_to_user(buf, tmp + *offset, count) != 0) {
return -EFAULT;
}
*offset += count;
return count;
} else {
if (copy_to_user(buf, tmp + *offset, avail) != 0) {
return -EFAULT;
}
*offset += avail;
return avail;
}
}
static ssize_t
hello_write(struct file *filep, const char __user *buf, size_t count,
loff_t *offset)
{
size_t avail;
printk(KERN_INFO "write\n");
avail = sizeof(tmp) - *offset;
memset(tmp + *offset, 0, avail);
if (count > avail) {
if (copy_from_user(tmp + *offset, buf, avail) != 0) {
return -EFAULT;
}
*offset += avail;
return avail;
} else {
if (copy_from_user(tmp + *offset, buf, count) != 0) {
return -EFAULT;
}
*offset += count;
return count;
}
}
static loff_t
hello_llseek(struct file *filep, loff_t off, int whence)
{
loff_t newpos;
switch (whence) {
case 0: /* SEEK_SET */
newpos = off;
break;
case 1: /* SEEK_CUR */
newpos = filep->f_pos + off;
break;
case 2: /* SEEK_END */
newpos = sizeof(tmp) + off;
break;
default:
return -EINVAL;
}
if (newpos < 0) {
return -EINVAL;
}
filep->f_pos = newpos;
return newpos;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = hello_open,
.release = hello_release,
.read = hello_read,
.llseek = hello_llseek,
.write = hello_write,
};
static int __init hello_init(void) {
int ret;
printk(KERN_INFO "Load hello\n");
devno = MKDEV(111, 0);
ret = register_chrdev_region(devno, 1, "hello");
if (ret < 0) {
return ret;
}
cdev_init(&cdev, &fops);
cdev.owner = THIS_MODULE;
cdev_add(&cdev, devno, 1);
return 0;
}
static void __exit hello_cleanup(void) {
printk(KERN_INFO "cleanup hello\n");
unregister_chrdev_region(devno, 1);
cdev_del(&cdev);
}
module_init(hello_init);
module_exit(hello_cleanup);
MODULE_LICENSE("GPL");