Sophie

Sophie

distrib > Mandriva > 9.0 > i586 > by-pkgid > 98e91bc877e03cf3582cd163550eb7e3 > files > 853

kernel-doc-html-2.4.19-16mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML
><HEAD
><TITLE
>A single call back for many files</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Linux Kernel Procfs Guide"
HREF="book1.html"><LINK
REL="UP"
TITLE="Communicating with userland"
HREF="c177.html"><LINK
REL="PREVIOUS"
TITLE="Writing data"
HREF="x232.html"><LINK
REL="NEXT"
TITLE="Tips and tricks"
HREF="c275.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Linux Kernel Procfs Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x232.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Communicating with userland</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c275.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="USINGDATA"
></A
>A single call back for many files</H1
><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
         <TT
CLASS="STRUCTFIELD"
><I
>data</I
></TT
> field in <SPAN
CLASS="STRUCTNAME"
>struct
         proc_dir_entry</SPAN
>. First of all, the
         <TT
CLASS="STRUCTFIELD"
><I
>data</I
></TT
> field has to be initialised:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><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-&#62;data = file_data;
      </PRE
></TD
></TR
></TABLE
><P
>          The <TT
CLASS="STRUCTFIELD"
><I
>data</I
></TT
> field is a <SPAN
CLASS="TYPE"
>void
          *</SPAN
>, so it can be initialised with anything.
      </P
><P
>        Now that the <TT
CLASS="STRUCTFIELD"
><I
>data</I
></TT
> field is set, the
        <TT
CLASS="FUNCTION"
>read_proc</TT
> and
        <TT
CLASS="FUNCTION"
>write_proc</TT
> can use it to distinguish
        between files because they get it passed into their
        <TT
CLASS="PARAMETER"
><I
>data</I
></TT
> parameter:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><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
></TD
></TR
></TABLE
><P
>        Be sure to free the <TT
CLASS="STRUCTFIELD"
><I
>data</I
></TT
> data field
        when removing the procfs entry.
      </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x232.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c275.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Writing data</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c177.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Tips and tricks</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>