procfs2.c 2.6 KB

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