This chapter describes the Linux system calls. For a list of the Linux system calls, see syscalls(2).
In most cases, it is unnecessary to invoke a system call directly, but there are times when the Standard C library does not implement a nice function call for you. In this case, the programmer must manually invoke the system call using syscall(2). Historically, this was also possible using one of the _syscall macros described below.
The important thing to know about a system call is its prototype. You need to know how many arguments, their types, and the function return type. There are six macros that make the actual call into the system easier. They have the form:
_syscall
X
(type
,name
,type1
,arg1
,type2
,arg2
,...)where
X
is 0–5, which are the number of arguments taken by the system call
type
is the return type of the system call
name
is the name of the system call
typeN
is the Nth argument's type
argN
is the name of the Nth argument
These macros create a function called name
with the arguments
you specify. Once you include the _syscall() in your
source file, you call the system call by name
.
Certain codes are used to indicate Unix variants and standards to which calls in the section conform. See standards(7).
Starting around kernel 2.6.18, the _syscall macros were removed from header files supplied to user space. Use syscall(2) instead. (Some architectures, notably ia64, never provided the _syscall macros; on those architectures, syscall(2) was always required.)
The _syscall() macros do not produce a prototype. You may have to create one, especially for C++ users.
System calls are not required to return only positive
or negative error codes. You need to read the source to
be sure how it will return errors. Usually, it is the
negative of a standard error code, for example,
−EPERM. The
_syscall() macros will return the result r
of the system call when
r
is
non-negative, but will return −1 and set the
variable errno
to
−r
when
r
is negative.
For the error codes, see errno(3).
Some system calls, such as mmap(2), require more than five arguments. These are handled by pushing the arguments on the stack and passing a pointer to the block of arguments.
When defining a system call, the argument types
must
be passed
by-value or by-pointer (for aggregates like structs).
#include <stdio.h> #include <errno.h> #include <linux/unistd.h> /* for _syscallX macros/related stuff */ #include <linux/kernel.h> /* for struct sysinfo */ _syscall1(int, sysinfo, struct sysinfo *, info); /* Note: if you copy directly from the nroff source, remember to REMOVE the extra backslashes in the printf statement. */ int main(void) { struct sysinfo s_info; int error; error = sysinfo(&s_info); printf("code error = %d\n", error); printf("Uptime = %lds\nLoad: 1 min %lu / 5 min %lu / 15 min %lu\n" "RAM: total %lu / free %lu / shared %lu\n" "Memory in buffers = %lu\nSwap: total %lu / free %lu\n" "Number of processes = %d\n", s_info.uptime, s_info.loads[0], s_info.loads[1], s_info.loads[2], s_info.totalram, s_info.freeram, s_info.sharedram, s_info.bufferram, s_info.totalswap, s_info.freeswap, s_info.procs); exit(EXIT_SUCCESS); }
|