1
0

procfs2.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. } else {
  45. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  46. *offset += len;
  47. }
  48. return ret;
  49. }
  50. /**
  51. * This function is called with the /proc file is written
  52. *
  53. */
  54. static ssize_t procfile_write(struct file *file,
  55. const char *buff,
  56. size_t len,
  57. loff_t *off)
  58. {
  59. procfs_buffer_size = len;
  60. if (procfs_buffer_size > PROCFS_MAX_SIZE)
  61. procfs_buffer_size = PROCFS_MAX_SIZE;
  62. if (copy_from_user(procfs_buffer, buff, procfs_buffer_size))
  63. return -EFAULT;
  64. procfs_buffer[procfs_buffer_size] = '\0';
  65. return procfs_buffer_size;
  66. }
  67. #ifdef HAVE_PROC_OPS
  68. static const struct proc_ops proc_file_fops = {
  69. .proc_read = procfile_read,
  70. .proc_write = procfile_write,
  71. };
  72. #else
  73. static const struct file_operations proc_file_fops = {
  74. .read = procfile_read,
  75. .write = procfile_write,
  76. };
  77. #endif
  78. static int __init procfs2_init(void)
  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. static void __exit procfs2_exit(void)
  90. {
  91. proc_remove(Our_Proc_File);
  92. pr_info("/proc/%s removed\n", PROCFS_NAME);
  93. }
  94. module_init(procfs2_init);
  95. module_exit(procfs2_exit);
  96. MODULE_LICENSE("GPL");