seq_file.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#seq-file */
  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/slab.h> /* kmalloc, kfree */
  9. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  10. #include <uapi/linux/stat.h> /* S_IRUSR */
  11. static int max = 2;
  12. module_param(max, int, S_IRUSR | S_IWUSR);
  13. static struct dentry *debugfs_file;
  14. /* Called at the beginning of every read.
  15. *
  16. * The return value is passsed to the first show.
  17. * It normally represents the current position of the iterator.
  18. * It could be any struct, but we use just a single integer here.
  19. *
  20. * NULL return means stop should be called next, and so the read will be empty..
  21. * This happens for example for an ftell that goes beyond the file size.
  22. */
  23. static void *start(struct seq_file *s, loff_t *pos)
  24. {
  25. loff_t *spos;
  26. pr_info("start pos = %llx\n", (unsigned long long)*pos);
  27. spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
  28. if (!spos || *pos >= max)
  29. return NULL;
  30. *spos = *pos;
  31. return spos;
  32. }
  33. /* The return value is passed to next show.
  34. * If NULL, stop is called next instead of show, and read ends.
  35. *
  36. * Can get called multiple times, until enough data is returned for the read.
  37. */
  38. static void *next(struct seq_file *s, void *v, loff_t *pos)
  39. {
  40. loff_t *spos;
  41. spos = v;
  42. pr_info("next pos = %llx\n", (unsigned long long)*pos);
  43. if (*pos >= max)
  44. return NULL;
  45. *pos = ++*spos;
  46. return spos;
  47. }
  48. /* Called at the end of every read. */
  49. static void stop(struct seq_file *s, void *v)
  50. {
  51. pr_info("stop\n");
  52. kfree(v);
  53. }
  54. /* Return 0 means success, SEQ_SKIP ignores previous prints, negative for error. */
  55. static int show(struct seq_file *s, void *v)
  56. {
  57. loff_t *spos;
  58. spos = v;
  59. pr_info("show pos = %llx\n", (unsigned long long)*spos);
  60. seq_printf(s, "%llx\n", (long long unsigned)*spos);
  61. return 0;
  62. }
  63. static struct seq_operations my_seq_ops = {
  64. .next = next,
  65. .show = show,
  66. .start = start,
  67. .stop = stop,
  68. };
  69. static int open(struct inode *inode, struct file *file)
  70. {
  71. pr_info("open\n");
  72. return seq_open(file, &my_seq_ops);
  73. }
  74. static struct file_operations fops = {
  75. .owner = THIS_MODULE,
  76. .llseek = seq_lseek,
  77. .open = open,
  78. .read = seq_read,
  79. .release = seq_release
  80. };
  81. static int myinit(void)
  82. {
  83. debugfs_file = debugfs_create_file(
  84. "lkmc_seq_file", S_IRUSR, NULL, NULL, &fops);
  85. if (debugfs_file) {
  86. return 0;
  87. } else {
  88. return -EINVAL;
  89. }
  90. }
  91. static void myexit(void)
  92. {
  93. debugfs_remove(debugfs_file);
  94. }
  95. module_init(myinit)
  96. module_exit(myexit)
  97. MODULE_LICENSE("GPL");