procfs-guide.tmpl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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>page</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>eof</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>page</parameter>. For proper use, the function
  294. should start writing at an offset of
  295. <parameter>off</parameter> in <parameter>page</parameter> and
  296. write at most <parameter>count</parameter> bytes, but because
  297. most read functions are quite simple and only return a small
  298. amount of information, these two parameters are usually
  299. ignored (it breaks pagers like <literal>more</literal> and
  300. <literal>less</literal>, but <literal>cat</literal> still
  301. works).
  302. </para>
  303. <para>
  304. If the <parameter>off</parameter> and
  305. <parameter>count</parameter> parameters are properly used,
  306. <parameter>eof</parameter> should be used to signal that the
  307. end of the file has been reached by writing
  308. <literal>1</literal> to the memory location
  309. <parameter>eof</parameter> points to.
  310. </para>
  311. <para>
  312. The parameter <parameter>start</parameter> doesn't seem to be
  313. used anywhere in the kernel. The <parameter>data</parameter>
  314. parameter can be used to create a single call back function for
  315. several files, see <xref linkend="usingdata"/>.
  316. </para>
  317. <para>
  318. The <function>read_func</function> function must return the
  319. number of bytes written into the <parameter>page</parameter>.
  320. </para>
  321. <para>
  322. <xref linkend="example"/> shows how to use a read call back
  323. function.
  324. </para>
  325. </sect1>
  326. <sect1>
  327. <title>Writing data</title>
  328. <para>
  329. The write call back function allows a userland process to write
  330. data to the kernel, so it has some kind of control over the
  331. kernel. The write function should have the following format:
  332. </para>
  333. <funcsynopsis>
  334. <funcprototype>
  335. <funcdef>int <function>write_func</function></funcdef>
  336. <paramdef>struct file* <parameter>file</parameter></paramdef>
  337. <paramdef>const char* <parameter>buffer</parameter></paramdef>
  338. <paramdef>unsigned long <parameter>count</parameter></paramdef>
  339. <paramdef>void* <parameter>data</parameter></paramdef>
  340. </funcprototype>
  341. </funcsynopsis>
  342. <para>
  343. The write function should read <parameter>count</parameter>
  344. bytes at maximum from the <parameter>buffer</parameter>. Note
  345. that the <parameter>buffer</parameter> doesn't live in the
  346. kernel's memory space, so it should first be copied to kernel
  347. space with <function>copy_from_user</function>. The
  348. <parameter>file</parameter> parameter is usually
  349. ignored. <xref linkend="usingdata"/> shows how to use the
  350. <parameter>data</parameter> parameter.
  351. </para>
  352. <para>
  353. Again, <xref linkend="example"/> shows how to use this call back
  354. function.
  355. </para>
  356. </sect1>
  357. <sect1 id="usingdata">
  358. <title>A single call back for many files</title>
  359. <para>
  360. When a large number of almost identical files is used, it's
  361. quite inconvenient to use a separate call back function for
  362. each file. A better approach is to have a single call back
  363. function that distinguishes between the files by using the
  364. <structfield>data</structfield> field in <structname>struct
  365. proc_dir_entry</structname>. First of all, the
  366. <structfield>data</structfield> field has to be initialised:
  367. </para>
  368. <programlisting>
  369. struct proc_dir_entry* entry;
  370. struct my_file_data *file_data;
  371. file_data = kmalloc(sizeof(struct my_file_data), GFP_KERNEL);
  372. entry->data = file_data;
  373. </programlisting>
  374. <para>
  375. The <structfield>data</structfield> field is a <type>void
  376. *</type>, so it can be initialised with anything.
  377. </para>
  378. <para>
  379. Now that the <structfield>data</structfield> field is set, the
  380. <function>read_proc</function> and
  381. <function>write_proc</function> can use it to distinguish
  382. between files because they get it passed into their
  383. <parameter>data</parameter> parameter:
  384. </para>
  385. <programlisting>
  386. int foo_read_func(char *page, char **start, off_t off,
  387. int count, int *eof, void *data)
  388. {
  389. int len;
  390. if(data == file_data) {
  391. /* special case for this file */
  392. } else {
  393. /* normal processing */
  394. }
  395. return len;
  396. }
  397. </programlisting>
  398. <para>
  399. Be sure to free the <structfield>data</structfield> data field
  400. when removing the procfs entry.
  401. </para>
  402. </sect1>
  403. </chapter>
  404. <chapter id="tips">
  405. <title>Tips and tricks</title>
  406. <sect1 id="convenience">
  407. <title>Convenience functions</title>
  408. <funcsynopsis>
  409. <funcprototype>
  410. <funcdef>struct proc_dir_entry* <function>create_proc_read_entry</function></funcdef>
  411. <paramdef>const char* <parameter>name</parameter></paramdef>
  412. <paramdef>mode_t <parameter>mode</parameter></paramdef>
  413. <paramdef>struct proc_dir_entry* <parameter>parent</parameter></paramdef>
  414. <paramdef>read_proc_t* <parameter>read_proc</parameter></paramdef>
  415. <paramdef>void* <parameter>data</parameter></paramdef>
  416. </funcprototype>
  417. </funcsynopsis>
  418. <para>
  419. This function creates a regular file in exactly the same way
  420. as <function>create_proc_entry</function> from <xref
  421. linkend="regularfile"/> does, but also allows to set the read
  422. function <parameter>read_proc</parameter> in one call. This
  423. function can set the <parameter>data</parameter> as well, like
  424. explained in <xref linkend="usingdata"/>.
  425. </para>
  426. </sect1>
  427. <sect1>
  428. <title>Modules</title>
  429. <para>
  430. If procfs is being used from within a module, be sure to set
  431. the <structfield>owner</structfield> field in the
  432. <structname>struct proc_dir_entry</structname> to
  433. <constant>THIS_MODULE</constant>.
  434. </para>
  435. <programlisting>
  436. struct proc_dir_entry* entry;
  437. entry->owner = THIS_MODULE;
  438. </programlisting>
  439. </sect1>
  440. <sect1>
  441. <title>Mode and ownership</title>
  442. <para>
  443. Sometimes it is useful to change the mode and/or ownership of
  444. a procfs entry. Here is an example that shows how to achieve
  445. that:
  446. </para>
  447. <programlisting>
  448. struct proc_dir_entry* entry;
  449. entry->mode = S_IWUSR |S_IRUSR | S_IRGRP | S_IROTH;
  450. entry->uid = 0;
  451. entry->gid = 100;
  452. </programlisting>
  453. </sect1>
  454. </chapter>
  455. <chapter id="example">
  456. <title>Example</title>
  457. <!-- be careful with the example code: it shouldn't be wider than
  458. approx. 60 columns, or otherwise it won't fit properly on a page
  459. -->
  460. &procfsexample;
  461. </chapter>
  462. </book>