1// This file includes selected Linux header files, and supplementary
2// definitions, needed for general-purpose code.
3
4#include "support.h"
5
6// Selected Linux headers.
7
8#include <linux/auxvec.h>
9#include <linux/eventpoll.h>
10#include <linux/fadvise.h>
11#include <linux/falloc.h>
12#include <linux/fcntl.h>
13#include <linux/fs.h>
14#include <linux/futex.h>
15#include <linux/in.h>
16#include <linux/inotify.h>
17#include <linux/ip.h>
18#include <linux/in6.h>
19#include <linux/ipv6.h>
20#include <linux/limits.h>
21#include <linux/magic.h>
22#include <linux/mman.h>
23#include <linux/net.h>
24#include <linux/poll.h>
25#include <linux/prctl.h>
26#include <linux/random.h>
27#include <linux/resource.h>
28#include <linux/sched.h>
29#include <linux/signal.h>
30#include <linux/socket.h>
31#include <linux/stat.h>
32#include <linux/sysinfo.h>
33#include <linux/tcp.h>
34#include <linux/termios.h>
35#include <linux/time.h>
36#include <linux/types.h>
37#include <linux/uio.h>
38#include <linux/un.h>
39#include <linux/unistd.h>
40#include <linux/utsname.h>
41#include <linux/wait.h>
42
43#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
44#include <linux/memfd.h>
45#endif
46
47#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)
48#include <linux/membarrier.h>
49#endif
50
51#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,20,0)
52#include <linux/timerfd.h>
53#endif
54
55#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
56#include <linux/openat2.h>
57#endif
58
59#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)
60#include <linux/userfaultfd.h>
61#endif
62
63#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0)
64#include <linux/io_uring.h>
65#endif
66
67// Miscellaneous definitions which don't appear to be defined in Linux's public
68// headers, but which are nonetheless part of the ABI, and necessary for
69// interoperability.
70//
71// When adding definitions here, please only include content needed for
72// interoperability with Linux's public ABI, and please only include types
73// and constants.
74//
75// In particular, please don't copy comments from other sources. And please
76// don't include any functions or function-style macros, as bindgen isn't
77// able to generate bindings for them.
78//
79// Also, please be aware that libc implementations (and thus the Rust libc
80// crate as well) sometimes define types and constants with similar names but
81// which are ABI-incompatible with the Linux kernel ABI. This file should
82// only describe the kernel ABI.
83
84struct sockaddr {
85    struct __kernel_sockaddr_storage __storage;
86};
87#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,32)
88typedef uint16_t __kernel_sa_family_t;
89#endif
90
91struct linger {
92    int l_onoff;
93    int l_linger;
94};
95
96#if LINUX_VERSION_CODE <= KERNEL_VERSION(4,4,0)
97typedef long long __kernel_time64_t;
98struct __kernel_timespec {
99    __kernel_time64_t tv_sec;
100    long long         tv_nsec;
101};
102#endif
103
104#define DT_UNKNOWN 0
105#define DT_FIFO    1
106#define DT_CHR     2
107#define DT_DIR     4
108#define DT_BLK     6
109#define DT_REG     8
110#define DT_LNK    10
111#define DT_SOCK   12
112
113#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
114#define WIFEXITED(status)   (((status) & 0x7f) == 0)
115
116#define SHUT_RD   0
117#define SHUT_WR   1
118#define SHUT_RDWR 2
119
120struct linux_dirent64 {
121    __UINT64_TYPE__ d_ino;
122    __INT64_TYPE__  d_off;
123    __u16           d_reclen;
124    __u8            d_type;
125    char            d_name[];
126};
127
128typedef __UINT32_TYPE__ socklen_t;
129
130// Obtain the definitions of structs stat/stat64 and statfs/statfs64.
131#include <asm/stat.h>
132#include <asm/statfs.h>
133
134// Linux only defines this as a macro; make it available as a typedef.
135// And use the libc name. And mips and s390x are special.
136#if defined(__mips__) || defined(__s390x__)
137typedef __u32 __fsword_t;
138#elif defined(__mips64__)
139typedef long __fsword_t;
140#else
141typedef __statfs_word __fsword_t;
142#endif
143
144#if defined(__mips__) || defined(__mips64__)
145#define SOCK_STREAM    2
146#define SOCK_DGRAM     1
147#else
148#define SOCK_STREAM    1
149#define SOCK_DGRAM     2
150#endif
151#define SOCK_RAW       3
152#define SOCK_RDM       4
153#define SOCK_SEQPACKET 5
154
155#define F_OK 0
156#define R_OK 4
157#define W_OK 2
158#define X_OK 1
159
160#define UTIME_NOW 0x3fffffff
161#define UTIME_OMIT 0x3ffffffe
162
163#define MSG_DONTWAIT 0x40
164
165#define AF_UNSPEC     0
166#define AF_UNIX       1
167#define AF_INET       2
168#define AF_AX25       3
169#define AF_IPX        4
170#define AF_APPLETALK  5
171#define AF_NETROM     6
172#define AF_BRIDGE     7
173#define AF_ATMPVC     8
174#define AF_X25        9
175#define AF_INET6      10
176#define AF_ROSE       11
177#define AF_DECnet     12
178#define AF_NETBEUI    13
179#define AF_SECURITY   14
180#define AF_KEY        15
181#define AF_NETLINK    16
182#define AF_PACKET     17
183#define AF_ASH        18
184#define AF_ECONET     19
185#define AF_ATMSVC     20
186#define AF_RDS        21
187#define AF_SNA        22
188#define AF_IRDA       23
189#define AF_PPPOX      24
190#define AF_WANPIPE    25
191#define AF_LLC        26
192#define AF_CAN        29
193#define AF_TIPC       30
194#define AF_BLUETOOTH  31
195#define AF_IUCV       32
196#define AF_RXRPC      33
197#define AF_ISDN       34
198#define AF_PHONET     35
199#define AF_IEEE802154 36
200#define AF_MAX        37
201
202#define MSG_OOB          0x1
203#define MSG_PEEK         0x2
204#define MSG_DONTROUTE    0x4
205#define MSG_CTRUNC       0x8
206#define MSG_PROBE        0x10
207#define MSG_TRUNC        0x20
208#define MSG_DONTWAIT     0x40
209#define MSG_EOR          0x80
210#define MSG_WAITALL      0x100
211#define MSG_FIN          0x200
212#define MSG_SYN          0x400
213#define MSG_CONFIRM      0x800
214#define MSG_RST          0x1000
215#define MSG_ERRQUEUE     0x2000
216#define MSG_NOSIGNAL     0x4000
217#define MSG_MORE         0x8000
218#define MSG_CMSG_CLOEXEC 0x40000000
219
220#define STDIN_FILENO  0
221#define STDOUT_FILENO 1
222#define STDERR_FILENO 2
223
224// Linux exports these, but the definitions have syntax that bindgen doesn't
225// recognize as constants.
226#undef RWF_HIPRI
227#undef RWF_DSYNC
228#undef RWF_SYNC
229#undef RWF_NOWAIT
230#undef RWF_APPEND
231#define RWF_HIPRI       0x00000001
232#define RWF_DSYNC       0x00000002
233#define RWF_SYNC        0x00000004
234#define RWF_NOWAIT      0x00000008
235#define RWF_APPEND      0x00000010
236
237// Linux doesn't appear to export <linux/eventfd.h> at all.
238#define EFD_SEMAPHORE   1
239#define EFD_CLOEXEC     O_CLOEXEC
240#define EFD_NONBLOCK    O_NONBLOCK
241
242// Flags for epoll_create1.
243#define EPOLL_CLOEXEC O_CLOEXEC
244
245// Constants for `epoll_ctl`.
246#define EPOLL_CTL_ADD 1
247#define EPOLL_CTL_DEL 2
248#define EPOLL_CTL_MOD 3
249
250// Flags for epoll events
251#define EPOLLIN        0x00000001
252#define EPOLLPRI       0x00000002
253#define EPOLLOUT       0x00000004
254#define EPOLLERR       0x00000008
255#define EPOLLHUP       0x00000010
256#define EPOLLNVAL      0x00000020
257#define EPOLLRDNORM    0x00000040
258#define EPOLLRDBAND    0x00000080
259#define EPOLLWRNORM    0x00000100
260#define EPOLLWRBAND    0x00000200
261#define EPOLLMSG       0x00000400
262#define EPOLLRDHUP     0x00002000
263#define EPOLLEXCLUSIVE 0x10000000
264#define EPOLLWAKEUP    0x20000000
265#define EPOLLONESHOT   0x40000000
266#define EPOLLET        0x80000000
267
268// Flags for timerfd
269#define TFD_TIMER_ABSTIME      1
270#define TFD_CLOEXEC            O_CLOEXEC
271#define TFD_NONBLOCK           O_NONBLOCK
272#define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
273#define TFD_CREATE_FLAGS       TFD_SHARED_FCNTL_FLAGS
274#define TFD_SETTIME_FLAGS      TFD_TIMER_ABSTIME
275
276struct user_desc {
277    unsigned entry_number;
278    unsigned base_addr;
279    unsigned limit;
280    unsigned seg_32bit:1;
281    unsigned contents:2;
282    unsigned read_exec_only:1;
283    unsigned limit_in_pages:1;
284    unsigned seg_not_present:1;
285    unsigned useable:1;
286#ifdef __x86_64__
287    unsigned lm:1;
288#endif
289};
290
291#if defined(__x86_64__) || defined(__i386__)
292#define ARCH_SET_FS 0x1002
293#endif
294
295struct msghdr {
296    void         *msg_name;
297    int           msg_namelen;
298    struct iovec *msg_iov;
299    size_t        msg_iovlen;
300    void         *msg_control;
301    size_t        msg_controllen;
302    unsigned int  msg_flags;
303};
304
305struct cmsghdr {
306    size_t cmsg_len;
307    int    cmsg_level;
308    int    cmsg_type;
309};
310
311
312#define SCM_RIGHTS      0x01
313#define SCM_CREDENTIALS 0x02
314#define SCM_SECURITY    0x03
315
316struct ucred {
317    __u32 pid;
318    __u32 uid;
319    __u32 gid;
320};
321
322struct mmsghdr {
323    struct msghdr msg_hdr;
324    unsigned int  msg_len;
325};
326
327#define UFFD_API 0xAA
328
329#define UFFDIO_REGISTER_MODE_MISSING  0x1
330#define UFFDIO_REGISTER_MODE_WP       0x2
331#define UFFDIO_REGISTER_MODE_MINOR    0x4
332
333#define UFFDIO_COPY_MODE_DONTWAKE     0x1
334#define UFFDIO_COPY_MODE_WP           0x2
335
336#define UFFDIO_ZEROPAGE_MODE_DONTWAKE 0x1
337
338#if defined(__mips)
339#define POLLWRNORM 0x4
340
341#undef TCSANOW
342#undef TCSADRAIN
343#undef TCSAFLUSH
344#define TCSANOW 0x540e
345#define TCSADRAIN 0x540f
346#define TCSAFLUSH 0x5410
347#endif
348
349#define SPLICE_F_MOVE      1
350#define SPLICE_F_NONBLOCK  2
351#define SPLICE_F_MORE      4
352#define SPLICE_F_GIFT      8
353