rand/noise.c (noise_filter): Use <mLib/mdup.h>.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 13 Jul 2013 15:34:40 +0000 (16:34 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 13 Mar 2014 01:51:43 +0000 (01:51 +0000)
The code was slightly incorrect previously: if the standard descriptors
are closed to begin with, then the pipe ends up being closed on entry.
As far as I know, this has never actually happened, but I have the
machinery to fix this bug so I might as well use it.

rand/noise.c

index aa45e03..fb4c453 100644 (file)
@@ -47,6 +47,7 @@
 #endif
 
 #include <mLib/bits.h>
+#include <mLib/mdup.h>
 #include <mLib/tv.h>
 
 #include "noise.h"
@@ -262,20 +263,16 @@ int noise_filter(rand_pool *r, int good, const char *c)
   /* --- Handle the child end of the deal --- */
 
   if (kid == 0) {
-    int f;
+    mdup_fd mfd[3];
+    int f, i = 0;
 
     /* --- Set the pipe as standard output, close standard input --- */
 
-    close(0); close(1); close(2);
-
-    if (fd[1] != 1) {
-      if (dup2(fd[1], 1) < 0) _exit(127);
-      close(fd[1]);
-    }
-
-    if ((f = open("/dev/null", O_RDONLY)) != 0 ||
-       (f = open("/dev/null", O_WRONLY)) != 2)
-      _exit(127);
+    if ((f = open("/dev/null", O_RDONLY)) < 0) _exit(127);
+    mfd[i].cur = f; mfd[i].want = 0; i++;
+    mfd[i].cur = fd[1]; mfd[i].want = 1; i++;
+    mfd[i].cur = f; mfd[i].want = 2; i++;
+    if (mdup(mfd, i)) _exit(127);
 
     /* --- Play games with uids --- */