commit bedc175c8af6efc34802b10158dd146b4dae7602
parent a02a31bf53bf9fd0e2317bf1f8791e0bae984b06
Author: Jim Newsome <jnewsome@torproject.org>
Date: Mon, 2 Jun 2025 17:09:20 -0500
Add tor_pipe_cloexec
Diffstat:
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) */