procfs.c 984 B

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