procfs-guide.tmpl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
  4. <!ENTITY procfsexample SYSTEM "procfs_example.xml">
  5. ]>
  6. <book id="LKProcfsGuide">
  7. <bookinfo>
  8. <title>Linux Kernel Procfs Guide</title>
  9. <authorgroup>
  10. <author>
  11. <firstname>Erik</firstname>
  12. <othername>(J.A.K.)</othername>
  13. <surname>Mouw</surname>
  14. <affiliation>
  15. <address>
  16. <email>mouw@nl.linux.org</email>
  17. </address>
  18. </affiliation>
  19. </author>
  20. <othercredit>
  21. <contrib>
  22. This software and documentation were written while working on the
  23. LART computing board
  24. (<ulink url="http://www.lartmaker.nl/">http://www.lartmaker.nl/</ulink>),
  25. which was sponsored by the Delt University of Technology projects
  26. Mobile Multi-media Communications and Ubiquitous Communications.
  27. </contrib>
  28. </othercredit>
  29. </authorgroup>
  30. <revhistory>
  31. <revision>
  32. <revnumber>1.0</revnumber>
  33. <date>May 30, 2001</date>
  34. <revremark>Initial revision posted to linux-kernel</revremark>
  35. </revision>
  36. <revision>
  37. <revnumber>1.1</revnumber>
  38. <date>June 3, 2001</date>
  39. <revremark>Revised after comments from linux-kernel</revremark>
  40. </revision>
  41. </revhistory>
  42. <copyright>
  43. <year>2001</year>
  44. <holder>Erik Mouw</holder>
  45. </copyright>
  46. <legalnotice>
  47. <para>
  48. This documentation is free software; you can redistribute it
  49. and/or modify it under the terms of the GNU General Public
  50. License as published by the Free Software Foundation; either
  51. version 2 of the License, or (at your option) any later
  52. version.
  53. </para>
  54. <para>
  55. This documentation is distributed in the hope that it will be
  56. useful, but WITHOUT ANY WARRANTY; without even the implied
  57. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  58. PURPOSE. See the GNU General Public License for more details.
  59. </para>
  60. <para>
  61. You should have received a copy of the GNU General Public
  62. License along with this program; if not, write to the Free
  63. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  64. MA 02111-1307 USA
  65. </para>
  66. <para>
  67. For more details see the file COPYING in the source
  68. distribution of Linux.
  69. </para>
  70. </legalnotice>
  71. </bookinfo>
  72. <toc>
  73. </toc>
  74. <preface id="Preface">
  75. <title>Preface</title>
  76. <para>
  77. This guide describes the use of the procfs file system from
  78. within the Linux kernel. The idea to write this guide came up on
  79. the #kernelnewbies IRC channel (see <ulink
  80. url="http://www.kernelnewbies.org/">http://www.kernelnewbies.org/</ulink>),
  81. when Jeff Garzik explained the use of procfs and forwarded me a
  82. message Alexander Viro wrote to the linux-kernel mailing list. I
  83. agreed to write it up nicely, so here it is.
  84. </para>
  85. <para>
  86. I'd like to thank Jeff Garzik
  87. <email>jgarzik@pobox.com</email> and Alexander Viro
  88. <email>viro@parcelfarce.linux.theplanet.co.uk</email> for their input,
  89. Tim Waugh <email>twaugh@redhat.com</email> for his <ulink
  90. url="http://people.redhat.com/twaugh/docbook/selfdocbook/">Selfdocbook</ulink>,
  91. and Marc Joosen <email>marcj@historia.et.tudelft.nl</email> for
  92. proofreading.
  93. </para>
  94. <para>
  95. Erik
  96. </para>
  97. </preface>
  98. <chapter id="intro">
  99. <title>Introduction</title>
  100. <para>
  101. The <filename class="directory">/proc</filename> file system
  102. (procfs) is a special file system in the linux kernel. It's a
  103. virtual file system: it is not associated with a block device
  104. but exists only in memory. The files in the procfs are there to
  105. allow userland programs access to certain information from the
  106. kernel (like process information in <filename
  107. class="directory">/proc/[0-9]+/</filename>), but also for debug
  108. purposes (like <filename>/proc/ksyms</filename>).
  109. </para>
  110. <para>
  111. This guide describes the use of the procfs file system from
  112. within the Linux kernel. It starts by introducing all relevant
  113. functions to manage the files within the file system. After that
  114. it shows how to communicate with userland, and some tips and
  115. tricks will be pointed out. Finally a complete example will be
  116. shown.
  117. </para>
  118. <para>
  119. Note that the files in <filename
  120. class="directory">/proc/sys</filename> are sysctl files: they
  121. don't belong to procfs and are governed by a completely
  122. different API described in the Kernel API book.
  123. </para>
  124. </chapter>
  125. <chapter id="managing">
  126. <title>Managing procfs entries</title>
  127. <para>
  128. This chapter describes the functions that various kernel
  129. components use to populate the procfs with files, symlinks,
  130. device nodes, and directories.
  131. </para>
  132. <para>
  133. A minor note before we start: if you want to use any of the
  134. procfs functions, be sure to include the correct header file!
  135. This should be one of the first lines in your code:
  136. </para>
  137. <programlisting>
  138. #include &lt;linux/proc_fs.h&gt;
  139. </programlisting>
  140. <sect1 id="regularfile">
  141. <title>Creating a regular file</title>
  142. <funcsynopsis>
  143. <funcprototype>
  144. <funcdef>struct proc_dir_entry* <function>create_proc_entry</function></funcdef>
  145. <paramdef>const char* <parameter>name</parameter></paramdef>
  146. <paramdef>mode_t <parameter>mode</parameter></paramdef>
  147. <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
  148. </funcprototype>
  149. </funcsynopsis>
  150. <para>
  151. This function creates a regular file with the name
  152. <parameter>name</parameter>, file mode
  153. <parameter>mode</parameter> in the directory
  154. <parameter>parent</parameter>. To create a file in the root of
  155. the procfs, use <constant>NULL</constant> as
  156. <parameter>parent</parameter> parameter. When successful, the
  157. function will return a pointer to the freshly created
  158. <structname>struct proc_dir_entry</structname>; otherwise it
  159. will return <constant>NULL</constant>. <xref
  160. linkend="userland"/> describes how to do something useful with
  161. regular files.
  162. </para>
  163. <para>
  164. Note that it is specifically supported that you can pass a
  165. path that spans multiple directories. For example
  166. <function>create_proc_entry</function>(<parameter>"drivers/via0/info"</parameter>)
  167. will create the <filename class="directory">via0</filename>
  168. directory if necessary, with standard
  169. <constant>0755</constant> permissions.
  170. </para>
  171. <para>
  172. If you only want to be able to read the file, the function
  173. <function>create_proc_read_entry</function> described in <xref
  174. linkend="convenience"/> may be used to create and initialise
  175. the procfs entry in one single call.
  176. </para>
  177. </sect1>
  178. <sect1 id="Creating_a_symlink">
  179. <title>Creating a symlink</title>
  180. <funcsynopsis>
  181. <funcprototype>
  182. <funcdef>struct proc_dir_entry*
  183. <function>proc_symlink</function></funcdef> <paramdef>const
  184. char* <parameter>name</parameter></paramdef>
  185. <paramdef>struct proc_dir_entry*
  186. <parameter>parent</parameter></paramdef> <paramdef>const
  187. char* <parameter>dest</parameter></paramdef>
  188. </funcprototype>
  189. </funcsynopsis>
  190. <para>
  191. This creates a symlink in the procfs directory
  192. <parameter>parent</parameter> that points from
  193. <parameter>name</parameter> to
  194. <parameter>dest</parameter>. This translates in userland to
  195. <literal>ln -s</literal> <parameter>dest</parameter>
  196. <parameter>name</parameter>.
  197. </para>
  198. </sect1>
  199. <sect1 id="Creating_a_directory">
  200. <title>Creating a directory</title>
  201. <funcsynopsis>
  202. <funcprototype>
  203. <funcdef>struct proc_dir_entry* <function>proc_mkdir</function></funcdef>
  204. <paramdef>const char* <parameter>name</parameter></paramdef>
  205. <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
  206. </funcprototype>
  207. </funcsynopsis>
  208. <para>
  209. Create a directory <parameter>name</parameter> in the procfs
  210. directory <parameter>parent</parameter>.
  211. </para>
  212. </sect1>
  213. <sect1 id="Removing_an_entry">
  214. <title>Removing an entry</title>
  215. <funcsynopsis>
  216. <funcprototype>
  217. <funcdef>void <function>remove_proc_entry</function></funcdef>
  218. <paramdef>const char* <parameter>name</parameter></paramdef>
  219. <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
  220. </funcprototype>
  221. </funcsynopsis>
  222. <para>
  223. Removes the entry <parameter>name</parameter> in the directory
  224. <parameter>parent</parameter> from the procfs. Entries are
  225. removed by their <emphasis>name</emphasis>, not by the
  226. <structname>struct proc_dir_entry</structname> returned by the
  227. various create functions. Note that this function doesn't
  228. recursively remove entries.
  229. </para>
  230. <para>
  231. Be sure to free the <structfield>data</structfield> entry from
  232. the <structname>struct proc_dir_entry</structname> before
  233. <function>remove_proc_entry</function> is called (that is: if
  234. there was some <structfield>data</structfield> allocated, of
  235. course). See <xref linkend="usingdata"/> for more information
  236. on using the <structfield>data</structfield> entry.
  237. </para>
  238. </sect1>
  239. </chapter>
  240. <chapter id="userland">
  241. <title>Communicating with userland</title>
  242. <para>
  243. Instead of reading (or writing) information directly from
  244. kernel memory, procfs works with <emphasis>call back
  245. functions</emphasis> for files: functions that are called when
  246. a specific file is being read or written. Such functions have
  247. to be initialised after the procfs file is created by setting
  248. the <structfield>read_proc</structfield> and/or
  249. <structfield>write_proc</structfield> fields in the
  250. <structname>struct proc_dir_entry*</structname> that the
  251. function <function>create_proc_entry</function> returned:
  252. </para>
  253. <programlisting>
  254. struct proc_dir_entry* entry;
  255. entry->read_proc = read_proc_foo;
  256. entry->write_proc = write_proc_foo;
  257. </programlisting>
  258. <para>
  259. If you only want to use a the
  260. <structfield>read_proc</structfield>, the function
  261. <function>create_proc_read_entry</function> described in <xref
  262. linkend="convenience"/> may be used to create and initialise the
  263. procfs entry in one single call.
  264. </para>
  265. <sect1 id="Reading_data">
  266. <title>Reading data</title>
  267. <para>
  268. The read function is a call back function that allows userland
  269. processes to read data from the kernel. The read function
  270. should have the following format:
  271. </para>
  272. <funcsynopsis>
  273. <funcprototype>
  274. <funcdef>int <function>read_func</function></funcdef>
  275. <paramdef>char* <parameter>buffer</parameter></paramdef>
  276. <paramdef>char** <parameter>start</parameter></paramdef>
  277. <paramdef>off_t <parameter>off</parameter></paramdef>
  278. <paramdef>int <parameter>count</parameter></paramdef>
  279. <paramdef>int* <parameter>peof</parameter></paramdef>
  280. <paramdef>void* <parameter>data</parameter></paramdef>
  281. </funcprototype>
  282. </funcsynopsis>
  283. <para>
  284. The read function should write its information into the
  285. <parameter>buffer</parameter>, which will be exactly
  286. <literal>PAGE_SIZE</literal> bytes long.
  287. </para>
  288. <para>
  289. The parameter
  290. <parameter>peof</parameter> should be used to signal that the
  291. end of the file has been reached by writing
  292. <literal>1</literal> to the memory location
  293. <parameter>peof</parameter> points to.
  294. </para>
  295. <para>
  296. The <parameter>data</parameter>
  297. parameter can be used to create a single call back function for
  298. several files, see <xref linkend="usingdata"/>.
  299. </para>
  300. <para>
  301. The rest of the parameters and the return value are described
  302. by a comment in <filename>fs/proc/generic.c</filename> as follows:
  303. </para>
  304. <blockquote>
  305. <para>
  306. You have three ways to return data:
  307. </para>
  308. <orderedlist>
  309. <listitem>
  310. <para>
  311. Leave <literal>*start = NULL</literal>. (This is the default.)
  312. Put the data of the requested offset at that
  313. offset within the buffer. Return the number (<literal>n</literal>)
  314. of bytes there are from the beginning of the
  315. buffer up to the last byte of data. If the
  316. number of supplied bytes (<literal>= n - offset</literal>) is
  317. greater than zero and you didn't signal eof
  318. and the reader is prepared to take more data
  319. you will be called again with the requested
  320. offset advanced by the number of bytes
  321. absorbed. This interface is useful for files
  322. no larger than the buffer.
  323. </para>
  324. </listitem>
  325. <listitem>
  326. <para>
  327. Set <literal>*start</literal> to an unsigned long value less than
  328. the buffer address but greater than zero.
  329. Put the data of the requested offset at the
  330. beginning of the buffer. Return the number of
  331. bytes of data placed there. If this number is
  332. greater than zero and you didn't signal eof
  333. and the reader is prepared to take more data
  334. you will be called again with the requested
  335. offset advanced by <literal>*start</literal>. This interface is
  336. useful when you have a large file consisting
  337. of a series of blocks which you want to count
  338. and return as wholes.
  339. (Hack by Paul.Russell@rustcorp.com.au)
  340. </para>
  341. </listitem>
  342. <listitem>
  343. <para>
  344. Set <literal>*start</literal> to an address within the buffer.
  345. Put the data of the requested offset at <literal>*start</literal>.
  346. Return the number of bytes of data placed there.
  347. If this number is greater than zero and you
  348. didn't signal eof and the reader is prepared to
  349. take more data you will be called again with the
  350. requested offset advanced by the number of bytes
  351. absorbed.
  352. </para>
  353. </listitem>
  354. </orderedlist>
  355. </blockquote>
  356. <para>
  357. <xref linkend="example"/> shows how to use a read call back
  358. function.
  359. </para>
  360. </sect1>
  361. <sect1 id="Writing_data">
  362. <title>Writing data</title>
  363. <para>
  364. The write call back function allows a userland process to write
  365. data to the kernel, so it has some kind of control over the
  366. kernel. The write function should have the following format:
  367. </para>
  368. <funcsynopsis>
  369. <funcprototype>
  370. <funcdef>int <function>write_func</function></funcdef>
  371. <paramdef>struct file* <parameter>file</parameter></paramdef>
  372. <paramdef>const char* <parameter>buffer</parameter></paramdef>
  373. <paramdef>unsigned long <parameter>count</parameter></paramdef>
  374. <paramdef>void* <parameter>data</parameter></paramdef>
  375. </funcprototype>
  376. </funcsynopsis>
  377. <para>
  378. The write function should read <parameter>count</parameter>
  379. bytes at maximum from the <parameter>buffer</parameter>. Note
  380. that the <parameter>buffer</parameter> doesn't live in the
  381. kernel's memory space, so it should first be copied to kernel
  382. space with <function>copy_from_user</function>. The
  383. <parameter>file</parameter> parameter is usually
  384. ignored. <xref linkend="usingdata"/> shows how to use the
  385. <parameter>data</parameter> parameter.
  386. </para>
  387. <para>
  388. Again, <xref linkend="example"/> shows how to use this call back
  389. function.
  390. </para>
  391. </sect1>
  392. <sect1 id="usingdata">
  393. <title>A single call back for many files</title>
  394. <para>
  395. When a large number of almost identical files is used, it's
  396. quite inconvenient to use a separate call back function for
  397. each file. A better approach is to have a single call back
  398. function that distinguishes between the files by using the
  399. <structfield>data</structfield> field in <structname>struct
  400. proc_dir_entry</structname>. First of all, the
  401. <structfield>data</structfield> field has to be initialised:
  402. </para>
  403. <programlisting>
  404. struct proc_dir_entry* entry;
  405. struct my_file_data *file_data;
  406. file_data = kmalloc(sizeof(struct my_file_data), GFP_KERNEL);
  407. entry->data = file_data;
  408. </programlisting>
  409. <para>
  410. The <structfield>data</structfield> field is a <type>void
  411. *</type>, so it can be initialised with anything.
  412. </para>
  413. <para>
  414. Now that the <structfield>data</structfield> field is set, the
  415. <function>read_proc</function> and
  416. <function>write_proc</function> can use it to distinguish
  417. between files because they get it passed into their
  418. <parameter>data</parameter> parameter:
  419. </para>
  420. <programlisting>
  421. int foo_read_func(char *page, char **start, off_t off,
  422. int count, int *eof, void *data)
  423. {
  424. int len;
  425. if(data == file_data) {
  426. /* special case for this file */
  427. } else {
  428. /* normal processing */
  429. }
  430. return len;
  431. }
  432. </programlisting>
  433. <para>
  434. Be sure to free the <structfield>data</structfield> data field
  435. when removing the procfs entry.
  436. </para>
  437. </sect1>
  438. </chapter>
  439. <chapter id="tips">
  440. <title>Tips and tricks</title>
  441. <sect1 id="convenience">
  442. <title>Convenience functions</title>
  443. <funcsynopsis>
  444. <funcprototype>
  445. <funcdef>struct proc_dir_entry* <function>create_proc_read_entry</function></funcdef>
  446. <paramdef>const char* <parameter>name</parameter></paramdef>
  447. <paramdef>mode_t <parameter>mode</parameter></paramdef>
  448. <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
  449. <paramdef>read_proc_t* <parameter>read_proc</parameter></paramdef>
  450. <paramdef>void* <parameter>data</parameter></paramdef>
  451. </funcprototype>
  452. </funcsynopsis>
  453. <para>
  454. This function creates a regular file in exactly the same way
  455. as <function>create_proc_entry</function> from <xref
  456. linkend="regularfile"/> does, but also allows to set the read
  457. function <parameter>read_proc</parameter> in one call. This
  458. function can set the <parameter>data</parameter> as well, like
  459. explained in <xref linkend="usingdata"/>.
  460. </para>
  461. </sect1>
  462. <sect1 id="Modules">
  463. <title>Modules</title>
  464. <para>
  465. If procfs is being used from within a module, be sure to set
  466. the <structfield>owner</structfield> field in the
  467. <structname>struct proc_dir_entry</structname> to
  468. <constant>THIS_MODULE</constant>.
  469. </para>
  470. <programlisting>
  471. struct proc_dir_entry* entry;
  472. entry->owner = THIS_MODULE;
  473. </programlisting>
  474. </sect1>
  475. <sect1 id="Mode_and_ownership">
  476. <title>Mode and ownership</title>
  477. <para>
  478. Sometimes it is useful to change the mode and/or ownership of
  479. a procfs entry. Here is an example that shows how to achieve
  480. that:
  481. </para>
  482. <programlisting>
  483. struct proc_dir_entry* entry;
  484. entry->mode = S_IWUSR |S_IRUSR | S_IRGRP | S_IROTH;
  485. entry->uid = 0;
  486. entry->gid = 100;
  487. </programlisting>
  488. </sect1>
  489. </chapter>
  490. <chapter id="example">
  491. <title>Example</title>
  492. <!-- be careful with the example code: it shouldn't be wider than
  493. approx. 60 columns, or otherwise it won't fit properly on a page
  494. -->
  495. &procfsexample;
  496. </chapter>
  497. </book>