tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

commit ed28f2a1b3b1d40027a8b8d8b8a3a5d112ae3829
parent 0591db2d7cc28ff6ba22e732c3ced5258835178f
Author: Jim Newsome <jnewsome@torproject.org>
Date:   Mon,  2 Jun 2025 17:09:20 -0500

Add tor_pipe_cloexec

Diffstat:
Msrc/lib/fdio/fdio.c | 28++++++++++++++++++++++++++++
Msrc/lib/fdio/fdio.h | 1+
2 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/src/lib/fdio/fdio.c b/src/lib/fdio/fdio.c @@ -14,6 +14,9 @@ #ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif #ifdef _WIN32 #include <windows.h> #endif @@ -118,3 +121,28 @@ write_all_to_fd_minimal(int fd, const char *buf, size_t count) } return 0; } + +#if defined(HAVE_PIPE2) && defined(O_CLOEXEC) +int +tor_pipe_cloexec(int pipefd[2]) +{ + return pipe2(pipefd, O_CLOEXEC); +} +#elif defined(HAVE_PIPE) && defined(FD_CLOEXEC) +int +tor_pipe_cloexec(int pipefd[2]) +{ + if (pipe(pipefd)) { + return -1; + } + if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC)) { + return -1; + } + if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC)) { + return -1; + } + return 0; +} +#else +/* Intentionally leave symbol undefined. */ +#endif diff --git a/src/lib/fdio/fdio.h b/src/lib/fdio/fdio.h @@ -22,5 +22,6 @@ int tor_fd_setpos(int fd, off_t pos); int tor_fd_seekend(int fd); int tor_ftruncate(int fd); int write_all_to_fd_minimal(int fd, const char *buf, size_t count); +int tor_pipe_cloexec(int pipefd[2]); #endif /* !defined(TOR_FDIO_H) */