xref: /unit/src/nxt_unix.h (revision 1855)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
30Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
40Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
50Sigor@sysoev.ru  */
60Sigor@sysoev.ru 
70Sigor@sysoev.ru 
80Sigor@sysoev.ru #ifndef _NXT_UNIX_H_INCLUDED_
90Sigor@sysoev.ru #define _NXT_UNIX_H_INCLUDED_
100Sigor@sysoev.ru 
110Sigor@sysoev.ru 
120Sigor@sysoev.ru #if (NXT_LINUX)
130Sigor@sysoev.ru 
140Sigor@sysoev.ru #ifdef _FORTIFY_SOURCE
150Sigor@sysoev.ru /*
160Sigor@sysoev.ru  * _FORTIFY_SOURCE
170Sigor@sysoev.ru  *     may call sigaltstack() while _longjmp() checking;
180Sigor@sysoev.ru  *     may cause _longjmp() to fail with message:
190Sigor@sysoev.ru  *         "longjmp() causes uninitialized stack frame";
200Sigor@sysoev.ru  *     does not allow to use "(void) write()";
210Sigor@sysoev.ru  *     does surplus checks.
220Sigor@sysoev.ru  */
230Sigor@sysoev.ru #undef _FORTIFY_SOURCE
240Sigor@sysoev.ru #endif
250Sigor@sysoev.ru 
260Sigor@sysoev.ru #ifndef _GNU_SOURCE
270Sigor@sysoev.ru #define _GNU_SOURCE                 /* pread(), pwrite(), gethostname(). */
280Sigor@sysoev.ru #endif
290Sigor@sysoev.ru 
300Sigor@sysoev.ru #define _FILE_OFFSET_BITS  64
310Sigor@sysoev.ru 
320Sigor@sysoev.ru #include <malloc.h>                 /* malloc_usable_size(). */
330Sigor@sysoev.ru #include <sys/syscall.h>            /* syscall(SYS_gettid). */
340Sigor@sysoev.ru 
350Sigor@sysoev.ru #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4)
360Sigor@sysoev.ru /*
370Sigor@sysoev.ru  * POSIX semaphores using NPTL atomic/futex operations
380Sigor@sysoev.ru  * were introduced during glibc 2.3 development time.
390Sigor@sysoev.ru  */
400Sigor@sysoev.ru #define NXT_HAVE_SEM_TRYWAIT_FAST  1
410Sigor@sysoev.ru #endif
420Sigor@sysoev.ru 
430Sigor@sysoev.ru #endif /* NXT_LINUX */
440Sigor@sysoev.ru 
450Sigor@sysoev.ru 
460Sigor@sysoev.ru #if (NXT_FREEBSD)
470Sigor@sysoev.ru 
480Sigor@sysoev.ru #if (NXT_HAVE_MALLOC_USABLE_SIZE)
490Sigor@sysoev.ru #include <malloc_np.h>              /* malloc_usable_size(). */
500Sigor@sysoev.ru #endif
510Sigor@sysoev.ru 
520Sigor@sysoev.ru #if (__FreeBSD_version >= 900007)
530Sigor@sysoev.ru /* POSIX semaphores using atomic/umtx. */
540Sigor@sysoev.ru #define NXT_HAVE_SEM_TRYWAIT_FAST  1
550Sigor@sysoev.ru #endif
560Sigor@sysoev.ru 
570Sigor@sysoev.ru #endif /* NXT_FREEBSD */
580Sigor@sysoev.ru 
590Sigor@sysoev.ru 
600Sigor@sysoev.ru #if (NXT_SOLARIS)
610Sigor@sysoev.ru 
6293Smax.romanov@nginx.com #ifndef _FILE_OFFSET_BITS
630Sigor@sysoev.ru #define _FILE_OFFSET_BITS  64       /* Must be before <sys/types.h>. */
6493Smax.romanov@nginx.com #endif
650Sigor@sysoev.ru 
660Sigor@sysoev.ru #ifndef _REENTRANT                  /* May be set by "-mt" options. */
670Sigor@sysoev.ru #define _REENTRANT                  /* Thread safe errno. */
680Sigor@sysoev.ru #endif
690Sigor@sysoev.ru 
7026Sigor@sysoev.ru #ifndef _POSIX_PTHREAD_SEMANTICS
710Sigor@sysoev.ru #define _POSIX_PTHREAD_SEMANTICS    /* 2 arguments in sigwait(). */
7226Sigor@sysoev.ru #endif
730Sigor@sysoev.ru 
740Sigor@sysoev.ru /*
750Sigor@sysoev.ru  * Solaris provides two sockets API:
760Sigor@sysoev.ru  *
770Sigor@sysoev.ru  * 1) 4.3BSD sockets (int instead of socklen_t in accept(), etc.;
780Sigor@sysoev.ru  *    struct msghdr.msg_accrights) in libsocket;
790Sigor@sysoev.ru  * 2) X/Open sockets (socklen_t, struct msghdr.msg_control) with __xnet_
800Sigor@sysoev.ru  *    function name prefix in libxnet and libsocket.
810Sigor@sysoev.ru  */
820Sigor@sysoev.ru 
830Sigor@sysoev.ru /* Enable X/Open sockets API. */
840Sigor@sysoev.ru #define _XOPEN_SOURCE
850Sigor@sysoev.ru #define _XOPEN_SOURCE_EXTENDED  1
860Sigor@sysoev.ru /* Enable Solaris extensions disabled by _XOPEN_SOURCE. */
8726Sigor@sysoev.ru #ifndef __EXTENSIONS__
880Sigor@sysoev.ru #define __EXTENSIONS__
8926Sigor@sysoev.ru #endif
900Sigor@sysoev.ru 
910Sigor@sysoev.ru #endif /* NXT_SOLARIS */
920Sigor@sysoev.ru 
930Sigor@sysoev.ru 
940Sigor@sysoev.ru #if (NXT_MACOSX)
950Sigor@sysoev.ru 
9626Sigor@sysoev.ru #ifndef _DARWIN_C_SOURCE
970Sigor@sysoev.ru #define _DARWIN_C_SOURCE            /* pthread_threadid_np(), mach_port_t. */
9826Sigor@sysoev.ru #endif
990Sigor@sysoev.ru 
1000Sigor@sysoev.ru #include <mach/mach_time.h>         /* mach_absolute_time(). */
1010Sigor@sysoev.ru #include <malloc/malloc.h>          /* malloc_size(). */
1020Sigor@sysoev.ru 
1030Sigor@sysoev.ru #endif /* NXT_MACOSX */
1040Sigor@sysoev.ru 
1050Sigor@sysoev.ru 
1060Sigor@sysoev.ru #if (NXT_AIX)
1070Sigor@sysoev.ru 
1080Sigor@sysoev.ru #define _THREAD_SAFE                /* Must before any include. */
1090Sigor@sysoev.ru 
1100Sigor@sysoev.ru #endif /* NXT_AIX */
1110Sigor@sysoev.ru 
1120Sigor@sysoev.ru 
1130Sigor@sysoev.ru #if (NXT_HPUX)
1140Sigor@sysoev.ru 
1150Sigor@sysoev.ru #define _FILE_OFFSET_BITS  64
1160Sigor@sysoev.ru 
1170Sigor@sysoev.ru /*
1180Sigor@sysoev.ru  * HP-UX provides three sockets API:
1190Sigor@sysoev.ru  *
1200Sigor@sysoev.ru  * 1) 4.3BSD sockets (int instead of socklen_t in accept(), etc.;
1210Sigor@sysoev.ru  *    struct msghdr.msg_accrights) in libc;
1220Sigor@sysoev.ru  * 2) X/Open sockets (socklen_t, struct msghdr.msg_control) with _xpg_
1230Sigor@sysoev.ru  *    function name prefix in libc;
1240Sigor@sysoev.ru  * 3) and X/Open sockets (socklen_t, struct msghdr.msg_control) in libxnet.
1250Sigor@sysoev.ru  */
1260Sigor@sysoev.ru 
1270Sigor@sysoev.ru /* Enable X/Open sockets API. */
1280Sigor@sysoev.ru #define _XOPEN_SOURCE
1290Sigor@sysoev.ru #define _XOPEN_SOURCE_EXTENDED
1300Sigor@sysoev.ru /* Enable static function wrappers for _xpg_ X/Open sockets API in libc. */
1310Sigor@sysoev.ru #define _HPUX_ALT_XOPEN_SOCKET_API
1320Sigor@sysoev.ru 
1330Sigor@sysoev.ru #include <sys/mpctl.h>
1340Sigor@sysoev.ru 
1350Sigor@sysoev.ru #if (NXT_HAVE_HG_GETHRTIME)
1360Sigor@sysoev.ru #include <sys/mercury.h>
1370Sigor@sysoev.ru #endif
1380Sigor@sysoev.ru 
1390Sigor@sysoev.ru #endif /* NXT_HPUX */
1400Sigor@sysoev.ru 
1410Sigor@sysoev.ru 
1420Sigor@sysoev.ru #if (NXT_HAVE_ALLOCA_H)
1430Sigor@sysoev.ru #include <alloca.h>
1440Sigor@sysoev.ru #endif
1450Sigor@sysoev.ru #include <dlfcn.h>
1460Sigor@sysoev.ru #include <errno.h>
1470Sigor@sysoev.ru #include <fcntl.h>
1480Sigor@sysoev.ru #include <grp.h>
1490Sigor@sysoev.ru #include <limits.h>
1500Sigor@sysoev.ru #include <netdb.h>
1510Sigor@sysoev.ru #include <netinet/in.h>
1520Sigor@sysoev.ru #include <netinet/tcp.h>
1530Sigor@sysoev.ru #include <poll.h>
1540Sigor@sysoev.ru #include <pwd.h>
1550Sigor@sysoev.ru #include <semaphore.h>
1560Sigor@sysoev.ru #include <setjmp.h>
1570Sigor@sysoev.ru #include <sched.h>
1580Sigor@sysoev.ru #include <signal.h>
1590Sigor@sysoev.ru #if (NXT_HAVE_POSIX_SPAWN)
1600Sigor@sysoev.ru #include <spawn.h>
1610Sigor@sysoev.ru #endif
1620Sigor@sysoev.ru #include <stdarg.h>
1630Sigor@sysoev.ru #include <stddef.h>                 /* offsetof() */
1640Sigor@sysoev.ru #include <stdio.h>
1650Sigor@sysoev.ru #include <stdint.h>
1660Sigor@sysoev.ru #include <stdlib.h>
1670Sigor@sysoev.ru #include <string.h>
1680Sigor@sysoev.ru #include <syslog.h>
1690Sigor@sysoev.ru #if (NXT_HAVE_SYS_FILIO_H)
1700Sigor@sysoev.ru #include <sys/filio.h>              /* FIONBIO */
1710Sigor@sysoev.ru #endif
1720Sigor@sysoev.ru #include <sys/ioctl.h>
1730Sigor@sysoev.ru #include <sys/mman.h>
1740Sigor@sysoev.ru #include <sys/param.h>              /* MAXPATHLEN */
1750Sigor@sysoev.ru #include <sys/resource.h>
1760Sigor@sysoev.ru #include <sys/socket.h>
1770Sigor@sysoev.ru #include <sys/stat.h>
1780Sigor@sysoev.ru #include <sys/time.h>
1790Sigor@sysoev.ru #include <sys/types.h>
1800Sigor@sysoev.ru #include <sys/uio.h>
1810Sigor@sysoev.ru #if (NXT_HAVE_UNIX_DOMAIN)
1820Sigor@sysoev.ru #include <sys/un.h>
1830Sigor@sysoev.ru #endif
1840Sigor@sysoev.ru #include <sys/wait.h>
1850Sigor@sysoev.ru #include <time.h>
1860Sigor@sysoev.ru #include <unistd.h>
1870Sigor@sysoev.ru #include <pthread.h>
1880Sigor@sysoev.ru 
1890Sigor@sysoev.ru #if (NXT_HAVE_EPOLL)
1900Sigor@sysoev.ru #include <sys/epoll.h>
1910Sigor@sysoev.ru 
1920Sigor@sysoev.ru #ifdef EPOLLRDHUP
1930Sigor@sysoev.ru /*
1940Sigor@sysoev.ru  * Epoll edge-tiggered mode is pretty much useless without EPOLLRDHUP support.
1950Sigor@sysoev.ru  */
1960Sigor@sysoev.ru #define NXT_HAVE_EPOLL_EDGE  1
1970Sigor@sysoev.ru #endif
1980Sigor@sysoev.ru 
1990Sigor@sysoev.ru #endif
2000Sigor@sysoev.ru 
2010Sigor@sysoev.ru #if (NXT_HAVE_SIGNALFD)
2020Sigor@sysoev.ru #include <sys/signalfd.h>
2030Sigor@sysoev.ru #endif
2040Sigor@sysoev.ru 
2050Sigor@sysoev.ru #if (NXT_HAVE_EVENTFD)
2060Sigor@sysoev.ru #include <sys/eventfd.h>
2070Sigor@sysoev.ru #endif
2080Sigor@sysoev.ru 
2090Sigor@sysoev.ru #if (NXT_HAVE_KQUEUE)
2100Sigor@sysoev.ru #include <sys/event.h>
2110Sigor@sysoev.ru #endif
2120Sigor@sysoev.ru 
2130Sigor@sysoev.ru #if (NXT_HAVE_EVENTPORT)
2140Sigor@sysoev.ru #include <port.h>
2150Sigor@sysoev.ru #endif
2160Sigor@sysoev.ru 
2170Sigor@sysoev.ru #if (NXT_HAVE_DEVPOLL)
2180Sigor@sysoev.ru #include <sys/devpoll.h>
2190Sigor@sysoev.ru #endif
2200Sigor@sysoev.ru 
2210Sigor@sysoev.ru #if (NXT_HAVE_POLLSET)
2220Sigor@sysoev.ru #include <sys/pollset.h>
2230Sigor@sysoev.ru #endif
2240Sigor@sysoev.ru 
2250Sigor@sysoev.ru #if (NXT_HAVE_LINUX_SENDFILE)
2260Sigor@sysoev.ru #include <sys/sendfile.h>
2270Sigor@sysoev.ru #endif
2280Sigor@sysoev.ru 
2290Sigor@sysoev.ru #if (NXT_HAVE_SOLARIS_SENDFILEV)
2300Sigor@sysoev.ru #include <sys/sendfile.h>
2310Sigor@sysoev.ru #endif
2320Sigor@sysoev.ru 
233676Spluknet@nginx.com #if (NXT_HAVE_GETRANDOM)
234676Spluknet@nginx.com #include <sys/random.h>             /* getrandom(). */
235676Spluknet@nginx.com #elif (NXT_HAVE_LINUX_SYS_GETRANDOM)
236676Spluknet@nginx.com #include <linux/random.h>           /* SYS_getrandom. */
237733Spluknet@nginx.com #elif (NXT_HAVE_GETENTROPY_SYS_RANDOM)
238733Spluknet@nginx.com #include <sys/random.h>             /* getentropy(). */
239676Spluknet@nginx.com #endif
240676Spluknet@nginx.com 
2411489St.nateldemoura@f5.com #if (NXT_HAVE_ISOLATION_ROOTFS)
2421489St.nateldemoura@f5.com #include <sys/mount.h>
2431489St.nateldemoura@f5.com #endif
2440Sigor@sysoev.ru 
245*1855Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
246*1855Sz.hong@f5.com #include <linux/openat2.h>
247*1855Sz.hong@f5.com #endif
248*1855Sz.hong@f5.com 
2490Sigor@sysoev.ru #if (NXT_TEST_BUILD)
2501Sigor@sysoev.ru #include <nxt_test_build.h>
2510Sigor@sysoev.ru #endif
2520Sigor@sysoev.ru 
2530Sigor@sysoev.ru 
2540Sigor@sysoev.ru /*
2550Sigor@sysoev.ru  * On Linux IOV_MAX is 1024.  Linux uses kernel stack for 8 iovec's
2560Sigor@sysoev.ru  * to avoid kernel allocation/deallocation.
2570Sigor@sysoev.ru  *
2580Sigor@sysoev.ru  * On FreeBSD IOV_MAX is 1024.  FreeBSD used kernel stack for 8 iovec's
2590Sigor@sysoev.ru  * to avoid kernel allocation/deallocation until FreeBSD 5.2.
2600Sigor@sysoev.ru  * FreeBSD 5.2 and later do not use stack at all.
2610Sigor@sysoev.ru  *
2620Sigor@sysoev.ru  * On Solaris IOV_MAX is 16 and Solaris uses only kernel stack.
2630Sigor@sysoev.ru  *
2640Sigor@sysoev.ru  * On MacOSX IOV_MAX is 1024.  MacOSX used kernel stack for 8 iovec's
2650Sigor@sysoev.ru  * to avoid kernel allocation/deallocation until MacOSX 10.4 (Tiger).
2660Sigor@sysoev.ru  * MacOSX 10.4 and later do not use stack at all.
2670Sigor@sysoev.ru  *
2680Sigor@sysoev.ru  * On NetBSD, OpenBSD, and DragonFlyBSD IOV_MAX is 1024.  All these OSes
2690Sigor@sysoev.ru  * uses kernel stack for 8 iovec's to avoid kernel allocation/deallocation.
2700Sigor@sysoev.ru  *
2710Sigor@sysoev.ru  * On AIX and HP-UX IOV_MAX is 16.
2720Sigor@sysoev.ru  */
2730Sigor@sysoev.ru #define NXT_IOBUF_MAX  8
2740Sigor@sysoev.ru 
2750Sigor@sysoev.ru 
2760Sigor@sysoev.ru typedef struct iovec   nxt_iobuf_t;
2770Sigor@sysoev.ru 
2780Sigor@sysoev.ru #define                                                                       \
2790Sigor@sysoev.ru nxt_iobuf_data(iob)                                                           \
2800Sigor@sysoev.ru     (iob)->iov_base
2810Sigor@sysoev.ru 
2820Sigor@sysoev.ru #define                                                                       \
2830Sigor@sysoev.ru nxt_iobuf_size(iob)                                                           \
2840Sigor@sysoev.ru     (iob)->iov_len
2850Sigor@sysoev.ru 
2860Sigor@sysoev.ru #define                                                                       \
2870Sigor@sysoev.ru nxt_iobuf_set(iob, p, size)                                                   \
2880Sigor@sysoev.ru     do {                                                                      \
2890Sigor@sysoev.ru         (iob)->iov_base = (void *) p;                                         \
2900Sigor@sysoev.ru         (iob)->iov_len = size;                                                \
2910Sigor@sysoev.ru     } while (0)
2920Sigor@sysoev.ru 
2930Sigor@sysoev.ru #define                                                                       \
2940Sigor@sysoev.ru nxt_iobuf_add(iob, size)                                                      \
2950Sigor@sysoev.ru     (iob)->iov_len += size
2960Sigor@sysoev.ru 
2970Sigor@sysoev.ru 
2980Sigor@sysoev.ru #endif /* _NXT_UNIX_H_INCLUDED_ */
299