seq_file.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Adapted from: Documentation/filesystems/seq_file.txt
  3. but we limit the count to the max module parameter.
  4. Writting trivial read fops is repetitive and error prone.
  5. The seq_file API makes the process much easier for those trivial cases.
  6. This example is behaves just like a file that contains:
  7. 0
  8. 1
  9. 2
  10. However, we only store a single integer in memory
  11. and calculate the file on the fly in an iterator fashion.
  12. There is not write version, as writes are more complex:
  13. https://stackoverflow.com/questions/30710517/how-to-implement-a-writable-proc-file-by-using-seq-file-in-a-driver-module
  14. Bibliography:
  15. - Documentation/filesystems/seq_file.txt
  16. - https://stackoverflow.com/questions/25399112/how-to-use-a-seq-file-in-linux-modules
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/errno.h> /* EFAULT */
  20. #include <linux/fs.h>
  21. #include <linux/module.h>
  22. #include <linux/printk.h> /* pr_info */
  23. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
  24. #include <linux/slab.h> /* kmalloc, kfree */
  25. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  26. #include <uapi/linux/stat.h> /* S_IRUSR */
  27. static int max = 2;
  28. module_param(max, int, S_IRUSR | S_IWUSR);
  29. static struct dentry *debugfs_file;
  30. /* Called at the beginning of every read.
  31. *
  32. * The return value is passsed to the first show.
  33. * It normally represents the current position of the iterator.
  34. * It could be any struct, but we use just a single integer here.
  35. *
  36. * NULL return means stop should be called next, and so the read will be empty..
  37. * This happens for example for an ftell that goes beyond the file size.
  38. */
  39. static void *start(struct seq_file *s, loff_t *pos)
  40. {
  41. loff_t *spos;
  42. pr_info("start pos = %llx\n", (unsigned long long)*pos);
  43. spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
  44. if (!spos || *pos >= max)
  45. return NULL;
  46. *spos = *pos;
  47. return spos;
  48. }
  49. /* The return value is passed to next show.
  50. * If NULL, stop is called next instead of show, and read ends.
  51. *
  52. * Can get called multiple times, until enough data is returned for the read.
  53. */
  54. static void *next(struct seq_file *s, void *v, loff_t *pos)
  55. {
  56. loff_t *spos;
  57. spos = v;
  58. pr_info("next pos = %llx\n", (unsigned long long)*pos);
  59. if (*pos >= max)
  60. return NULL;
  61. *pos = ++*spos;
  62. return spos;
  63. }
  64. /* Called at the end of every read. */
  65. static void stop(struct seq_file *s, void *v)
  66. {
  67. pr_info("stop\n");
  68. kfree(v);
  69. }
  70. /* Return 0 means success, SEQ_SKIP ignores previous prints, negative for error. */
  71. static int show(struct seq_file *s, void *v)
  72. {
  73. loff_t *spos;
  74. spos = v;
  75. pr_info("show pos = %llx\n", (unsigned long long)*spos);
  76. seq_printf(s, "%llx\n", (long long unsigned)*spos);
  77. return 0;
  78. }
  79. static struct seq_operations my_seq_ops = {
  80. .next = next,
  81. .show = show,
  82. .start = start,
  83. .stop = stop,
  84. };
  85. static int open(struct inode *inode, struct file *file)
  86. {
  87. pr_info("open\n");
  88. return seq_open(file, &my_seq_ops);
  89. }
  90. static struct file_operations fops = {
  91. .owner = THIS_MODULE,
  92. .llseek = seq_lseek,
  93. .open = open,
  94. .read = seq_read,
  95. .release = seq_release
  96. };
  97. static int myinit(void)
  98. {
  99. debugfs_file = debugfs_create_file(
  100. "lkmc_seq_file", S_IRUSR, NULL, NULL, &fops);
  101. if (debugfs_file) {
  102. return 0;
  103. } else {
  104. return -EINVAL;
  105. }
  106. }
  107. static void myexit(void)
  108. {
  109. debugfs_remove(debugfs_file);
  110. }
  111. module_init(myinit)
  112. module_exit(myexit)
  113. MODULE_LICENSE("GPL");