Browse Source

Fix for not correctly buffering subprocess output in shutdown

We were closing the output pipe *before* forking instead of just after.
As a result, the subprocesses (eg umount, swapoff) would have put output
directly to stdout. In most cases this is harmless, however, the reason
for the buffering is so that shutdown can execute even if stdout (which
may be a serial line etc) becomes blocked.
Davin McCall 1 year ago
parent
commit
0da959ea3f
1 changed files with 6 additions and 2 deletions
  1. 6 2
      src/shutdown.cc

+ 6 - 2
src/shutdown.cc

@@ -551,7 +551,6 @@ static void run_process(const char * prog_args[], loop_t &loop, subproc_buffer &
     subproc_out_watch owatch {sub_buf};
 
     if (have_pipe) {
-        close(pipefds[1]);
         try {
             owatch.add_watch(loop, pipefds[0], dasynq::IN_EVENTS);
         }
@@ -560,6 +559,7 @@ static void run_process(const char * prog_args[], loop_t &loop, subproc_buffer &
             // our stdout/stderr
             sub_buf.append("Warning: could not create output watch for subprocess\n");
             close(pipefds[0]);
+            close(pipefds[1]);
             have_pipe = false;
         }
     }
@@ -582,13 +582,17 @@ static void run_process(const char * prog_args[], loop_t &loop, subproc_buffer &
         perror(prog_args[0]);
         _exit(1);
     }
+    if (have_pipe) {
+        close(pipefds[1]);
+    }
 
     do {
         loop.run();
-    } while (! sp_watcher.terminated);
+    } while (!sp_watcher.terminated);
 
     if (have_pipe) {
         owatch.deregister(loop);
+        close(pipefds[0]);
     }
 }