procfs4.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #define PROC_NAME "iter"
  11. MODULE_AUTHOR("Philippe Reynes");
  12. MODULE_LICENSE("GPL");
  13. /**
  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. */
  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. } else {
  28. /* no => it's the end of the sequence, return end to stop reading */
  29. *pos = 0;
  30. return NULL;
  31. }
  32. }
  33. /**
  34. * This function is called after the beginning of a sequence.
  35. * It's called untill the return is NULL (this ends the sequence).
  36. *
  37. */
  38. static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
  39. {
  40. unsigned long *tmp_v = (unsigned long *) v;
  41. (*tmp_v)++;
  42. (*pos)++;
  43. return NULL;
  44. }
  45. /**
  46. * This function is called at the end of a sequence
  47. *
  48. */
  49. static void my_seq_stop(struct seq_file *s, void *v)
  50. {
  51. /* nothing to do, we use a static value in start() */
  52. }
  53. /**
  54. * This function is called for each "step" of a sequence
  55. *
  56. */
  57. static int my_seq_show(struct seq_file *s, void *v)
  58. {
  59. loff_t *spos = (loff_t *) v;
  60. seq_printf(s, "%Ld\n", *spos);
  61. return 0;
  62. }
  63. /**
  64. * This structure gather "function" to manage the sequence
  65. *
  66. */
  67. static struct seq_operations my_seq_ops = {.start = my_seq_start,
  68. .next = my_seq_next,
  69. .stop = my_seq_stop,
  70. .show = my_seq_show};
  71. /**
  72. * This function is called when the /proc file is open.
  73. *
  74. */
  75. static int my_open(struct inode *inode, struct file *file)
  76. {
  77. return seq_open(file, &my_seq_ops);
  78. };
  79. /**
  80. * This structure gather "function" that manage the /proc file
  81. *
  82. */
  83. static struct proc_ops my_file_ops = {.proc_open = my_open,
  84. .proc_read = seq_read,
  85. .proc_lseek = seq_lseek,
  86. .proc_release = seq_release};
  87. /**
  88. * This function is called when the module is loaded
  89. *
  90. */
  91. int init_module(void)
  92. {
  93. struct proc_dir_entry *entry;
  94. entry = proc_create(PROC_NAME, 0, NULL, &my_file_ops);
  95. if (entry == NULL) {
  96. remove_proc_entry(PROC_NAME, NULL);
  97. pr_debug("Error: Could not initialize /proc/%s\n", PROC_NAME);
  98. return -ENOMEM;
  99. }
  100. return 0;
  101. }
  102. /**
  103. * This function is called when the module is unloaded.
  104. *
  105. */
  106. void cleanup_module(void)
  107. {
  108. remove_proc_entry(PROC_NAME, NULL);
  109. pr_debug("/proc/%s removed\n", PROC_NAME);
  110. }