xref: /unit/src/nxt_unix.h (revision 1855:a946d8cd7f8c)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 
8 #ifndef _NXT_UNIX_H_INCLUDED_
9 #define _NXT_UNIX_H_INCLUDED_
10 
11 
12 #if (NXT_LINUX)
13 
14 #ifdef _FORTIFY_SOURCE
15 /*
16  * _FORTIFY_SOURCE
17  *     may call sigaltstack() while _longjmp() checking;
18  *     may cause _longjmp() to fail with message:
19  *         "longjmp() causes uninitialized stack frame";
20  *     does not allow to use "(void) write()";
21  *     does surplus checks.
22  */
23 #undef _FORTIFY_SOURCE
24 #endif
25 
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE                 /* pread(), pwrite(), gethostname(). */
28 #endif
29 
30 #define _FILE_OFFSET_BITS  64
31 
32 #include <malloc.h>                 /* malloc_usable_size(). */
33 #include <sys/syscall.h>            /* syscall(SYS_gettid). */
34 
35 #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4)
36 /*
37  * POSIX semaphores using NPTL atomic/futex operations
38  * were introduced during glibc 2.3 development time.
39  */
40 #define NXT_HAVE_SEM_TRYWAIT_FAST  1
41 #endif
42 
43 #endif /* NXT_LINUX */
44 
45 
46 #if (NXT_FREEBSD)
47 
48 #if (NXT_HAVE_MALLOC_USABLE_SIZE)
49 #include <malloc_np.h>              /* malloc_usable_size(). */
50 #endif
51 
52 #if (__FreeBSD_version >= 900007)
53 /* POSIX semaphores using atomic/umtx. */
54 #define NXT_HAVE_SEM_TRYWAIT_FAST  1
55 #endif
56 
57 #endif /* NXT_FREEBSD */
58 
59 
60 #if (NXT_SOLARIS)
61 
62 #ifndef _FILE_OFFSET_BITS
63 #define _FILE_OFFSET_BITS  64       /* Must be before <sys/types.h>. */
64 #endif
65 
66 #ifndef _REENTRANT                  /* May be set by "-mt" options. */
67 #define _REENTRANT                  /* Thread safe errno. */
68 #endif
69 
70 #ifndef _POSIX_PTHREAD_SEMANTICS
71 #define _POSIX_PTHREAD_SEMANTICS    /* 2 arguments in sigwait(). */
72 #endif
73 
74 /*
75  * Solaris provides two sockets API:
76  *
77  * 1) 4.3BSD sockets (int instead of socklen_t in accept(), etc.;
78  *    struct msghdr.msg_accrights) in libsocket;
79  * 2) X/Open sockets (socklen_t, struct msghdr.msg_control) with __xnet_
80  *    function name prefix in libxnet and libsocket.
81  */
82 
83 /* Enable X/Open sockets API. */
84 #define _XOPEN_SOURCE
85 #define _XOPEN_SOURCE_EXTENDED  1
86 /* Enable Solaris extensions disabled by _XOPEN_SOURCE. */
87 #ifndef __EXTENSIONS__
88 #define __EXTENSIONS__
89 #endif
90 
91 #endif /* NXT_SOLARIS */
92 
93 
94 #if (NXT_MACOSX)
95 
96 #ifndef _DARWIN_C_SOURCE
97 #define _DARWIN_C_SOURCE            /* pthread_threadid_np(), mach_port_t. */
98 #endif
99 
100 #include <mach/mach_time.h>         /* mach_absolute_time(). */
101 #include <malloc/malloc.h>          /* malloc_size(). */
102 
103 #endif /* NXT_MACOSX */
104 
105 
106 #if (NXT_AIX)
107 
108 #define _THREAD_SAFE                /* Must before any include. */
109 
110 #endif /* NXT_AIX */
111 
112 
113 #if (NXT_HPUX)
114 
115 #define _FILE_OFFSET_BITS  64
116 
117 /*
118  * HP-UX provides three sockets API:
119  *
120  * 1) 4.3BSD sockets (int instead of socklen_t in accept(), etc.;
121  *    struct msghdr.msg_accrights) in libc;
122  * 2) X/Open sockets (socklen_t, struct msghdr.msg_control) with _xpg_
123  *    function name prefix in libc;
124  * 3) and X/Open sockets (socklen_t, struct msghdr.msg_control) in libxnet.
125  */
126 
127 /* Enable X/Open sockets API. */
128 #define _XOPEN_SOURCE
129 #define _XOPEN_SOURCE_EXTENDED
130 /* Enable static function wrappers for _xpg_ X/Open sockets API in libc. */
131 #define _HPUX_ALT_XOPEN_SOCKET_API
132 
133 #include <sys/mpctl.h>
134 
135 #if (NXT_HAVE_HG_GETHRTIME)
136 #include <sys/mercury.h>
137 #endif
138 
139 #endif /* NXT_HPUX */
140 
141 
142 #if (NXT_HAVE_ALLOCA_H)
143 #include <alloca.h>
144 #endif
145 #include <dlfcn.h>
146 #include <errno.h>
147 #include <fcntl.h>
148 #include <grp.h>
149 #include <limits.h>
150 #include <netdb.h>
151 #include <netinet/in.h>
152 #include <netinet/tcp.h>
153 #include <poll.h>
154 #include <pwd.h>
155 #include <semaphore.h>
156 #include <setjmp.h>
157 #include <sched.h>
158 #include <signal.h>
159 #if (NXT_HAVE_POSIX_SPAWN)
160 #include <spawn.h>
161 #endif
162 #include <stdarg.h>
163 #include <stddef.h>                 /* offsetof() */
164 #include <stdio.h>
165 #include <stdint.h>
166 #include <stdlib.h>
167 #include <string.h>
168 #include <syslog.h>
169 #if (NXT_HAVE_SYS_FILIO_H)
170 #include <sys/filio.h>              /* FIONBIO */
171 #endif
172 #include <sys/ioctl.h>
173 #include <sys/mman.h>
174 #include <sys/param.h>              /* MAXPATHLEN */
175 #include <sys/resource.h>
176 #include <sys/socket.h>
177 #include <sys/stat.h>
178 #include <sys/time.h>
179 #include <sys/types.h>
180 #include <sys/uio.h>
181 #if (NXT_HAVE_UNIX_DOMAIN)
182 #include <sys/un.h>
183 #endif
184 #include <sys/wait.h>
185 #include <time.h>
186 #include <unistd.h>
187 #include <pthread.h>
188 
189 #if (NXT_HAVE_EPOLL)
190 #include <sys/epoll.h>
191 
192 #ifdef EPOLLRDHUP
193 /*
194  * Epoll edge-tiggered mode is pretty much useless without EPOLLRDHUP support.
195  */
196 #define NXT_HAVE_EPOLL_EDGE  1
197 #endif
198 
199 #endif
200 
201 #if (NXT_HAVE_SIGNALFD)
202 #include <sys/signalfd.h>
203 #endif
204 
205 #if (NXT_HAVE_EVENTFD)
206 #include <sys/eventfd.h>
207 #endif
208 
209 #if (NXT_HAVE_KQUEUE)
210 #include <sys/event.h>
211 #endif
212 
213 #if (NXT_HAVE_EVENTPORT)
214 #include <port.h>
215 #endif
216 
217 #if (NXT_HAVE_DEVPOLL)
218 #include <sys/devpoll.h>
219 #endif
220 
221 #if (NXT_HAVE_POLLSET)
222 #include <sys/pollset.h>
223 #endif
224 
225 #if (NXT_HAVE_LINUX_SENDFILE)
226 #include <sys/sendfile.h>
227 #endif
228 
229 #if (NXT_HAVE_SOLARIS_SENDFILEV)
230 #include <sys/sendfile.h>
231 #endif
232 
233 #if (NXT_HAVE_GETRANDOM)
234 #include <sys/random.h>             /* getrandom(). */
235 #elif (NXT_HAVE_LINUX_SYS_GETRANDOM)
236 #include <linux/random.h>           /* SYS_getrandom. */
237 #elif (NXT_HAVE_GETENTROPY_SYS_RANDOM)
238 #include <sys/random.h>             /* getentropy(). */
239 #endif
240 
241 #if (NXT_HAVE_ISOLATION_ROOTFS)
242 #include <sys/mount.h>
243 #endif
244 
245 #if (NXT_HAVE_OPENAT2)
246 #include <linux/openat2.h>
247 #endif
248 
249 #if (NXT_TEST_BUILD)
250 #include <nxt_test_build.h>
251 #endif
252 
253 
254 /*
255  * On Linux IOV_MAX is 1024.  Linux uses kernel stack for 8 iovec's
256  * to avoid kernel allocation/deallocation.
257  *
258  * On FreeBSD IOV_MAX is 1024.  FreeBSD used kernel stack for 8 iovec's
259  * to avoid kernel allocation/deallocation until FreeBSD 5.2.
260  * FreeBSD 5.2 and later do not use stack at all.
261  *
262  * On Solaris IOV_MAX is 16 and Solaris uses only kernel stack.
263  *
264  * On MacOSX IOV_MAX is 1024.  MacOSX used kernel stack for 8 iovec's
265  * to avoid kernel allocation/deallocation until MacOSX 10.4 (Tiger).
266  * MacOSX 10.4 and later do not use stack at all.
267  *
268  * On NetBSD, OpenBSD, and DragonFlyBSD IOV_MAX is 1024.  All these OSes
269  * uses kernel stack for 8 iovec's to avoid kernel allocation/deallocation.
270  *
271  * On AIX and HP-UX IOV_MAX is 16.
272  */
273 #define NXT_IOBUF_MAX  8
274 
275 
276 typedef struct iovec   nxt_iobuf_t;
277 
278 #define                                                                       \
279 nxt_iobuf_data(iob)                                                           \
280     (iob)->iov_base
281 
282 #define                                                                       \
283 nxt_iobuf_size(iob)                                                           \
284     (iob)->iov_len
285 
286 #define                                                                       \
287 nxt_iobuf_set(iob, p, size)                                                   \
288     do {                                                                      \
289         (iob)->iov_base = (void *) p;                                         \
290         (iob)->iov_len = size;                                                \
291     } while (0)
292 
293 #define                                                                       \
294 nxt_iobuf_add(iob, size)                                                      \
295     (iob)->iov_len += size
296 
297 
298 #endif /* _NXT_UNIX_H_INCLUDED_ */
299