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