procfs4.c 3.2 KB

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