]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add tor_pipe_cloexec
authorJim Newsome <jnewsome@torproject.org>
Mon, 2 Jun 2025 22:09:20 +0000 (17:09 -0500)
committerJim Newsome <jnewsome@torproject.org>
Tue, 3 Jun 2025 15:26:51 +0000 (10:26 -0500)
src/lib/fdio/fdio.c
src/lib/fdio/fdio.h

index 7e276440673e71e712f3b27d88dfb9c65789ea56..f132e370d1507e96bf0cd93211eef18bfd6e7388 100644 (file)
@@ -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
index 7551dedb9e7483180554800a09a460e91d2f448f..4fd35fda437db66b977eb7a5f0ff2774c103da4d 100644 (file)
@@ -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) */