\author {Sven Goldt}
\title {The Linux Programmer's Guide}
\section {System calls}
  A system call is usually a request to the operating system (kernel)
  to do a hardware/system specific or privileged work.
  As of linux-1.1.50 140 system calls are defined.
  System calls like close() are implemented in the linux libc.
  This is often done by using a macro which around some corners
  finally calls syscall() which gets as arguments the number of the
  system call followed by the needed arguments.
  The actual system call numbers can be found in $<linux/unistd.h>$ while
  $<sys/syscall.h>$ gets updated with a new libc.
  If new calls appear that don't have a stub in libc yet,
  you can aswell use syscall().
  As an example for syscall(), you can close a file like this (not advised):\\
  
\setlength {\parindent} {0 pt}
\setlength {\parskip} {0 pt}
\#include $<syscall.h>$ \\
\bigskip  

extern int syscall(int, ...); \\
\bigskip  

 int my\_close(int filedescriptor) \\
 \{ \\
      return syscall(SYS\_close, filedescriptor); \\
 \} \\
\clearpage

  On the i386 architecture system calls can not have more than 
  5 arguments beside the system call number because there are not more
  registers free for them.
  If you use linux on another architecture you can check $<asm/unistd.h>$
  for the \_syscall macros to see how many arguments
  your hardware supports or how many the developers support.
  These \_syscall macros can be used instead of syscall(), but
  this is not recommended since such a macro expands to a full
  function which might already exist in a library. Therefore
  only kernel hackers should play with the \_syscall macros.
  To demonstrate it we use a \_syscall() macro on our open() example:\\
  
\bigskip
\#include $<linux\verb|/|unistd.h>$ \\
\bigskip

\_syscall1(int, close, int, filedescriptor); \\
\bigskip
  
  After expanding the \_syscall1 macro the function close() is
  revealed. So we have close() twice in libc and in our program.
  The return value of syscall() or a \_syscall macro is -1 if the
  system call failed otherwise a value greater or equal than 0.
  Take a look at the global variable errno to see what happened if
  a system call failed.
  
  The following system calls that are available on BSD and
  SYS V are not available on linux: \\
  audit(), auditon(), auditsvc(), fchroot(), getauid(), getdents(), 
  getmsg(), mincore(), poll(), putmsg(), setaudit(), setauid().

\endinput
