xref: /unit/src/nxt_unix.h (revision 2151:fab207c6836c)
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 #include <spawn.h>
160 #include <stdarg.h>
161 #include <stddef.h>                 /* offsetof() */
162 #include <stdio.h>
163 #include <stdint.h>
164 #include <stdlib.h>
165 #include <string.h>
166 #include <syslog.h>
167 #if (NXT_HAVE_SYS_FILIO_H)
168 #include <sys/filio.h>              /* FIONBIO */
169 #endif
170 #include <sys/ioctl.h>
171 #include <sys/mman.h>
172 #include <sys/param.h>              /* MAXPATHLEN */
173 #include <sys/resource.h>
174 #include <sys/socket.h>
175 #include <sys/stat.h>
176 #include <sys/time.h>
177 #include <sys/types.h>
178 #include <sys/uio.h>
179 #if (NXT_HAVE_UNIX_DOMAIN)
180 #include <sys/un.h>
181 #endif
182 #include <sys/wait.h>
183 #include <time.h>
184 #include <unistd.h>
185 #include <pthread.h>
186 
187 #if (NXT_HAVE_EPOLL)
188 #include <sys/epoll.h>
189 
190 #ifdef EPOLLRDHUP
191 /*
192  * Epoll edge-tiggered mode is pretty much useless without EPOLLRDHUP support.
193  */
194 #define NXT_HAVE_EPOLL_EDGE  1
195 #endif
196 
197 #endif
198 
199 #if (NXT_HAVE_SIGNALFD)
200 #include <sys/signalfd.h>
201 #endif
202 
203 #if (NXT_HAVE_EVENTFD)
204 #include <sys/eventfd.h>
205 #endif
206 
207 #if (NXT_HAVE_KQUEUE)
208 #include <sys/event.h>
209 #endif
210 
211 #if (NXT_HAVE_EVENTPORT)
212 #include <port.h>
213 #endif
214 
215 #if (NXT_HAVE_DEVPOLL)
216 #include <sys/devpoll.h>
217 #endif
218 
219 #if (NXT_HAVE_POLLSET)
220 #include <sys/pollset.h>
221 #endif
222 
223 #if (NXT_HAVE_LINUX_SENDFILE)
224 #include <sys/sendfile.h>
225 #endif
226 
227 #if (NXT_HAVE_SOLARIS_SENDFILEV)
228 #include <sys/sendfile.h>
229 #endif
230 
231 #if (NXT_HAVE_GETRANDOM)
232 #include <sys/random.h>             /* getrandom(). */
233 #elif (NXT_HAVE_LINUX_SYS_GETRANDOM)
234 #include <linux/random.h>           /* SYS_getrandom. */
235 #elif (NXT_HAVE_GETENTROPY_SYS_RANDOM)
236 #include <sys/random.h>             /* getentropy(). */
237 #endif
238 
239 #if (NXT_HAVE_ISOLATION_ROOTFS)
240 #include <sys/mount.h>
241 #endif
242 
243 #if (NXT_HAVE_OPENAT2)
244 #include <linux/openat2.h>
245 #endif
246 
247 #if (NXT_TEST_BUILD)
248 #include <nxt_test_build.h>
249 #endif
250 
251 
252 /*
253  * On Linux IOV_MAX is 1024.  Linux uses kernel stack for 8 iovec's
254  * to avoid kernel allocation/deallocation.
255  *
256  * On FreeBSD IOV_MAX is 1024.  FreeBSD used kernel stack for 8 iovec's
257  * to avoid kernel allocation/deallocation until FreeBSD 5.2.
258  * FreeBSD 5.2 and later do not use stack at all.
259  *
260  * On Solaris IOV_MAX is 16 and Solaris uses only kernel stack.
261  *
262  * On MacOSX IOV_MAX is 1024.  MacOSX used kernel stack for 8 iovec's
263  * to avoid kernel allocation/deallocation until MacOSX 10.4 (Tiger).
264  * MacOSX 10.4 and later do not use stack at all.
265  *
266  * On NetBSD, OpenBSD, and DragonFlyBSD IOV_MAX is 1024.  All these OSes
267  * uses kernel stack for 8 iovec's to avoid kernel allocation/deallocation.
268  *
269  * On AIX and HP-UX IOV_MAX is 16.
270  */
271 #define NXT_IOBUF_MAX  8
272 
273 
274 typedef struct iovec   nxt_iobuf_t;
275 
276 #define nxt_iobuf_data(iob)                                                   \
277     (iob)->iov_base
278 
279 #define nxt_iobuf_size(iob)                                                   \
280     (iob)->iov_len
281 
282 #define nxt_iobuf_set(iob, p, size)                                           \
283     do {                                                                      \
284         (iob)->iov_base = (void *) p;                                         \
285         (iob)->iov_len = size;                                                \
286     } while (0)
287 
288 #define nxt_iobuf_add(iob, size)                                              \
289     (iob)->iov_len += size
290 
291 
292 #endif /* _NXT_UNIX_H_INCLUDED_ */
293