1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;4.&#160;ioctls: Not writing a new system call</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel"><link rel="up" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel"><link rel="prev" href="basic-rules.html" title="Chapter&#160;3.&#160;Some Basic Rules"><link rel="next" href="deadlock-recipes.html" title="Chapter&#160;5.&#160;Recipes for Deadlock"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;4.&#160;ioctls: Not writing a new system call</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="basic-rules.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="deadlock-recipes.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="ioctls"></a>Chapter&#160;4.&#160;ioctls: Not writing a new system call</h1></div></div></div><p>
2   A system call generally looks like this
3  </p><pre class="programlisting">
4asmlinkage long sys_mycall(int arg)
5{
6        return 0; 
7}
8  </pre><p>
9   First, in most cases you don't want to create a new system call.
10   You create a character device and implement an appropriate ioctl
11   for it.  This is much more flexible than system calls, doesn't have
12   to be entered in every architecture's
13   <code class="filename">include/asm/unistd.h</code> and
14   <code class="filename">arch/kernel/entry.S</code> file, and is much more
15   likely to be accepted by Linus.
16  </p><p>
17   If all your routine does is read or write some parameter, consider
18   implementing a <code class="function">sysfs</code> interface instead.
19  </p><p>
20   Inside the ioctl you're in user context to a process.  When a
21   error occurs you return a negated errno (see
22   <code class="filename">include/linux/errno.h</code>),
23   otherwise you return <span class="returnvalue">0</span>.
24  </p><p>
25   After you slept you should check if a signal occurred: the
26   Unix/Linux way of handling signals is to temporarily exit the
27   system call with the <code class="constant">-ERESTARTSYS</code> error.  The
28   system call entry code will switch back to user context, process
29   the signal handler and then your system call will be restarted
30   (unless the user disabled that).  So you should be prepared to
31   process the restart, e.g. if you're in the middle of manipulating
32   some data structure.
33  </p><pre class="programlisting">
34if (signal_pending(current))
35        return -ERESTARTSYS;
36  </pre><p>
37   If you're doing longer computations: first think userspace. If you
38   <span class="emphasis"><em>really</em></span> want to do it in kernel you should
39   regularly check if you need to give up the CPU (remember there is
40   cooperative multitasking per CPU).  Idiom:
41  </p><pre class="programlisting">
42cond_resched(); /* Will sleep */ 
43  </pre><p>
44   A short note on interface design: the UNIX system call motto is
45   "Provide mechanism not policy".
46  </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="basic-rules.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="deadlock-recipes.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;3.&#160;Some Basic Rules&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Chapter&#160;5.&#160;Recipes for Deadlock</td></tr></table></div></body></html>
47