chiark / gitweb /
Use new mdup(3mLib) function.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 4 Jan 2009 17:48:43 +0000 (17:48 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 4 Jan 2009 17:49:12 +0000 (17:49 +0000)
It makes descriptor juggling much more reliable.  Increase version
requirement on mLib to 2.1.0.

client/tripectl.c
configure.ac
server/privsep.c
server/tripe.h
server/tun-slip.c

index 2df7935814c6f7211397301cc0cc00a37c16617e..514506162429860fd6047c85db2b49159c08b2ca 100644 (file)
@@ -52,6 +52,7 @@
 #include <mLib/daemonize.h>
 #include <mLib/darray.h>
 #include <mLib/dstr.h>
+#include <mLib/mdup.h>
 #include <mLib/mdwopt.h>
 #include <mLib/quis.h>
 #include <mLib/report.h>
@@ -367,6 +368,7 @@ int main(int argc, char *argv[])
   uid_t u = -1;
   gid_t g = -1;
   int pfd[2], efd[2];
+  mdup_fd md[3];
   pid_t kid;
   struct sigaction sa;
   sigset_t newmask, oldmask;
@@ -516,11 +518,11 @@ int main(int argc, char *argv[])
     if ((kid = fork()) < 0)
       die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
     if (!kid) {
-      dup2(pfd[1], STDIN_FILENO);
-      dup2(pfd[1], STDOUT_FILENO);
-      dup2(efd[1], STDERR_FILENO);
-      close(pfd[0]); close(pfd[1]);
-      close(efd[0]); close(efd[1]);
+      close(pfd[0]); close(efd[0]);
+      md[0].cur = pfd[1]; md[0].want = STDIN_FILENO;
+      md[1].cur = pfd[1]; md[1].want = STDOUT_FILENO;
+      md[2].cur = efd[1]; md[2].want = STDERR_FILENO;
+      mdup(md, 3);
       if (logfp) fclose(logfp);
       if (pidfp) fclose(pidfp);
       closelog();
index 61ed4a8fd1c1c25119d33bcd6989b15b9cc64d09..a8723c2560ff8fe1f286f41cbabfe5785b8ea7e5 100644 (file)
@@ -57,7 +57,7 @@ case "$host_os" in
     ;;
 esac
 
-PKG_CHECK_MODULES([mLib], [mLib >= 2.0.4])
+PKG_CHECK_MODULES([mLib], [mLib >= 2.1.0])
 PKG_CHECK_MODULES([catacomb], [catacomb >= 2.1.1])
 
 CFLAGS="$CFLAGS $mLib_CFLAGS $catacomb_CFLAGS"
index 5bc2b89220d3d4b4d7c2085ccd39d43ec605201c..bbd4814ae6fbe72e1c947f79744575f6835b6b06 100644 (file)
@@ -173,6 +173,7 @@ void ps_split(int detachp)
 {
   pid_t kid;
   int fd[2];
+  mdup_fd md[1];
   const char *helper;
 
   if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd)) {
@@ -189,8 +190,9 @@ void ps_split(int detachp)
   if (kid == 0) {
     signal(SIGCHLD, SIG_DFL);
     if (detachp) detachtty();
-    if (dup2(fd[0], 0) < 0) goto lose;
-    close(fd[0]); close(fd[1]);
+    close(fd[1]);
+    md[0].cur = fd[0]; md[0].want = STDIN_FILENO;
+    if (mdup(md, 1)) goto lose;
     execl(helper, helper, (char *)0);
   lose:
     fprintf(stderr, "helper: failed to run helper: %s\n", strerror(errno));
index ab15787f386d3dc9e2ee05035969346b3f1d40e4..2b6f950a8a2c679f027d3f62bbab227abaa588ac 100644 (file)
@@ -75,6 +75,7 @@
 #include <mLib/fwatch.h>
 #include <mLib/hash.h>
 #include <mLib/macros.h>
+#include <mLib/mdup.h>
 #include <mLib/mdwopt.h>
 #include <mLib/quis.h>
 #include <mLib/report.h>
index 40830f9d6417acd06583911fa27f9ec5e77f98e8..04a5840263208b10467788c59e193b0e91d086c4 100644 (file)
@@ -258,6 +258,7 @@ static tunnel *t_create(peer *p, int fd, char **ifn)
 {
   slipif *sl = 0;
   int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
+  mdup_fd md[2];
   pid_t kid = -1;
   dstr d = DSTR_INIT;
   unsigned char ch;
@@ -292,10 +293,10 @@ static tunnel *t_create(peer *p, int fd, char **ifn)
     goto fail;
   }
   if (!kid) {
-    close(pin[1]);
-    close(pout[0]);
-    dup2(pin[0], STDIN_FILENO);
-    dup2(pout[1], STDOUT_FILENO);
+    close(pin[1]); close(pout[0]);
+    md[0].cur = pin[0];  md[0].want = STDIN_FILENO;
+    md[1].cur = pout[0]; md[1].want = STDOUT_FILENO;
+    mdup(md, 2);
     execlp(slipcmd, slipcmd, p_name(p), (char *)0);
     _exit(127);
   }