procfs2.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * procfs2.c - create a "file" in /proc
  3. */
  4. #include <linux/kernel.h> /* We're doing kernel work */
  5. #include <linux/module.h> /* Specifically, a module */
  6. #include <linux/proc_fs.h> /* Necessary because we use the proc fs */
  7. #include <linux/uaccess.h> /* for copy_from_user */
  8. #include <linux/version.h>
  9. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
  10. #define HAVE_PROC_OPS
  11. #endif
  12. #define PROCFS_MAX_SIZE 1024
  13. #define PROCFS_NAME "buffer1k"
  14. /**
  15. * This structure hold information about the /proc file
  16. *
  17. */
  18. static struct proc_dir_entry *Our_Proc_File;
  19. /**
  20. * The buffer used to store character for this module
  21. *
  22. */
  23. static char procfs_buffer[PROCFS_MAX_SIZE];
  24. /**
  25. * The size of the buffer
  26. *
  27. */
  28. static unsigned long procfs_buffer_size = 0;
  29. /**
  30. * This function is called then the /proc file is read
  31. *
  32. */
  33. ssize_t procfile_read(struct file *filePointer,
  34. char *buffer,
  35. size_t buffer_length,
  36. loff_t *offset)
  37. {
  38. int ret = 0;
  39. if (strlen(buffer) == 0) {
  40. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  41. ret = copy_to_user(buffer, "HelloWorld!\n", sizeof("HelloWorld!\n"));
  42. ret = sizeof("HelloWorld!\n");
  43. }
  44. return ret;
  45. }
  46. /**
  47. * This function is called with the /proc file is written
  48. *
  49. */
  50. static ssize_t procfile_write(struct file *file,
  51. const char *buff,
  52. size_t len,
  53. loff_t *off)
  54. {
  55. procfs_buffer_size = len;
  56. if (procfs_buffer_size > PROCFS_MAX_SIZE)
  57. procfs_buffer_size = PROCFS_MAX_SIZE;
  58. if (copy_from_user(procfs_buffer, buff, procfs_buffer_size))
  59. return -EFAULT;
  60. procfs_buffer[procfs_buffer_size] = '\0';
  61. return procfs_buffer_size;
  62. }
  63. #ifdef HAVE_PROC_OPS
  64. static const struct proc_ops proc_file_fops = {
  65. .proc_read = procfile_read,
  66. .proc_write = procfile_write,
  67. };
  68. #else
  69. static const struct file_operations proc_file_fops = {
  70. .read = procfile_read,
  71. .write = procfile_write,
  72. };
  73. #endif
  74. /**
  75. *This function is called when the module is loaded
  76. *
  77. */
  78. int init_module()
  79. {
  80. Our_Proc_File = proc_create(PROCFS_NAME, 0644, NULL, &proc_file_fops);
  81. if (NULL == Our_Proc_File) {
  82. proc_remove(Our_Proc_File);
  83. pr_alert("Error:Could not initialize /proc/%s\n", PROCFS_NAME);
  84. return -ENOMEM;
  85. }
  86. pr_info("/proc/%s created\n", PROCFS_NAME);
  87. return 0;
  88. }
  89. /**
  90. *This function is called when the module is unloaded
  91. *
  92. */
  93. void cleanup_module()
  94. {
  95. proc_remove(Our_Proc_File);
  96. pr_info("/proc/%s removed\n", PROCFS_NAME);
  97. }
  98. MODULE_LICENSE("GPL");