WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH 15/16] xl: Domain creation logging fixes

To: <xen-devel@xxxxxxxxxxxxxxxxxxx>
Subject: [Xen-devel] [PATCH 15/16] xl: Domain creation logging fixes
From: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
Date: Mon, 12 Apr 2010 15:41:45 +0100
Cc: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>, Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
Delivery-date: Mon, 12 Apr 2010 08:02:58 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <1271083306-11126-15-git-send-email-ian.jackson@xxxxxxxxxxxxx>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
References: <1271083306-11126-1-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-2-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-3-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-4-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-5-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-6-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-7-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-8-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-9-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-10-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-11-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-12-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-13-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-14-git-send-email-ian.jackson@xxxxxxxxxxxxx> <1271083306-11126-15-git-send-email-ian.jackson@xxxxxxxxxxxxx>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
 * Make create_domain always return to caller
 * Have create_domain set its log callback sooner
 * Actually write things to logfile, and some error checking

With some combinations of options, create_domain would never return to
the caller, since it would have called daemon and will later exit.  So
we fork an additional time, so that we can call daemon in the child
and also return to the caller in the parent.  It's a shame that
there's no version of daemon(3) that allows us to do this without the
extra code and pointless extra fork.

daemon(0,0) closes all the fds.  So we need to call daemon(0,1) and
organise detaching our stdin/out/err ourselves.  Doing this makes
messages actually appear in the xl logfile in /var/log/xen.

Finally, make create_domain call libxl_ctx_set_log sooner.  This makes
some lost messages appear.

Signed-off-by: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
---
 tools/libxl/xl.c |   50 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c
index ff605aa..82cd25e 100644
--- a/tools/libxl/xl.c
+++ b/tools/libxl/xl.c
@@ -767,7 +767,7 @@ static void create_domain(int debug, int daemonize, const 
char *config_file, con
     int num_disks = 0, num_vifs = 0, num_pcidevs = 0, num_vfbs = 0, num_vkbs = 
0;
     int i, fd;
     int need_daemon = 1;
-    int ret;
+    int ret, rc;
     libxl_device_model_starting *dm_starting = 0;
     libxl_waiter *w1 = NULL, *w2 = NULL;
     void *config_data = 0;
@@ -781,6 +781,7 @@ static void create_domain(int debug, int daemonize, const 
char *config_file, con
         fprintf(stderr, "cannot init xl context\n");
         exit(1);
     }
+    libxl_ctx_set_log(&ctx, log_callback, NULL);
 
     if (restore_file) {
         uint8_t *optdata_begin = 0;
@@ -948,17 +949,56 @@ start:
 
     if (need_daemon) {
         char *fullname, *name;
+        pid_t child1, got_child;
+        int nullfd;
+
+        child1 = libxl_fork(&ctx);
+        if (child1) {
+            int status;
+            for (;;) {
+                got_child = waitpid(child1, &status, 0);
+                if (got_child == child1) break;
+                assert(got_child == -1);
+                if (errno != EINTR) {
+                    perror("failed to wait for daemonizing child");
+                    return ERROR_FAIL;
+                }
+            }
+            if (status) {
+                libxl_report_child_exitstatus(&ctx, XL_LOG_ERROR,
+                           "daemonizing child", child1, status);
+                return ERROR_FAIL;
+            }
+            return 0; /* caller gets success in parent */
+        }
+
+        rc = libxl_ctx_postfork(&ctx);
+        if (rc) {
+            LOG("failed to reinitialise context after fork");
+            exit(-1);
+        }
 
         asprintf(&name, "xl-%s", info1.name);
-        libxl_create_logfile(&ctx, name, &fullname);
-        logfile = open(fullname, O_WRONLY|O_CREAT, 0644);
+        rc = libxl_create_logfile(&ctx, name, &fullname);
+        if (rc) {
+            LOG("failed to open logfile %s",fullname,strerror(errno));
+            exit(-1);
+        }
+
+        CHK_ERRNO(( logfile = open(fullname, O_WRONLY|O_CREAT, 0644) )<0);
         free(fullname);
         free(name);
 
-        daemon(0, 0);
+        CHK_ERRNO(( nullfd = open("/dev/null", O_RDONLY) )<0);
+        dup2(nullfd, 0);
+        dup2(logfile, 1);
+        dup2(logfile, 2);
+
+        daemon(0, 1);
         need_daemon = 0;
     }
-    LOG("Waiting for domain %s (domid %d) to die", info1.name, domid);
+    LOG("Waiting for domain %s (domid %d) to die [pid %ld]",
+        info1.name, domid, (long)getpid());
     w1 = (libxl_waiter*) xmalloc(sizeof(libxl_waiter) * num_disks);
     w2 = (libxl_waiter*) xmalloc(sizeof(libxl_waiter));
     libxl_wait_for_disk_ejects(&ctx, domid, disks, num_disks, w1);
-- 
1.5.6.5


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel