procfs-guide.tmpl 20 KB

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