script: make it work even if fd 0 is closed
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 16 Mar 2014 11:34:53 +0000 (12:34 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 16 Mar 2014 11:34:53 +0000 (12:34 +0100)
Testcase:
script -q -c "echo hey" /dev/null 0>&-

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/getpty.c
util-linux/script.c

index 435e4d09f301e6e5bcefe9c81c8986728aaa3c34..391d729f2232653e9d67f2a0e72cae5a2bcedde4 100644 (file)
@@ -16,7 +16,7 @@ int FAST_FUNC xgetpty(char *line)
 
 #if ENABLE_FEATURE_DEVPTS
        p = open("/dev/ptmx", O_RDWR);
-       if (p > 0) {
+       if (p >= 0) {
                grantpt(p); /* chmod+chown corresponding slave pty */
                unlockpt(p); /* (what does this do?) */
 # ifndef HAVE_PTSNAME_R
index 8fb991d15f8a1b1038a1102bd4c4ee88c7a83580..abcd73bff5c0fc477c26a33c385491f8a6c8effc 100644 (file)
@@ -77,8 +77,15 @@ int script_main(int argc UNUSED_PARAM, char **argv)
        if (!(opt & OPT_q)) {
                printf("Script started, file is %s\n", fname);
        }
+
        shell = get_shell_name();
 
+       /* Some people run "script ... 0>&-".
+        * Our code assumes that STDIN_FILENO != pty.
+        * Ensure STDIN_FILENO is not closed:
+        */
+       bb_sanitize_stdio();
+
        pty = xgetpty(pty_line);
 
        /* get current stdin's tty params */