1_syscall(2) System Calls Manual _syscall(2)
2
3
4
6 _syscall - invoking a system call without library support (OBSOLETE)
7
9 #include <linux/unistd.h>
10
11 A _syscall macro
12
13 desired system call
14
16 The important thing to know about a system call is its prototype. You
17 need to know how many arguments, their types, and the function return
18 type. There are seven macros that make the actual call into the system
19 easier. They have the form:
20
21 _syscallX(type,name,type1,arg1,type2,arg2,...)
22
23 where
24
25 X is 0–6, which are the number of arguments taken by the system
26 call
27
28 type is the return type of the system call
29
30 name is the name of the system call
31
32 typeN is the Nth argument's type
33
34 argN is the name of the Nth argument
35
36 These macros create a function called name with the arguments you spec‐
37 ify. Once you include the _syscall() in your source file, you call the
38 system call by name.
39
41 /usr/include/linux/unistd.h
42
44 Linux.
45
47 Starting around Linux 2.6.18, the _syscall macros were removed from
48 header files supplied to user space. Use syscall(2) instead. (Some
49 architectures, notably ia64, never provided the _syscall macros; on
50 those architectures, syscall(2) was always required.)
51
53 The _syscall() macros do not produce a prototype. You may have to cre‐
54 ate one, especially for C++ users.
55
56 System calls are not required to return only positive or negative error
57 codes. You need to read the source to be sure how it will return er‐
58 rors. Usually, it is the negative of a standard error code, for exam‐
59 ple, -EPERM. The _syscall() macros will return the result r of the
60 system call when r is nonnegative, but will return -1 and set the vari‐
61 able errno to -r when r is negative. For the error codes, see er‐
62 rno(3).
63
64 When defining a system call, the argument types must be passed by-value
65 or by-pointer (for aggregates like structs).
66
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <errno.h>
71 #include <linux/unistd.h> /* for _syscallX macros/related stuff */
72 #include <linux/kernel.h> /* for struct sysinfo */
73
74 _syscall1(int, sysinfo, struct sysinfo *, info);
75
76 int
77 main(void)
78 {
79 struct sysinfo s_info;
80 int error;
81
82 error = sysinfo(&s_info);
83 printf("code error = %d\n", error);
84 printf("Uptime = %lds\nLoad: 1 min %lu / 5 min %lu / 15 min %lu\n"
85 "RAM: total %lu / free %lu / shared %lu\n"
86 "Memory in buffers = %lu\nSwap: total %lu / free %lu\n"
87 "Number of processes = %d\n",
88 s_info.uptime, s_info.loads[0],
89 s_info.loads[1], s_info.loads[2],
90 s_info.totalram, s_info.freeram,
91 s_info.sharedram, s_info.bufferram,
92 s_info.totalswap, s_info.freeswap,
93 s_info.procs);
94 exit(EXIT_SUCCESS);
95 }
96
97 Sample output
98 code error = 0
99 uptime = 502034s
100 Load: 1 min 13376 / 5 min 5504 / 15 min 1152
101 RAM: total 15343616 / free 827392 / shared 8237056
102 Memory in buffers = 5066752
103 Swap: total 27881472 / free 24698880
104 Number of processes = 40
105
107 intro(2), syscall(2), errno(3)
108
109
110
111Linux man-pages 6.04 2023-03-30 _syscall(2)