<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>A single call back for many files</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="Linux Kernel Procfs Guide" /><link rel="up" href="ch03.html" title="Chapter 3. Communicating with userland" /><link rel="prev" href="ch03s02.html" title="Writing data" /><link rel="next" href="ch04.html" title="Chapter 4. Tips and tricks" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">A single call back for many files</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03s02.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Communicating with userland</th><td width="20%" align="right"> <a accesskey="n" href="ch04.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="A single call back for many files"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="usingdata"></a>A single call back for many files</h2></div></div></div><p> When a large number of almost identical files is used, it's quite inconvenient to use a separate call back function for each file. A better approach is to have a single call back function that distinguishes between the files by using the <em class="structfield"><code>data</code></em> field in <span class="structname">struct proc_dir_entry</span>. First of all, the <em class="structfield"><code>data</code></em> field has to be initialised: </p><pre class="programlisting"> struct proc_dir_entry* entry; struct my_file_data *file_data; file_data = kmalloc(sizeof(struct my_file_data), GFP_KERNEL); entry->data = file_data; </pre><p> The <em class="structfield"><code>data</code></em> field is a <span class="type">void *</span>, so it can be initialised with anything. </p><p> Now that the <em class="structfield"><code>data</code></em> field is set, the <code class="function">read_proc</code> and <code class="function">write_proc</code> can use it to distinguish between files because they get it passed into their <em class="parameter"><code>data</code></em> parameter: </p><pre class="programlisting"> int foo_read_func(char *page, char **start, off_t off, int count, int *eof, void *data) { int len; if(data == file_data) { /* special case for this file */ } else { /* normal processing */ } return len; } </pre><p> Be sure to free the <em class="structfield"><code>data</code></em> data field when removing the procfs entry. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch03.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing data </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 4. Tips and tricks</td></tr></table></div></body></html>