Linux Kernel Procfs Guide
Linux Kernel Procfs Guide
Revision History
Revision 1.0 May 30, 2001
Initial revision posted to linux-kernel
Revision 1.1 June 3, 2001
Revised after comments from linux-kernel
Table of Contents
Preface .......................................................................................................................................................iv
1. Introduction............................................................................................................................................1
2. Managing procfs entries........................................................................................................................2
2.1. Creating a regular file..................................................................................................................2
2.2. Creating a symlink ......................................................................................................................2
2.3. Creating a directory.....................................................................................................................2
2.4. Removing an entry ......................................................................................................................3
3. Communicating with userland .............................................................................................................4
3.1. Reading data................................................................................................................................4
3.2. Writing data.................................................................................................................................5
3.3. A single call back for many files.................................................................................................5
4. Tips and tricks........................................................................................................................................7
4.1. Convenience functions ................................................................................................................7
4.2. Modules.......................................................................................................................................7
4.3. Mode and ownership ...................................................................................................................7
5. Example ..................................................................................................................................................8
iii
Preface
This guide describes the use of the procfs file system from within the Linux kernel. The idea to write this
guide came up on the #kernelnewbies IRC channel (see http://www.kernelnewbies.org/), when Jeff
Garzik explained the use of procfs and forwarded me a message Alexander Viro wrote to the linux-kernel
mailing list. I agreed to write it up nicely, so here it is.
Id like to thank Jeff Garzik <[email protected]> and Alexander Viro
<[email protected]> for their input, Tim Waugh
<[email protected]> for his Selfdocbook (http://people.redhat.com/twaugh/docbook/selfdocbook/),
and Marc Joosen <[email protected]> for proofreading.
Erik
iv
Chapter 1. Introduction
The /proc file system (procfs) is a special file system in the linux kernel. Its a virtual file system: it is
not associated with a block device but exists only in memory. The files in the procfs are there to allow
userland programs access to certain information from the kernel (like process information in
/proc/[0-9]+/), but also for debug purposes (like /proc/ksyms).
This guide describes the use of the procfs file system from within the Linux kernel. It starts by
introducing all relevant functions to manage the files within the file system. After that it shows how to
communicate with userland, and some tips and tricks will be pointed out. Finally a complete example
will be shown.
Note that the files in /proc/sys are sysctl files: they dont belong to procfs and are governed by a
completely different API described in the Kernel API book.
This function creates a regular file with the name name, file mode mode in the directory parent. To
create a file in the root of the procfs, use NULL as parent parameter. When successful, the function will
return a pointer to the freshly created struct proc_dir_entry; otherwise it will return NULL. Chapter 3
describes how to do something useful with regular files.
Note that it is specifically supported that you can pass a path that spans multiple directories. For example
create_proc_entry("drivers/via0/info") will create the via0 directory if necessary, with
standard 0755 permissions.
If you only want to be able to read the file, the function create_proc_read_entry described in
Section 4.1 may be used to create and initialise the procfs entry in one single call.
This creates a symlink in the procfs directory parent that points from name to dest. This translates in
userland to ln -s dest name.
Removes the entry name in the directory parent from the procfs. Entries are removed by their name,
not by the struct proc_dir_entry returned by the various create functions. Note that this function doesnt
recursively remove entries.
Be sure to free the data entry from the struct proc_dir_entry before remove_proc_entry is called
(that is: if there was some data allocated, of course). See Section 3.3 for more information on using the
data entry.
If you only want to use a the read_proc, the function create_proc_read_entry described in
Section 4.1 may be used to create and initialise the procfs entry in one single call.
The read function should write its information into the buffer , which will be exactly PAGE_SIZE bytes
long.
The parameter peof should be used to signal that the end of the file has been reached by writing 1 to the
memory location peof points to.
The data parameter can be used to create a single call back function for several files, see Section 3.3.
The rest of the parameters and the return value are described by a comment in fs/proc/generic.c as
follows:
The write function should read count bytes at maximum from the buffer . Note that the buffer
doesnt live in the kernels memory space, so it should first be copied to kernel space with
copy_from_user. The file parameter is usually ignored. Section 3.3 shows how to use the data
parameter.
Again, Chapter 5 shows how to use this call back function.
Be sure to free the data data field when removing the procfs entry.
This function creates a regular file in exactly the same way as create_proc_entry from Section 2.1
does, but also allows to set the read function read_proc in one call. This function can set the data as
well, like explained in Section 3.3.
4.2. Modules
If procfs is being used from within a module, be sure to set the owner field in the struct proc_dir_entry
to THIS_MODULE.
struct proc_dir_entry* entry;
entry->owner = THIS_MODULE;
Chapter 5. Example
/*
* procfs_example.c: an example proc interface
*
* Copyright (C) 2001, Erik Mouw ([email protected])
*
* This file accompanies the procfs-guide in the Linux kernel
* source. Its main use is to demonstrate the concepts and
* functions described in the guide.
*
* This software has been developed while working on the LART
* computing board (http://www.lartmaker.nl), which was sponsored
* by the Delt University of Technology projects Mobile Multi-media
* Communications and Ubiquitous Communications.
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*
*/
#include
#include
#include
#include
#include
#include
<linux/module.h>
<linux/kernel.h>
<linux/init.h>
<linux/proc_fs.h>
<linux/jiffies.h>
<asm/uaccess.h>
Chapter 5. Example
Chapter 5. Example
fb_data->value[len] = \0;
return len;
}
10
Chapter 5. Example
strcpy(bar_data.name, "bar");
strcpy(bar_data.value, "bar");
bar_file->data = &bar_data;
bar_file->read_proc = proc_read_foobar;
bar_file->write_proc = proc_write_foobar;
bar_file->owner = THIS_MODULE;
/* create symlink */
symlink = proc_symlink("jiffies_too", example_dir,
"jiffies");
if(symlink == NULL) {
rv = -ENOMEM;
goto no_symlink;
}
symlink->owner = THIS_MODULE;
/* everything OK */
printk(KERN_INFO "%s %s initialised\n",
MODULE_NAME, MODULE_VERS);
return 0;
no_symlink:
remove_proc_entry("bar", example_dir);
no_bar:
remove_proc_entry("foo", example_dir);
no_foo:
remove_proc_entry("jiffies", example_dir);
no_jiffies:
remove_proc_entry(MODULE_NAME, NULL);
out:
return rv;
}
module_init(init_procfs_example);
module_exit(cleanup_procfs_example);
MODULE_AUTHOR("Erik Mouw");
MODULE_DESCRIPTION("procfs examples");
11
Chapter 5. Example
MODULE_LICENSE("GPL");
12