socketpair: support pipe2 where available

By replacing pipe with pipe2, it would save us 4 extra system calls of
setting O_NONBLOCK and O_CLOEXEC. This system call is widely supported
across UNIX-like OS's: Linux, *BSD, and SunOS derivatives - Solaris,
illumos, etc.

Ref:
https://man7.org/linux/man-pages/man2/pipe.2.html
https://man.freebsd.org/cgi/man.cgi?query=pipe
https://man.dragonflybsd.org/?command=pipe2
https://man.netbsd.org/pipe.2
https://man.openbsd.org/pipe.2
https://docs.oracle.com/cd/E88353_01/html/E37841/pipe2-2.html
https://illumos.org/man/2/pipe2
https://www.gnu.org/software/gnulib/manual/html_node/pipe2.html

Closes #16987
This commit is contained in:
Andy Pan 2025-04-06 20:37:10 +08:00 committed by Daniel Stenberg
parent 8988f33f62
commit 131a2fd5aa
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 23 additions and 2 deletions

View file

@ -430,6 +430,9 @@
/* Define to 1 if you have the `pipe' function. */
#cmakedefine HAVE_PIPE 1
/* Define to 1 if you have the `pipe2' function. */
#cmakedefine HAVE_PIPE2 1
/* Define to 1 if you have the `eventfd' function. */
#cmakedefine HAVE_EVENTFD 1

View file

@ -50,6 +50,11 @@ int Curl_eventfd(curl_socket_t socks[2], bool nonblocking)
int Curl_pipe(curl_socket_t socks[2], bool nonblocking)
{
#ifdef HAVE_PIPE2
int flags = nonblocking ? O_NONBLOCK | O_CLOEXEC : O_CLOEXEC;
if(pipe2(socks, flags))
return -1;
#else
if(pipe(socks))
return -1;
#ifdef HAVE_FCNTL
@ -70,6 +75,7 @@ int Curl_pipe(curl_socket_t socks[2], bool nonblocking)
return -1;
}
}
#endif
return 0;
}