-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_group_root.c
268 lines (200 loc) · 6.11 KB
/
ipc_group_root.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/list.h>
#include "ipc_module_costants.h"
#include "ipc_kernel_macros.h"
#include "ipc_group_root.h"
#include "ipc_group.h"
int ipc_group_root_open(struct inode *inode, struct file *filp) {
if (group_root_dev -> closing) return -GROUP_CLOSING;
return SUCCESS;
}
long int ipc_group_root_ioctl(struct file *filp,
unsigned int ioctl_num,
unsigned long ioctl_param){
int res;
GR_DEBUG( "ioctl %d %ld", ioctl_num, ioctl_param);
spin_lock(&(group_root_dev -> lock));
switch (ioctl_num)
{
case IPC_GROUP_INSTALL:
res = ipc_group_install((group_t) ioctl_param);
break;
case IPC_GROUP_UNINSTALL:
res = ipc_group_uninstall((group_t) ioctl_param);
break;
default:
GR_DEBUG( "unrecognized %d", ioctl_num);
break;
}
spin_unlock(&(group_root_dev -> lock));
return res;
}
int ipc_group_root_release(struct inode *inode, struct file *filp)
{
GR_DEBUG( "root ipc dev release");
return SUCCESS;
}
struct file_operations ipc_group_root_ops = {
.open = ipc_group_root_open,
.unlocked_ioctl = ipc_group_root_ioctl,
.release = ipc_group_root_release
};
int ipc_group_root_install(void)
{
int res;
dev_t devno;
struct device* group_root_device;
GR_DEBUG( "installing");
/* Register device major and minor */
res = alloc_chrdev_region(&devno, 0, IPC_MAX_GROUPS+1, IPC_ROOT_DEV_NAME);
if (res < 0) {
printk(KERN_ERR "alloc_chrdev_region failed\n");
goto ALLOC_CHRDEV_REGION_FAIL;
} else {
GR_DEBUG( "alloc_chrdev_region: major %d and minor %d", MAJOR(devno), MINOR(devno));
group_major = MAJOR(devno);
}
/* Create a class : appears at /sys/class */
group_dev_class = class_create(THIS_MODULE, IPC_CLASS_NAME);
if (group_dev_class == NULL) {
printk(KERN_ERR "Failed creating class\n");
goto CLASS_CREATE_FAIL;
} else {
GR_DEBUG( "Class creation success");
}
/* Allocating memory */
group_root_dev = kmalloc(sizeof(ipc_group_root_dev), GFP_USER);
if (group_root_dev == NULL) {
printk(KERN_ERR "kmalloc ko: %p", group_root_dev);
goto CDEV_ALLOC_FAIL;
} else {
GR_DEBUG( "kmalloc ok: %p", group_root_dev);
}
/* Initializing device values */
cdev_init(&(group_root_dev -> cdev) , &ipc_group_root_ops);
spin_lock_init(&(group_root_dev->lock));
group_root_dev -> cdev.owner = THIS_MODULE;
group_root_dev -> closing = false;
/* Registering device to the kernel */
res = cdev_add(&(group_root_dev -> cdev) ,devno , 1);
if (res < 0) {
printk(KERN_ERR "Failed making cdev live \n");
goto CDEV_ADD_FAIL;
} else {
GR_DEBUG( "cdev is now live");
}
/* Creating the device into the pseudo file system */
group_root_device = device_create(group_dev_class, NULL, devno, NULL, IPC_ROOT_DEV_NAME);
if (group_root_device < 0) {
GR_ERROR( "Failed creating device\n");
res = ( int ) group_root_device;
goto DEVICE_CREATE_FAIL;
} else {
GR_DEBUG( "Device creation success");
}
return 0;
// device_destroy(group_dev_class, group_root_device);
DEVICE_CREATE_FAIL:
cdev_del(&(group_root_dev -> cdev));
CDEV_ADD_FAIL:
kfree(group_root_dev);
CDEV_ALLOC_FAIL:
class_destroy(group_dev_class);
CLASS_CREATE_FAIL:
unregister_chrdev_region(devno, IPC_MAX_GROUPS+1);
ALLOC_CHRDEV_REGION_FAIL:
return res;
}
int ipc_group_root_uninstall(void)
{
int i;
dev_t devno;
spin_lock(&(group_root_dev -> lock));
group_root_dev -> closing = true;
spin_unlock(&(group_root_dev -> lock));
for (i=1; i<= IPC_MAX_GROUPS; i++){
ipc_group_uninstall((group_t)i);
}
GR_DEBUG( "uninstalling");
devno = MKDEV(group_major, 0);
GR_DEBUG( "device_destroy");
device_destroy(group_dev_class, devno);
GR_DEBUG( "cdev_del");
cdev_del(&(group_root_dev -> cdev));
GR_DEBUG( "kfree %p",group_root_dev );
kfree(group_root_dev);
GR_DEBUG( "class_destroy" );
class_destroy(group_dev_class);
GR_DEBUG( "unregister_chrdev_region");
unregister_chrdev_region(devno, IPC_MAX_GROUPS+1);
GR_DEBUG( "uninstalled");
return SUCCESS;
}
int ipc_group_install(group_t groupno)
{
int res;
dev_t group_devno;
char devname[IPC_DEV_NAMESIZE] = {0};
struct device* group_device ;
ipc_group_dev* group_dev;
GR_DEBUG( "Installing group %d", groupno);
if (groupno < 1 || groupno > IPC_MAX_GROUPS){
GR_DEBUG("Invalid group number, min is 1 and max is %d", IPC_MAX_GROUPS);
return -INVALID_GROUP_NUM;
} else if(group_devs[groupno] != NULL) {
GR_DEBUG("Group already installed");
return -GROUP_ALREADY_INSTALLED;
}
group_devno = MKDEV(group_major, groupno);
snprintf(devname,IPC_DEV_NAMESIZE, "aosv_ipc_dev%d" , groupno);
group_dev = kmalloc(sizeof(ipc_group_dev), GFP_USER);
/* Allocate space for cdev */
if (group_dev == NULL) {
printk(KERN_ERR "Device %d allocation failed", groupno);
goto CDEV_ALLOC_FAIL;
} else {
GR_DEBUG( "Device %d allocation ok", groupno);
}
/* Registering device to the kernel */
cdev_init(&(group_dev->cdev), &ipc_group_ops);
spin_lock_init(&(group_dev->lock));
spin_lock_init(&(group_dev->delayed_lock));
group_dev -> cdev.owner = THIS_MODULE;
group_dev -> msg_count = 0;
group_dev -> delayed_msg_count = 0;
group_dev -> delay = ktime_set(0,0);
group_dev -> waiting_count = 0;
group_dev -> awaking_count = 0;
group_dev -> closing = false;
init_waitqueue_head( &(group_dev -> wait_queue));
INIT_LIST_HEAD(&( group_dev -> msg_list ));
INIT_LIST_HEAD(&( group_dev -> delayed_msg_list ));
res = cdev_add(&(group_dev->cdev), group_devno, 1);
if (res < 0) {
printk(KERN_ERR "Device %d add failed", groupno);
goto CDEV_ADD_FAIL;
} else {
GR_DEBUG( "Device %d add successful", groupno);
}
/* Creating the device into the pseudo file system */
group_device = device_create(group_dev_class, NULL, group_devno, NULL, devname);
if (group_device < 0) {
printk(KERN_ERR "Device %d creation failed", groupno);
goto DEVICE_CREATE_FAIL;
} else {
GR_DEBUG( "Device %d creation success", groupno);
}
group_devs[groupno] = group_dev;
return 0;
DEVICE_CREATE_FAIL:
cdev_del(&(group_dev->cdev));
CDEV_ADD_FAIL:
kfree(group_dev);
CDEV_ALLOC_FAIL:
group_devs[groupno] = NULL;
return res;
}