-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpidipc.c
120 lines (100 loc) · 2.68 KB
/
lpidipc.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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <asm/uaccess.h>
#include <linux/pid.h>
#include <linux/delay.h>
#define DEVICE_NAME "comchar"
#define CLASS_NAME "ipc"
static struct class* class;
static struct device* device;
static int major;
static int given_task_pid = -1;
static struct task_struct* given_task = NULL;
static struct cfs_rq* pid_cfs_rq = NULL;
atomic_t sync = ATOMIC_INIT(0);
/* executed once the device is closed or releaseed by userspace
* @param inodep: pointer to struct inode
* @param filep: pointer to struct file
*/
static int comchar_release(struct inode *inodep, struct file *filep)
{
pr_info("comchar: Device successfully closed\n");
return 0;
}
/* executed once the device is opened.
* It is not threads safe yet.
*
*/
static int comchar_open(struct inode *inodep, struct file *filep)
{
int ret = 0;
pr_info("comchar: Device opened\n");
return ret;
}
static ssize_t comchar_read(struct file *filep, char *buffer, size_t len, loff_t *offset)
{
size_t ret = 0;
atomic_dec(&sync);
while(atomic_read(&sync) < 0)
{
//ndelay(1);
usleep_range(5, 10);
//schedule_timeout_idle(0);
}
return ret;
}
static ssize_t comchar_write(struct file *filep, const char *buffer, size_t len, loff_t *offset)
{
size_t ret = 0;
atomic_inc(&sync);
return ret;
}
static const struct file_operations comchar_fops = {
.open = comchar_open,
.read = comchar_read,
.write = comchar_write,
.release = comchar_release,
/*.unlocked_ioctl = comchar_ioctl,*/
.owner = THIS_MODULE,
};
static int __init comchar_init(void)
{
int ret = 0;
major = register_chrdev(0, DEVICE_NAME, &comchar_fops);
if (major < 0) {
pr_info("comchar: fail to register major number!");
ret = major;
goto out;
}
class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(class)){
unregister_chrdev(major, DEVICE_NAME);
pr_info("comchar: failed to register device class");
ret = PTR_ERR(class);
goto out;
}
device = device_create(class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
if (IS_ERR(device)) {
class_destroy(class);
unregister_chrdev(major, DEVICE_NAME);
ret = PTR_ERR(device);
goto out;
}
out:
return ret;
}
static void __exit comchar_exit(void)
{
device_destroy(class, MKDEV(major, 0));
class_unregister(class);
class_destroy(class);
unregister_chrdev(major, DEVICE_NAME);
pr_info("comchar: unregistered!");
}
module_init(comchar_init);
module_exit(comchar_exit);
MODULE_LICENSE("GPL");