1
0

procfs4.c 2.9 KB

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