Advertisement
bueddl

Procfs demo

Aug 8th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4.  
  5. #include <linux/proc_fs.h>
  6. #include <asm/uaccess.h>
  7.  
  8. static ssize_t my_read(struct file *instance, char __user *buffer, size_t max_bytes_to_write, loff_t *offset)
  9. {
  10.     char kernel_buffer[10];
  11.     size_t to_copy, not_copied;
  12.  
  13.     if (*offset > 5)
  14.         return 0;
  15.  
  16.     strncpy(kernel_buffer, "benis\n", sizeof(kernel_buffer));
  17.     to_copy = min(strlen(kernel_buffer), max_bytes_to_write);
  18.     not_copied = copy_to_user(buffer, kernel_buffer, to_copy);
  19.     *offset += to_copy - not_copied;
  20.     return to_copy - not_copied;
  21. }
  22.  
  23. static const struct file_operations proc_fops =
  24. {
  25.     .owner = THIS_MODULE, // Fürs handling intern, u.a. für die erkennung ob strukturen leaken
  26.     .read  = my_read,
  27. };
  28.  
  29. static struct proc_dir_entry *my_proc_file;
  30. #define PROC_FILE_NAME "myFile"
  31.  
  32. static int __init blah_init(void)
  33. {
  34.     my_proc_file = proc_create(PROC_FILE_NAME, 0666, NULL, &proc_fops);
  35.     if (my_proc_file == NULL)
  36.         return 1;
  37.  
  38.     return 0;
  39. }
  40.  
  41. static void __exit blah_exit(void)
  42. {
  43.     remove_proc_entry(PROC_FILE_NAME, NULL);
  44. }
  45.  
  46. module_init(blah_init);
  47. module_exit(blah_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement