procfs_example.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * procfs_example.c: an example proc interface
  3. *
  4. * Copyright (C) 2001, Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  5. *
  6. * This file accompanies the procfs-guide in the Linux kernel
  7. * source. Its main use is to demonstrate the concepts and
  8. * functions described in the guide.
  9. *
  10. * This software has been developed while working on the LART
  11. * computing board (http://www.lart.tudelft.nl/), which is
  12. * sponsored by the Mobile Multi-media Communications
  13. * (http://www.mmc.tudelft.nl/) and Ubiquitous Communications
  14. * (http://www.ubicom.tudelft.nl/) projects.
  15. *
  16. * The author can be reached at:
  17. *
  18. * Erik Mouw
  19. * Information and Communication Theory Group
  20. * Faculty of Information Technology and Systems
  21. * Delft University of Technology
  22. * P.O. Box 5031
  23. * 2600 GA Delft
  24. * The Netherlands
  25. *
  26. *
  27. * This program is free software; you can redistribute
  28. * it and/or modify it under the terms of the GNU General
  29. * Public License as published by the Free Software
  30. * Foundation; either version 2 of the License, or (at your
  31. * option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be
  34. * useful, but WITHOUT ANY WARRANTY; without even the implied
  35. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  36. * PURPOSE. See the GNU General Public License for more
  37. * details.
  38. *
  39. * You should have received a copy of the GNU General Public
  40. * License along with this program; if not, write to the
  41. * Free Software Foundation, Inc., 59 Temple Place,
  42. * Suite 330, Boston, MA 02111-1307 USA
  43. *
  44. */
  45. #include <linux/module.h>
  46. #include <linux/kernel.h>
  47. #include <linux/init.h>
  48. #include <linux/proc_fs.h>
  49. #include <linux/jiffies.h>
  50. #include <asm/uaccess.h>
  51. #define MODULE_VERS "1.0"
  52. #define MODULE_NAME "procfs_example"
  53. #define FOOBAR_LEN 8
  54. struct fb_data_t {
  55. char name[FOOBAR_LEN + 1];
  56. char value[FOOBAR_LEN + 1];
  57. };
  58. static struct proc_dir_entry *example_dir, *foo_file,
  59. *bar_file, *jiffies_file, *symlink;
  60. struct fb_data_t foo_data, bar_data;
  61. static int proc_read_jiffies(char *page, char **start,
  62. off_t off, int count,
  63. int *eof, void *data)
  64. {
  65. int len;
  66. len = sprintf(page, "jiffies = %ld\n",
  67. jiffies);
  68. return len;
  69. }
  70. static int proc_read_foobar(char *page, char **start,
  71. off_t off, int count,
  72. int *eof, void *data)
  73. {
  74. int len;
  75. struct fb_data_t *fb_data = (struct fb_data_t *)data;
  76. /* DON'T DO THAT - buffer overruns are bad */
  77. len = sprintf(page, "%s = '%s'\n",
  78. fb_data->name, fb_data->value);
  79. return len;
  80. }
  81. static int proc_write_foobar(struct file *file,
  82. const char *buffer,
  83. unsigned long count,
  84. void *data)
  85. {
  86. int len;
  87. struct fb_data_t *fb_data = (struct fb_data_t *)data;
  88. if(count > FOOBAR_LEN)
  89. len = FOOBAR_LEN;
  90. else
  91. len = count;
  92. if(copy_from_user(fb_data->value, buffer, len))
  93. return -EFAULT;
  94. fb_data->value[len] = '\0';
  95. return len;
  96. }
  97. static int __init init_procfs_example(void)
  98. {
  99. int rv = 0;
  100. /* create directory */
  101. example_dir = proc_mkdir(MODULE_NAME, NULL);
  102. if(example_dir == NULL) {
  103. rv = -ENOMEM;
  104. goto out;
  105. }
  106. example_dir->owner = THIS_MODULE;
  107. /* create jiffies using convenience function */
  108. jiffies_file = create_proc_read_entry("jiffies",
  109. 0444, example_dir,
  110. proc_read_jiffies,
  111. NULL);
  112. if(jiffies_file == NULL) {
  113. rv = -ENOMEM;
  114. goto no_jiffies;
  115. }
  116. jiffies_file->owner = THIS_MODULE;
  117. /* create foo and bar files using same callback
  118. * functions
  119. */
  120. foo_file = create_proc_entry("foo", 0644, example_dir);
  121. if(foo_file == NULL) {
  122. rv = -ENOMEM;
  123. goto no_foo;
  124. }
  125. strcpy(foo_data.name, "foo");
  126. strcpy(foo_data.value, "foo");
  127. foo_file->data = &foo_data;
  128. foo_file->read_proc = proc_read_foobar;
  129. foo_file->write_proc = proc_write_foobar;
  130. foo_file->owner = THIS_MODULE;
  131. bar_file = create_proc_entry("bar", 0644, example_dir);
  132. if(bar_file == NULL) {
  133. rv = -ENOMEM;
  134. goto no_bar;
  135. }
  136. strcpy(bar_data.name, "bar");
  137. strcpy(bar_data.value, "bar");
  138. bar_file->data = &bar_data;
  139. bar_file->read_proc = proc_read_foobar;
  140. bar_file->write_proc = proc_write_foobar;
  141. bar_file->owner = THIS_MODULE;
  142. /* create symlink */
  143. symlink = proc_symlink("jiffies_too", example_dir,
  144. "jiffies");
  145. if(symlink == NULL) {
  146. rv = -ENOMEM;
  147. goto no_symlink;
  148. }
  149. symlink->owner = THIS_MODULE;
  150. /* everything OK */
  151. printk(KERN_INFO "%s %s initialised\n",
  152. MODULE_NAME, MODULE_VERS);
  153. return 0;
  154. no_symlink:
  155. remove_proc_entry("tty", example_dir);
  156. no_tty:
  157. remove_proc_entry("bar", example_dir);
  158. no_bar:
  159. remove_proc_entry("foo", example_dir);
  160. no_foo:
  161. remove_proc_entry("jiffies", example_dir);
  162. no_jiffies:
  163. remove_proc_entry(MODULE_NAME, NULL);
  164. out:
  165. return rv;
  166. }
  167. static void __exit cleanup_procfs_example(void)
  168. {
  169. remove_proc_entry("jiffies_too", example_dir);
  170. remove_proc_entry("tty", example_dir);
  171. remove_proc_entry("bar", example_dir);
  172. remove_proc_entry("foo", example_dir);
  173. remove_proc_entry("jiffies", example_dir);
  174. remove_proc_entry(MODULE_NAME, NULL);
  175. printk(KERN_INFO "%s %s removed\n",
  176. MODULE_NAME, MODULE_VERS);
  177. }
  178. module_init(init_procfs_example);
  179. module_exit(cleanup_procfs_example);
  180. MODULE_AUTHOR("Erik Mouw");
  181. MODULE_DESCRIPTION("procfs examples");