debuggers.hg

changeset 21194:f0c305aaa6a2

xl: Domain creation logging fixes

* 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@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 12 17:46:39 2010 +0100 (2010-04-12)
parents 4405b50cb183
children b010b792c0f8
files tools/libxl/xl.c
line diff
     1.1 --- a/tools/libxl/xl.c	Mon Apr 12 17:46:10 2010 +0100
     1.2 +++ b/tools/libxl/xl.c	Mon Apr 12 17:46:39 2010 +0100
     1.3 @@ -767,7 +767,7 @@ static void create_domain(int debug, int
     1.4      int num_disks = 0, num_vifs = 0, num_pcidevs = 0, num_vfbs = 0, num_vkbs = 0;
     1.5      int i, fd;
     1.6      int need_daemon = 1;
     1.7 -    int ret;
     1.8 +    int ret, rc;
     1.9      libxl_device_model_starting *dm_starting = 0;
    1.10      libxl_waiter *w1 = NULL, *w2 = NULL;
    1.11      void *config_data = 0;
    1.12 @@ -781,6 +781,7 @@ static void create_domain(int debug, int
    1.13          fprintf(stderr, "cannot init xl context\n");
    1.14          exit(1);
    1.15      }
    1.16 +    libxl_ctx_set_log(&ctx, log_callback, NULL);
    1.17  
    1.18      if (restore_file) {
    1.19          uint8_t *optdata_begin = 0;
    1.20 @@ -948,17 +949,56 @@ start:
    1.21  
    1.22      if (need_daemon) {
    1.23          char *fullname, *name;
    1.24 +        pid_t child1, got_child;
    1.25 +        int nullfd;
    1.26 +
    1.27 +        child1 = libxl_fork(&ctx);
    1.28 +        if (child1) {
    1.29 +            int status;
    1.30 +            for (;;) {
    1.31 +                got_child = waitpid(child1, &status, 0);
    1.32 +                if (got_child == child1) break;
    1.33 +                assert(got_child == -1);
    1.34 +                if (errno != EINTR) {
    1.35 +                    perror("failed to wait for daemonizing child");
    1.36 +                    return ERROR_FAIL;
    1.37 +                }
    1.38 +            }
    1.39 +            if (status) {
    1.40 +                libxl_report_child_exitstatus(&ctx, XL_LOG_ERROR,
    1.41 +                           "daemonizing child", child1, status);
    1.42 +                return ERROR_FAIL;
    1.43 +            }
    1.44 +            return 0; /* caller gets success in parent */
    1.45 +        }
    1.46 +
    1.47 +        rc = libxl_ctx_postfork(&ctx);
    1.48 +        if (rc) {
    1.49 +            LOG("failed to reinitialise context after fork");
    1.50 +            exit(-1);
    1.51 +        }
    1.52  
    1.53          asprintf(&name, "xl-%s", info1.name);
    1.54 -        libxl_create_logfile(&ctx, name, &fullname);
    1.55 -        logfile = open(fullname, O_WRONLY|O_CREAT, 0644);
    1.56 +        rc = libxl_create_logfile(&ctx, name, &fullname);
    1.57 +        if (rc) {
    1.58 +            LOG("failed to open logfile %s",fullname,strerror(errno));
    1.59 +            exit(-1);
    1.60 +        }
    1.61 +
    1.62 +        CHK_ERRNO(( logfile = open(fullname, O_WRONLY|O_CREAT, 0644) )<0);
    1.63          free(fullname);
    1.64          free(name);
    1.65  
    1.66 -        daemon(0, 0);
    1.67 +        CHK_ERRNO(( nullfd = open("/dev/null", O_RDONLY) )<0);
    1.68 +        dup2(nullfd, 0);
    1.69 +        dup2(logfile, 1);
    1.70 +        dup2(logfile, 2);
    1.71 +
    1.72 +        daemon(0, 1);
    1.73          need_daemon = 0;
    1.74      }
    1.75 -    LOG("Waiting for domain %s (domid %d) to die", info1.name, domid);
    1.76 +    LOG("Waiting for domain %s (domid %d) to die [pid %ld]",
    1.77 +        info1.name, domid, (long)getpid());
    1.78      w1 = (libxl_waiter*) xmalloc(sizeof(libxl_waiter) * num_disks);
    1.79      w2 = (libxl_waiter*) xmalloc(sizeof(libxl_waiter));
    1.80      libxl_wait_for_disk_ejects(&ctx, domid, disks, num_disks, w1);