2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V3.1//EN"[
3 <!ENTITY procfsexample SYSTEM "procfs_example.sgml">
6 <book id="LKProcfsGuide">
8 <title>Linux Kernel Procfs Guide</title>
12 <firstname>Erik</firstname>
13 <othername>(J.A.K.)</othername>
14 <surname>Mouw</surname>
16 <orgname>Delft University of Technology</orgname>
17 <orgdiv>Faculty of Information Technology and Systems</orgdiv>
19 <email>J.A.K.Mouw@its.tudelft.nl</email>
20 <pob>PO BOX 5031</pob>
21 <postcode>2600 GA</postcode>
23 <country>The Netherlands</country>
31 <revnumber>1.0 </revnumber>
32 <date>May 30, 2001</date>
33 <revremark>Initial revision posted to linux-kernel</revremark>
36 <revnumber>1.1 </revnumber>
37 <date>June 3, 2001</date>
38 <revremark>Revised after comments from linux-kernel</revremark>
44 <holder>Erik Mouw</holder>
50 This documentation is free software; you can redistribute it
51 and/or modify it under the terms of the GNU General Public
52 License as published by the Free Software Foundation; either
53 version 2 of the License, or (at your option) any later
58 This documentation is distributed in the hope that it will be
59 useful, but WITHOUT ANY WARRANTY; without even the implied
60 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
61 PURPOSE. See the GNU General Public License for more details.
65 You should have received a copy of the GNU General Public
66 License along with this program; if not, write to the Free
67 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
72 For more details see the file COPYING in the source
73 distribution of Linux.
88 <title>Preface</title>
91 This guide describes the use of the procfs file system from
92 within the Linux kernel. The idea to write this guide came up on
93 the #kernelnewbies IRC channel (see <ulink
94 url="http://www.kernelnewbies.org/">http://www.kernelnewbies.org/</ulink>),
95 when Jeff Garzik explained the use of procfs and forwarded me a
96 message Alexander Viro wrote to the linux-kernel mailing list. I
97 agreed to write it up nicely, so here it is.
101 I'd like to thank Jeff Garzik
102 <email>jgarzik@pobox.com</email> and Alexander Viro
103 <email>viro@parcelfarce.linux.theplanet.co.uk</email> for their input,
104 Tim Waugh <email>twaugh@redhat.com</email> for his <ulink
105 url="http://people.redhat.com/twaugh/docbook/selfdocbook/">Selfdocbook</ulink>,
106 and Marc Joosen <email>marcj@historia.et.tudelft.nl</email> for
111 This documentation was written while working on the LART
112 computing board (<ulink
113 url="http://www.lart.tudelft.nl/">http://www.lart.tudelft.nl/</ulink>),
114 which is sponsored by the Mobile Multi-media Communications
116 url="http://www.mmc.tudelft.nl/">http://www.mmc.tudelft.nl/</ulink>)
117 and Ubiquitous Communications (<ulink
118 url="http://www.ubicom.tudelft.nl/">http://www.ubicom.tudelft.nl/</ulink>)
131 <title>Introduction</title>
134 The <filename class="directory">/proc</filename> file system
135 (procfs) is a special file system in the linux kernel. It's a
136 virtual file system: it is not associated with a block device
137 but exists only in memory. The files in the procfs are there to
138 allow userland programs access to certain information from the
139 kernel (like process information in <filename
140 class="directory">/proc/[0-9]+/</filename>), but also for debug
141 purposes (like <filename>/proc/ksyms</filename>).
145 This guide describes the use of the procfs file system from
146 within the Linux kernel. It starts by introducing all relevant
147 functions to manage the files within the file system. After that
148 it shows how to communicate with userland, and some tips and
149 tricks will be pointed out. Finally a complete example will be
154 Note that the files in <filename
155 class="directory">/proc/sys</filename> are sysctl files: they
156 don't belong to procfs and are governed by a completely
157 different API described in the Kernel API book.
164 <chapter id="managing">
165 <title>Managing procfs entries</title>
168 This chapter describes the functions that various kernel
169 components use to populate the procfs with files, symlinks,
170 device nodes, and directories.
174 A minor note before we start: if you want to use any of the
175 procfs functions, be sure to include the correct header file!
176 This should be one of the first lines in your code:
180 #include <linux/proc_fs.h>
186 <sect1 id="regularfile">
187 <title>Creating a regular file</title>
191 <funcdef>struct proc_dir_entry* <function>create_proc_entry</function></funcdef>
192 <paramdef>const char* <parameter>name</parameter></paramdef>
193 <paramdef>mode_t <parameter>mode</parameter></paramdef>
194 <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
199 This function creates a regular file with the name
200 <parameter>name</parameter>, file mode
201 <parameter>mode</parameter> in the directory
202 <parameter>parent</parameter>. To create a file in the root of
203 the procfs, use <constant>NULL</constant> as
204 <parameter>parent</parameter> parameter. When successful, the
205 function will return a pointer to the freshly created
206 <structname>struct proc_dir_entry</structname>; otherwise it
207 will return <constant>NULL</constant>. <xref
208 linkend="userland"> describes how to do something useful with
213 Note that it is specifically supported that you can pass a
214 path that spans multiple directories. For example
215 <function>create_proc_entry</function>(<parameter>"drivers/via0/info"</parameter>)
216 will create the <filename class="directory">via0</filename>
217 directory if necessary, with standard
218 <constant>0755</constant> permissions.
222 If you only want to be able to read the file, the function
223 <function>create_proc_read_entry</function> described in <xref
224 linkend="convenience"> may be used to create and initialise
225 the procfs entry in one single call.
233 <title>Creating a symlink</title>
237 <funcdef>struct proc_dir_entry*
238 <function>proc_symlink</function></funcdef> <paramdef>const
239 char* <parameter>name</parameter></paramdef>
240 <paramdef>struct proc_dir_entry*
241 <parameter>parent</parameter></paramdef> <paramdef>const
242 char* <parameter>dest</parameter></paramdef>
247 This creates a symlink in the procfs directory
248 <parameter>parent</parameter> that points from
249 <parameter>name</parameter> to
250 <parameter>dest</parameter>. This translates in userland to
251 <literal>ln -s</literal> <parameter>dest</parameter>
252 <parameter>name</parameter>.
257 <title>Creating a directory</title>
261 <funcdef>struct proc_dir_entry* <function>proc_mkdir</function></funcdef>
262 <paramdef>const char* <parameter>name</parameter></paramdef>
263 <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
268 Create a directory <parameter>name</parameter> in the procfs
269 directory <parameter>parent</parameter>.
277 <title>Removing an entry</title>
281 <funcdef>void <function>remove_proc_entry</function></funcdef>
282 <paramdef>const char* <parameter>name</parameter></paramdef>
283 <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
288 Removes the entry <parameter>name</parameter> in the directory
289 <parameter>parent</parameter> from the procfs. Entries are
290 removed by their <emphasis>name</emphasis>, not by the
291 <structname>struct proc_dir_entry</structname> returned by the
292 various create functions. Note that this function doesn't
293 recursively remove entries.
297 Be sure to free the <structfield>data</structfield> entry from
298 the <structname>struct proc_dir_entry</structname> before
299 <function>remove_proc_entry</function> is called (that is: if
300 there was some <structfield>data</structfield> allocated, of
301 course). See <xref linkend="usingdata"> for more information
302 on using the <structfield>data</structfield> entry.
310 <chapter id="userland">
311 <title>Communicating with userland</title>
314 Instead of reading (or writing) information directly from
315 kernel memory, procfs works with <emphasis>call back
316 functions</emphasis> for files: functions that are called when
317 a specific file is being read or written. Such functions have
318 to be initialised after the procfs file is created by setting
319 the <structfield>read_proc</structfield> and/or
320 <structfield>write_proc</structfield> fields in the
321 <structname>struct proc_dir_entry*</structname> that the
322 function <function>create_proc_entry</function> returned:
326 struct proc_dir_entry* entry;
328 entry->read_proc = read_proc_foo;
329 entry->write_proc = write_proc_foo;
333 If you only want to use a the
334 <structfield>read_proc</structfield>, the function
335 <function>create_proc_read_entry</function> described in <xref
336 linkend="convenience"> may be used to create and initialise the
337 procfs entry in one single call.
343 <title>Reading data</title>
346 The read function is a call back function that allows userland
347 processes to read data from the kernel. The read function
348 should have the following format:
353 <funcdef>int <function>read_func</function></funcdef>
354 <paramdef>char* <parameter>page</parameter></paramdef>
355 <paramdef>char** <parameter>start</parameter></paramdef>
356 <paramdef>off_t <parameter>off</parameter></paramdef>
357 <paramdef>int <parameter>count</parameter></paramdef>
358 <paramdef>int* <parameter>eof</parameter></paramdef>
359 <paramdef>void* <parameter>data</parameter></paramdef>
364 The read function should write its information into the
365 <parameter>page</parameter>. For proper use, the function
366 should start writing at an offset of
367 <parameter>off</parameter> in <parameter>page</parameter> and
368 write at most <parameter>count</parameter> bytes, but because
369 most read functions are quite simple and only return a small
370 amount of information, these two parameters are usually
371 ignored (it breaks pagers like <literal>more</literal> and
372 <literal>less</literal>, but <literal>cat</literal> still
377 If the <parameter>off</parameter> and
378 <parameter>count</parameter> parameters are properly used,
379 <parameter>eof</parameter> should be used to signal that the
380 end of the file has been reached by writing
381 <literal>1</literal> to the memory location
382 <parameter>eof</parameter> points to.
386 The parameter <parameter>start</parameter> doesn't seem to be
387 used anywhere in the kernel. The <parameter>data</parameter>
388 parameter can be used to create a single call back function for
389 several files, see <xref linkend="usingdata">.
393 The <function>read_func</function> function must return the
394 number of bytes written into the <parameter>page</parameter>.
398 <xref linkend="example"> shows how to use a read call back
407 <title>Writing data</title>
410 The write call back function allows a userland process to write
411 data to the kernel, so it has some kind of control over the
412 kernel. The write function should have the following format:
417 <funcdef>int <function>write_func</function></funcdef>
418 <paramdef>struct file* <parameter>file</parameter></paramdef>
419 <paramdef>const char* <parameter>buffer</parameter></paramdef>
420 <paramdef>unsigned long <parameter>count</parameter></paramdef>
421 <paramdef>void* <parameter>data</parameter></paramdef>
426 The write function should read <parameter>count</parameter>
427 bytes at maximum from the <parameter>buffer</parameter>. Note
428 that the <parameter>buffer</parameter> doesn't live in the
429 kernel's memory space, so it should first be copied to kernel
430 space with <function>copy_from_user</function>. The
431 <parameter>file</parameter> parameter is usually
432 ignored. <xref linkend="usingdata"> shows how to use the
433 <parameter>data</parameter> parameter.
437 Again, <xref linkend="example"> shows how to use this call back
445 <sect1 id="usingdata">
446 <title>A single call back for many files</title>
449 When a large number of almost identical files is used, it's
450 quite inconvenient to use a separate call back function for
451 each file. A better approach is to have a single call back
452 function that distinguishes between the files by using the
453 <structfield>data</structfield> field in <structname>struct
454 proc_dir_entry</structname>. First of all, the
455 <structfield>data</structfield> field has to be initialised:
459 struct proc_dir_entry* entry;
460 struct my_file_data *file_data;
462 file_data = kmalloc(sizeof(struct my_file_data), GFP_KERNEL);
463 entry->data = file_data;
467 The <structfield>data</structfield> field is a <type>void
468 *</type>, so it can be initialised with anything.
472 Now that the <structfield>data</structfield> field is set, the
473 <function>read_proc</function> and
474 <function>write_proc</function> can use it to distinguish
475 between files because they get it passed into their
476 <parameter>data</parameter> parameter:
480 int foo_read_func(char *page, char **start, off_t off,
481 int count, int *eof, void *data)
485 if(data == file_data) {
486 /* special case for this file */
488 /* normal processing */
496 Be sure to free the <structfield>data</structfield> data field
497 when removing the procfs entry.
506 <title>Tips and tricks</title>
511 <sect1 id="convenience">
512 <title>Convenience functions</title>
516 <funcdef>struct proc_dir_entry* <function>create_proc_read_entry</function></funcdef>
517 <paramdef>const char* <parameter>name</parameter></paramdef>
518 <paramdef>mode_t <parameter>mode</parameter></paramdef>
519 <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
520 <paramdef>read_proc_t* <parameter>read_proc</parameter></paramdef>
521 <paramdef>void* <parameter>data</parameter></paramdef>
526 This function creates a regular file in exactly the same way
527 as <function>create_proc_entry</function> from <xref
528 linkend="regularfile"> does, but also allows to set the read
529 function <parameter>read_proc</parameter> in one call. This
530 function can set the <parameter>data</parameter> as well, like
531 explained in <xref linkend="usingdata">.
538 <title>Modules</title>
541 If procfs is being used from within a module, be sure to set
542 the <structfield>owner</structfield> field in the
543 <structname>struct proc_dir_entry</structname> to
544 <constant>THIS_MODULE</constant>.
548 struct proc_dir_entry* entry;
550 entry->owner = THIS_MODULE;
558 <title>Mode and ownership</title>
561 Sometimes it is useful to change the mode and/or ownership of
562 a procfs entry. Here is an example that shows how to achieve
567 struct proc_dir_entry* entry;
569 entry->mode = S_IWUSR |S_IRUSR | S_IRGRP | S_IROTH;
580 <chapter id="example">
581 <title>Example</title>
583 <!-- be careful with the example code: it shouldn't be wider than
584 approx. 60 columns, or otherwise it won't fit properly on a page