1
0

procfs1.c 1.5 KB

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