1
0

procfs3.c 2.7 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, char *buffer, size_t length,
  19. loff_t *offset)
  20. {
  21. static int finished = 0;
  22. if (finished) {
  23. pr_debug("procfs_read: END\n");
  24. finished = 0;
  25. return 0;
  26. }
  27. finished = 1;
  28. if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
  29. return -EFAULT;
  30. pr_debug("procfs_read: read %lu bytes\n", procfs_buffer_size);
  31. return procfs_buffer_size;
  32. }
  33. static ssize_t procfs_write(struct file *file, const char *buffer, size_t len,
  34. loff_t *off)
  35. {
  36. if (len > PROCFS_MAX_SIZE)
  37. procfs_buffer_size = PROCFS_MAX_SIZE;
  38. else
  39. procfs_buffer_size = len;
  40. if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
  41. return -EFAULT;
  42. pr_debug("procfs_write: write %lu bytes\n", procfs_buffer_size);
  43. return procfs_buffer_size;
  44. }
  45. int procfs_open(struct inode *inode, struct file *file)
  46. {
  47. try_module_get(THIS_MODULE);
  48. return 0;
  49. }
  50. int procfs_close(struct inode *inode, struct file *file)
  51. {
  52. module_put(THIS_MODULE);
  53. return 0;
  54. }
  55. #ifdef HAVE_PROC_OPS
  56. static struct proc_ops file_ops_4_our_proc_file = {
  57. .proc_read = procfs_read,
  58. .proc_write = procfs_write,
  59. .proc_open = procfs_open,
  60. .proc_release = procfs_close,
  61. };
  62. #else
  63. static const struct file_operations file_ops_4_our_proc_file = {
  64. .read = procfs_read,
  65. .write = procfs_write,
  66. .open = procfs_open,
  67. .release = procfs_close,
  68. };
  69. #endif
  70. static int __init procfs3_init(void)
  71. {
  72. our_proc_file = proc_create(PROCFS_ENTRY_FILENAME, 0644, NULL,
  73. &file_ops_4_our_proc_file);
  74. if (our_proc_file == NULL) {
  75. remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL);
  76. pr_debug("Error: Could not initialize /proc/%s\n",
  77. PROCFS_ENTRY_FILENAME);
  78. return -ENOMEM;
  79. }
  80. proc_set_size(our_proc_file, 80);
  81. proc_set_user(our_proc_file, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
  82. pr_debug("/proc/%s created\n", PROCFS_ENTRY_FILENAME);
  83. return 0;
  84. }
  85. static void __exit procfs3_exit(void)
  86. {
  87. remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL);
  88. pr_debug("/proc/%s removed\n", PROCFS_ENTRY_FILENAME);
  89. }
  90. module_init(procfs3_init);
  91. module_exit(procfs3_exit);
  92. MODULE_LICENSE("GPL");