seq_file_single.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. If you have the entire read output upfront, single_open
  3. is an even more convenient version of seq_file.
  4. This example behaves like a file that contains:
  5. ab
  6. cd
  7. */
  8. #include <linux/debugfs.h>
  9. #include <linux/errno.h> /* EFAULT */
  10. #include <linux/fs.h>
  11. #include <linux/module.h>
  12. #include <linux/printk.h> /* pr_info */
  13. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
  14. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  15. #include <uapi/linux/stat.h> /* S_IRUSR */
  16. static struct dentry *debugfs_file;
  17. static int show(struct seq_file *m, void *v)
  18. {
  19. seq_printf(m, "ab\ncd\n");
  20. return 0;
  21. }
  22. static int open(struct inode *inode, struct file *file)
  23. {
  24. return single_open(file, show, NULL);
  25. }
  26. static const struct file_operations fops = {
  27. .llseek = seq_lseek,
  28. .open = open,
  29. .owner = THIS_MODULE,
  30. .read = seq_read,
  31. .release = single_release,
  32. };
  33. static int myinit(void)
  34. {
  35. debugfs_file = debugfs_create_file(
  36. "lkmc_seq_file_single", S_IRUSR, NULL, NULL, &fops);
  37. return 0;
  38. }
  39. static void myexit(void)
  40. {
  41. debugfs_remove(debugfs_file);
  42. }
  43. module_init(myinit)
  44. module_exit(myexit)
  45. MODULE_LICENSE("GPL");