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