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