procfs.c 833 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#procfs */
  2. #include <linux/module.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_open, single_release */
  5. #include <uapi/linux/stat.h> /* S_IRUSR */
  6. static const char *filename = "lkmc_procfs";
  7. static int show(struct seq_file *m, void *v)
  8. {
  9. seq_printf(m, "abcd\n");
  10. return 0;
  11. }
  12. static int open(struct inode *inode, struct file *file)
  13. {
  14. return single_open(file, show, NULL);
  15. }
  16. static const struct proc_ops pops = {
  17. .proc_lseek = seq_lseek,
  18. .proc_open = open,
  19. .proc_read = seq_read,
  20. .proc_release = single_release,
  21. };
  22. static int myinit(void)
  23. {
  24. proc_create(filename, 0, NULL, &pops);
  25. return 0;
  26. }
  27. static void myexit(void)
  28. {
  29. remove_proc_entry(filename, NULL);
  30. }
  31. module_init(myinit)
  32. module_exit(myexit)
  33. MODULE_LICENSE("GPL");