procfs4.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. MODULE_LICENSE("GPL");
  15. /* This function is called at the beginning of a sequence.
  16. * ie, when:
  17. * - the /proc file is read (first time)
  18. * - after the function stop (end of sequence)
  19. */
  20. static void *my_seq_start(struct seq_file *s, loff_t *pos)
  21. {
  22. static unsigned long counter = 0;
  23. /* beginning a new sequence? */
  24. if (*pos == 0) {
  25. /* yes => return a non null value to begin the sequence */
  26. return &counter;
  27. }
  28. /* no => it is the end of the sequence, return end to stop reading */
  29. *pos = 0;
  30. return NULL;
  31. }
  32. /* This function is called after the beginning of a sequence.
  33. * It is called untill the return is NULL (this ends the sequence).
  34. */
  35. static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
  36. {
  37. unsigned long *tmp_v = (unsigned long *) v;
  38. (*tmp_v)++;
  39. (*pos)++;
  40. return NULL;
  41. }
  42. /* This function is called at the end of a sequence. */
  43. static void my_seq_stop(struct seq_file *s, void *v)
  44. {
  45. /* nothing to do, we use a static value in start() */
  46. }
  47. /* This function is called for each "step" of a sequence. */
  48. static int my_seq_show(struct seq_file *s, void *v)
  49. {
  50. loff_t *spos = (loff_t *) v;
  51. seq_printf(s, "%Ld\n", *spos);
  52. return 0;
  53. }
  54. /* This structure gather "function" to manage the sequence */
  55. static struct seq_operations my_seq_ops = {
  56. .start = my_seq_start,
  57. .next = my_seq_next,
  58. .stop = my_seq_stop,
  59. .show = my_seq_show,
  60. };
  61. /* This function is called when the /proc file is open. */
  62. static int my_open(struct inode *inode, struct file *file)
  63. {
  64. return seq_open(file, &my_seq_ops);
  65. };
  66. /* This structure gather "function" that manage the /proc file */
  67. #ifdef HAVE_PROC_OPS
  68. static const struct proc_ops my_file_ops = {
  69. .proc_open = my_open,
  70. .proc_read = seq_read,
  71. .proc_lseek = seq_lseek,
  72. .proc_release = seq_release,
  73. };
  74. #else
  75. static const struct file_operations my_file_ops = {
  76. .open = my_open,
  77. .read = seq_read,
  78. .llseek = seq_lseek,
  79. .release = seq_release,
  80. };
  81. #endif
  82. /* This function is called when the module is loaded. */
  83. int init_module(void)
  84. {
  85. struct proc_dir_entry *entry;
  86. entry = proc_create(PROC_NAME, 0, NULL, &my_file_ops);
  87. if (entry == NULL) {
  88. remove_proc_entry(PROC_NAME, NULL);
  89. pr_debug("Error: Could not initialize /proc/%s\n", PROC_NAME);
  90. return -ENOMEM;
  91. }
  92. return 0;
  93. }
  94. /* This function is called when the module is unloaded. */
  95. void cleanup_module(void)
  96. {
  97. remove_proc_entry(PROC_NAME, NULL);
  98. pr_debug("/proc/%s removed\n", PROC_NAME);
  99. }