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/jiffies.h>
51 #include <asm/uaccess.h>
54 #define MODULE_VERS "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
, *symlink
;
69 struct fb_data_t foo_data
, bar_data
;
72 static int proc_read_jiffies(char *page
, char **start
,
78 len
= sprintf(page
, "jiffies = %ld\n",
85 static int proc_read_foobar(char *page
, char **start
,
90 struct fb_data_t
*fb_data
= (struct fb_data_t
*)data
;
92 /* DON'T DO THAT - buffer overruns are bad */
93 len
= sprintf(page
, "%s = '%s'\n",
94 fb_data
->name
, fb_data
->value
);
100 static int proc_write_foobar(struct file
*file
,
106 struct fb_data_t
*fb_data
= (struct fb_data_t
*)data
;
108 if(count
> FOOBAR_LEN
)
113 if(copy_from_user(fb_data
->value
, buffer
, len
))
116 fb_data
->value
[len
] = '\0';
122 static int __init
init_procfs_example(void)
126 /* create directory */
127 example_dir
= proc_mkdir(MODULE_NAME
, NULL
);
128 if(example_dir
== NULL
) {
133 example_dir
->owner
= THIS_MODULE
;
135 /* create jiffies using convenience function */
136 jiffies_file
= create_proc_read_entry("jiffies",
140 if(jiffies_file
== NULL
) {
145 jiffies_file
->owner
= THIS_MODULE
;
147 /* create foo and bar files using same callback
150 foo_file
= create_proc_entry("foo", 0644, example_dir
);
151 if(foo_file
== NULL
) {
156 strcpy(foo_data
.name
, "foo");
157 strcpy(foo_data
.value
, "foo");
158 foo_file
->data
= &foo_data
;
159 foo_file
->read_proc
= proc_read_foobar
;
160 foo_file
->write_proc
= proc_write_foobar
;
161 foo_file
->owner
= THIS_MODULE
;
163 bar_file
= create_proc_entry("bar", 0644, example_dir
);
164 if(bar_file
== NULL
) {
169 strcpy(bar_data
.name
, "bar");
170 strcpy(bar_data
.value
, "bar");
171 bar_file
->data
= &bar_data
;
172 bar_file
->read_proc
= proc_read_foobar
;
173 bar_file
->write_proc
= proc_write_foobar
;
174 bar_file
->owner
= THIS_MODULE
;
177 symlink
= proc_symlink("jiffies_too", example_dir
,
179 if(symlink
== NULL
) {
184 symlink
->owner
= THIS_MODULE
;
187 printk(KERN_INFO
"%s %s initialised\n",
188 MODULE_NAME
, MODULE_VERS
);
192 remove_proc_entry("tty", example_dir
);
194 remove_proc_entry("bar", example_dir
);
196 remove_proc_entry("foo", example_dir
);
198 remove_proc_entry("jiffies", example_dir
);
200 remove_proc_entry(MODULE_NAME
, NULL
);
206 static void __exit
cleanup_procfs_example(void)
208 remove_proc_entry("jiffies_too", example_dir
);
209 remove_proc_entry("tty", example_dir
);
210 remove_proc_entry("bar", example_dir
);
211 remove_proc_entry("foo", example_dir
);
212 remove_proc_entry("jiffies", example_dir
);
213 remove_proc_entry(MODULE_NAME
, NULL
);
215 printk(KERN_INFO
"%s %s removed\n",
216 MODULE_NAME
, MODULE_VERS
);
220 module_init(init_procfs_example
);
221 module_exit(cleanup_procfs_example
);
223 MODULE_AUTHOR("Erik Mouw");
224 MODULE_DESCRIPTION("procfs examples");