procfs3.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * procfs3.c
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/proc_fs.h>
  7. #include <linux/sched.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/version.h>
  10. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
  11. #define HAVE_PROC_OPS
  12. #endif
  13. #define PROCFS_MAX_SIZE 2048
  14. #define PROCFS_ENTRY_FILENAME "buffer2k"
  15. struct proc_dir_entry *Our_Proc_File;
  16. static char procfs_buffer[PROCFS_MAX_SIZE];
  17. static unsigned long procfs_buffer_size = 0;
  18. static ssize_t procfs_read(struct file *filp,
  19. char *buffer,
  20. size_t length,
  21. loff_t *offset)
  22. {
  23. static int finished = 0;
  24. if (finished) {
  25. pr_debug("procfs_read: END\n");
  26. finished = 0;
  27. return 0;
  28. }
  29. finished = 1;
  30. if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
  31. return -EFAULT;
  32. pr_debug("procfs_read: read %lu bytes\n", procfs_buffer_size);
  33. return procfs_buffer_size;
  34. }
  35. static ssize_t procfs_write(struct file *file,
  36. const char *buffer,
  37. size_t len,
  38. loff_t *off)
  39. {
  40. if (len > PROCFS_MAX_SIZE)
  41. procfs_buffer_size = PROCFS_MAX_SIZE;
  42. else
  43. procfs_buffer_size = len;
  44. if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
  45. return -EFAULT;
  46. pr_debug("procfs_write: write %lu bytes\n", procfs_buffer_size);
  47. return procfs_buffer_size;
  48. }
  49. int procfs_open(struct inode *inode, struct file *file)
  50. {
  51. try_module_get(THIS_MODULE);
  52. return 0;
  53. }
  54. int procfs_close(struct inode *inode, struct file *file)
  55. {
  56. module_put(THIS_MODULE);
  57. return 0;
  58. }
  59. #ifdef HAVE_PROC_OPS
  60. static struct proc_ops File_Ops_4_Our_Proc_File = {
  61. .proc_read = procfs_read,
  62. .proc_write = procfs_write,
  63. .proc_open = procfs_open,
  64. .proc_release = procfs_close,
  65. };
  66. #else
  67. static const struct file_operations File_Ops_4_Our_Proc_File = {
  68. .read = procfs_read,
  69. .write = procfs_write,
  70. .open = procfs_open,
  71. .release = procfs_close,
  72. };
  73. #endif
  74. static int __init procfs3_init(void)
  75. {
  76. Our_Proc_File = proc_create(PROCFS_ENTRY_FILENAME, 0644, NULL,
  77. &File_Ops_4_Our_Proc_File);
  78. if (Our_Proc_File == NULL) {
  79. remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL);
  80. pr_debug("Error: Could not initialize /proc/%s\n",
  81. PROCFS_ENTRY_FILENAME);
  82. return -ENOMEM;
  83. }
  84. proc_set_size(Our_Proc_File, 80);
  85. proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
  86. pr_debug("/proc/%s created\n", PROCFS_ENTRY_FILENAME);
  87. return 0;
  88. }
  89. static void __exit procfs3_exit(void)
  90. {
  91. remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL);
  92. pr_debug("/proc/%s removed\n", PROCFS_ENTRY_FILENAME);
  93. }
  94. module_init(procfs3_init);
  95. module_exit(procfs3_exit);
  96. MODULE_LICENSE("GPL");