]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
wakeup_create: use FD_CLOEXEC/SOCK_CLOEXEC
authorAndrew <unlightable@gmail.com>
Mon, 13 May 2024 17:34:06 +0000 (22:34 +0500)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 16 May 2024 21:02:12 +0000 (23:02 +0200)
for `pipe()`/`socketpair()`

Fixes #13618
Closes #13625

lib/multihandle.h
lib/socketpair.c
lib/socketpair.h

index 9126801511d7cc85b613ec6fcfa70d8798df49fe..add9a05184a7e4c20180f468095aafc9aa9ed698 100644 (file)
@@ -159,7 +159,7 @@ struct Curl_multi {
   WSAEVENT wsa_event; /* winsock event used for waits */
 #else
 #ifdef ENABLE_WAKEUP
-  curl_socket_t wakeup_pair[2]; /* socketpair() used for wakeup
+  curl_socket_t wakeup_pair[2]; /* pipe()/socketpair() used for wakeup
                                    0 is used for read, 1 is used for write */
 #endif
 #endif
index d01b255134a6f3a590037586c4d9b00127800324..d7e3afd889f266852d4f65abf1dcc5f81389dc3f 100644 (file)
 #include "urldata.h"
 #include "rand.h"
 
+#if defined(HAVE_PIPE) && defined(HAVE_FCNTL)
+#include <fcntl.h>
+
+int Curl_pipe(curl_socket_t socks[2])
+{
+  if(pipe(socks))
+    return -1;
+
+  if(fcntl(socks[0], F_SETFD, FD_CLOEXEC) ||
+     fcntl(socks[1], F_SETFD, FD_CLOEXEC) ) {
+    close(socks[0]);
+    close(socks[1]);
+    socks[0] = socks[1] = CURL_SOCKET_BAD;
+    return -1;
+  }
+
+  return 0;
+}
+#endif
+
+
 #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
 #ifdef _WIN32
 /*
index 09d975477a84c654e015ba2625981c461c338f7e..ddd44374a7555624d5d1e0287b995636ecce32ab 100644 (file)
 #define wakeup_write  write
 #define wakeup_read   read
 #define wakeup_close  close
-#define wakeup_create pipe
+#define wakeup_create(p) Curl_pipe(p)
+
+#ifdef HAVE_FCNTL
+#include <curl/curl.h>
+int Curl_pipe(curl_socket_t socks[2]);
+#else
+#define Curl_pipe(p) pipe(p)
+#endif
 
 #else /* HAVE_PIPE */
 
 #define wakeup_close     sclose
 
 #if defined(USE_UNIX_SOCKETS) && defined(HAVE_SOCKETPAIR)
-#define SOCKET_FAMILY AF_UNIX
+#define SOCKETPAIR_FAMILY AF_UNIX
 #elif !defined(HAVE_SOCKETPAIR)
-#define SOCKET_FAMILY 0 /* not used */
+#define SOCKETPAIR_FAMILY 0 /* not used */
 #else
 #error "unsupported unix domain and socketpair build combo"
 #endif
 
-#define wakeup_create(p) Curl_socketpair(SOCKET_FAMILY, SOCK_STREAM, 0, p)
+#ifdef SOCK_CLOEXEC
+#define SOCKETPAIR_TYPE (SOCK_STREAM | SOCK_CLOEXEC)
+#else
+#define SOCKETPAIR_TYPE SOCK_STREAM
+#endif
+
+#define wakeup_create(p)\
+Curl_socketpair(SOCKETPAIR_FAMILY, SOCKETPAIR_TYPE, 0, p)
 
 #endif /* HAVE_PIPE */
 
+
 #ifndef HAVE_SOCKETPAIR
 #include <curl/curl.h>