<?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>Chapter 4. ioctls: Not writing a new system call</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><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="ch03.html" title="Chapter 3. Some Basic Rules" /><link rel="next" href="ch05.html" title="Chapter 5. Recipes for Deadlock" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 4. ioctls: Not writing a new system call</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch05.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 4. ioctls: Not writing a new system call"><div class="titlepage"><div><div><h2 class="title"><a id="ioctls"></a>Chapter 4. ioctls: Not writing a new system call</h2></div></div></div><p> A system call generally looks like this </p><pre class="programlisting"> asmlinkage long sys_mycall(int arg) { return 0; } </pre><p> First, in most cases you don't want to create a new system call. You create a character device and implement an appropriate ioctl for it. This is much more flexible than system calls, doesn't have to be entered in every architecture's <code class="filename">include/asm/unistd.h</code> and <code class="filename">arch/kernel/entry.S</code> file, and is much more likely to be accepted by Linus. </p><p> If all your routine does is read or write some parameter, consider implementing a <code class="function">sysfs</code> interface instead. </p><p> Inside the ioctl you're in user context to a process. When a error occurs you return a negated errno (see <code class="filename">include/linux/errno.h</code>), otherwise you return <span class="returnvalue">0</span>. </p><p> After you slept you should check if a signal occurred: the Unix/Linux way of handling signals is to temporarily exit the system call with the <code class="constant">-ERESTARTSYS</code> error. The system call entry code will switch back to user context, process the signal handler and then your system call will be restarted (unless the user disabled that). So you should be prepared to process the restart, e.g. if you're in the middle of manipulating some data structure. </p><pre class="programlisting"> if (signal_pending()) return -ERESTARTSYS; </pre><p> If you're doing longer computations: first think userspace. If you <span class="emphasis"><em>really</em></span> want to do it in kernel you should regularly check if you need to give up the CPU (remember there is cooperative multitasking per CPU). Idiom: </p><pre class="programlisting"> cond_resched(); /* Will sleep */ </pre><p> A short note on interface design: the UNIX system call motto is "Provide mechanism not policy". </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 3. Some Basic Rules </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 5. Recipes for Deadlock</td></tr></table></div></body></html>