From: Jim Newsome Date: Mon, 2 Jun 2025 22:09:20 +0000 (-0500) Subject: Add tor_pipe_cloexec X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bedc175c8af6efc34802b10158dd146b4dae7602;p=thirdparty%2Ftor.git Add tor_pipe_cloexec --- diff --git a/src/lib/fdio/fdio.c b/src/lib/fdio/fdio.c index 7e27644067..f132e370d1 100644 --- a/src/lib/fdio/fdio.c +++ b/src/lib/fdio/fdio.c @@ -14,6 +14,9 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_FCNTL_H +#include +#endif #ifdef _WIN32 #include #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 index 7551dedb9e..4fd35fda43 100644 --- 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) */