[Libguestfs] [PATCH 5/7] cmd: Move all the common processing code for running the child into a subfunction.

Richard W.M. Jones rjones at redhat.com
Tue Sep 29 11:54:52 UTC 2015


Anything that doesn't involve pipes [parent-child communication] is
moved into the run_child function, so we can reuse the same code when
writing the new pipe APIs (see following commit).  Anything involving
setting up pipes stays in the existing run_command function.

This is just refactoring.
---
 src/command.c | 50 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 19 deletions(-)

diff --git a/src/command.c b/src/command.c
index 893653a..8fb0714 100644
--- a/src/command.c
+++ b/src/command.c
@@ -408,18 +408,13 @@ debug_command (struct command *cmd)
   }
 }
 
+static void run_child (struct command *cmd) __attribute__((noreturn));
+
 static int
 run_command (struct command *cmd)
 {
-  struct sigaction sa;
-  int i, fd, max_fd, r;
   int errorfd[2] = { -1, -1 };
   int outfd[2] = { -1, -1 };
-  char status_string[80];
-#ifdef HAVE_SETRLIMIT
-  struct child_rlimits *child_rlimit;
-  struct rlimit rlimit;
-#endif
 
   /* Set up a pipe to capture command output and send it to the error log. */
   if (cmd->capture_errors) {
@@ -480,6 +475,33 @@ run_command (struct command *cmd)
   if (cmd->stderr_to_stdout)
     dup2 (1, 2);
 
+  run_child (cmd);
+  /*NOTREACHED*/
+
+ error:
+  if (errorfd[0] >= 0)
+    close (errorfd[0]);
+  if (errorfd[1] >= 0)
+    close (errorfd[1]);
+  if (outfd[0] >= 0)
+    close (outfd[0]);
+  if (outfd[1] >= 0)
+    close (outfd[1]);
+
+  return -1;
+}
+
+static void
+run_child (struct command *cmd)
+{
+  struct sigaction sa;
+  int i, fd, max_fd, r;
+  char status_string[80];
+#ifdef HAVE_SETRLIMIT
+  struct child_rlimits *child_rlimit;
+  struct rlimit rlimit;
+#endif
+
   /* Remove all signal handlers.  See the justification here:
    * https://www.redhat.com/archives/libvir-list/2008-August/msg00303.html
    * We don't mask signal handlers yet, so this isn't completely
@@ -557,19 +579,9 @@ run_command (struct command *cmd)
   case COMMAND_STYLE_NOT_SELECTED:
     abort ();
   }
+
   /*NOTREACHED*/
-
- error:
-  if (errorfd[0] >= 0)
-    close (errorfd[0]);
-  if (errorfd[1] >= 0)
-    close (errorfd[1]);
-  if (outfd[0] >= 0)
-    close (outfd[0]);
-  if (outfd[1] >= 0)
-    close (outfd[1]);
-
-  return -1;
+  abort ();
 }
 
 /* The loop which reads errors and output and directs it either
-- 
2.5.0




More information about the Libguestfs mailing list