title: Problems calling syscall function in C
body: <p>For a homework assignment, I have to modify the linux kernel.</p>
  
  <p>I am working on a virtual machine, and I added a system call to the kernel, which I called <code>get_unique_id</code>. Here is the code for <code>get_unique_id.c</code> :</p>
  
  <pre><code>#include &lt;linux/linkage.h&gt;
  #include &lt;asm/uaccess.h&gt;
  
  asmlinkage long sys_get_unique_id(int * uuid)
  {
      // static because we want its state to persist between calls
      static int uid = 0;
  
      ++uid;
  
      // assign new uid value to user-provided mem location
      // returns non-zero if success or -EFAULT otherwise
      int ret = put_user(uid, uuid);
      return ret;
  }
  </code></pre>
  
  <p>I also added this line to <code>syscalls.h</code> :</p>
  
  <pre><code>asmlinkage long sys_get_unique_id(int * uuid);
  </code></pre>
  
  <p>This line to syscall_32.tbl :</p>
  
  <pre><code>383 i386    get_unique_id       sys_get_unique_id
  </code></pre>
  
  <p>And finally this line to <code>syscall_64.tbl</code> :</p>
  
  <pre><code>548 common  get_unique_id       sys_get_unique_id
  </code></pre>
  
  <p>After recompiling and reloading the kernel, I wrote a little C program to test my system call, here is the code for the C test file :</p>
  
  <pre><code>// get_unique_id_test.c
  #include &lt;stdio.h&gt;
  #include &lt;limits.h&gt;
  
  #include "syscalls_test.h"
  
  int main(void)
  {
      // initialize the ints we want
      int id1;
      int id2;
  
      // check the id's are unique and that no error occured
      for (int i = INT_MIN; i &lt; INT_MAX - 1; i += 2) {
          long ret1 = get_unique_id(&amp;id1);
          long ret2 = get_unique_id(&amp;id2);
  
          if (ret1 != 0)
              printf("ERROR: get_unique_id returned: %ld\n", ret1);
  
          if (ret2 != 0)
              printf("ERROR: get_unique_id returned: %ld\n", ret2);
  
          if (id2 != id1 + 1)
              printf("ERROR: successive id's did not increment properly: id1 = %d, id2 = %d\n", id1, id2);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>And its header file :</p>
  
  <pre><code>// syscalls_test.h
  #include &lt;errno.h&gt;
  #include &lt;sys/syscall.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  
  #define __NR_get_unique_id 383
  
  inline long get_unique_id(int * uuid)
  {
      return syscall(__NR_get_unique_id, uuid) ? errno : 0;
  }
  </code></pre>
  
  <p>Unfortunately, while trying to compile the C test file with the following command : <code>gcc -std=c99 get_unique_id_test.c -o get_unique_id_test</code>, I get the following error :</p>
  
  <pre><code>In file included from get_unique_id_test.c:4:0:
  syscalls_test.h: In function ‘get_unique_id’:
  syscalls_test.h:10:5: warning: implicit declaration of function        ‘syscall’ [-Wimplicit-function-declaration]
       return syscall(__NR_get_unique_id, uuid) ? errno : 0;
       ^
  syscalls_test.h: In function ‘get_unique_id’:
  syscalls_test.h:10:5: warning: implicit declaration of function ‘syscall’ [-Wimplicit-function-declaration]
       return syscall(__NR_get_unique_id, uuid) ? errno : 0;
       ^
  /tmp/cc1euZ3r.o: In function `main':
  get_unique_id_test.c:(.text+0x22): undefined reference to `get_unique_id'
  get_unique_id_test.c:(.text+0x34): undefined reference to `get_unique_id'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>It appears gcc cannot find the function <code>get_unique_id(int * uuid)</code>, which is declared in <code>syscalls_test.h</code>, and the <code>syscall</code> function, which should be declared, I believe, in <code>syscall.h</code>, right ?</p>
  
  <p>I don't understand why this happens. Does anybody have an idea ?</p>
  
  <p><strong>EDIT :</strong> my problems were solved using a3f's solution (see below) <strong>PLUS</strong> moving the <code>#include "syscalls_test.h"</code> at the very top of the file, as he said in the comments. Thank you very much.</p>