From b5cb23d69898da70f33a61e1074f31da604b715f Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 14:52:05 -0800 Subject: [PATCH] os: Use fallback implementations when *_CLOEXEC are unavailable at build time On platforms without F_DUPFD_CLOEXEC, MSG_CMSG_CLOEXEC, or SOCK_CLOEXEC, we should skip past their usage and just use the fcntl() fallback. Signed-off-by: Jeremy Huddleston Sequoia --- src/wayland-os.c | 6 +++++- tests/os-wrappers-test.c | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/wayland-os.c b/src/wayland-os.c index 6651c8ce..86e9816d 100644 --- a/src/wayland-os.c +++ b/src/wayland-os.c @@ -69,11 +69,13 @@ wl_os_socket_cloexec(int domain, int type, int protocol) { int fd; +#ifdef SOCK_CLOEXEC fd = socket(domain, type | SOCK_CLOEXEC, protocol); if (fd >= 0) return fd; if (errno != EINVAL) return -1; +#endif fd = socket(domain, type, protocol); return set_cloexec_or_close(fd); @@ -140,11 +142,13 @@ wl_os_dupfd_cloexec(int fd, int minfd) { int newfd; +#ifdef F_DUPFD_CLOEXEC newfd = fcntl(fd, F_DUPFD_CLOEXEC, minfd); if (newfd >= 0) return newfd; if (errno != EINVAL) return -1; +#endif newfd = fcntl(fd, F_DUPFD, minfd); return set_cloexec_or_close(newfd); @@ -192,7 +196,7 @@ wl_os_recvmsg_cloexec(int sockfd, struct msghdr *msg, int flags) * fix (https://cgit.freebsd.org/src/commit/?id=6ceacebdf52211). */ #pragma message("Using fallback directly since MSG_CMSG_CLOEXEC is broken.") -#else +#elif defined(MSG_CMSG_CLOEXEC) ssize_t len; len = recvmsg(sockfd, msg, flags | MSG_CMSG_CLOEXEC); diff --git a/tests/os-wrappers-test.c b/tests/os-wrappers-test.c index 8d8c3ab9..552eddbd 100644 --- a/tests/os-wrappers-test.c +++ b/tests/os-wrappers-test.c @@ -141,10 +141,12 @@ recvmsg(int sockfd, struct msghdr *msg, int flags) { wrapped_calls_recvmsg++; +#ifdef MSG_CMSG_CLOEXEC if (fall_back && (flags & MSG_CMSG_CLOEXEC)) { errno = EINVAL; return -1; } +#endif return real_recvmsg(sockfd, msg, flags); } @@ -342,9 +344,10 @@ do_os_wrappers_recvmsg_cloexec(int n) struct marshal_data data; data.nr_fds_begin = count_open_fds(); -#if HAVE_BROKEN_MSG_CMSG_CLOEXEC +#if HAVE_BROKEN_MSG_CMSG_CLOEXEC || !defined(MSG_CMSG_CLOEXEC) /* We call the fallback directly on FreeBSD versions with a broken - * MSG_CMSG_CLOEXEC, so we don't call the local recvmsg() wrapper. */ + * MSG_CMSG_CLOEXEC or platforms without MSG_CMSG_CLOEXEC, so we + * don't call the local recvmsg() wrapper. */ data.wrapped_calls = 0; #else data.wrapped_calls = n;