procfs4.c 3.2 KB

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