From e3106f50ac730b480f4a9dc05ee02e32d318bef6 Mon Sep 17 00:00:00 2001 From: Zero King Date: Fri, 8 Sep 2017 15:12:06 +0000 Subject: [PATCH] support SOCK_CLOEXEC on macOS --- compat/socket.c | 22 ++++++++++++++++++++++ include/sys/socket.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/compat/socket.c b/compat/socket.c index fd699f9..e13ced1 100644 --- a/compat/socket.c +++ b/compat/socket.c @@ -26,4 +26,26 @@ _socket(int domain, int type, int protocol) return s; } +int +_socketpair(int domain, int type, int protocol, int socket_vector[2]) +{ + int rc = socketpair(domain, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK), protocol, socket_vector); + int flags, i; + if (rc == -1) + return rc; + + for (i = 0; i < 2; i++) { + if (type & SOCK_CLOEXEC) { + flags = fcntl(socket_vector[i], F_GETFD); + fcntl(socket_vector[i], F_SETFD, flags | FD_CLOEXEC); + } + + if (type & SOCK_NONBLOCK) { + flags = fcntl(socket_vector[i], F_GETFL); + fcntl(socket_vector[i], F_SETFL, flags | O_NONBLOCK); + } + } + return rc; +} + #endif diff --git a/include/sys/socket.h b/include/sys/socket.h index 4cbc55d..86abda5 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -16,8 +16,10 @@ #if !defined(SOCK_NONBLOCK) || !defined(SOCK_CLOEXEC) #define NEED_SOCKET_FLAGS int _socket(int domain, int type, int protocol); +int _socketpair(int domain, int type, int protocol, int socket_vector[2]); #ifndef SOCKET_FLAGS_PRIV #define socket(d, t, p) _socket(d, t, p) +#define socketpair(d, t, p, s) _socketpair(d, t, p, s) #endif #endif