cmogstored 1.8.1 - use default system stack size
[cmogstored.git] / cloexec_detect.c
blob723e20fa34bf0718b66500d0279993a3ac81d579
1 /*
2 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
3 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 */
5 #include "cmogstored.h"
6 bool mog_cloexec_atomic;
8 /*
9 * The presence of O_CLOEXEC in headers doesn't mean the kernel supports it
11 #if defined(O_CLOEXEC) && (O_CLOEXEC != 0) && \
12 defined(SOCK_CLOEXEC) && \
13 defined(HAVE_ACCEPT4)
14 __attribute__((constructor)) static void cloexec_detect(void)
16 int flags;
17 int fd = open("/dev/null", O_RDONLY|O_CLOEXEC);
19 if (fd < 0) {
20 if (errno == EINVAL) goto out;
21 die_errno("open(/dev/null) failed");
24 flags = fcntl(fd, F_GETFD);
25 if (flags != -1)
26 mog_cloexec_atomic = ((flags & FD_CLOEXEC) == FD_CLOEXEC);
27 mog_close(fd);
29 if (! mog_cloexec_atomic) goto out;
31 /* try to ensure sockets are sane, too */
32 fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
33 if (fd < 0) {
34 if (errno == EINVAL) goto out;
35 die_errno("socket(AF_INET, ...) failed");
37 flags = fcntl(fd, F_GETFD);
38 if (flags != -1)
39 mog_cloexec_atomic = ((flags & FD_CLOEXEC) == FD_CLOEXEC);
40 mog_close(fd);
42 out:
43 if (mog_cloexec_atomic)
44 mog_cloexec_works();
45 else
46 warn("close-on-exec is NOT atomic");
48 #endif /* no O_CLOEXEC at all */