procfs2.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /* This structure hold information about the /proc file */
  15. static struct proc_dir_entry *Our_Proc_File;
  16. /* The buffer used to store character for this module */
  17. static char procfs_buffer[PROCFS_MAX_SIZE];
  18. /* The size of the buffer */
  19. static unsigned long procfs_buffer_size = 0;
  20. /* This function is called then the /proc file is read */
  21. ssize_t procfile_read(struct file *filePointer,
  22. char *buffer,
  23. size_t buffer_length,
  24. loff_t *offset)
  25. {
  26. char s[13] = "HelloWorld!\n";
  27. int len = sizeof(s);
  28. ssize_t ret = len;
  29. if (*offset >= len || copy_to_user(buffer, s, len)) {
  30. pr_info("copy_to_user failed\n");
  31. ret = 0;
  32. } else {
  33. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  34. *offset += len;
  35. }
  36. return ret;
  37. }
  38. /* This function is called with the /proc file is written. */
  39. static ssize_t procfile_write(struct file *file,
  40. const char *buff,
  41. size_t len,
  42. loff_t *off)
  43. {
  44. procfs_buffer_size = len;
  45. if (procfs_buffer_size > PROCFS_MAX_SIZE)
  46. procfs_buffer_size = PROCFS_MAX_SIZE;
  47. if (copy_from_user(procfs_buffer, buff, procfs_buffer_size))
  48. return -EFAULT;
  49. procfs_buffer[procfs_buffer_size] = '\0';
  50. return procfs_buffer_size;
  51. }
  52. #ifdef HAVE_PROC_OPS
  53. static const struct proc_ops proc_file_fops = {
  54. .proc_read = procfile_read,
  55. .proc_write = procfile_write,
  56. };
  57. #else
  58. static const struct file_operations proc_file_fops = {
  59. .read = procfile_read,
  60. .write = procfile_write,
  61. };
  62. #endif
  63. static int __init procfs2_init(void)
  64. {
  65. Our_Proc_File = proc_create(PROCFS_NAME, 0644, NULL, &proc_file_fops);
  66. if (NULL == Our_Proc_File) {
  67. proc_remove(Our_Proc_File);
  68. pr_alert("Error:Could not initialize /proc/%s\n", PROCFS_NAME);
  69. return -ENOMEM;
  70. }
  71. pr_info("/proc/%s created\n", PROCFS_NAME);
  72. return 0;
  73. }
  74. static void __exit procfs2_exit(void)
  75. {
  76. proc_remove(Our_Proc_File);
  77. pr_info("/proc/%s removed\n", PROCFS_NAME);
  78. }
  79. module_init(procfs2_init);
  80. module_exit(procfs2_exit);
  81. MODULE_LICENSE("GPL");