seq_file_single_open.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#seq-file-single-open */
  2. #include <linux/debugfs.h>
  3. #include <linux/errno.h> /* EFAULT */
  4. #include <linux/fs.h>
  5. #include <linux/module.h>
  6. #include <linux/printk.h> /* pr_info */
  7. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
  8. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  9. #include <uapi/linux/stat.h> /* S_IRUSR */
  10. static struct dentry *debugfs_file;
  11. static int show(struct seq_file *m, void *v)
  12. {
  13. seq_printf(m, "ab\ncd\n");
  14. return 0;
  15. }
  16. static int open(struct inode *inode, struct file *file)
  17. {
  18. return single_open(file, show, NULL);
  19. }
  20. static const struct file_operations fops = {
  21. .llseek = seq_lseek,
  22. .open = open,
  23. .owner = THIS_MODULE,
  24. .read = seq_read,
  25. .release = single_release,
  26. };
  27. static int myinit(void)
  28. {
  29. debugfs_file = debugfs_create_file("lkmc_seq_file_single_open", S_IRUSR, NULL, NULL, &fops);
  30. return 0;
  31. }
  32. static void myexit(void)
  33. {
  34. debugfs_remove(debugfs_file);
  35. }
  36. module_init(myinit)
  37. module_exit(myexit)
  38. MODULE_LICENSE("GPL");