-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKDyCircularQueue.c
More file actions
202 lines (180 loc) · 5.86 KB
/
Copy pathKDyCircularQueue.c
File metadata and controls
202 lines (180 loc) · 5.86 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
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
#include <linux/module.h> /* Needed by all modules */
#include <linux/init.h> /* Needed for the macros */
#include <linux/fs.h>/* Needed for the file operations */
#include<linux/ioctl.h>/* Needed for the I/O operations */
#include <linux/uaccess.h>
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include<linux/slab.h> /* Needed for dynamic memory allocation */
#define MYMAJOR 60
#define DRIVER_NAME "/dev/shrish"
#define SET_SIZE_OF_QUEUE _IOW('q',1,int)
#define PUSH_DATA _IOW('q',2,struct queue_data)
#define POP_DATA _IO('q',3)
#define PRINT_QUEUE _IO('q',4)
/*
@brief declaration , similar convention of declaration and definition followed in c
*/
static int my_init(void); // Prototype for init function
static void my_exit(void); // Prototype for exit function
static int driver_open(struct inode *driver_file , struct file *instance);
static int driver_exit(struct inode *driver_file , struct file *instance);
static long driver_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
// File Operation
static struct file_operations fops ={
.owner = THIS_MODULE,
.open = driver_open,
.release = driver_exit,
.unlocked_ioctl = driver_ioctl,
};
struct queue_data{
int length;
char* data;
};
struct circular_queue{
int s, e;
char **arr;
int size;
int capacity;
};
static struct circular_queue queue;
/*
@brief Driver functions are written below
- driver_open called when driver is openned
- driver_ioctl for ioctl calls
- driver_exit called when driver is closed
*/
static int driver_open(struct inode *driver_file , struct file *instance){
printk(KERN_INFO "CircularQueue: Device opened\n");
return 0;
};
static long driver_ioctl(struct file *file, unsigned int cmd, unsigned long arg){
struct queue_data user_data;
char* temp;
int new_size;
switch (cmd)
{
case SET_SIZE_OF_QUEUE:
if(copy_from_user(&new_size,(int __user* )arg,sizeof(new_size)))
return -EFAULT;
kfree(queue.arr); // free up the existing memory
queue.arr = kmalloc_array(new_size,sizeof(char *),GFP_KERNEL);
if(!queue.arr)
return -ENOMEM;
queue.capacity = new_size;
queue.size = 0;
queue.s = -1;
queue.e = -1;
printk(KERN_INFO "CircularQueue: Initialized with size %d\n", new_size);
break;
case PUSH_DATA:
if (copy_from_user(&user_data, (struct queue_data __user *)arg, sizeof(user_data))){
return -EFAULT;
}
temp = kmalloc(user_data.length,GFP_KERNEL);
if(!temp)
return -ENOMEM;
if(copy_from_user(temp,user_data.data,user_data.length)){
kfree(temp);
return -EFAULT;
}
// Check if the queue is full
if (queue.size == queue.capacity) {
printk(KERN_WARNING "CircularQueue: Queue is full, cannot push data\n");
kfree(temp);
return -ENOMEM;
}
if(queue.s==-1) queue.s++;
queue.e = (queue.e + 1)%(queue.capacity);
queue.arr[queue.e] = temp;
queue.size++;
printk(KERN_INFO "CircularQueue: Pushed Data of Length %d \n",user_data.length);
break;
case POP_DATA:
if(queue.s==-1){
printk(KERN_INFO "CircularQueue: Invalid Pop!!, Queue is Empty");
return -EINVAL;
}
kfree(queue.arr[queue.s]);
if(queue.s==queue.e){
queue.s =-1;
queue.e =-1;
}
else{
queue.s = (queue.s+1)%(queue.capacity);
}
queue.size--;
printk(KERN_INFO "CircularQueue: Popped data of length %d\n", user_data.length);
break;
case PRINT_QUEUE:
if(queue.size == 0) {
printk(KERN_DEBUG "CircularQueue: Queue is Empty\n");
return -EINVAL;
}
int temp = queue.s;
while (temp != queue.e) {
if (queue.arr[temp] != NULL) {
printk(KERN_INFO "Queue data: %s\n", queue.arr[temp]);
} else {
printk(KERN_WARNING "Queue data: NULL pointer detected at index %d\n", temp);
}
temp = (temp + 1) % queue.capacity;
}
// Print the last element (queue.e should always have data if not empty)
if (queue.arr[temp] != NULL) {
printk(KERN_INFO "Queue data: %s\n", queue.arr[temp]);
} else {
printk(KERN_WARNING "Queue data: NULL pointer detected at last index %d\n", temp);
}
break;
default:
return -EINVAL;
break;
}
return 0;
}
static int driver_exit(struct inode *driver_file , struct file *instance){
printk(KERN_INFO "CircularQueue: Device closed\n");
return 0;
};
/*
@brief This function is called when file is loaded into the kernel
*/
static int my_init(void){
int retval;
retval = register_chrdev(MYMAJOR,"my_dev_nr",&fops);
if(retval == 0) {
printk("dev_nr - registered Device number Major: %d, Minor: %d\n", MYMAJOR, 0);
}
else if(retval > 0) {
printk("dev_nr - registered Device number Major: %d, Minor: %d\n", retval>>20, retval&0xfffff);
}
else{
printk("Could not register device number !\n");
return -1;
}
queue.arr = NULL;
queue.s = -1;
queue.e = -1;
queue.size =0;
queue.capacity =0;
printk(KERN_INFO "CircularQueue: Module initialized\n");
return 0;
}
/*
@brief This function is called when file is unloaded from the kernel
*/
static void my_exit(void){
while (queue.size > 0) {
kfree(queue.arr[queue.s]);
queue.s = (queue.s + 1) % queue.capacity;
queue.size--;
}
kfree(queue.arr);
unregister_chrdev(MYMAJOR,"my_dev_nr");
printk(KERN_INFO "CircularQueue: Module unloaded\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shrishvesh Reddy <reddyshrish518@gmail.com>");
MODULE_DESCRIPTION("Implementing a dynamic circular queue in linux char device which takes data from IOCTL calls.");