xl: Remove clone-and-hack in do_daemonize
authorIan Jackson <ian.jackson@eu.citrix.com>
Wed, 19 Mar 2014 14:07:05 +0000 (14:07 +0000)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Fri, 21 Mar 2014 14:35:52 +0000 (14:35 +0000)
do_daemonize had open-coded handling of the results from xl_waitpid.

Instead, break out the meat of console_child_report into a new
function child_report (which returns an error code), and use it.

No functional change other than a change to the message printed if
forking the daemonic child fails.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Ian Campbell <Ian.Campbell@citrix.com>
tools/libxl/xl.h
tools/libxl/xl_cmdimpl.c

index 40a42b31bee692a5864b54023f0e3540935d34ff..10a2e66a4d5baa93e1fbacc7ca8439e3fdf11336 100644 (file)
@@ -153,6 +153,10 @@ void xl_report_child_exitstatus(xentoollog_level level,
                                 xlchildnum child, pid_t pid, int status);
     /* like libxl_report_child_exitstatus, but uses children[].description */
 
+int child_report(xlchildnum child);
+    /* waits and expects child to exit status 0.
+     * otherwise, logs and returns ERROR_FAIL */
+
 /* global options */
 extern int autoballoon;
 extern int run_hotplug_scripts;
index d61d30161468010fc6e706a19d3e61c0a1870f77..27b6f405a2027985cae720d2325d3a6b67005388 100644 (file)
@@ -205,19 +205,28 @@ static uint32_t find_domain(const char *p)
     return domid;
 }
 
-static void console_child_report(xlchildnum child)
+int child_report(xlchildnum child)
 {
-    if (xl_child_pid(child)) {
-        int status;
-        pid_t got = xl_waitpid(child, &status, 0);
-        if (got < 0)
-            fprintf(stderr, "xl: warning, failed to waitpid for %s: %s\n",
-                    children[child].description, strerror(errno));
-        else if (status)
-            xl_report_child_exitstatus(XTL_ERROR, child, got, status);
+    int status;
+    pid_t got = xl_waitpid(child, &status, 0);
+    if (got < 0) {
+        fprintf(stderr, "xl: warning, failed to waitpid for %s: %s\n",
+                children[child].description, strerror(errno));
+        return ERROR_FAIL;
+    } else if (status) {
+        xl_report_child_exitstatus(XTL_ERROR, child, got, status);
+        return ERROR_FAIL;
+    } else {
+        return 0;
     }
 }
 
+static void console_child_report(xlchildnum child)
+{
+    if (xl_child_pid(child))
+        child_report(child);
+}
+
 static int vncviewer(uint32_t domid, int autopass)
 {
     libxl_vncviewer_exec(ctx, domid, autopass);
@@ -431,26 +440,13 @@ out:
 static int do_daemonize(char *name)
 {
     char *fullname;
-    pid_t child1, got_child;
+    pid_t child1;
     int nullfd, ret = 0;
-    int status = 0;
 
-    child1 = xl_fork(child_waitdaemon, "domain monitoring daemon");
+    child1 = xl_fork(child_waitdaemon, "domain monitoring daemonizing child");
     if (child1) {
-        got_child = xl_waitpid(child_waitdaemon, &status, 0);
-        if (got_child != child1) {
-            assert(got_child == -1);
-            LOG("failed to wait for daemonizing child: %s", strerror(errno));
-            ret = ERROR_FAIL;
-            goto out;
-        }
-
-        if (status) {
-            libxl_report_child_exitstatus(ctx, XTL_ERROR,
-                       "daemonizing child", child1, status);
-            ret = ERROR_FAIL;
-            goto out;
-        }
+        ret = child_report(child_waitdaemon);
+        if (ret) goto out;
         ret = 1;
         goto out;
     }