procfs4.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * procfs4.c - create a "file" in /proc
  3. * This program uses the seq_file library to manage the /proc file.
  4. */
  5. #include <linux/kernel.h> /* We are doing kernel work */
  6. #include <linux/module.h> /* Specifically, a module */
  7. #include <linux/proc_fs.h> /* Necessary because we use proc fs */
  8. #include <linux/seq_file.h> /* for seq_file */
  9. #include <linux/version.h>
  10. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
  11. #define HAVE_PROC_OPS
  12. #endif
  13. #define PROC_NAME "iter"
  14. /* This function is called at the beginning of a sequence.
  15. * ie, when:
  16. * - the /proc file is read (first time)
  17. * - after the function stop (end of sequence)
  18. */
  19. static void *my_seq_start(struct seq_file *s, loff_t *pos)
  20. {
  21. static unsigned long counter = 0;
  22. /* beginning a new sequence? */
  23. if (*pos == 0) {
  24. /* yes => return a non null value to begin the sequence */
  25. return &counter;
  26. }
  27. /* no => it is the end of the sequence, return end to stop reading */
  28. *pos = 0;
  29. return NULL;
  30. }
  31. /* This function is called after the beginning of a sequence.
  32. * It is called until the return is NULL (this ends the sequence).
  33. */
  34. static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
  35. {
  36. unsigned long *tmp_v = (unsigned long *)v;
  37. (*tmp_v)++;
  38. (*pos)++;
  39. return NULL;
  40. }
  41. /* This function is called at the end of a sequence. */
  42. static void my_seq_stop(struct seq_file *s, void *v)
  43. {
  44. /* nothing to do, we use a static value in start() */
  45. }
  46. /* This function is called for each "step" of a sequence. */
  47. static int my_seq_show(struct seq_file *s, void *v)
  48. {
  49. loff_t *spos = (loff_t *)v;
  50. seq_printf(s, "%Ld\n", *spos);
  51. return 0;
  52. }
  53. /* This structure gather "function" to manage the sequence */
  54. static struct seq_operations my_seq_ops = {
  55. .start = my_seq_start,
  56. .next = my_seq_next,
  57. .stop = my_seq_stop,
  58. .show = my_seq_show,
  59. };
  60. /* This function is called when the /proc file is open. */
  61. static int my_open(struct inode *inode, struct file *file)
  62. {
  63. return seq_open(file, &my_seq_ops);
  64. };
  65. /* This structure gather "function" that manage the /proc file */
  66. #ifdef HAVE_PROC_OPS
  67. static const struct proc_ops my_file_ops = {
  68. .proc_open = my_open,
  69. .proc_read = seq_read,
  70. .proc_lseek = seq_lseek,
  71. .proc_release = seq_release,
  72. };
  73. #else
  74. static const struct file_operations my_file_ops = {
  75. .open = my_open,
  76. .read = seq_read,
  77. .llseek = seq_lseek,
  78. .release = seq_release,
  79. };
  80. #endif
  81. static int __init procfs4_init(void)
  82. {
  83. struct proc_dir_entry *entry;
  84. entry = proc_create(PROC_NAME, 0, NULL, &my_file_ops);
  85. if (entry == NULL) {
  86. pr_debug("Error: Could not initialize /proc/%s\n", PROC_NAME);
  87. return -ENOMEM;
  88. }
  89. return 0;
  90. }
  91. static void __exit procfs4_exit(void)
  92. {
  93. remove_proc_entry(PROC_NAME, NULL);
  94. pr_debug("/proc/%s removed\n", PROC_NAME);
  95. }
  96. module_init(procfs4_init);
  97. module_exit(procfs4_exit);
  98. MODULE_LICENSE("GPL");