Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/kernel.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>
  7. #include <linux/fs.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/types.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/proc_fs.h>
  13.  
  14. #define PROCFS_MAX_SIZE 1024
  15. #define PROCFS_NAME "gusproc"
  16.  
  17. MODULE_LICENSE("Dual BSD/GPL");
  18.  
  19. char data[100];
  20. static struct proc_dir_entry *gusproc;
  21. static char procfs_buffer[PROCFS_MAX_SIZE];
  22. static unsigned long procfs_buffer_size = 0;
  23.  
  24. static ssize_t dev_read(struct file *file, char* buf, size_t len, loff_t* offset) {
  25.     if (*offset >= sizeof(data)) return 0;
  26.     copy_to_user(buf, data, sizeof(data));
  27.     *offset += len;
  28.     return len;
  29. }
  30.  
  31. static ssize_t dev_write(struct file *file, const char* buf, size_t len, loff_t* offset) {
  32.     memcpy(data, buf, len<=100 ? len : 99);
  33.     data[len<=100 ? len : 99]='\0';
  34.     printk(KERN_INFO "%s", data);
  35.         return len;
  36. }
  37.  
  38. static struct file_operations fops = {
  39.     .read = dev_read,
  40.     .write = dev_write,
  41. };
  42.  
  43. static struct file_operations procfs_ops = {
  44.     .read = dev_read,
  45.     .write = dev_write,
  46. };
  47.  
  48. int procfile_read(char *buffer,  char **buffer_location, off_t offset, int buffer_length, int *eof, void *data) {
  49.     int ret;
  50.     printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);
  51.     if (offset > 0) ret = 0;
  52.     else {
  53.         memcpy(buffer, procfs_buffer, procfs_buffer_size);
  54.         ret = procfs_buffer_size;
  55.     }
  56.     return ret;
  57. }
  58.  
  59. int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {
  60.     procfs_buffer_size = count;
  61.     if (procfs_buffer_size > PROCFS_MAX_SIZE) procfs_buffer_size = PROCFS_MAX_SIZE;
  62.     if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size)) return -EFAULT;
  63.     return procfs_buffer_size;
  64. }
  65.  
  66. uint8_t major;
  67. static struct class* myclass = NULL;
  68.  
  69. static int hello_init(void) {
  70.     //Chardev
  71.     printk(KERN_INFO "Hello, world\n");
  72.     major = register_chrdev(0, "mydev", &fops);
  73.     printk(KERN_INFO "MAJOR = %d", major);
  74.     myclass = class_create(THIS_MODULE, "myclass");
  75.     device_create(myclass, NULL, MKDEV(major, 0), NULL, "mydev%d", 0);
  76.        
  77.     //Procfs
  78.     gusproc = proc_create(PROCFS_NAME, 0, NULL, &procfs_ops);
  79.     if (!gusproc) {
  80.         remove_proc_entry(PROCFS_NAME, NULL);
  81.         printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
  82.         PROCFS_NAME);
  83.         return -ENOMEM;
  84.     }
  85.     printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);
  86.    
  87.     return 0;
  88. }
  89. static void hello_exit(void) {
  90.    
  91.     //Chardev
  92.     device_destroy(myclass, MKDEV(major, 0));
  93.     class_unregister(myclass);
  94.     class_destroy(myclass);
  95.     unregister_chrdev_region(MKDEV(major, 0), 1);
  96.     printk(KERN_INFO "Goodbye, cruel world\n");
  97.    
  98.     //Procfs
  99.     remove_proc_entry(PROCFS_NAME, NULL);
  100.     printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
  101. }
  102.  
  103. module_init(hello_init);
  104. module_exit(hello_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement