xref: /unit/src/nxt_unix.h (revision 0:a63ceefd6ab0)
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 #define _POSIX_PTHREAD_SEMANTICS    /* 2 arguments in sigwait(). */
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 #define __EXTENSIONS__
88 
89 #endif /* NXT_SOLARIS */
90 
91 
92 #if (NXT_MACOSX)
93 
94 #define _XOPEN_SOURCE               /* ucontext(3). */
95 #define _DARWIN_C_SOURCE            /* pthread_threadid_np(), mach_port_t. */
96 
97 #include <mach/mach_time.h>         /* mach_absolute_time(). */
98 #include <malloc/malloc.h>          /* malloc_size(). */
99 
100 #endif /* NXT_MACOSX */
101 
102 
103 #if (NXT_AIX)
104 
105 #define _THREAD_SAFE                /* Must before any include. */
106 
107 #endif /* NXT_AIX */
108 
109 
110 #if (NXT_HPUX)
111 
112 #define _FILE_OFFSET_BITS  64
113 
114 /*
115  * HP-UX provides three sockets API:
116  *
117  * 1) 4.3BSD sockets (int instead of socklen_t in accept(), etc.;
118  *    struct msghdr.msg_accrights) in libc;
119  * 2) X/Open sockets (socklen_t, struct msghdr.msg_control) with _xpg_
120  *    function name prefix in libc;
121  * 3) and X/Open sockets (socklen_t, struct msghdr.msg_control) in libxnet.
122  */
123 
124 /* Enable X/Open sockets API. */
125 #define _XOPEN_SOURCE
126 #define _XOPEN_SOURCE_EXTENDED
127 /* Enable static function wrappers for _xpg_ X/Open sockets API in libc. */
128 #define _HPUX_ALT_XOPEN_SOCKET_API
129 
130 #include <sys/mpctl.h>
131 
132 #if (NXT_HAVE_HG_GETHRTIME)
133 #include <sys/mercury.h>
134 #endif
135 
136 #endif /* NXT_HPUX */
137 
138 
139 #if (NXT_HAVE_ALLOCA_H)
140 #include <alloca.h>
141 #endif
142 #include <dlfcn.h>
143 #include <errno.h>
144 #include <fcntl.h>
145 #include <grp.h>
146 #include <limits.h>
147 #include <netdb.h>
148 #include <netinet/in.h>
149 #include <netinet/tcp.h>
150 #include <poll.h>
151 #include <pwd.h>
152 #include <semaphore.h>
153 #include <setjmp.h>
154 #include <sched.h>
155 #include <signal.h>
156 #if (NXT_HAVE_POSIX_SPAWN)
157 #include <spawn.h>
158 #endif
159 #include <stdarg.h>
160 #include <stddef.h>                 /* offsetof() */
161 #include <stdio.h>
162 #include <stdint.h>
163 #include <stdlib.h>
164 #include <string.h>
165 #include <syslog.h>
166 #if (NXT_HAVE_SYS_FILIO_H)
167 #include <sys/filio.h>              /* FIONBIO */
168 #endif
169 #include <sys/ioctl.h>
170 #include <sys/mman.h>
171 #include <sys/param.h>              /* MAXPATHLEN */
172 #include <sys/resource.h>
173 #include <sys/socket.h>
174 #include <sys/stat.h>
175 #include <sys/time.h>
176 #include <sys/types.h>
177 #include <sys/uio.h>
178 #if (NXT_HAVE_UNIX_DOMAIN)
179 #include <sys/un.h>
180 #endif
181 #include <sys/wait.h>
182 #include <time.h>
183 #include <ucontext.h>
184 #include <unistd.h>
185 
186 
187 #if (NXT_THREADS)
188 #include <pthread.h>
189 #endif
190 
191 #if (NXT_HAVE_EPOLL)
192 #include <sys/epoll.h>
193 
194 #ifdef EPOLLRDHUP
195 /*
196  * Epoll edge-tiggered mode is pretty much useless without EPOLLRDHUP support.
197  */
198 #define NXT_HAVE_EPOLL_EDGE  1
199 #endif
200 
201 #endif
202 
203 #if (NXT_HAVE_SIGNALFD)
204 #include <sys/signalfd.h>
205 #endif
206 
207 #if (NXT_HAVE_EVENTFD)
208 #include <sys/eventfd.h>
209 #endif
210 
211 #if (NXT_HAVE_KQUEUE)
212 #include <sys/event.h>
213 #endif
214 
215 #if (NXT_HAVE_EVENTPORT)
216 #include <port.h>
217 #endif
218 
219 #if (NXT_HAVE_DEVPOLL)
220 #include <sys/devpoll.h>
221 #endif
222 
223 #if (NXT_HAVE_POLLSET)
224 #include <sys/pollset.h>
225 #endif
226 
227 #if (NXT_HAVE_LINUX_SENDFILE)
228 #include <sys/sendfile.h>
229 #endif
230 
231 #if (NXT_HAVE_SOLARIS_SENDFILEV)
232 #include <sys/sendfile.h>
233 #endif
234 
235 
236 #if (NXT_TEST_BUILD)
237 #include <unix/nxt_test_build.h>
238 #endif
239 
240 
241 /*
242  * On Linux IOV_MAX is 1024.  Linux uses kernel stack for 8 iovec's
243  * to avoid kernel allocation/deallocation.
244  *
245  * On FreeBSD IOV_MAX is 1024.  FreeBSD used kernel stack for 8 iovec's
246  * to avoid kernel allocation/deallocation until FreeBSD 5.2.
247  * FreeBSD 5.2 and later do not use stack at all.
248  *
249  * On Solaris IOV_MAX is 16 and Solaris uses only kernel stack.
250  *
251  * On MacOSX IOV_MAX is 1024.  MacOSX used kernel stack for 8 iovec's
252  * to avoid kernel allocation/deallocation until MacOSX 10.4 (Tiger).
253  * MacOSX 10.4 and later do not use stack at all.
254  *
255  * On NetBSD, OpenBSD, and DragonFlyBSD IOV_MAX is 1024.  All these OSes
256  * uses kernel stack for 8 iovec's to avoid kernel allocation/deallocation.
257  *
258  * On AIX and HP-UX IOV_MAX is 16.
259  */
260 #define NXT_IOBUF_MAX  8
261 
262 
263 typedef struct iovec   nxt_iobuf_t;
264 
265 #define                                                                       \
266 nxt_iobuf_data(iob)                                                           \
267     (iob)->iov_base
268 
269 #define                                                                       \
270 nxt_iobuf_size(iob)                                                           \
271     (iob)->iov_len
272 
273 #define                                                                       \
274 nxt_iobuf_set(iob, p, size)                                                   \
275     do {                                                                      \
276         (iob)->iov_base = (void *) p;                                         \
277         (iob)->iov_len = size;                                                \
278     } while (0)
279 
280 #define                                                                       \
281 nxt_iobuf_add(iob, size)                                                      \
282     (iob)->iov_len += size
283 
284 
285 #endif /* _NXT_UNIX_H_INCLUDED_ */
286