procfs1.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. char s[13] = "HelloWorld!\n";
  20. int len = sizeof(s);
  21. ssize_t ret = len;
  22. if (*offset >= len || copy_to_user(buffer, s, len)) {
  23. pr_info("copy_to_user failed\n");
  24. ret = 0;
  25. }
  26. else {
  27. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  28. *offset += len;
  29. }
  30. return ret;
  31. }
  32. #ifdef HAVE_PROC_OPS
  33. static const struct proc_ops proc_file_fops = {
  34. .proc_read = procfile_read,
  35. };
  36. #else
  37. static const struct file_operations proc_file_fops = {
  38. .read = procfile_read,
  39. };
  40. #endif
  41. int init_module()
  42. {
  43. Our_Proc_File = proc_create(procfs_name, 0644, NULL, &proc_file_fops);
  44. if (NULL == Our_Proc_File) {
  45. proc_remove(Our_Proc_File);
  46. pr_alert("Error:Could not initialize /proc/%s\n", procfs_name);
  47. return -ENOMEM;
  48. }
  49. pr_info("/proc/%s created\n", procfs_name);
  50. return 0;
  51. }
  52. void cleanup_module()
  53. {
  54. proc_remove(Our_Proc_File);
  55. pr_info("/proc/%s removed\n", procfs_name);
  56. }
  57. MODULE_LICENSE("GPL");