procfs1.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #define procfs_name "helloworld"
  9. struct proc_dir_entry *Our_Proc_File;
  10. ssize_t procfile_read(struct file *filePointer,
  11. char *buffer,
  12. size_t buffer_length,
  13. loff_t *offset)
  14. {
  15. int ret = 0;
  16. if (strlen(buffer) == 0) {
  17. pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
  18. ret = copy_to_user(buffer, "HelloWorld!\n", sizeof("HelloWorld!\n"));
  19. ret = sizeof("HelloWorld!\n");
  20. }
  21. return ret;
  22. }
  23. static const struct proc_ops proc_file_fops = {
  24. .proc_read = procfile_read,
  25. };
  26. int init_module()
  27. {
  28. Our_Proc_File = proc_create(procfs_name, 0644, NULL, &proc_file_fops);
  29. if (NULL == Our_Proc_File) {
  30. proc_remove(Our_Proc_File);
  31. pr_alert("Error:Could not initialize /proc/%s\n", procfs_name);
  32. return -ENOMEM;
  33. }
  34. pr_info("/proc/%s created\n", procfs_name);
  35. return 0;
  36. }
  37. void cleanup_module()
  38. {
  39. proc_remove(Our_Proc_File);
  40. pr_info("/proc/%s removed\n", procfs_name);
  41. }
  42. MODULE_LICENSE("GPL");