title: Adding a new system call to linux kernel doesn't seem to work
body: <p>I'm taking an OS course and the first assigment was to add a few system calls to the kernel (Redhat, kernel version 2.4).</p>
  
  <p>I've added everything that I thought that needs to be added and it still doesn't work. </p>
  
  <p>I'm trying to run tests and I noticed the wrappers (I've added an example for one below and the sys_call code for it) aren't linked the system calls. I know this because I put a printk inside the system calls and they never appear (and the test itself doesn't give the result I'm expecting).</p>
  
  <p>Now from what I understand the development process for adding a system call is:</p>
  
  <ol>
  <li>Create a wrappers like the example wrapper function in the instructions (lets call it aservice״).</li>
  <li>Write the sys_aservice in a .c file which'll go to one of the many files in the linux-2.4.18-14custom folder. The file it goes to depends on the type of service I want to add ( e.g if it were a file system service I'd put it in fs).</li>
  <li>Add the entry for the system call in the table at <code>/usr/src/linuxlinux-2.4.18-14custom/arch/i386/kernel/ entry.S</code></li>
  </ol>
  
  <p>This is what I did at first but it doesn't seem to be enough (because it doesn't seem to work).</p>
  
  <p>I looked it up online (and read: <a href="http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_add_systemcall.html" rel="nofollow">How to add a system call</a>) and I read that I need to add/change the following things inorder for the linking to work.</p>
  
  <ol>
  <li><p>Add a line in unistd.h for the system call. It'll look something like</p>
  
  <p>define __NR_aservice</p></li>
  <li><p>Add the aservice to the makefile. So for example if I put the aservice.c in the fs folder I'd add aservice.c to the makefile in fs.</p></li>
  <li><p>Create a aservice.h file which'll go to linux-2.4.18-14custom/include/linux (where all the h files go apparently?). This .h file will include something called a "stub" which is automatically generated from a macro in unistd.h (which the .h file will have to include) so that a user program can use your system call.
  The stub's declaration looks as follows: _syscallN(return type, function name, arg1 type, arg1 name ...)" where "N" is the number of parameters.</p></li>
  <li><p>Prepend to every sys_X function in aservice.c the keyword "asmlinkage" (which requires inclusion of linkage.h). So function declarations look like:
  asmlinkage int sys_aservice(...){..}
  Afterward include the .h file in the .c file.</p></li>
  </ol>
  
  <p>Is all this correct? Because even after doing the following it still seems as if the system call isn't called.</p>
  
  <p>Here is an example wrapper and system call implementation:</p>
  
  <pre><code> int stop_monitor(int pid){
          long __res;
          __asm__ volatile (
                  "movl $244, %%eax;" &lt;&lt; 244 is the entry in the sys_call table in entry.S 
                  "movl %1, %%ebx;"
                  "int $0x80;"
                  "movl %%eax, %0"
                  : "=m" (__res)
                  : "m" (pid)
                  : "%eax", "%ebx"
          );
          if((unsigned long)(__res) &gt;= (unsigned long)(-125)) {
              errno = -(__res);
              __res = -1;
          }
          return (int)(__res);
      }
  
  
  asmlinkage int sys_stop_monitor(int pid){
      task_t* task = find_task_by_pid(pid);
      if(!task)
          return -ESRCH;
      if(task-&gt;flagMonitor == 0)
          return -EPERM;
      task-&gt;flagMonitor = 0;
      return 0;
  }
  </code></pre>
  
  <p>What am I missing?</p>