1
0

procfs1.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. procfs1.c
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/proc_fs.h>
  7. #include <linux/uaccess.h>
  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_name "helloworld"
  13. struct proc_dir_entry *Our_Proc_File;
  14. ssize_t procfile_read(struct file *filePointer,
  15. char *buffer,
  16. size_t buffer_length,
  17. loff_t *offset)
  18. {
  19. int ret = 0;
  20. if (strlen(buffer) == 0) {
  21. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  22. ret = copy_to_user(buffer, "HelloWorld!\n", sizeof("HelloWorld!\n"));
  23. ret = sizeof("HelloWorld!\n");
  24. }
  25. return ret;
  26. }
  27. #ifdef HAVE_PROC_OPS
  28. static const struct proc_ops proc_file_fops = {
  29. .proc_read = procfile_read,
  30. };
  31. #else
  32. static const struct file_operations proc_file_fops = {
  33. .read = procfile_read,
  34. };
  35. #endif
  36. int init_module()
  37. {
  38. Our_Proc_File = proc_create(procfs_name, 0644, NULL, &proc_file_fops);
  39. if (NULL == Our_Proc_File) {
  40. proc_remove(Our_Proc_File);
  41. pr_alert("Error:Could not initialize /proc/%s\n", procfs_name);
  42. return -ENOMEM;
  43. }
  44. pr_info("/proc/%s created\n", procfs_name);
  45. return 0;
  46. }
  47. void cleanup_module()
  48. {
  49. proc_remove(Our_Proc_File);
  50. pr_info("/proc/%s removed\n", procfs_name);
  51. }
  52. MODULE_LICENSE("GPL");