Advertisement
alsiva

kernel_module_danila

Dec 18th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.64 KB | None | 0 0
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3.  
  4. #include <linux/version.h>
  5. #include <linux/fs.h>
  6.  
  7. #include "character_dev.h"
  8.  
  9. #include <asm/uaccess.h>
  10. #include <linux/list.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/pci.h>
  13. #include <linux/vmalloc.h>
  14.  
  15. MODULE_LICENSE("GPL");
  16. MODULE_VERSION("2.0.22");
  17. MODULE_AUTHOR("Uzbek");
  18. MODULE_DESCRIPTION("OS LAB2");
  19.  
  20. static int lab_dev_open(struct inode *inode, struct file *file);
  21. static int lab_dev_release(struct inode *inode, struct file *file);
  22. static ssize_t lab_dev_read(struct file *filp, char __user *buf, size_t len, loff_t *off);
  23. static ssize_t lab_dev_write(struct file *filp, const char *buf, size_t len, loff_t *off);
  24. static long lab_dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  25. /*
  26. ** This function will be called when we open the Device file
  27. */
  28. static int lab_dev_open(struct inode *inode, struct file *file)
  29. {
  30.     printk(KERN_INFO "Device File Opened...!!!\n");
  31.     return 0;
  32. }
  33. /*
  34. ** This function will be called when we close the Device file
  35. */
  36. static int lab_dev_release(struct inode *inode, struct file *file)
  37. {
  38.  
  39.     printk(KERN_INFO "Device File Closed...!!!\n");
  40.     return 0;
  41. }
  42. /*
  43. ** This function will be called when we read the Device file
  44. */
  45. static ssize_t lab_dev_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  46. {
  47.     printk(KERN_INFO "Read Function\n");
  48.     return 0;
  49. }
  50. /*
  51. ** This function will be called when we write the Device file
  52. */
  53. static ssize_t lab_dev_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  54. {
  55.     printk(KERN_INFO "Write function\n");
  56.     return len;
  57. }
  58.  
  59. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
  60. static int device_ioctl(struct inode *inode,
  61.                         struct file *file,
  62.                         unsigned int ioctl_num,
  63.                         unsigned long ioctl_param)
  64. #else
  65. static long lab_dev_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
  66. #endif
  67. {
  68.     printk(KERN_INFO "lab_dev_ioctl(%p,%lu,%lu)", file, ioctl_num, ioctl_param);
  69.     if (ioctl_num == IOCTL_GET_PCI_DEV)
  70.     {
  71.  
  72.         struct pci_dev_info *pdi = vmalloc(sizeof(struct pci_dev_info));
  73.         int i = 0;
  74.         struct pci_dev *dev = NULL; ;
  75.         while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) && (i<MAX_COUNT_PCI_DEV))
  76.         {
  77.             pdi->devices[i] = dev->device;
  78.             i++;
  79.         };
  80.         pdi->actual_count = i - 1;
  81.         copy_to_user((struct pci_dev_info *)ioctl_param, pdi, sizeof(struct pci_dev_info));
  82.         vfree(pdi);
  83.     }
  84.     if (ioctl_num == IOCTL_GET_VM_AREA_STRUCT)
  85.     {
  86.  
  87.         struct vm_area_struct_info *vasi = vmalloc(sizeof(struct vm_area_struct_info));
  88.         copy_from_user(vasi, (struct vm_area_struct_info *)ioctl_param, sizeof(struct vm_area_struct_info));
  89.         struct task_struct *task;
  90.  
  91.         task = get_pid_task(find_get_pid(vasi->pid), PIDTYPE_PID);
  92.         if (task == NULL)
  93.         {
  94.             pr_err("Process with <PID> = %d doesn't exist\n",vasi->pid);
  95.             return 1;
  96.         }
  97.  
  98.         if (task->mm == NULL)
  99.         {
  100.             printk(KERN_INFO "Can't find vm_area_struct with this pid\n");
  101.             return 2;
  102.         }
  103.         struct mm_struct *mm;
  104.         struct vm_area_struct *vm_area;
  105.         printk(KERN_INFO "vm area struct\n");
  106.         struct vm_area_struct *pos = NULL;
  107.         int i = 0;
  108.         for (pos = task->mm->mmap, i = 0; pos != NULL && i<MAX_COUNT_VM_AREA_STRUCTES; pos = pos->vm_next, i++)
  109.         {
  110.             vasi->vapi[i].permissions = pos->vm_flags;
  111.             vasi->vapi[i].vm_start = pos->vm_start;
  112.             vasi->vapi[i].vm_end = pos->vm_end;
  113.             vasi->vapi[i].rb_subtree_gap = pos->rb_subtree_gap;
  114.         }
  115.         vasi->actual_count = i - 1;
  116.         copy_to_user((struct vm_area_struct_info *)ioctl_param, vasi, sizeof(struct vm_area_struct_info));
  117.         vfree(vasi);
  118.     }
  119.     return 0;
  120. }
  121.  
  122. struct file_operations file_ops =
  123.     {
  124.         .owner = THIS_MODULE,
  125.         .read = lab_dev_read,
  126.         .write = lab_dev_write,
  127.         .open = lab_dev_open,
  128.         .release = lab_dev_release,
  129. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
  130.         .ioctl = device_ioctl
  131. #else
  132.         .unlocked_ioctl = lab_dev_ioctl
  133. #endif
  134. };
  135.  
  136. int init_module()
  137. {
  138.     int ret_val;
  139.     ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &file_ops);
  140.     if (ret_val < 0)
  141.     {
  142.         printk(KERN_ALERT "%s failed with %d\n", "Sorry, registering the character device \n", ret_val);
  143.         return ret_val;
  144.     }
  145.  
  146.     return 0;
  147. }
  148.  
  149. void cleanup_module()
  150. {
  151.     unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement