Fix race condition
authorAlexey Gladkov <legion@altlinux.org>
Mon, 19 Feb 2007 07:47:49 +0000 (10:47 +0300)
committerAlexey Gladkov <legion@altlinux.org>
Mon, 19 Feb 2007 07:47:49 +0000 (10:47 +0300)
Open stdin, stdout, stderr in /dev/null if they are not opened.

Signed-off-by: Alexey Gladkov <legion@altlinux.org>
openvt/openvt.c

index 99f174e..b830a4d 100644 (file)
@@ -39,7 +39,7 @@ int
 main(int argc, char *argv[])
 {
 
-   int opt, pid;
+   int opt, pid, i;
    struct vt_stat vtstat;
    int vtno     = -1;
    int fd0      = -1;
@@ -105,6 +105,12 @@ main(int argc, char *argv[])
       }
    }
 
+    for (i=0; i<3; i++) {
+       struct stat st;
+       if (fstat(i, &st) == -1 && open("/dev/null", O_RDWR) == -1)
+               error(EXIT_FAILURE, errno, "open");
+    }
+
    consfd = getfd(NULL);
    if (consfd < 0) {
       fprintf(stderr,