procfs.c 878 B

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