From cf3173b6f424121f4239e8cdfcbc944749425c87 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 28 Jan 2021 08:41:46 -0800 Subject: [PATCH] posix: use SOCK_CLOEXEC and accept4() If available, ensure O_CLOEXEC is set atomically. Continue to F_SETFD as well (paranoia). Available at least on Linux, freebsd, and RTEMS 5 w/ libbsd --- modules/libcom/src/osi/os/posix/osdSock.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/libcom/src/osi/os/posix/osdSock.c b/modules/libcom/src/osi/os/posix/osdSock.c index 4479f1403..84b6a9bfb 100644 --- a/modules/libcom/src/osi/os/posix/osdSock.c +++ b/modules/libcom/src/osi/os/posix/osdSock.c @@ -28,6 +28,14 @@ #include "epicsAssert.h" #include "errlog.h" +/* Linux and *BSD (at least) specific way to atomically set O_CLOEXEC */ +#ifdef SOCK_CLOEXEC +/* with glibc, SOCK_CLOEXEC does not expand to a simple constant */ +# define HAVE_SOCK_CLOEXEC +#else +# define SOCK_CLOEXEC (0) +#endif + /* * Protect some routines which are not thread-safe */ @@ -71,7 +79,7 @@ void osiSockRelease() LIBCOM_API SOCKET epicsStdCall epicsSocketCreate ( int domain, int type, int protocol ) { - SOCKET sock = socket ( domain, type, protocol ); + SOCKET sock = socket ( domain, type | SOCK_CLOEXEC, protocol ); if ( sock < 0 ) { sock = INVALID_SOCKET; } @@ -94,7 +102,11 @@ LIBCOM_API SOCKET epicsStdCall epicsSocketCreate ( LIBCOM_API int epicsStdCall epicsSocketAccept ( int sock, struct sockaddr * pAddr, osiSocklen_t * addrlen ) { +#ifndef HAVE_SOCK_CLOEXEC int newSock = accept ( sock, pAddr, addrlen ); +#else + int newSock = accept4 ( sock, pAddr, addrlen, SOCK_CLOEXEC ); +#endif if ( newSock < 0 ) { newSock = INVALID_SOCKET; }