1
0

procfs2.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. char s[13] = "HelloWorld!\n";
  39. int len = sizeof(s);
  40. ssize_t ret = len;
  41. if (*offset >= len || copy_to_user(buffer, s, len)) {
  42. pr_info("copy_to_user failed\n");
  43. ret = 0;
  44. }
  45. else {
  46. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  47. *offset += len;
  48. }
  49. return ret;
  50. }
  51. /**
  52. * This function is called with the /proc file is written
  53. *
  54. */
  55. static ssize_t procfile_write(struct file *file,
  56. const char *buff,
  57. size_t len,
  58. loff_t *off)
  59. {
  60. procfs_buffer_size = len;
  61. if (procfs_buffer_size > PROCFS_MAX_SIZE)
  62. procfs_buffer_size = PROCFS_MAX_SIZE;
  63. if (copy_from_user(procfs_buffer, buff, procfs_buffer_size))
  64. return -EFAULT;
  65. procfs_buffer[procfs_buffer_size] = '\0';
  66. return procfs_buffer_size;
  67. }
  68. #ifdef HAVE_PROC_OPS
  69. static const struct proc_ops proc_file_fops = {
  70. .proc_read = procfile_read,
  71. .proc_write = procfile_write,
  72. };
  73. #else
  74. static const struct file_operations proc_file_fops = {
  75. .read = procfile_read,
  76. .write = procfile_write,
  77. };
  78. #endif
  79. /**
  80. *This function is called when the module is loaded
  81. *
  82. */
  83. int init_module()
  84. {
  85. Our_Proc_File = proc_create(PROCFS_NAME, 0644, NULL, &proc_file_fops);
  86. if (NULL == Our_Proc_File) {
  87. proc_remove(Our_Proc_File);
  88. pr_alert("Error:Could not initialize /proc/%s\n", PROCFS_NAME);
  89. return -ENOMEM;
  90. }
  91. pr_info("/proc/%s created\n", PROCFS_NAME);
  92. return 0;
  93. }
  94. /**
  95. *This function is called when the module is unloaded
  96. *
  97. */
  98. void cleanup_module()
  99. {
  100. proc_remove(Our_Proc_File);
  101. pr_info("/proc/%s removed\n", PROCFS_NAME);
  102. }
  103. MODULE_LICENSE("GPL");