procfs_example.c 4.6 KB

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