|
16 | 16 | #define SET_SIZE 512
|
17 | 17 | #define NUM_SETS DEV_SIZE/SET_SIZE
|
18 | 18 | #define DEBUG
|
19 |
| - |
| 19 | +#define MESSAGE_LEN 20 |
20 | 20 | #define FOURMB_DEBUG1
|
21 | 21 |
|
22 | 22 | #ifdef FOURMB_DEBUG1
|
|
28 | 28 | #define FOURMB_IOC_HELLO _IO(FOURMB_IOC_MAGIC,1)
|
29 | 29 | #define FOURMB_IOC_STM _IOW(FOURMB_IOC_MAGIC,2,unsigned long) /* write a message */
|
30 | 30 | #define FOURMB_IOC_LDM _IOR(FOURMB_IOC_MAGIC,3,unsigned long) /* read a message*/
|
31 |
| -#define FOURMB_IOC_LDSTM _IORW(FOURMB_IOC_MAGIC,4,unsigned long) /* Do both */ |
| 31 | +#define FOURMB_IOC_LDSTM _IOWR(FOURMB_IOC_MAGIC,4,unsigned long) /* Do both */ |
32 | 32 | #define FOURMB_IOC_MAXNR 14
|
33 | 33 |
|
34 | 34 | int fourmb_major = MAJOR_NUMBER;
|
@@ -61,7 +61,7 @@ struct fourmb_dev {
|
61 | 61 | */
|
62 | 62 | unsigned long size;
|
63 | 63 | struct cdev cdev;
|
64 |
| - char* dev_msg; // used in ioctl method. |
| 64 | + char dev_msg[MESSAGE_LEN]; // used in ioctl method. |
65 | 65 | };
|
66 | 66 |
|
67 | 67 | struct fourmb_dev* fourmb_device; /* Device Instance */
|
@@ -285,7 +285,7 @@ loff_t fourmb_lseek(struct file* filep, loff_t off, int whence) {
|
285 | 285 | long fourmb_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) {
|
286 | 286 | struct fourmb_dev *dev = filep->private_data;
|
287 | 287 | int retval, err = 0;
|
288 |
| - //char* dev_msg; |
| 288 | + char tmp_msg[MESSAGE_LEN]; |
289 | 289 |
|
290 | 290 | /* check for appropriate commands */
|
291 | 291 | if (_IOC_TYPE(cmd) != FOURMB_IOC_MAGIC) return -ENOTTY;
|
@@ -314,23 +314,43 @@ long fourmb_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) {
|
314 | 314 | * I seriously dont know why it works vis-a-vis the commented
|
315 | 315 | * code above and below, but never mind
|
316 | 316 | */
|
317 |
| - dev->dev_msg = (char *)arg; |
| 317 | + //dev->dev_msg = (char *)arg; |
318 | 318 |
|
319 |
| - //if(copy_from_user(dev->dev_msg,(char *)arg,MESSAGE_LEN)) { |
320 |
| - // printk(KERN_ERR "fourmb_device: ioctl failed to name the device"); |
321 |
| - // return -ENOTTY; |
322 |
| - //} |
| 319 | + if(copy_from_user(dev->dev_msg,(char *)arg,MESSAGE_LEN)) { |
| 320 | + printk(KERN_ERR "fourmb_device: ioctl failed to name the device\n"); |
| 321 | + return -ENOTTY; |
| 322 | + } |
323 | 323 | printk(KERN_INFO "fourmb_device: ioctl naming the device as %s\n",dev->dev_msg);
|
324 | 324 | //strcpy(dev->dev_msg,dev_msg);
|
325 | 325 | break;
|
326 | 326 |
|
327 |
| - case FOURMB_IOC_LDM: /* read the device string */ |
| 327 | + case FOURMB_IOC_LDM: /* read the device string */ |
| 328 | + if(copy_to_user((char *)arg,dev->dev_msg,MESSAGE_LEN)) { |
| 329 | + printk(KERN_ERR "fourmb_device: ioctl failed to retrieve the device name\n"); |
| 330 | + return -ENOTTY; |
| 331 | + } |
| 332 | + break; |
328 | 333 | // retval = __put_user(dev_msg,(char __user *)arg);
|
329 | 334 | // if(retval) {
|
330 | 335 | // printk(KERN_INFO "fourmb_device: ioctl device name retrieval failed\n");
|
331 | 336 | // return retval;
|
332 | 337 | // }
|
333 | 338 | // break;
|
| 339 | + case FOURMB_IOC_LDSTM: |
| 340 | + if(copy_from_user(tmp_msg,(char *)arg,MESSAGE_LEN)) { |
| 341 | + printk(KERN_ERR "fourmb_device: ioctl failed to tmp_msg store the name for swap\n"); |
| 342 | + return -ENOTTY; |
| 343 | + } |
| 344 | + printk(KERN_INFO "fourmb_device: ioctl temporarily stored the name for swap %s\n",tmp_msg); |
| 345 | + if(copy_to_user((char *)arg,dev->dev_msg,MESSAGE_LEN)) { |
| 346 | + printk(KERN_ERR "fourmb_device: ioctl failed to swap the device name\n"); |
| 347 | + return -ENOTTY; |
| 348 | + } |
| 349 | + printk(KERN_INFO "fourmb_device: ioctl successfuly swapped the device name\n"); |
| 350 | + strcpy(dev->dev_msg,tmp_msg); |
| 351 | + printk(KERN_INFO "fourmb_device: ioctl new device name after swap %s\n",dev->dev_msg); |
| 352 | + break; |
| 353 | + |
334 | 354 | default:
|
335 | 355 | return -ENOTTY;
|
336 | 356 | }
|
@@ -380,7 +400,7 @@ static int __init fourmb_device_init(void) {
|
380 | 400 | memset(fourmb_device,0,sizeof(struct fourmb_dev));
|
381 | 401 |
|
382 | 402 | /* Device Initialization */
|
383 |
| - fourmb_device->dev_msg = "anonymous"; |
| 403 | + strcpy(fourmb_device->dev_msg,"anonymous"); |
384 | 404 | cdev_init(&(fourmb_device->cdev),&fourmb_fops);
|
385 | 405 | fourmb_device->cdev.owner = THIS_MODULE;
|
386 | 406 | fourmb_device->cdev.ops = &fourmb_fops;
|
|
0 commit comments