]> granicus.if.org Git - sudo/commitdiff
Keep reading from the pty master -> log file until read returns <= 0.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Sat, 19 Sep 2009 11:35:20 +0000 (11:35 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Sat, 19 Sep 2009 11:35:20 +0000 (11:35 +0000)
Do our best to write everything to stdout when flushing any remaining bits.

script.c

index 4eeae869dc4ea45f4fb8319813e7c18633f30f93..eb1c514e33ce6f8ee26a3119771507f5e1cab4b2 100644 (file)
--- a/script.c
+++ b/script.c
@@ -386,7 +386,7 @@ script_execv(path, argv)
        sizeof(fd_mask));
     zero_bytes(&input, sizeof(input));
     zero_bytes(&output, sizeof(output));
-    while (alive) {
+    for (;;) {
        if (input.off == input.len)
            input.off = input.len = 0;
        if (output.off == output.len)
@@ -394,14 +394,16 @@ script_execv(path, argv)
 
        zero_bytes(fdsw, howmany(script_fds[SFD_MASTER] + 1, NFDBITS) * sizeof(fd_mask));
        zero_bytes(fdsr, howmany(script_fds[SFD_MASTER] + 1, NFDBITS) * sizeof(fd_mask));
-       if (input.len != sizeof(input.buf))
-           FD_SET(STDIN_FILENO, fdsr);
        if (output.len != sizeof(output.buf))
            FD_SET(script_fds[SFD_MASTER], fdsr);
        if (output.len > output.off)
            FD_SET(STDOUT_FILENO, fdsw);
-       if (input.len > input.off)
-           FD_SET(script_fds[SFD_MASTER], fdsw);
+       if (alive) {
+           if (input.len != sizeof(input.buf))
+               FD_SET(STDIN_FILENO, fdsr);
+           if (input.len > input.off)
+               FD_SET(script_fds[SFD_MASTER], fdsw);
+       }
 
        nready = select(script_fds[SFD_MASTER] + 1, fdsr, fdsw, NULL, NULL);
        if (nready == -1) {
@@ -468,23 +470,20 @@ script_execv(path, argv)
     }
 
     /* Flush any remaining output. */
-    n = fcntl(STDIN_FILENO, F_GETFL, 0);
+    n = fcntl(STDOUT_FILENO, F_GETFL, 0);
     if (n != -1) {
        n &= ~O_NONBLOCK;
-       (void) fcntl(STDIN_FILENO, F_SETFL, n);
+       (void) fcntl(STDOUT_FILENO, F_SETFL, n);
     }
     if (output.len > output.off) {
-       n = output.len - output.off;
-       write(STDOUT_FILENO, output.buf + output.off, n);
-       log_output(&output, n, &then, &now, ofile, tfile);
-    }
-    for (;;) {
-       output.off = 0;
-       n = read(script_fds[SFD_MASTER], output.buf, sizeof(output.buf));
-       if (n <= 0)
-           break;
-       write(STDOUT_FILENO, output.buf, n);
-       log_output(&output, n, &then, &now, ofile, tfile);
+       log_output(&output, output.len - output.off, &then, &now, ofile, tfile);
+       do {
+           n = write(STDOUT_FILENO, output.buf + output.off,
+               output.len - output.off);
+           if (n == -1)
+               break;
+           output.len += n;
+       } while (output.len > output.off);
     }
     term_restore(STDIN_FILENO);