tor

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

commit 933c5491db00c703d5d8264fdabd5a5b10aff96f
parent a02a31bf53bf9fd0e2317bf1f8791e0bae984b06
Author: David Goulet <dgoulet@torproject.org>
Date:   Tue,  3 Jun 2025 16:10:43 +0000

Merge branch 'cloexec-pipes' into 'main'

start_daemon: open pipe with cloexec

Closes #41013 and #41088

See merge request tpo/core/tor!904
Diffstat:
Achanges/bug41088 | 4++++
Msrc/lib/fdio/fdio.c | 28++++++++++++++++++++++++++++
Msrc/lib/fdio/fdio.h | 1+
Msrc/lib/process/.may_include | 1+
Msrc/lib/process/daemon.c | 3++-
5 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/changes/bug41088 b/changes/bug41088 @@ -0,0 +1,4 @@ + o Minor bugfixes (bridges, pluggable transport): + - Fix a bug causing the initial tor process to hang intead of exiting with + RunAsDaemon, when pluggable transports are used. + Fixes bug 41088; bugfix on 0.4.9.1-alpha. 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) */ diff --git a/src/lib/process/.may_include b/src/lib/process/.may_include @@ -6,6 +6,7 @@ lib/container/*.h lib/ctime/*.h lib/err/*.h lib/evloop/*.h +lib/fdio/*.h lib/fs/*.h lib/intmath/*.h lib/log/*.h diff --git a/src/lib/process/daemon.c b/src/lib/process/daemon.c @@ -13,6 +13,7 @@ #ifndef _WIN32 +#include "lib/fdio/fdio.h" #include "lib/fs/files.h" #include "lib/log/log.h" #include "lib/thread/threads.h" @@ -63,7 +64,7 @@ start_daemon(void) return 0; start_daemon_called = 1; - if (pipe(daemon_filedes)) { + if (tor_pipe_cloexec(daemon_filedes)) { /* LCOV_EXCL_START */ log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno)); exit(1); // exit ok: during daemonize, pipe failed.