2 * procfs_example.c: an example proc interface
4 * Copyright (C) 2001, Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
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.
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.
16 * The author can be reached at:
19 * Information and Communication Theory Group
20 * Faculty of Information Technology and Systems
21 * Delft University of Technology
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.
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
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
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/init.h>
49 #include <linux/proc_fs.h>
50 #include <linux/sched.h>
51 #include <asm/uaccess.h>
54 #define MODULE_VERSION "1.0"
55 #define MODULE_NAME "procfs_example"
60 char name[FOOBAR_LEN + 1];
61 char value[FOOBAR_LEN + 1];
65 static struct proc_dir_entry *example_dir, *foo_file,
66 *bar_file, *jiffies_file, *tty_device, *symlink;
69 struct fb_data_t foo_data, bar_data;
72 static int proc_read_jiffies(char *page, char **start,
80 len = sprintf(page, "jiffies = %ld\n",
89 static int proc_read_foobar(char *page, char **start,
94 struct fb_data_t *fb_data = (struct fb_data_t *)data;
98 len = sprintf(page, "%s = '%s'\n",
99 fb_data->name, fb_data->value);
107 static int proc_write_foobar(struct file *file,
113 struct fb_data_t *fb_data = (struct fb_data_t *)data;
117 if(count > FOOBAR_LEN)
122 if(copy_from_user(fb_data->value, buffer, len)) {
127 fb_data->value[len] = '\0';
135 static int __init init_procfs_example(void)
139 /* create directory */
140 example_dir = proc_mkdir(MODULE_NAME, NULL);
141 if(example_dir == NULL) {
146 example_dir->owner = THIS_MODULE;
148 /* create jiffies using convenience function */
149 jiffies_file = create_proc_read_entry("jiffies",
153 if(jiffies_file == NULL) {
158 jiffies_file->owner = THIS_MODULE;
160 /* create foo and bar files using same callback
163 foo_file = create_proc_entry("foo", 0644, example_dir);
164 if(foo_file == NULL) {
169 strcpy(foo_data.name, "foo");
170 strcpy(foo_data.value, "foo");
171 foo_file->data = &foo_data;
172 foo_file->read_proc = proc_read_foobar;
173 foo_file->write_proc = proc_write_foobar;
174 foo_file->owner = THIS_MODULE;
176 bar_file = create_proc_entry("bar", 0644, example_dir);
177 if(bar_file == NULL) {
182 strcpy(bar_data.name, "bar");
183 strcpy(bar_data.value, "bar");
184 bar_file->data = &bar_data;
185 bar_file->read_proc = proc_read_foobar;
186 bar_file->write_proc = proc_write_foobar;
187 bar_file->owner = THIS_MODULE;
189 /* create tty device */
190 tty_device = proc_mknod("tty", S_IFCHR | 0666,
191 example_dir, MKDEV(5, 0));
192 if(tty_device == NULL) {
197 tty_device->owner = THIS_MODULE;
200 symlink = proc_symlink("jiffies_too", example_dir,
202 if(symlink == NULL) {
207 symlink->owner = THIS_MODULE;
210 printk(KERN_INFO "%s %s initialised\n",
211 MODULE_NAME, MODULE_VERSION);
215 remove_proc_entry("tty", example_dir);
217 remove_proc_entry("bar", example_dir);
219 remove_proc_entry("foo", example_dir);
221 remove_proc_entry("jiffies", example_dir);
223 remove_proc_entry(MODULE_NAME, NULL);
229 static void __exit cleanup_procfs_example(void)
231 remove_proc_entry("jiffies_too", example_dir);
232 remove_proc_entry("tty", example_dir);
233 remove_proc_entry("bar", example_dir);
234 remove_proc_entry("foo", example_dir);
235 remove_proc_entry("jiffies", example_dir);
236 remove_proc_entry(MODULE_NAME, NULL);
238 printk(KERN_INFO "%s %s removed\n",
239 MODULE_NAME, MODULE_VERSION);
243 module_init(init_procfs_example);
244 module_exit(cleanup_procfs_example);
246 MODULE_AUTHOR("Erik Mouw");
247 MODULE_DESCRIPTION("procfs examples");