Skip to contentSkip to conversation

libxl: subprocess handling

PATCH Superseded Ian Jackson 3 participants 36 messages Apr 13, 2012 – Apr 16, 2012

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover libxl: subprocess handling

I have not tested the tip of this series. Up to 12/20 have been tested, in an earlier version.

Changes since v4 include:
- all comments addressed as described in emails on the list
- new patches to convert device model spawning, domain creation, and console connect callback, to use event model
- possibly, some bugfixes

At the end of this series I think there are no more impermissible users of fork (or libxl_fork) in libxl. xl stilll uses libxl_fork, about which something should be done.

Tested and previously-posted (these were 20-31/31 from v4):
01/20 libxl: handle POLLERR, POLLHUP, POLLNVAL properly 02/20 libxl: support multiple libxl__ev_fds for the same fd 03/20 libxl: event API: new facilities for waiting for subprocesses 04/20 autoconf: New test for openpty et al.
05/20 libxl: provide libxl__remove_file et al.
06/20 libxl: Introduce libxl__sendmsg_fds and libxl__recvmsg_fds 07/20 libxl: Clean up setdefault in do_domain_create 08/20 libxl: provide libxl__datacopier_*
09/20 libxl: provide libxl__openpty_*
10/20 libxl: ao: Convert libxl_run_bootloader
11/20 libxl: make libxl_create_logfile const-correct 12/20 libxl: log bootloader output

Preparatory work (new in this version of the series):
13/20 libxl: Allow AO_GC and EGC_GC even if not used 14/20 libxl: remove ctx->waitpid_instead
15/20 libxl: change some structures to unit arrays 19/20 libxl: remove malloc failure handling from NEW_EVENT

Heavy lifting in domain creation (new in this version of the series):
16/20 libxl: ao: convert libxl__spawn_*
17/20 libxl: make libxl_create run bootloader via callback 18/20 libxl: provide progress reporting for long-running operations 20/20 libxl: convert console callback to libxl_asyncprogress_how

#1 lore raw

PATCH v6 1/20 libxl: handle POLLERR, POLLHUP, POLLNVAL properly

Pass POLLERR and POLLHUP to fd callbacks, as is necessary. Crash on POLLNVAL since that means our fds are messed up.

Document the behaviour (including the fact that poll sometimes sets POLLHUP or POLLERR even if only POLLIN was requested.

Fix the one current fd callback to do something with POLLERR|POLLHUP.

---
 tools/libxl/libxl_event.c    |    7 ++++++-
 tools/libxl/libxl_internal.h |    5 +++++
 2 files changed, 11 insertions(+), 1 deletions(-)
tools/libxl/libxl_event.c +6 -1
index 638b9ab..5e1a207 100644
@@ -335,6 +335,9 @@ static void watchfd_callback(libxl__egc *egc, libxl__ev_fd *ev,
 {
     EGC_GC;
 
+    if (revents & (POLLERR|POLLHUP))
+        LIBXL__EVENT_DISASTER(egc, "unexpected poll event on watch fd", 0, 0);
+
     for (;;) {
         char **event = xs_check_watch(CTX->xsh);
         if (!event) {
@@ -739,7 +742,9 @@ static int afterpoll_check_fd(libxl__poller *poller,
         /* again, stale slot entry */
         return 0;
 
-    int revents = fds[slot].revents & events;
+    assert(!(fds[slot].revents & POLLNVAL));
+
+    int revents = fds[slot].revents & (events | POLLERR | POLLHUP);
     /* we mask in case requested events have changed */
 
     return revents;
tools/libxl/libxl_internal.h +5 -0
index a4b933b..af631fb 100644
@@ -127,6 +127,11 @@ void libxl__alloc_failed(libxl_ctx *, const char *func,
 typedef struct libxl__ev_fd libxl__ev_fd;
 typedef void libxl__ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
                                    int fd, short events, short revents);
+  /* Note that revents may contain POLLERR or POLLHUP regardless of
+   * events; otherwise revents contains only bits in events.  Contrary
+   * to the documentation for poll(2), POLLERR and POLLHUP can occur
+   * even if only POLLIN was set in events.  (POLLNVAL is a fatal
+   * error and will cause libxl event machinery to fail an assertion.) */
 struct libxl__ev_fd {
     /* caller should include this in their own struct */
     /* read-only for caller, who may read only when registered: */
Signature · 1 line
1.7.2.5
Acked-by on v6 1/20
#2 lore raw

Re: [PATCH 01/20] libxl: handle POLLERR, POLLHUP, POLLNVAL properly

Quoted 9 lines · Ian Jackson

Pass POLLERR and POLLHUP to fd callbacks, as is necessary. Crash on POLLNVAL since that means our fds are messed up.

Document the behaviour (including the fact that poll sometimes sets POLLHUP or POLLERR even if only POLLIN was requested.

Fix the one current fd callback to do something with POLLERR|POLLHUP.

Quoted 46 lines
---
 tools/libxl/libxl_event.c    |    7 ++++++-
 tools/libxl/libxl_internal.h |    5 +++++
 2 files changed, 11 insertions(+), 1 deletions(-)
tools/libxl/libxl_event.c +6 -1
index 638b9ab..5e1a207 100644
@@ -335,6 +335,9 @@ static void watchfd_callback(libxl__egc *egc, libxl__ev_fd *ev,
 {
     EGC_GC;
 
+    if (revents & (POLLERR|POLLHUP))
+        LIBXL__EVENT_DISASTER(egc, "unexpected poll event on watch fd", 0, 0);
+
     for (;;) {
         char **event = xs_check_watch(CTX->xsh);
         if (!event) {
@@ -739,7 +742,9 @@ static int afterpoll_check_fd(libxl__poller *poller,
         /* again, stale slot entry */
         return 0;
 
-    int revents = fds[slot].revents & events;
+    assert(!(fds[slot].revents & POLLNVAL));
+
+    int revents = fds[slot].revents & (events | POLLERR | POLLHUP);
     /* we mask in case requested events have changed */
 
     return revents;
tools/libxl/libxl_internal.h +5 -0
index a4b933b..af631fb 100644
@@ -127,6 +127,11 @@ void libxl__alloc_failed(libxl_ctx *, const char *func,
 typedef struct libxl__ev_fd libxl__ev_fd;
 typedef void libxl__ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
                                    int fd, short events, short revents);
+  /* Note that revents may contain POLLERR or POLLHUP regardless of
+   * events; otherwise revents contains only bits in events.  Contrary
+   * to the documentation for poll(2), POLLERR and POLLHUP can occur
+   * even if only POLLIN was set in events.  (POLLNVAL is a fatal
+   * error and will cause libxl event machinery to fail an assertion.) */
 struct libxl__ev_fd {
     /* caller should include this in their own struct */
     /* read-only for caller, who may read only when registered: */
#3 lore raw

PATCH v6 2/20 libxl: support multiple libxl__ev_fds for the same fd

In reply to Ian Jackson

We need a slightly more sophisticated data structure to allow this, where we record the slot not just for each fd but also for each (fd,eventbit) where eventbit is POLLIN, POLLPRI, POLLOUT.

Document the new relaxed restriction.

---
 tools/libxl/libxl_event.c    |   62 +++++++++++++++++++++++------------------
 tools/libxl/libxl_internal.h |   10 +++++--
 2 files changed, 42 insertions(+), 30 deletions(-)
tools/libxl/libxl_event.c +35 -27
index 5e1a207..672e3fe 100644
@@ -635,10 +635,11 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
 
 
     /*
-     * In order to be able to efficiently find the libxl__ev_fd
-     * for a struct poll during _afterpoll, we maintain a shadow
-     * data structure in CTX->fd_beforepolled: each slot in
-     * the fds array corresponds to a slot in fd_beforepolled.
+     * In order to be able to efficiently find the libxl__ev_fd for a
+     * struct poll during _afterpoll, we maintain a shadow data
+     * structure in CTX->fd_rindices: each fd corresponds to a slot in
+     * fd_rindices, and each elemebnt in the rindices is three indices
+     * into the fd array (for POLLIN, POLLPRI and POLLOUT).
      */
 
     if (*nfds_io) {
@@ -659,14 +660,16 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
         });
 
         /* make sure our array is as big as *nfds_io */
-        if (poller->fd_rindex_allocd < maxfd) {
-            assert(maxfd < INT_MAX / sizeof(int) / 2);
-            int *newarray = realloc(poller->fd_rindex, sizeof(int) * maxfd);
-            if (!newarray) { rc = ERROR_NOMEM; goto out; }
-            memset(newarray + poller->fd_rindex_allocd, 0,
-                   sizeof(int) * (maxfd - poller->fd_rindex_allocd));
-            poller->fd_rindex = newarray;
-            poller->fd_rindex_allocd = maxfd;
+        if (poller->fd_rindices_allocd < maxfd) {
+            assert(ARRAY_SIZE_OK(poller->fd_rindices, maxfd));
+            poller->fd_rindices =
+                libxl__realloc(0, poller->fd_rindices,
+                               maxfd * sizeof(*poller->fd_rindices));
+            memset(poller->fd_rindices + poller->fd_rindices_allocd,
+                   0,
+                   (maxfd - poller->fd_rindices_allocd)
+                     * sizeof(*poller->fd_rindices));
+            poller->fd_rindices_allocd = maxfd;
         }
     }
 
@@ -677,8 +680,10 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
             fds[used].fd = req_fd;
             fds[used].events = req_events;
             fds[used].revents = 0;
-            assert(req_fd < poller->fd_rindex_allocd);
-            poller->fd_rindex[req_fd] = used;
+            assert(req_fd < poller->fd_rindices_allocd);
+            if (req_events & POLLIN)  poller->fd_rindices[req_fd][0] = used;
+            if (req_events & POLLPRI) poller->fd_rindices[req_fd][1] = used;
+            if (req_events & POLLOUT) poller->fd_rindices[req_fd][2] = used;
         }
         used++;
     });
@@ -706,7 +711,6 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
             *timeout_upd = our_timeout;
     }
 
- out:
     return rc;
 }
 
@@ -728,24 +732,28 @@ static int afterpoll_check_fd(libxl__poller *poller,
                               int fd, int events)
     /* returns mask of events which were requested and occurred */
 {
-    if (fd >= poller->fd_rindex_allocd)
+    if (fd >= poller->fd_rindices_allocd)
         /* added after we went into poll, have to try again */
         return 0;
 
-    int slot = poller->fd_rindex[fd];
+    int i, revents = 0;
+    for (i=0; i<3; i++) {
+        int slot = poller->fd_rindices[fd][i];
 
-    if (slot >= nfds)
-        /* stale slot entry; again, added afterwards */
-        return 0;
+        if (slot >= nfds)
+            /* stale slot entry; again, added afterwards */
+            continue;
 
-    if (fds[slot].fd != fd)
-        /* again, stale slot entry */
-        return 0;
+        if (fds[slot].fd != fd)
+            /* again, stale slot entry */
+            continue;
 
-    assert(!(fds[slot].revents & POLLNVAL));
+        assert(!(fds[slot].revents & POLLNVAL));
+        revents |= fds[slot].revents;
+    }
 
-    int revents = fds[slot].revents & (events | POLLERR | POLLHUP);
     /* we mask in case requested events have changed */
+    revents &= (events | POLLERR | POLLHUP);
 
     return revents;
 }
@@ -1009,7 +1017,7 @@ int libxl__poller_init(libxl_ctx *ctx, libxl__poller *p)
 {
     int r, rc;
     p->fd_polls = 0;
-    p->fd_rindex = 0;
+    p->fd_rindices = 0;
 
     r = pipe(p->wakeup_pipe);
     if (r) {
@@ -1036,7 +1044,7 @@ void libxl__poller_dispose(libxl__poller *p)
     if (p->wakeup_pipe[1] > 0) close(p->wakeup_pipe[1]);
     if (p->wakeup_pipe[0] > 0) close(p->wakeup_pipe[0]);
     free(p->fd_polls);
-    free(p->fd_rindex);
+    free(p->fd_rindices);
 }
 
 libxl__poller *libxl__poller_get(libxl_ctx *ctx)
tools/libxl/libxl_internal.h +7 -3
index af631fb..2ce4bb5 100644
@@ -131,7 +131,11 @@ typedef void libxl__ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
    * events; otherwise revents contains only bits in events.  Contrary
    * to the documentation for poll(2), POLLERR and POLLHUP can occur
    * even if only POLLIN was set in events.  (POLLNVAL is a fatal
-   * error and will cause libxl event machinery to fail an assertion.) */
+   * error and will cause libxl event machinery to fail an assertion.)
+   *
+   * It is not permitted to listen for the same or overlapping events
+   * on the same fd using multiple different libxl__ev_fd's.
+   */
 struct libxl__ev_fd {
     /* caller should include this in their own struct */
     /* read-only for caller, who may read only when registered: */
@@ -257,8 +261,8 @@ struct libxl__poller {
     struct pollfd *fd_polls;
     int fd_polls_allocd;
 
-    int fd_rindex_allocd;
-    int *fd_rindex; /* see libxl_osevent_beforepoll */
+    int fd_rindices_allocd;
+    int (*fd_rindices)[3]; /* see libxl_osevent_beforepoll */
 
     int wakeup_pipe[2]; /* 0 means no fd allocated */
 };
Signature · 1 line
1.7.2.5
Acked-by on v6 2/20
#4 lore raw

Re: [PATCH 02/20] libxl: support multiple libxl__ev_fds for the same fd

Quoted 3 lines · Ian Jackson

We need a slightly more sophisticated data structure to allow this, where we record the slot not just for each fd but also for each (fd,eventbit) where eventbit is POLLIN, POLLPRI, POLLOUT.

Just to be sure I'm following: By multiple you mean you can have one libxl__ev_fds listening for e.g. POLLIN and another for POLLOUT but you specifically exclude the case where two libxl__ev_fds both want to listen for POLLIN? Similarly one listening for POLLIN|POLLPRI and the other for POLLOUT|POLLPRI (overlapping) is forbidden.

Quoted 14 lines

Document the new relaxed restriction.

---
 tools/libxl/libxl_event.c    |   62 +++++++++++++++++++++++------------------
 tools/libxl/libxl_internal.h |   10 +++++--
 2 files changed, 42 insertions(+), 30 deletions(-)
tools/libxl/libxl_event.c +0 -0
index 5e1a207..672e3fe 100644
@@ -635,10 +635,11 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
 

tools/libxl/libxl_event.c static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller, · line 641

Quoted 10 lines
tools/libxl/libxl_event.c +4 -4
 
     /*
-     * In order to be able to efficiently find the libxl__ev_fd
-     * for a struct poll during _afterpoll, we maintain a shadow
-     * data structure in CTX->fd_beforepolled: each slot in
-     * the fds array corresponds to a slot in fd_beforepolled.
+     * In order to be able to efficiently find the libxl__ev_fd for a
+     * struct poll during _afterpoll, we maintain a shadow data
+     * structure in CTX->fd_rindices: each fd corresponds to a slot in
+     * fd_rindices, and each elemebnt in the rindices is three indices

element

Quoted 30 lines
tools/libxl/libxl_event.c +11 -8
+     * into the fd array (for POLLIN, POLLPRI and POLLOUT).
      */
 
     if (*nfds_io) {
@@ -659,14 +660,16 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
         });
 
         /* make sure our array is as big as *nfds_io */
-        if (poller->fd_rindex_allocd < maxfd) {
-            assert(maxfd < INT_MAX / sizeof(int) / 2);
-            int *newarray = realloc(poller->fd_rindex, sizeof(int) * maxfd);
-            if (!newarray) { rc = ERROR_NOMEM; goto out; }
-            memset(newarray + poller->fd_rindex_allocd, 0,
-                   sizeof(int) * (maxfd - poller->fd_rindex_allocd));
-            poller->fd_rindex = newarray;
-            poller->fd_rindex_allocd = maxfd;
+        if (poller->fd_rindices_allocd < maxfd) {
+            assert(ARRAY_SIZE_OK(poller->fd_rindices, maxfd));
+            poller->fd_rindices =
+                libxl__realloc(0, poller->fd_rindices,
+                               maxfd * sizeof(*poller->fd_rindices));
+            memset(poller->fd_rindices + poller->fd_rindices_allocd,
+                   0,
+                   (maxfd - poller->fd_rindices_allocd)
+                     * sizeof(*poller->fd_rindices));
+            poller->fd_rindices_allocd = maxfd;
         }
     }
 

tools/libxl/libxl_event.c static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller, · line 686

Quoted 9 lines
tools/libxl/libxl_event.c +4 -2
@@ -677,8 +680,10 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,
             fds[used].fd = req_fd;
             fds[used].events = req_events;
             fds[used].revents = 0;
-            assert(req_fd < poller->fd_rindex_allocd);
-            poller->fd_rindex[req_fd] = used;
+            assert(req_fd < poller->fd_rindices_allocd);
+            if (req_events & POLLIN)  poller->fd_rindices[req_fd][0] = used;
+            if (req_events & POLLPRI) poller->fd_rindices[req_fd][1] = used;
+            if (req_events & POLLOUT) poller->fd_rindices[req_fd][2] = used;

Would it be possible to have a little struct here instead of the [3]? So you'd get

	poller->fd_rindeices[req_fd].{in,pri,out}

?

Ah, I see this would make afterpoll_check_fd more complex which I think would outweigh any gain here.

So, other than the typoe:

Ian.

#5 lore raw

Re: [PATCH 02/20] libxl: support multiple libxl__ev_fds for the same fd

Ian Campbell writes ("Re: [Xen-devel] [PATCH 02/20] libxl: support multiple libxl__ev_fds for the same fd"):

Quoted 10 lines · Ian Jackson
Quoted 3 lines

We need a slightly more sophisticated data structure to allow this, where we record the slot not just for each fd but also for each (fd,eventbit) where eventbit is POLLIN, POLLPRI, POLLOUT.

Just to be sure I'm following: By multiple you mean you can have one libxl__ev_fds listening for e.g. POLLIN and another for POLLOUT but you specifically exclude the case where two libxl__ev_fds both want to listen for POLLIN? Similarly one listening for POLLIN|POLLPRI and the other for POLLOUT|POLLPRI (overlapping) is forbidden.

Yes, exactly. As I say in the new doc comment:

+   *
+   * It is not permitted to listen for the same or overlapping events
+   * on the same fd using multiple different libxl__ev_fd's.

This restriction is actually stronger than that required by the code. The code merely requires that for every distinct libxl__ev_fd listening on the same fd, it has at least one of the three flags all to itself. So your second scenario would actually work.

Would it be worth documenting this precise restriction ?

Quoted 3 lines
Quoted 1 line

+ * fd_rindices, and each elemebnt in the rindices is three indices

element

Fixed.

Quoted 3 lines

So, other than the typoe:

Thanks.

Ian.

#6 lore raw

Re: [PATCH 02/20] libxl: support multiple libxl__ev_fds for the same fd

Quoted 17 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 02/20] libxl: support multiple libxl__ev_fds for the same fd"):

Quoted 10 lines · Ian Jackson
Quoted 3 lines

We need a slightly more sophisticated data structure to allow this, where we record the slot not just for each fd but also for each (fd,eventbit) where eventbit is POLLIN, POLLPRI, POLLOUT.

Just to be sure I'm following: By multiple you mean you can have one libxl__ev_fds listening for e.g. POLLIN and another for POLLOUT but you specifically exclude the case where two libxl__ev_fds both want to listen for POLLIN? Similarly one listening for POLLIN|POLLPRI and the other for POLLOUT|POLLPRI (overlapping) is forbidden.

Yes, exactly. As I say in the new doc comment:

tools/libxl/libxl_internal.h +3 -0
+   *
+   * It is not permitted to listen for the same or overlapping events
+   * on the same fd using multiple different libxl__ev_fd's.
Quoted 6 lines

This restriction is actually stronger than that required by the code. The code merely requires that for every distinct libxl__ev_fd listening on the same fd, it has at least one of the three flags all to itself. So your second scenario would actually work.

Would it be worth documenting this precise restriction ?

I guess it would just confuse the language but only really add anything for what is a pretty small corner case?

Since it's a libxl internal thing I'm not too concerned about people coming to rely on the actual broader behaviour.

Quoted 14 lines
Quoted 4 lines
Quoted 2 lines

+ * fd_rindices, and each elemebnt in the rindices is three indices

element

Fixed.

Quoted 3 lines

So, other than the typoe:

Thanks.

Ian.

#7 lore raw

PATCH v6 3/20 libxl: event API: new facilities for waiting for subprocesses

In reply to Ian Jackson

The current arrangements in libxl for spawning subprocesses have two key problems: (i) they make unwarranted (and largely undocumented) assumptions about the caller's use of subprocesses, (ii) they aren't integrated into the event system and can't be made asynchronous etc.

So replace them with a new set of facilities.

Primarily, from the point of view of code inside libxl, this is libxl__ev_child_fork which is both (a) a version of fork() and (b) an event source which generates a callback when the child dies.

Quoted 1 line

From the point of view of the application, we fully document our use

of SIGCHLD. The application can tell us whether it wants to own SIGCHLD or not; if it does, it has to tell us about deaths of our children.

Currently there are no callers in libxl which use these facilities. All code in libxl which forks needs to be converted and libxl_fork needse to be be abolished.

---
 tools/libxl/libxl.c          |   17 +++-
 tools/libxl/libxl.h          |    1 +
 tools/libxl/libxl_event.c    |   53 +++++++--
 tools/libxl/libxl_event.h    |  147 +++++++++++++++++++++++-
 tools/libxl/libxl_fork.c     |  255 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   62 ++++++++++-
 6 files changed, 515 insertions(+), 20 deletions(-)
tools/libxl/libxl.c +16 -1
index 60dbfdc..42ac89f 100644
@@ -39,7 +39,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     memset(ctx, 0, sizeof(libxl_ctx));
     ctx->lg = lg;
 
-    /* First initialise pointers (cannot fail) */
+    /* First initialise pointers etc. (cannot fail) */
 
     LIBXL_TAILQ_INIT(&ctx->occurred);
 
@@ -58,6 +58,11 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     LIBXL_TAILQ_INIT(&ctx->death_list);
     libxl__ev_xswatch_init(&ctx->death_watch);
 
+    ctx->childproc_hooks = &libxl__childproc_default_hooks;
+    ctx->childproc_user = 0;
+        
+    ctx->sigchld_selfpipe[0] = -1;
+
     /* The mutex is special because we can't idempotently destroy it */
 
     if (libxl__init_recursive_mutex(ctx, &ctx->lock) < 0) {
@@ -160,6 +165,16 @@ int libxl_ctx_free(libxl_ctx *ctx)
 
     discard_events(&ctx->occurred);
 
+    /* If we have outstanding children, then the application inherits
+     * them; we wish the application good luck with understanding
+     * this if and when it reaps them. */
+    libxl__sigchld_removehandler(ctx);
+
+    if (ctx->sigchld_selfpipe[0] >= 0) {
+        close(ctx->sigchld_selfpipe[0]);
+        close(ctx->sigchld_selfpipe[1]);
+    }
+
     pthread_mutex_destroy(&ctx->lock);
 
     GC_FREE;
tools/libxl/libxl.h +1 -0
index edbca53..03e71f6 100644
@@ -380,6 +380,7 @@ enum {
     ERROR_NOT_READY = -11,
     ERROR_OSEVENT_REG_FAIL = -12,
     ERROR_BUFFERFULL = -13,
+    ERROR_UNKNOWN_CHILD = -14,
 };
 
 
tools/libxl/libxl_event.c +42 -11

85 lines not on this page · raw message

tools/libxl/libxl_event.h +142 -5

159 lines not on this page · raw message

tools/libxl/libxl_fork.c +255 -0

279 lines not on this page · raw message

tools/libxl/libxl_internal.h +59 -3

87 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#8 lore raw

PATCH v6 4/20 autoconf: New test for openpty et al.

In reply to Ian Jackson

We may need to #include <libutil.h>, and/or link with -lutil, to use openpty, login_tty, and the like. Provide INCLUDE_LIBUTIL_H (preprocessor constant, not always defined) and PTYFUNCS_LIBS (makefile variable).

We link libxl against PTYFUNCS_LIBS (which comes from autoconf) rather than UTIL_LIBS, and #include <libutil.h> where appropriate.

---
 config/Tools.mk.in             |    2 +
 tools/configure                |   51 ++++++++++++++++++++++++++++++++++++++++
 tools/configure.ac             |    2 +
 tools/libxl/Makefile           |    2 +-
 tools/libxl/libxl_bootloader.c |    4 +++
 tools/m4/ptyfuncs.m4           |   24 ++++++++++++++++++
 6 files changed, 84 insertions(+), 1 deletions(-)
 create mode 100644 tools/m4/ptyfuncs.m4
config/Tools.mk.in +2 -0
index 912d021..eb879dd 100644
@@ -30,6 +30,8 @@ PTHREAD_CFLAGS      := @PTHREAD_CFLAGS@
 PTHREAD_LDFLAGS     := @PTHREAD_LDFLAGS@
 PTHREAD_LIBS        := @PTHREAD_LIBS@
 
+PTYFUNCS_LIBS       := @PTYFUNCS_LIBS@
+
 # Download GIT repositories via HTTP or GIT's own protocol?
 # GIT's protocol is faster and more robust, when it works at all (firewalls
 # may block it). We make it the default, but if your GIT repository downloads
tools/configure +51 -0

69 lines not on this page · raw message

tools/configure.ac +2 -0
index 250dffd..b4f72e8 100644
@@ -35,6 +35,7 @@ m4_include([m4/uuid.m4])
 m4_include([m4/pkg.m4])
 m4_include([m4/curses.m4])
 m4_include([m4/pthread.m4])
+m4_include([m4/ptyfuncs.m4])
 
 # Enable/disable options
 AX_ARG_DEFAULT_DISABLE([githttp], [Download GIT repositories via HTTP])
@@ -132,6 +133,7 @@ AC_SUBST(libext2fs)
 AC_CHECK_LIB([gcrypt], [gcry_md_hash_buffer], [libgcrypt="y"], [libgcrypt="n"])
 AC_SUBST(libgcrypt)
 AX_CHECK_PTHREAD
+AX_CHECK_PTYFUNCS
 AC_CHECK_LIB([rt], [clock_gettime])
 AC_CHECK_LIB([yajl], [yajl_alloc], [],
     [AC_MSG_ERROR([Could not find yajl])])
tools/libxl/Makefile +1 -1
index e5ea867..5ba144f 100644
@@ -20,7 +20,7 @@ LIBUUID_LIBS += -luuid
 endif
 
 LIBXL_LIBS =
-LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS) $(LIBUUID_LIBS)
+LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)
 
 CFLAGS += $(PTHREAD_CFLAGS)
 LDFLAGS += $(PTHREAD_LDFLAGS)
tools/libxl/libxl_bootloader.c +4 -0
index 2774062..b50944a 100644
@@ -16,6 +16,10 @@
 
 #include <termios.h>
 
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+
 #include "libxl_internal.h"
 
 #define XENCONSOLED_BUF_SIZE 16
tools/m4/ptyfuncs.m4 new file +24 -0
new file mode 100644
index 0000000..2846a6d
@@ -0,0 +1,24 @@
+AC_DEFUN([AX_CHECK_PTYFUNCS], [
+    AC_CHECK_HEADER([libutil.h],[
+      AC_DEFINE([INCLUDE_LIBUTIL_H],[<libutil.h>],[libutil header file name])
+    ])
+    AC_CACHE_CHECK([for openpty et al], [ax_cv_ptyfuncs_libs], [
+        ax_cv_ptyfuncs_libs=-lutil
+        AX_SAVEVAR_SAVE(LIBS)
+        LIBS="$LIBS $ax_cv_ptyfuncs_libs"
+        AC_LINK_IFELSE([
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+int main(void) {
+  openpty(0,0,0,0,0);
+  login_tty(0);
+}
+])
+        AX_SAVEVAR_RESTORE(LIBS)],
+    [],[
+        AC_MSG_FAILURE([Unable to find -lutil for openpty and login_tty])
+    ])
+    PTYFUNCS_LIBS="$ax_cv_ptyfuncs_libs"
+    AC_SUBST(PTYFUNCS_LIBS)
+])
Signature · 1 line
1.7.2.5
#9 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Quoted 24 lines · Ian Jackson

We may need to #include <libutil.h>, and/or link with -lutil, to use openpty, login_tty, and the like. Provide INCLUDE_LIBUTIL_H (preprocessor constant, not always defined) and PTYFUNCS_LIBS (makefile variable).

We link libxl against PTYFUNCS_LIBS (which comes from autoconf) rather than UTIL_LIBS, and #include <libutil.h> where appropriate.

---
 config/Tools.mk.in             |    2 +
 tools/configure                |   51 ++++++++++++++++++++++++++++++++++++++++
 tools/configure.ac             |    2 +
 tools/libxl/Makefile           |    2 +-
 tools/libxl/libxl_bootloader.c |    4 +++
 tools/m4/ptyfuncs.m4           |   24 ++++++++++++++++++
 6 files changed, 84 insertions(+), 1 deletions(-)
 create mode 100644 tools/m4/ptyfuncs.m4
tools/libxl/Makefile +0 -0
index e5ea867..5ba144f 100644

tools/libxl/Makefile LIBUUID_LIBS += -luuid · line 23

Quoted 5 lines
tools/libxl/Makefile +1 -1
@@ -20,7 +20,7 @@ LIBUUID_LIBS += -luuid
 endif
 
 LIBXL_LIBS =
-LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS) $(LIBUUID_LIBS)
+LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)

Did you intend to remove the definition of UTIL_LIBS? I don't see a hunk which does that.

Quoted 20 lines
tools/m4/ptyfuncs.m4 new file +14 -0
new file mode 100644
index 0000000..2846a6d
@@ -0,0 +1,24 @@
+AC_DEFUN([AX_CHECK_PTYFUNCS], [
+    AC_CHECK_HEADER([libutil.h],[
+      AC_DEFINE([INCLUDE_LIBUTIL_H],[<libutil.h>],[libutil header file name])
+    ])
+    AC_CACHE_CHECK([for openpty et al], [ax_cv_ptyfuncs_libs], [
+        ax_cv_ptyfuncs_libs=-lutil
+        AX_SAVEVAR_SAVE(LIBS)
+        LIBS="$LIBS $ax_cv_ptyfuncs_libs"
+        AC_LINK_IFELSE([
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+int main(void) {
+  openpty(0,0,0,0,0);

tools/m4/ptyfuncs.m4 line 24

Quoted 10 lines
tools/m4/ptyfuncs.m4 new file +10 -0
+  login_tty(0);
+}
+])
+        AX_SAVEVAR_RESTORE(LIBS)],
+    [],[
+        AC_MSG_FAILURE([Unable to find -lutil for openpty and login_tty])
+    ])
+    PTYFUNCS_LIBS="$ax_cv_ptyfuncs_libs"
+    AC_SUBST(PTYFUNCS_LIBS)
+])

Does this fail if -lutil isn't available? My (perhaps mistaken) reading of the commit message was that -lutil was only needed on some platforms?

On the other hand the AC_MSG_FAILURE doesn't see to have appeared in the configure output -- so maybe I just understand what all these macros are actually doing.

Ian.

#10 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Ian Campbell writes ("Re: [Xen-devel] [PATCH 04/20] autoconf: New test for openpty et al."):

Quoted 7 lines · Ian Jackson

tools/libxl/Makefile LIBUUID_LIBS += -luuid · line 23

Quoted 3 lines

LIBXL_LIBS =

tools/libxl/Makefile +1 -1
-LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS) $(LIBUUID_LIBS)
+LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)

Did you intend to remove the definition of UTIL_LIBS? I don't see a hunk which does that.

It's also used by xenconsoled.

Quoted 19 lines
Quoted 19 lines
tools/m4/ptyfuncs.m4 new file +19 -0
+AC_DEFUN([AX_CHECK_PTYFUNCS], [
+    AC_CHECK_HEADER([libutil.h],[
+      AC_DEFINE([INCLUDE_LIBUTIL_H],[<libutil.h>],[libutil header file name])
+    ])
+    AC_CACHE_CHECK([for openpty et al], [ax_cv_ptyfuncs_libs], [
+        ax_cv_ptyfuncs_libs=-lutil
+        AX_SAVEVAR_SAVE(LIBS)
+        LIBS="$LIBS $ax_cv_ptyfuncs_libs"
+        AC_LINK_IFELSE([
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+int main(void) {
+  openpty(0,0,0,0,0);
+  login_tty(0);
+}
+])
+        AX_SAVEVAR_RESTORE(LIBS)],
+    [],[
Quoted 8 lines

tools/m4/ptyfuncs.m4 line 24

Quoted 5 lines
tools/m4/ptyfuncs.m4 new file +5 -0
+        AC_MSG_FAILURE([Unable to find -lutil for openpty and login_tty])
+    ])
+    PTYFUNCS_LIBS="$ax_cv_ptyfuncs_libs"
+    AC_SUBST(PTYFUNCS_LIBS)
+])

Does this fail if -lutil isn't available? My (perhaps mistaken) reading of the commit message was that -lutil was only needed on some platforms?

Yes, it will do. Currently -lutil is used everywhere except Solaris but surely Solaris support has rotted by now. I could change the patch to try both with and without, I guess.

Quoted 3 lines

On the other hand the AC_MSG_FAILURE doesn't see to have appeared in the configure output -- so maybe I just understand what all these macros are actually doing.

Linux has -lutil.

Ian.

#11 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Quoted 39 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 04/20] autoconf: New test for openpty et al."):

Quoted 7 lines · Ian Jackson

tools/libxl/Makefile LIBUUID_LIBS += -luuid · line 23

Quoted 3 lines

LIBXL_LIBS =

tools/libxl/Makefile +1 -1
-LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS) $(LIBUUID_LIBS)
+LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)

Did you intend to remove the definition of UTIL_LIBS? I don't see a hunk which does that.

It's also used by xenconsoled.

Quoted 19 lines
Quoted 19 lines
tools/m4/ptyfuncs.m4 new file +19 -0
+AC_DEFUN([AX_CHECK_PTYFUNCS], [
+    AC_CHECK_HEADER([libutil.h],[
+      AC_DEFINE([INCLUDE_LIBUTIL_H],[<libutil.h>],[libutil header file name])
+    ])
+    AC_CACHE_CHECK([for openpty et al], [ax_cv_ptyfuncs_libs], [
+        ax_cv_ptyfuncs_libs=-lutil
+        AX_SAVEVAR_SAVE(LIBS)
+        LIBS="$LIBS $ax_cv_ptyfuncs_libs"
+        AC_LINK_IFELSE([
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+int main(void) {
+  openpty(0,0,0,0,0);
+  login_tty(0);
+}
+])
+        AX_SAVEVAR_RESTORE(LIBS)],
+    [],[
Quoted 8 lines

tools/m4/ptyfuncs.m4 line 24

Quoted 5 lines
tools/m4/ptyfuncs.m4 new file +5 -0
+        AC_MSG_FAILURE([Unable to find -lutil for openpty and login_tty])
+    ])
+    PTYFUNCS_LIBS="$ax_cv_ptyfuncs_libs"
+    AC_SUBST(PTYFUNCS_LIBS)
+])

Does this fail if -lutil isn't available? My (perhaps mistaken) reading of the commit message was that -lutil was only needed on some platforms?

Quoted 3 lines

Yes, it will do. Currently -lutil is used everywhere except Solaris but surely Solaris support has rotted by now. I could change the patch to try both with and without, I guess.

OK, I think we can just leave it until someone ports Xen to a platform which has this behaviour (i.e. it can be tested)

Quoted 5 lines
Quoted 3 lines

On the other hand the AC_MSG_FAILURE doesn't see to have appeared in the configure output -- so maybe I just understand what all these macros are actually doing.

Linux has -lutil.

I meant that the message doesn't seem to appear in the generate configure script itself.

Ian.

#12 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Ian Campbell writes ("Re: [Xen-devel] [PATCH 04/20] autoconf: New test for openpty et al."):

Quoted 10 lines · Ian Jackson
Quoted 6 lines
Quoted 2 lines

Does this fail if -lutil isn't available? My (perhaps mistaken) reading of the commit message was that -lutil was only needed on some platforms?

Yes, it will do. Currently -lutil is used everywhere except Solaris but surely Solaris support has rotted by now. I could change the patch to try both with and without, I guess.

OK, I think we can just leave it until someone ports Xen to a platform which has this behaviour (i.e. it can be tested)

I've updated it anyway, as it wasn't too hard.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

On the other hand the AC_MSG_FAILURE doesn't see to have appeared in the configure output -- so maybe I just understand what all these macros are actually doing.

Linux has -lutil.

I meant that the message doesn't seem to appear in the generate configure script itself.

While editing this patch I found that I had some of the bracketing wrong. That was probably it. It'll be fixed in v2.

Ian.

#13 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

In reply to Ian Jackson
Quoted 24 lines · Ian Jackson

We may need to #include <libutil.h>, and/or link with -lutil, to use openpty, login_tty, and the like. Provide INCLUDE_LIBUTIL_H (preprocessor constant, not always defined) and PTYFUNCS_LIBS (makefile variable).

We link libxl against PTYFUNCS_LIBS (which comes from autoconf) rather than UTIL_LIBS, and #include <libutil.h> where appropriate.

---

config/Tools.mk.in             |    2 +
tools/configure                |   51 ++++++++++++++++++++++++++++++++++++++++
tools/configure.ac             |    2 +
tools/libxl/Makefile           |    2 +-
tools/libxl/libxl_bootloader.c |    4 +++
tools/m4/ptyfuncs.m4           |   24 ++++++++++++++++++
6 files changed, 84 insertions(+), 1 deletions(-)

create mode 100644 tools/m4/ptyfuncs.m4

tools/libxl/libxl_bootloader.c +0 -0
index 2774062..b50944a 100644

tools/libxl/libxl_bootloader.c line 22

Quoted 7 lines
tools/libxl/libxl_bootloader.c +4 -0
@@ -16,6 +16,10 @@
 
 #include <termios.h>
 
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+

Maybe I'm missing something, because this autoconf macros are hard to read, but shouldn't you add something like

#undef INCLUDE_LIBUTIL_H

to tools/config.h.in, so the generated config.h contains this define?

Quoted 3 lines
tools/libxl/libxl_bootloader.c +0 -0
 #include "libxl_internal.h"
 
 #define XENCONSOLED_BUF_SIZE 16
#14 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Roger Pau Monne writes ("Re: [Xen-devel] [PATCH 04/20] autoconf: New test for openpty et al."):

Quoted 10 lines

tools/configure $as_echo "$ax_cv_pthread_flags" >&6; } · line 7353

Quoted 4 lines
tools/configure +4 -0
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+

Maybe I'm missing something, because this autoconf macros are hard to read, but shouldn't you add something like

#undef INCLUDE_LIBUTIL_H

to tools/config.h.in, so the generated config.h contains this define?

Yes. This is normally done by autoheader, which is normally done by autogen.sh. But I see that we aren't running autoheader. I will see about fixing that.

Ian.

#15 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Quoted 8 lines · Ian Jackson

Roger Pau Monne writes ("Re: [Xen-devel] [PATCH 04/20] autoconf: New test for openpty et al."):

Quoted 7 lines

tools/configure $as_echo "$ax_cv_pthread_flags" >&6; } · line 7353

Quoted 4 lines
tools/configure +4 -0
+#ifdef INCLUDE_LIBUTIL_H
+#include INCLUDE_LIBUTIL_H
+#endif
+

Maybe I'm missing something, because this autoconf macros are hard to read, but shouldn't you add something like

Quoted 8 lines
Quoted 3 lines

#undef INCLUDE_LIBUTIL_H

to tools/config.h.in, so the generated config.h contains this define?

Yes. This is normally done by autoheader, which is normally done by autogen.sh. But I see that we aren't running autoheader. I will see about fixing that.

Autoheader was adding a lot of stuff which we weren't using, so I preferred to keep config.h.in (and config.h) with just the defines we really need.

#16 lore raw

Re: [PATCH 04/20] autoconf: New test for openpty et al.

Roger Pau Monne writes ("Re: [Xen-devel] [PATCH 04/20] autoconf: New test for openpty et al."):

Quoted 6 lines · Ian Jackson
Quoted 3 lines

Yes. This is normally done by autoheader, which is normally done by autogen.sh. But I see that we aren't running autoheader. I will see about fixing that.

Autoheader was adding a lot of stuff which we weren't using, so I preferred to keep config.h.in (and config.h) with just the defines we really need.

I just ran it and everything it generates seems to be produced by something in configure.ac, as I expected.

Surely the problem then is that configure.ac has too much useless stuff in it ? For example why are we checking for things like alarm and atexit and everything from <string.h>, which are going to exist everywhere ?

Ian.

#17 lore raw

PATCH v6 5/20 libxl: provide libxl__remove_file et al.

In reply to Ian Jackson

These utility functions cope with EINTR AND ENOENT, do error logging, and we provide a recursive version to delete whole directory trees.

---
 tools/libxl/libxl_internal.h |    7 ++++
 tools/libxl/libxl_utils.c    |   79 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +7 -0
index abc38fb..3996824 100644
@@ -442,6 +442,13 @@ _hidden char *libxl__strndup(libxl__gc *gc_opt, const char *c, size_t n);
  * string. (similar to a gc'd dirname(3)). */
 _hidden char *libxl__dirname(libxl__gc *gc_opt, const char *s);
 
+/* Each of these logs errors and returns a libxl error code.
+ * They do not mind if path is already removed.
+ * For _file, path must not be a directory; for _directory it must be. */
+_hidden int libxl__remove_file(libxl__gc *gc, const char *path);
+_hidden int libxl__remove_directory(libxl__gc *gc, const char *path);
+_hidden int libxl__remove_file_or_directory(libxl__gc *gc, const char *path);
+
 _hidden char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array, int length);
 
 _hidden int libxl__xs_writev(libxl__gc *gc, xs_transaction_t t,
tools/libxl/libxl_utils.c +79 -0

85 lines not on this page · raw message

Signature · 1 line
1.7.2.5
Acked-by on v6 5/20
#18 lore raw

Re: [PATCH 05/20] libxl: provide libxl__remove_file et al.

Quoted 8 lines
tools/libxl/libxl_utils.c +7 -0
+int libxl__remove_directory(libxl__gc *gc, const char *dirpath)
+{
+    int rc = 0;
+    DIR *d = 0;
+
+    d = opendir(dirpath);
+    if (!d) {

tools/libxl/libxl_utils.c int libxl_read_file_contents(libxl_ctx *ctx, const char *filename, · line 409

Quoted 10 lines
tools/libxl/libxl_utils.c +10 -0
+        if (errno == ENOENT)
+            goto out;
+
+        LOGE(ERROR, "failed to opendir %s for removal", dirpath);
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    size_t need = offsetof(struct dirent, d_name) +
+        pathconf(dirpath, _PC_NAME_MAX) + 1;

This was an interesting pattern, but I see that readdir(3) recommends portable programs do things this way...

Quoted 10 lines
tools/libxl/libxl_utils.c +10 -0
+    struct dirent *de_buf = libxl__zalloc(gc, need);
+    struct dirent *de;
+
+    for (;;) {
+        int r = readdir_r(d, de_buf, &de);
+        if (r) {
+            LOGE(ERROR, "failed to readdir %s for removal", dirpath);
+            rc = ERROR_FAIL;
+            break;
+        }

tools/libxl/libxl_utils.c int libxl_read_file_contents(libxl_ctx *ctx, const char *filename, · line 429

Quoted 10 lines
tools/libxl/libxl_utils.c +10 -0
+        if (!de)
+            break;
+
+        if (!strcmp(de->d_name, ".") ||
+            !strcmp(de->d_name, ".."))
+            continue;
+
+        const char *subpath = GCSPRINTF("%s/%s", dirpath, de->d_name);
+        if (libxl__remove_file_or_directory(gc, subpath))
+            rc = ERROR_FAIL;

Deliberately doesn't "goto out" so as to remove as much as possible?

If so then:

Quoted 19 lines
tools/libxl/libxl_utils.c +16 -0
+    }
+
+    for (;;) {
+        int r = rmdir(dirpath);
+        if (!r) break;
+        if (errno == ENOENT) goto out;
+        if (errno == EINTR) continue;
+        LOGE(ERROR, "failed to remove emptied directory %s", dirpath);
+        rc = ERROR_FAIL;
+    }
+
+ out:
+    if (d) closedir(d);
+
+    return rc;
+}
 
 pid_t libxl_fork(libxl_ctx *ctx)
 {
#19 lore raw

Re: [PATCH 05/20] libxl: provide libxl__remove_file et al.

Ian Campbell writes ("Re: [Xen-devel] [PATCH 05/20] libxl: provide libxl__remove_file et al."):

Quoted 5 lines

tools/libxl/libxl_utils.c int libxl_read_file_contents(libxl_ctx *ctx, const char *filename, · line 409

Quoted 2 lines
tools/libxl/libxl_utils.c +2 -0
+    size_t need = offsetof(struct dirent, d_name) +
+        pathconf(dirpath, _PC_NAME_MAX) + 1;

This was an interesting pattern, but I see that readdir(3) recommends portable programs do things this way...

Bizarre, eh ?

Quoted 5 lines

tools/libxl/libxl_utils.c int libxl_read_file_contents(libxl_ctx *ctx, const char *filename, · line 429

Quoted 3 lines
tools/libxl/libxl_utils.c +3 -0
+        const char *subpath = GCSPRINTF("%s/%s", dirpath, de->d_name);
+        if (libxl__remove_file_or_directory(gc, subpath))
+            rc = ERROR_FAIL;

Deliberately doesn't "goto out" so as to remove as much as possible?

Yes.

Quoted 3 lines

If so then:

Thanks,
Ian.

#20 lore raw

PATCH v6 6/20 libxl: Introduce libxl__sendmsg_fds and libxl__recvmsg_fds

In reply to Ian Jackson

We will want to reuse the fd-sending code, so break it out into its own function, and provide the corresponding sending function.

---
 tools/libxl/libxl_internal.h |   11 ++++
 tools/libxl/libxl_qmp.c      |   31 ++-----------
 tools/libxl/libxl_utils.c    |  104 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 119 insertions(+), 27 deletions(-)
tools/libxl/libxl_internal.h +11 -0
index 3996824..6ceb362 100644
@@ -1128,6 +1128,17 @@ _hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
 _hidden int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid,
                                        const libxl_domain_config *guest_config);
 
+/* on failure, logs */
+int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
+                       const void *data, size_t datalen,
+                       int nfds, const int fds[], const char *what);
+
+/* Insists on receiving exactly nfds and datalen.  On failure, logs
+ * and leaves *fds untouched. */
+int libxl__recvmsg_fds(libxl__gc *gc, int carrier,
+                       void *databuf, size_t datalen,
+                       int nfds, int fds[], const char *what);
+
 /* from libxl_json */
 #include <yajl/yajl_gen.h>
 
tools/libxl/libxl_qmp.c +4 -27
index f5a3edc..83c22b3 100644
@@ -544,38 +544,15 @@ static int qmp_send_fd(libxl__gc *gc, libxl__qmp_handler *qmp,
                        qmp_request_context *context,
                        int fd)
 {
-    struct msghdr msg = { 0 };
-    struct cmsghdr *cmsg;
-    char control[CMSG_SPACE(sizeof (fd))];
-    struct iovec iov;
     char *buf = NULL;
+    int rc;
 
     buf = qmp_send_prepare(gc, qmp, "getfd", args, callback, opaque, context);
 
-    /* Response data */
-    iov.iov_base = buf;
-    iov.iov_len  = strlen(buf);
-
-    /* compose the message */
-    msg.msg_iov = &iov;
-    msg.msg_iovlen = 1;
-    msg.msg_control = control;
-    msg.msg_controllen = sizeof (control);
-
-    /* attach open fd */
-    cmsg = CMSG_FIRSTHDR(&msg);
-    cmsg->cmsg_level = SOL_SOCKET;
-    cmsg->cmsg_type = SCM_RIGHTS;
-    cmsg->cmsg_len = CMSG_LEN(sizeof (fd));
-    *(int *)CMSG_DATA(cmsg) = fd;
-
-    msg.msg_controllen = cmsg->cmsg_len;
+    rc = libxl__sendmsg_fds(gc, qmp->qmp_fd, buf, strlen(buf), 1, &fd,
+                            "QMP message to QEMU");
+    if (rc) return rc;
 
-    if (sendmsg(qmp->qmp_fd, &msg, 0) < 0) {
-        LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
-                         "Failed to send a QMP message to QEMU.");
-        return ERROR_FAIL;
-    }
     if (libxl_write_exactly(qmp->ctx, qmp->qmp_fd, "\r\n", 2,
                             "CRLF", "QMP socket")) {
         return ERROR_FAIL;
tools/libxl/libxl_utils.c +104 -0

110 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#21 lore raw

PATCH v6 7/20 libxl: Clean up setdefault in do_domain_create

In reply to Ian Jackson

do_domain_create called libxl__domain_create_info_setdefault twice.

---
 tools/libxl/libxl_create.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
tools/libxl/libxl_create.c +0 -3
index e63c7bd..3675afe 100644
@@ -552,9 +552,6 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     ret = libxl__domain_create_info_setdefault(gc, &d_config->c_info);
     if (ret) goto error_out;
 
-    ret = libxl__domain_create_info_setdefault(gc, &d_config->c_info);
-    if (ret) goto error_out;
-
     ret = libxl__domain_make(gc, &d_config->c_info, &domid);
     if (ret) {
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot make domain: %d", ret);
Signature · 1 line
1.7.2.5
#22 lore raw

PATCH v6 8/20 libxl: provide libxl__datacopier_*

In reply to Ian Jackson

General facility for ao operations to shovel data between fds.

This will be used by the bootloader machinery.

---
 tools/libxl/Makefile         |    3 +-
 tools/libxl/libxl_aoutils.c  |  188 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   40 +++++++++
 3 files changed, 230 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_aoutils.c
tools/libxl/Makefile +2 -1
index 5ba144f..6e253b1 100644
@@ -52,7 +52,8 @@ LIBXL_LIBS += -lyajl
 
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
-			libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
+			libxl_internal.o libxl_utils.o libxl_uuid.o \
+			libxl_json.o libxl_aoutils.o \
 			libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
tools/libxl/libxl_aoutils.c new file +188 -0

188 lines not on this page · raw message

tools/libxl/libxl_internal.h +40 -0
index 6ceb362..20c95db 100644
@@ -833,6 +833,7 @@ _hidden int libxl__ev_devstate_wait(libxl__gc *gc, libxl__ev_devstate *ds,
  */
 _hidden int libxl__try_phy_backend(mode_t st_mode);
 
+
 /* from libxl_pci */
 
 _hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, int starting);
@@ -1458,6 +1459,45 @@ int libxl__carefd_close(libxl__carefd*);
 int libxl__carefd_fd(const libxl__carefd*);
 
 
+/*----- datacopier: copies data from one fd to another -----*/
+
+typedef struct libxl__datacopier_state libxl__datacopier_state;
+typedef struct libxl__datacopier_buf libxl__datacopier_buf;
+
+/* onwrite==1 means failure happened when writing, logged, errnoval is valid
+ * onwrite==0 means failure happened when reading
+ *     errnoval==0 means we got eof and all data was written
+ *     errnoval!=0 means we had a read error, logged
+ * onwrite==-1 means some other internal failure, errnoval not valid, logged
+ * in all cases copier is killed before calling this callback */
+typedef void libxl__datacopier_callback(libxl__egc *egc,
+     libxl__datacopier_state *dc, int onwrite, int errnoval);
+
+struct libxl__datacopier_buf {
+    /* private to datacopier */
+    LIBXL_TAILQ_ENTRY(libxl__datacopier_buf) entry;
+    int used;
+    char buf[1000];
+};
+
+struct libxl__datacopier_state {
+    /* caller must fill these in, and they must all remain valid */
+    libxl__ao *ao;
+    int readfd, writefd;
+    ssize_t maxsz;
+    const char *copywhat, *readwhat, *writewhat; /* for error msgs */
+    libxl__datacopier_callback *callback;
+    /* remaining fields are private to datacopier */
+    libxl__ev_fd toread, towrite;
+    ssize_t used;
+    LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
+};
+
+_hidden void libxl__datacopier_init(libxl__datacopier_state *dc);
+_hidden void libxl__datacopier_kill(libxl__datacopier_state *dc);
+_hidden int libxl__datacopier_start(libxl__datacopier_state *dc);
+
+
 /*
  * Convenience macros.
  */
Signature · 1 line
1.7.2.5
#23 lore raw

PATCH v6 9/20 libxl: provide libxl__openpty_*

In reply to Ian Jackson

General facility for ao operations to open ptys.

This will be used by the bootloader machinery.

---
 tools/libxl/libxl_aoutils.c  |  127 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   36 ++++++++++++
 2 files changed, 163 insertions(+), 0 deletions(-)
tools/libxl/libxl_aoutils.c +127 -0
index 774fa03..023de6e 100644
@@ -186,3 +186,130 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
     return rc;
 }
 
+/*----- openpty -----*/
+
+/* implementation */
+    
+static void openpty_cleanup(libxl__openpty_state *op)
+{
+    int i;
+
+    for (i=0; i<op->count; i++) {
+        libxl__openpty_result *res = &op->results[i];
+        libxl__carefd_close(res->master);  res->master = 0;
+        libxl__carefd_close(res->slave);   res->slave = 0;
+    }
+}
+
+static void openpty_exited(libxl__egc *egc, libxl__ev_child *child,
+                           pid_t pid, int status) {
+    libxl__openpty_state *op = CONTAINER_OF(child, *op, child);
+    STATE_AO_GC(op->ao);
+
+    if (status) {
+        /* Perhaps the child gave us the fds and then exited nonzero.
+         * Well that would be odd but we don't really care. */
+        libxl_report_child_exitstatus(CTX, op->rc ? LIBXL__LOG_ERROR
+                                                  : LIBXL__LOG_WARNING,
+                                      "openpty child", pid, status);
+    }
+    if (op->rc)
+        openpty_cleanup(op);
+    op->callback(egc, op);
+}
+
+int libxl__openptys(libxl__openpty_state *op,
+                    const struct termios *termp,
+                    const struct winsize *winp) {
+    /*
+     * This is completely crazy.  openpty calls grantpt which the spec
+     * says may fork, and may not be called with a SIGCHLD handler.
+     * Now our application may have a SIGCHLD handler so that's bad.
+     * We could perhaps block it but we'd need to block it on all
+     * threads.  This is just Too Hard.
+     *
+     * So instead, we run openpty in a child process.  That child
+     * process then of course has only our own thread and our own
+     * signal handlers.  We pass the fds back.
+     *
+     * Since our only current caller actually wants two ptys, we
+     * support calling openpty multiple times for a single fork.
+     */
+    STATE_AO_GC(op->ao);
+    int count = op->count;
+    int r, i, rc, sockets[2], ptyfds[count][2];
+    libxl__carefd *for_child = 0;
+    pid_t pid = -1;
+
+    for (i=0; i<count; i++) {
+        ptyfds[i][0] = ptyfds[i][1] = -1;
+        libxl__openpty_result *res = &op->results[i];
+        assert(!res->master);
+        assert(!res->slave);
+    }
+    sockets[0] = sockets[1] = -1; /* 0 is for us, 1 for our child */
+
+    libxl__carefd_begin();
+    r = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
+    if (r) { sockets[0] = sockets[1] = -1; }
+    for_child = libxl__carefd_opened(CTX, sockets[1]);
+    if (r) { LOGE(ERROR,"socketpair failed"); rc = ERROR_FAIL; goto out; }
+
+    pid = libxl__ev_child_fork(gc, &op->child, openpty_exited);
+    if (pid == -1) {
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    if (!pid) {
+        /* child */
+        close(sockets[0]);
+        signal(SIGCHLD, SIG_DFL);
+
+        for (i=0; i<count; i++) {
+            r = openpty(&ptyfds[i][0], &ptyfds[i][1], NULL, termp, winp);
+            if (r) { LOGE(ERROR,"openpty failed"); _exit(-1); }
+        }
+        rc = libxl__sendmsg_fds(gc, sockets[1], "",1,
+                                2*count, &ptyfds[0][0], "ptys");
+        if (rc) { LOGE(ERROR,"sendmsg to parent failed"); _exit(-1); }
+        _exit(0);
+    }
+
+    libxl__carefd_close(for_child);
+    for_child = 0;
+
+    /* this should be fast so do it synchronously */
+
+    libxl__carefd_begin();
+    char buf[1];
+    rc = libxl__recvmsg_fds(gc, sockets[0], buf,1,
+                            2*count, &ptyfds[0][0], "ptys");
+    if (!rc) {
+        for (i=0; i<count; i++) {
+            libxl__openpty_result *res = &op->results[i];
+            res->master = libxl__carefd_record(CTX, ptyfds[i][0]);
+            res->slave =  libxl__carefd_record(CTX, ptyfds[i][1]);
+        }
+    }
+    /* now the pty fds are in the carefds, if they were ever open */
+    libxl__carefd_unlock();
+    if (rc)
+        goto out;
+
+    rc = 0;
+
+ out:
+    if (sockets[0] >= 0) close(sockets[0]);
+    libxl__carefd_close(for_child);
+    if (libxl__ev_child_inuse(&op->child)) {
+        op->rc = rc;
+        /* we will get a callback when the child dies */
+        return 0;
+    }
+
+    assert(rc);
+    openpty_cleanup(op);
+    return rc;
+}
+
tools/libxl/libxl_internal.h +36 -0
index 20c95db..9af99ec 100644
@@ -1498,6 +1498,42 @@ _hidden void libxl__datacopier_kill(libxl__datacopier_state *dc);
 _hidden int libxl__datacopier_start(libxl__datacopier_state *dc);
 
 
+/*----- openpty -----*/
+
+/*
+ * opens count (>0) ptys like count calls to openpty, and then
+ * calls back.  On entry, all op[].master and op[].slave must be
+ * 0.  On callback, either rc==0 and master and slave are non-0,
+ * or rc is a libxl error and they are both 0.  If libxl__openpty
+ * returns non-0 no callback will happen and everything is left
+ * cleaned up.
+ */
+
+typedef struct libxl__openpty_state libxl__openpty_state;
+typedef struct libxl__openpty_result libxl__openpty_result;
+typedef void libxl__openpty_callback(libxl__egc *egc, libxl__openpty_state *op);
+
+struct libxl__openpty_state {
+    /* caller must fill these in, and they must all remain valid */
+    libxl__ao *ao;
+    libxl__openpty_callback *callback;
+    int count;
+    libxl__openpty_result *results; /* actual size is count, out parameter */
+    /* public, result, caller may only read in callback */
+    int rc;
+    /* private for implementation */
+    libxl__ev_child child;
+};
+
+struct libxl__openpty_result {
+    libxl__carefd *master, *slave;
+};
+
+int libxl__openptys(libxl__openpty_state *op,
+                    const struct termios *termp,
+                    const struct winsize *winp);
+
+
 /*
  * Convenience macros.
  */
Signature · 1 line
1.7.2.5
#24 lore raw

PATCH v6 10/20 libxl: ao: Convert libxl_run_bootloader

In reply to Ian Jackson

Convert libxl_run_bootloader to an ao_how-taking function.

It's implemented in terms of libxl__bootloader_run, which can be used internally. The resulting code is pretty much a rewrite.

Significant changes include:

- We direct the bootloader's results to a file, not a pipe. This makes it simpler to deal with as we don't have to read it concurrently along with everything else.

- We now issue a warning if we find unexpected statements in the bootloader results.

- The arrangements for buffering of bootloader input and output are completely changed. Now we have a fixed limit of 64k on output, and 4k on input, and discard the oldest data when this overflows (which it shouldn't). There is no timeout any more.

---
 tools/libxl/libxl.c            |    4 +
 tools/libxl/libxl.h            |    3 +-
 tools/libxl/libxl_bootloader.c |  708 +++++++++++++++++++++-------------------
 tools/libxl/libxl_create.c     |    8 +-
 tools/libxl/libxl_internal.h   |   34 ++
 5 files changed, 415 insertions(+), 342 deletions(-)
tools/libxl/libxl.c +4 -0
index 42ac89f..54f3813 100644
@@ -1748,6 +1748,10 @@ int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
      * For other device types assume that the blktap2 process is
      * needed by the soon to be started domain and do nothing.
      */
+    /*
+     * FIXME
+     * This appears to leak the disk in failure paths
+     */
 
     return 0;
 }
tools/libxl/libxl.h +2 -1
index 03e71f6..477b72a 100644
@@ -494,7 +494,8 @@ int libxl_get_max_cpus(libxl_ctx *ctx);
 int libxl_run_bootloader(libxl_ctx *ctx,
                          libxl_domain_build_info *info,
                          libxl_device_disk *disk,
-                         uint32_t domid);
+                         uint32_t domid,
+                         libxl_asyncop_how *ao_how);
 
   /* 0 means ERROR_ENOMEM, which we have logged */
 
tools/libxl/libxl_bootloader.c +369 -339

828 lines not on this page · raw message

tools/libxl/libxl_create.c +6 -2
index 3675afe..dbc3cf0 100644
@@ -572,8 +572,12 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         if (ret) goto error_out;
     }
 
-    if ( restore_fd < 0 ) {
-        ret = libxl_run_bootloader(ctx, &d_config->b_info, d_config->num_disks > 0 ? &d_config->disks[0] : NULL, domid);
+    libxl_device_disk *bootdisk =
+        d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
+
+    if (restore_fd < 0 && bootdisk) {
+        ret = libxl_run_bootloader(ctx, &d_config->b_info, bootdisk, domid,
+                                   0 /* fixme-ao */);
         if (ret) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                        "failed to run bootloader: %d", ret);
tools/libxl/libxl_internal.h +34 -0
index 9af99ec..b3f84ba 100644
@@ -42,6 +42,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/socket.h>
 
 #include <xs.h>
 #include <xenctrl.h>
@@ -449,6 +450,7 @@ _hidden int libxl__remove_file(libxl__gc *gc, const char *path);
 _hidden int libxl__remove_directory(libxl__gc *gc, const char *path);
 _hidden int libxl__remove_file_or_directory(libxl__gc *gc, const char *path);
 
+
 _hidden char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array, int length);
 
 _hidden int libxl__xs_writev(libxl__gc *gc, xs_transaction_t t,
@@ -1534,6 +1536,38 @@ int libxl__openptys(libxl__openpty_state *op,
                     const struct winsize *winp);
 
 
+/*----- bootloader -----*/
+
+typedef struct libxl__bootloader_state libxl__bootloader_state;
+typedef void libxl__run_bootloader_callback(libxl__egc*,
+                                libxl__bootloader_state*, int rc);
+
+struct libxl__bootloader_state {
+    /* caller must fill these in, and they must all remain valid */
+    libxl__ao *ao;
+    libxl__run_bootloader_callback *callback;
+    libxl_domain_build_info *info; /* u.pv.{kernel,ramdisk,cmdline} updated */
+    libxl_device_disk *disk;
+    uint32_t domid;
+    /* private to libxl__run_bootloader */
+    char *outputpath, *outputdir;
+    char *diskpath; /* not from gc, represents actually attached disk */
+    libxl__openpty_state openpty;
+    libxl__openpty_result ptys[2];  /* [0] is for bootloader */
+    libxl__ev_child child;
+    int nargs, argsspace;
+    const char **args;
+    libxl__datacopier_state keystrokes, display;
+    int rc;
+};
+
+_hidden void libxl__bootloader_init(libxl__bootloader_state *bl);
+
+/* Will definitely call st->callback, perhaps reentrantly.
+ * If callback is passed rc==0, will have updated st->info appropriately */
+_hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st);
+
+
 /*
  * Convenience macros.
  */
Signature · 1 line
1.7.2.5
#25 lore raw

PATCH v6 11/20 libxl: make libxl_create_logfile const-correct

In reply to Ian Jackson
---
 tools/libxl/libxl_utils.c |    2 +-
 tools/libxl/libxl_utils.h |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_utils.c +1 -1
index 19b4615..f0d94c6 100644
@@ -193,7 +193,7 @@ static int logrename(libxl__gc *gc, const char *old, const char *new)
     return 0;
 }
 
-int libxl_create_logfile(libxl_ctx *ctx, char *name, char **full_name)
+int libxl_create_logfile(libxl_ctx *ctx, const char *name, char **full_name)
 {
     GC_INIT(ctx);
     struct stat stat_buf;
tools/libxl/libxl_utils.h +1 -1
index ca53a8a..2b47622 100644
@@ -26,7 +26,7 @@ int libxl_name_to_cpupoolid(libxl_ctx *ctx, const char *name, uint32_t *poolid);
 char *libxl_cpupoolid_to_name(libxl_ctx *ctx, uint32_t poolid);
 int libxl_get_stubdom_id(libxl_ctx *ctx, int guest_domid);
 int libxl_is_stubdom(libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid);
-int libxl_create_logfile(libxl_ctx *ctx, char *name, char **full_name);
+int libxl_create_logfile(libxl_ctx *ctx, const char *name, char **full_name);
 int libxl_string_to_backend(libxl_ctx *ctx, char *s, libxl_disk_backend *backend);
 
 int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,
Signature · 1 line
1.7.2.5
#26 lore raw

PATCH v6 12/20 libxl: log bootloader output

In reply to Ian Jackson

This involves adding a new log feature to libxl__datacopier, and then using it.

If the bootloader exits nonzero we print the log filename in a log message from libxl.

---
 tools/libxl/libxl_aoutils.c    |   10 ++++++++++
 tools/libxl/libxl_bootloader.c |   24 ++++++++++++++++++++++++
 tools/libxl/libxl_internal.h   |    3 ++-
 3 files changed, 36 insertions(+), 1 deletions(-)
tools/libxl/libxl_aoutils.c +10 -0
index 023de6e..9b30544 100644
@@ -117,6 +117,16 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
             libxl__ev_fd_deregister(gc, &dc->toread);
             break;
         }
+        if (dc->log) {
+            int wrote = fwrite(buf->buf + buf->used, 1, r, dc->log);
+            if (wrote != r) {
+                assert(ferror(dc->log));
+                assert(errno);
+                LOGE(ERROR, "error logging %s", dc->copywhat);
+                datacopier_callback(egc, dc, 0, errno);
+                return;
+            }
+        }
         buf->used += r;
         dc->used += r;
         assert(buf->used <= sizeof(buf->buf));
tools/libxl/libxl_bootloader.c +24 -0
index 6cccb6c..eaec46a 100644
@@ -234,6 +234,10 @@ static void bootloader_cleanup(libxl__egc *egc, libxl__bootloader_state *bl)
         libxl__carefd_close(bl->ptys[i].master);
         libxl__carefd_close(bl->ptys[i].slave);
     }
+    if (bl->display.log) {
+        fclose(bl->display.log);
+        bl->display.log = NULL;
+    }
 }
 
 static void bootloader_setpaths(libxl__gc *gc, libxl__bootloader_state *bl)
@@ -256,6 +260,8 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
 {
     STATE_AO_GC(bl->ao);
     libxl_domain_build_info *info = bl->info;
+    uint32_t domid = bl->domid;
+    char *logfile_tmp = NULL;
     int rc, r;
 
     libxl__bootloader_init(bl);
@@ -268,6 +274,22 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
 
     bootloader_setpaths(gc, bl);
 
+    const char *logfile_leaf = GCSPRINTF("bootloader.%"PRIu32, domid);
+    rc = libxl_create_logfile(CTX, logfile_leaf, &logfile_tmp);
+    if (rc) goto out;
+
+    /* Transfer ownership of log filename to bl and the gc */
+    bl->logfile = logfile_tmp;
+    libxl__ptr_add(gc, logfile_tmp);
+    logfile_tmp = NULL;
+
+    bl->display.log = fopen(bl->logfile, "a");
+    if (!bl->display.log) {
+        LOGE(ERROR, "failed to create bootloader logfile %s", bl->logfile);
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
     for (;;) {
         r = mkdir(bl->outputdir, 0600);
         if (!r) break;
@@ -305,6 +327,7 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
     return;
 
  out:
+    free(logfile_tmp);
     assert(rc);
     bootloader_callback(egc, bl, rc);
 }
@@ -462,6 +485,7 @@ static void bootloader_finished(libxl__egc *egc, libxl__ev_child *child,
     libxl__datacopier_kill(&bl->display);
 
     if (status) {
+        LOG(ERROR, "bootloader failed - consult logfile %s", bl->logfile);
         libxl_report_child_exitstatus(CTX, XTL_ERROR, "bootloader",
                                       pid, status);
         rc = ERROR_FAIL;
tools/libxl/libxl_internal.h +2 -1
index b3f84ba..4cfb8d5 100644
@@ -1488,6 +1488,7 @@ struct libxl__datacopier_state {
     int readfd, writefd;
     ssize_t maxsz;
     const char *copywhat, *readwhat, *writewhat; /* for error msgs */
+    FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
@@ -1550,7 +1551,7 @@ struct libxl__bootloader_state {
     libxl_device_disk *disk;
     uint32_t domid;
     /* private to libxl__run_bootloader */
-    char *outputpath, *outputdir;
+    char *outputpath, *outputdir, *logfile;
     char *diskpath; /* not from gc, represents actually attached disk */
     libxl__openpty_state openpty;
     libxl__openpty_result ptys[2];  /* [0] is for bootloader */
Signature · 1 line
1.7.2.5
#27 lore raw

PATCH v6 13/20 libxl: Allow AO_GC and EGC_GC even if not used

In reply to Ian Jackson

Mark the gc produced by AO_GC and EGC_GC with the gcc feature __attribute__((unused)). This allows the use of EGC_INIT and STATE_AO_GC by functions which do actually use the gc.

This is convenient because those functions might want to use the ao or egc, rather than the gc; and also because it means that functions which morally ought to be fishing any gc they use out of an egc or state structure can be written do so regardless of whether the gc is actually used right then.

---
 tools/libxl/libxl_internal.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_internal.h +2 -2
index 4cfb8d5..74dc2c5 100644
@@ -1280,7 +1280,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
 /* useful for all functions which take an egc: */
 
 #define EGC_GC                                  \
-    libxl__gc *const gc = &egc->gc
+    libxl__gc *const gc __attribute__((unused)) = &egc->gc
 
 /* egc initialisation and destruction: */
 
@@ -1383,7 +1383,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);
     })
 
 #define AO_GC                                   \
-    libxl__gc *const gc = &ao->gc
+    libxl__gc *const gc __attribute__((unused)) = &ao->gc
 
 #define STATE_AO_GC(op_ao)                      \
     libxl__ao *const ao = (op_ao);              \
Signature · 1 line
1.7.2.5
Acked-by on v6 13/20
#28 lore raw

Re: [PATCH 13/20] libxl: Allow AO_GC and EGC_GC even if not used

Quoted 11 lines · Ian Jackson

Mark the gc produced by AO_GC and EGC_GC with the gcc feature __attribute__((unused)). This allows the use of EGC_INIT and STATE_AO_GC by functions which do actually use the gc.

This is convenient because those functions might want to use the ao or egc, rather than the gc; and also because it means that functions which morally ought to be fishing any gc they use out of an egc or state structure can be written do so regardless of whether the gc is actually used right then.

Needed this for one of my patches (just sent, with a reference to this patch). So:

Quoted 26 lines
---
 tools/libxl/libxl_internal.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_internal.h +2 -2
index 4cfb8d5..74dc2c5 100644
@@ -1280,7 +1280,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
 /* useful for all functions which take an egc: */
 
 #define EGC_GC                                  \
-    libxl__gc *const gc = &egc->gc
+    libxl__gc *const gc __attribute__((unused)) = &egc->gc
 
 /* egc initialisation and destruction: */
 
@@ -1383,7 +1383,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);
     })
 
 #define AO_GC                                   \
-    libxl__gc *const gc = &ao->gc
+    libxl__gc *const gc __attribute__((unused)) = &ao->gc
 
 #define STATE_AO_GC(op_ao)                      \
     libxl__ao *const ao = (op_ao);              \
#29 lore raw

PATCH v6 14/20 libxl: remove ctx->waitpid_instead

In reply to Ian Jackson

Remove this obsolete hook. Callers inside libxl which create and reap children should use the mechanisms provided by the event system.

---
 tools/libxl/libxl_exec.c     |   12 +++---------
 tools/libxl/libxl_internal.h |    3 ---
 2 files changed, 3 insertions(+), 12 deletions(-)
tools/libxl/libxl_exec.c +3 -9
index b10e79f..2ee2154 100644
@@ -19,11 +19,6 @@
 
 #include "libxl_internal.h"
 
-static int call_waitpid(pid_t (*waitpid_cb)(pid_t, int *, int), pid_t pid, int *status, int options)
-{
-    return (waitpid_cb) ? waitpid_cb(pid, status, options) : waitpid(pid, status, options);
-}
-
 static void check_open_fds(const char *what)
 {
     const char *env_debug;
@@ -344,7 +339,7 @@ int libxl__spawn_spawn(libxl__gc *gc,
 
     if (!for_spawn) _exit(0); /* just detach then */
 
-    got = call_waitpid(ctx->waitpid_instead, child, &status, 0);
+    got = waitpid(child, &status, 0);
     assert(got == child);
 
     rc = (WIFEXITED(status) ? WEXITSTATUS(status) :
@@ -404,7 +399,7 @@ int libxl__spawn_detach(libxl__gc *gc,
                          (unsigned long)for_spawn->intermediate);
             abort(); /* things are very wrong */
         }
-        got = call_waitpid(ctx->waitpid_instead, for_spawn->intermediate, &status, 0);
+        got = waitpid(for_spawn->intermediate, &status, 0);
         assert(got == for_spawn->intermediate);
         if (!(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)) {
             report_spawn_intermediate_status(gc, for_spawn, status);
@@ -421,14 +416,13 @@ int libxl__spawn_detach(libxl__gc *gc,
 
 int libxl__spawn_check(libxl__gc *gc, libxl__spawn_starting *for_spawn)
 {
-    libxl_ctx *ctx = libxl__gc_owner(gc);
     pid_t got;
     int status;
 
     if (!for_spawn) return 0;
 
     assert(for_spawn->intermediate);
-    got = call_waitpid(ctx->waitpid_instead, for_spawn->intermediate, &status, WNOHANG);
+    got = waitpid(for_spawn->intermediate, &status, WNOHANG);
     if (!got) return 0;
 
     assert(got == for_spawn->intermediate);
tools/libxl/libxl_internal.h +0 -3
index 74dc2c5..ae71f70 100644
@@ -334,9 +334,6 @@ struct libxl__ctx {
     int sigchld_selfpipe[2]; /* [0]==-1 means handler not installed */
     LIBXL_LIST_HEAD(, libxl__ev_child) children;
 
-    /* This is obsolete and must be removed: */
-    int (*waitpid_instead)(pid_t pid, int *status, int flags);
-
     libxl_version_info version_info;
 };
 
Signature · 1 line
1.7.2.5
#30 lore raw

PATCH v6 15/20 libxl: change some structures to unit arrays

In reply to Ian Jackson

In the next patch these variables will turn into actual pointers.

To clarify that patch, prepare the ground by changing these variables from "struct foo var" to "struct foo var[1]". This enables accesses to them and their members to be made as if they were pointers.

No functional change.

---
 tools/libxl/libxl_create.c |   18 +++++-----
 tools/libxl/libxl_dm.c     |   84 ++++++++++++++++++++++----------------------
 2 files changed, 51 insertions(+), 51 deletions(-)
tools/libxl/libxl_create.c +9 -9
index dbc3cf0..8408c26 100644
@@ -543,7 +543,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     libxl__spawner_starting *dm_starting = 0;
-    libxl__domain_build_state state;
+    libxl__domain_build_state state[1];
     uint32_t domid;
     int i, ret;
 
@@ -585,12 +585,12 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         }
     }
 
-    memset(&state, 0, sizeof(state));
+    memset(state, 0, sizeof(*state));
 
     if ( restore_fd >= 0 ) {
-        ret = domain_restore(gc, &d_config->b_info, domid, restore_fd, &state);
+        ret = domain_restore(gc, &d_config->b_info, domid, restore_fd, state);
     } else {
-        ret = libxl__domain_build(gc, &d_config->b_info, domid, &state);
+        ret = libxl__domain_build(gc, &d_config->b_info, domid, state);
     }
 
     if (ret) {
@@ -628,7 +628,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         ret = init_console_info(&console, 0);
         if ( ret )
             goto error_out;
-        libxl__device_console_add(gc, domid, &console, &state);
+        libxl__device_console_add(gc, domid, &console, state);
         libxl__device_console_dispose(&console);
 
         libxl_device_vkb_init(&vkb);
@@ -636,7 +636,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         libxl_device_vkb_dispose(&vkb);
 
         ret = libxl__create_device_model(gc, domid, d_config,
-                                         &state, &dm_starting);
+                                         state, &dm_starting);
         if (ret < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                        "failed to create device model: %d", ret);
@@ -665,11 +665,11 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         if (need_qemu)
              console.consback = LIBXL__CONSOLE_BACKEND_IOEMU;
 
-        libxl__device_console_add(gc, domid, &console, &state);
+        libxl__device_console_add(gc, domid, &console, state);
         libxl__device_console_dispose(&console);
 
         if (need_qemu) {
-            libxl__create_xenpv_qemu(gc, domid, d_config, &state, &dm_starting);
+            libxl__create_xenpv_qemu(gc, domid, d_config, state, &dm_starting);
         }
         break;
     }
@@ -683,7 +683,7 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
             == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
             libxl__qmp_initializations(gc, domid, d_config);
         }
-        ret = libxl__confirm_device_model_startup(gc, &state, dm_starting);
+        ret = libxl__confirm_device_model_startup(gc, state, dm_starting);
         if (ret < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                        "device model did not start: %d", ret);
tools/libxl/libxl_dm.c +42 -42

144 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#31 lore raw

PATCH v6 16/20 libxl: ao: convert libxl__spawn_*

In reply to Ian Jackson

libxl__spawn_spawn becomes a callback-style asynchronous function. The implementation is now in terms of libxl__ev_* including libxl_ev_child.

All the callers need to be updated. This includes the device model spawning functions libxl__create_device_model and libxl__create_stubdom; these are replaced with libxl__spawn_local_dm and libxl__spawn_stubdom. libxl__confirm_device_model_startup is abolished; instead the dm spawner calls back.

(The choice of which kind of device model to create is lifted out of what used to be libxl__create_device_model, because that function was indirectly recursive. Recursive callback-style operations are clumsy because they require a pointer indirection for the nested states.)

Waiting for proper device model startup it is no longer notionally optional. Previously the code appeared to tolerate this by passing NULL for various libxl__spawner_starting* parameters to device model spawners. However, this was not used anywhere.

Conversely, the "for_spawn" parameter to libxl__wait_for_offspring is no longer supported. It remains as an unused formal parameter to avoid updating, in this patch, all the call sites which pass NULL. libxl__wait_for_offspring is in any case itself an obsolete function, so this wrinkle will go away when its callers are updated to use the event system. Consequently libxl__spawn_check is also abolished.

The "console ready" callback also remains unchanged in this patch. The API for this needs to be reviewed in the context of the event series and its reentrancy restrictions documented.

Thus their callers need to be updated. These are the domain creation functions libxl_domain_create_new and _restore. These functions now take ao_hows, and have a private state structure.

However domain creation remains not completely converted to the event mechanism; in particular it runs the outward-facing function libxl_run_bootloader with a NULL ao_how, which is quite wrong. As it happens in the current code this is not a bug because none of the rest of the functionality surrounding the bootloader call will mind if the event loop is reentered in the middle of its execution.

The file-scope function libxl__set_fd_flag which was used by the previous spawn arrangements becomes unused and is removed; other places in libxl can use libxl_fd_set_nonblock and libxl_fd_set_cloexec, which of course remain.

---
 tools/libxl/libxl.h          |   14 ++-
 tools/libxl/libxl_create.c   |  198 +++++++++++++++++++-----
 tools/libxl/libxl_dm.c       |  219 +++++++++++++++-----------
 tools/libxl/libxl_exec.c     |  354 +++++++++++++++++++++---------------------
 tools/libxl/libxl_internal.h |  286 +++++++++++++++++++++++-----------
 tools/libxl/xl_cmdimpl.c     |    6 +-
 6 files changed, 677 insertions(+), 400 deletions(-)
tools/libxl/libxl.h +12 -2
index 477b72a..6f59364 100644
@@ -465,8 +465,18 @@ int libxl_ctx_free(libxl_ctx *ctx /* 0 is OK */);
 
 /* domain related functions */
 typedef int (*libxl_console_ready)(libxl_ctx *ctx, uint32_t domid, void *priv);
-int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, libxl_console_ready cb, void *priv, uint32_t *domid);
-int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config, libxl_console_ready cb, void *priv, uint32_t *domid, int restore_fd);
+  /* fixme-ao   Need to review this API.  If we keep it, the reentrancy
+   * properties need to be documented but they may turn out to be too
+   * awkward */
+
+int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config,
+                            libxl_console_ready cb, void *priv, uint32_t *domid,
+                            const libxl_asyncop_how *ao_how);
+int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
+                                libxl_console_ready cb, void *priv,
+                                uint32_t *domid, int restore_fd,
+                                const libxl_asyncop_how *ao_how);
+
 void libxl_domain_config_dispose(libxl_domain_config *d_config);
 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
                           uint32_t domid, int fd);
tools/libxl/libxl_create.c +156 -42

263 lines not on this page · raw message

tools/libxl/libxl_dm.c +130 -89

312 lines not on this page · raw message

tools/libxl/libxl_exec.c +178 -176

441 lines not on this page · raw message

tools/libxl/libxl_internal.h +198 -88

335 lines not on this page · raw message

tools/libxl/xl_cmdimpl.c +3 -3
index c9e9943..9e66536 100644
@@ -1668,8 +1668,8 @@ start:
 
     if ( restore_file ) {
         ret = libxl_domain_create_restore(ctx, &d_config,
-                                            cb, &child_console_pid,
-                                            &domid, restore_fd);
+                                          cb, &child_console_pid,
+                                          &domid, restore_fd, 0);
         /*
          * On subsequent reboot etc we should create the domain, not
          * restore/migrate-receive it again.
@@ -1677,7 +1677,7 @@ start:
         restore_file = NULL;
     }else{
         ret = libxl_domain_create_new(ctx, &d_config,
-                                        cb, &child_console_pid, &domid);
+                                      cb, &child_console_pid, &domid, 0);
     }
     if ( ret )
         goto error_out;
Signature · 1 line
1.7.2.5
#32 lore raw

PATCH v6 17/20 libxl: make libxl_create run bootloader via callback

In reply to Ian Jackson

Change initiate_domain_create to properly use libxl__bootloader_run rather than improperly calling libxl_run_bootloader with NULL ao_how.

---
 tools/libxl/libxl_create.c   |   53 +++++++++++++++++++++++++++++++----------
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 41 insertions(+), 13 deletions(-)
tools/libxl/libxl_create.c +40 -13
index 09a03a7..156c5c7 100644
@@ -550,6 +550,9 @@ static int store_libxl_entry(libxl__gc *gc, uint32_t domid,
 static void domcreate_devmodel_started(libxl__egc *egc,
                                        libxl__dm_spawn_state *dmss,
                                        int rc);
+static void domcreate_bootloader_done(libxl__egc *egc,
+                                      libxl__bootloader_state *bl,
+                                      int rc);
 
 /* Our own function to clean up and call the user's callback.
  * The final call in the sequence. */
@@ -570,6 +573,7 @@ static void initiate_domain_create(libxl__egc *egc,
     int restore_fd = dcs->restore_fd;
     libxl_console_ready cb = dcs->console_cb;
     void *priv = dcs->console_cb_priv;
+    memset(&dcs->build_state, 0, sizeof(dcs->build_state));
 
     domid = 0;
 
@@ -602,18 +606,40 @@ static void initiate_domain_create(libxl__egc *egc,
     libxl_device_disk *bootdisk =
         d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
 
-    if (restore_fd < 0 && bootdisk) {
-        ret = libxl_run_bootloader(ctx, &d_config->b_info, bootdisk, domid,
-                                   0 /* fixme-ao */);
-        if (ret) {
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
-                       "failed to run bootloader: %d", ret);
-            goto error_out;
-        }
+    if (restore_fd < 0 && dcs->bl.disk) {
+        dcs->bl.ao = ao;
+        dcs->bl.disk = bootdisk;
+        dcs->bl.callback = domcreate_bootloader_done;
+        dcs->bl.info = &d_config->b_info,
+            
+        libxl__bootloader_run(egc, &dcs->bl);
+    } else {
+        domcreate_bootloader_done(egc, &dcs->bl, 0);
     }
+    return;
 
-    memset(&dcs->build_state, 0, sizeof(dcs->build_state));
+error_out:
+    assert(ret);
+    domcreate_complete(egc, dcs, ret);
+}
+
+static void domcreate_bootloader_done(libxl__egc *egc,
+                                      libxl__bootloader_state *bl,
+                                      int ret)
+{
+    libxl__domain_create_state *dcs = CONTAINER_OF(bl, *dcs, bl);
+    STATE_AO_GC(bl->ao);
+    int i;
+
+    /* convenience aliases */
+    uint32_t domid = dcs->guest_domid;
+    libxl_domain_config *d_config = dcs->guest_config;
+    int restore_fd = dcs->restore_fd;
     libxl__domain_build_state *state = &dcs->build_state;
+    libxl_ctx *ctx = CTX;
+
+    if (ret) goto error_out;
+
     dcs->dmss.spawn.ao = ao;
     dcs->dmss.guest_config = dcs->guest_config;
     dcs->dmss.build_state = &dcs->build_state;
@@ -764,12 +790,13 @@ static void domcreate_devmodel_started(libxl__egc *egc,
 
     if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV &&
         libxl_defbool_val(d_config->b_info.u.pv.e820_host)) {
-        int rc;
-        rc = libxl__e820_alloc(gc, domid, d_config);
-        if (rc)
+        ret = libxl__e820_alloc(gc, domid, d_config);
+        if (ret) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
                       "Failed while collecting E820 with: %d (errno:%d)\n",
-                      rc, errno);
+                      ret, errno);
+            goto error_out;
+        }
     }
     if ( cb && (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM ||
                 (d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV &&
tools/libxl/libxl_internal.h +1 -0
index 5bab4d6..56c3eec 100644
@@ -1669,6 +1669,7 @@ struct libxl__domain_create_state {
     /* private to domain_create */
     int guest_domid;
     libxl__domain_build_state build_state;
+    libxl__bootloader_state bl;
     union {
         libxl__dm_spawn_state dmss;
         libxl__stubdom_spawn_state sdss;
Signature · 1 line
1.7.2.5
#33 lore raw

PATCH v6 18/20 libxl: provide progress reporting for long-running operations

In reply to Ian Jackson

This will be used for reporting, during domain creation, that the console is ready.

---
 tools/libxl/libxl.h          |   45 +++++++++++++++
 tools/libxl/libxl_event.c    |  122 ++++++++++++++++++++++++++++++++----------
 tools/libxl/libxl_internal.h |   28 ++++++++++
 3 files changed, 167 insertions(+), 28 deletions(-)
tools/libxl/libxl.h +45 -0
index 6f59364..1bfe684 100644
@@ -435,6 +435,51 @@ typedef struct {
     } u;
 } libxl_asyncop_how;
 
+/*
+ * Some more complex asynchronous operations can report intermediate
+ * progress.  How this is to be reported is controlled, for each
+ * function, by a parameter
+ *    libxl_asyncprogress_how *aop_FOO_how;
+ * for each kind of progress FOO supported by that function.  Each
+ * such kind of progress is associated with an event type.
+ *
+ * The function description will document whether, when, and how
+ * many times, the intermediate progress will be reported, and
+ * what the corresponding event type(s) are.
+ *
+ * If aop_FOO_how==NULL, intermediate progress reports are discarded.
+ *
+ * If aop_FOO_how->callback==NULL, intermediate progress reports
+ * generate libxl events which can be obtained from libxl_event_wait
+ * or libxl_event_check.
+ *
+ * If aop_FOO_how->callback!=NULL, libxl will report intermediate
+ * progress by calling callback(ctx, &event, for_callback).
+ *
+ * The rules for these events are otherwise the same as those for
+ * ordinary events.  The reentrancy and threading rules for the
+ * callback are the same as those for ao completion callbacks.
+ *
+ * Note that the callback, if provided, is responsible for freeing
+ * the event.
+ *
+ * If callbacks are requested, they will be made, and returned, before
+ * the long-running libxl operation is considered finished (so if the
+ * long-running libxl operation was invoked with ao_how==NULL then any
+ * callbacks will occur strictly before the long-running operation
+ * returns).  However, the callbacks may occur on any thread.
+ *
+ * In general, otherwise, no promises are made about the relative
+ * order of callbacks in a multithreaded program.  In particular
+ * different callbacks relating to the same long-running operation may
+ * be delivered out of order.
+ */
+
+typedef struct {
+    void (*callback)(libxl_ctx *ctx, libxl_event*, void *for_callback);
+    libxl_ev_user for_event; /* always used */
+    void *for_callback; /* passed to callback */
+} libxl_asyncprogress_how;
 
 #define LIBXL_VERSION 0
 
tools/libxl/libxl_event.c +94 -28

158 lines not on this page · raw message

tools/libxl/libxl_internal.h +28 -0
index 56c3eec..4e97f5e 100644
@@ -118,6 +118,7 @@ _hidden void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
 typedef struct libxl__gc libxl__gc;
 typedef struct libxl__egc libxl__egc;
 typedef struct libxl__ao libxl__ao;
+typedef struct libxl__aop_occurred libxl__aop_occurred;
 
 void libxl__alloc_failed(libxl_ctx *, const char *func,
                          size_t nmemb, size_t size) __attribute__((noreturn));
@@ -372,6 +373,14 @@ struct libxl__egc {
     struct libxl__gc gc;
     struct libxl__event_list occurred_for_callback;
     LIBXL_TAILQ_HEAD(, libxl__ao) aos_for_callback;
+    LIBXL_TAILQ_HEAD(, libxl__aop_occurred) aops_for_callback;
+};
+
+struct libxl__aop_occurred {
+    LIBXL_TAILQ_ENTRY(libxl__aop_occurred) entry;
+    libxl__ao *ao;
+    libxl_event *ev;
+    const libxl_asyncprogress_how *how;
 };
 
 #define LIBXL__AO_MAGIC              0xA0FACE00ul
@@ -380,6 +389,7 @@ struct libxl__egc {
 struct libxl__ao {
     uint32_t magic;
     unsigned constructing:1, in_initiator:1, complete:1, notified:1;
+    int progress_reports_outstanding;
     int rc;
     libxl__gc gc;
     libxl_asyncop_how how;
@@ -1369,6 +1379,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
         LIBXL_INIT_GC((egc).gc,ctx);                    \
         LIBXL_TAILQ_INIT(&(egc).occurred_for_callback); \
         LIBXL_TAILQ_INIT(&(egc).aos_for_callback);      \
+        LIBXL_TAILQ_INIT(&(egc).aops_for_callback);     \
     } while(0)
 
 _hidden void libxl__egc_cleanup(libxl__egc *egc);
@@ -1415,6 +1426,9 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);
  *        pointer to the internal event generation request routines
  *        libxl__evgen_FOO, so that at some point a CALLBACK will be
  *        made when the operation is complete.
+ *      - if the operation provides progress reports, the aop_how(s)
+ *        must be copied into the per-operation structure using
+ *        libxl__ao_progress_gethow.
  *
  * - If initiation is successful, the initiating function needs
  *   to run libxl__ao_inprogress right before unlocking and
@@ -1424,6 +1438,10 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);
  *   call libxl__ao_abort before unlocking and returning whatever
  *   error code is appropriate (AO_ABORT macro).
  *
+ * - If the operation supports progress reports, it may generate
+ *   suitable events with NEW_EVENT and report them with
+ *   libxl__ao_progress_report (with the ctx locked).
+ *
  * - Later, some callback function, whose callback has been requested
  *   directly or indirectly, should call libxl__ao_complete (with the
  *   ctx locked, as it will generally already be in any event callback
@@ -1479,8 +1497,18 @@ _hidden int libxl__ao_inprogress(libxl__ao *ao); /* temporarily unlocks */
 _hidden void libxl__ao_abort(libxl__ao *ao);
 _hidden void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc);
 
+/* Can be called at any time.  Use is essential for any aop user. */
+_hidden void libxl__ao_progress_gethow(libxl_asyncprogress_how *in_state,
+                                       const libxl_asyncprogress_how *from_app);
+
+/* Must be called with the ctx locked.  Will fill in ev->for_user,
+ * so caller need not do that. */
+_hidden void libxl__ao_progress_report(libxl__egc *egc, libxl__ao *ao,
+   const libxl_asyncprogress_how *how, libxl_event *ev /* consumed */);
+
 /* For use by ao machinery ONLY */
 _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);
+_hidden void libxl__ao_complete_check_progress_reports(libxl__egc*, libxl__ao*);
 
 
 /*
Signature · 1 line
1.7.2.5
#34 lore raw

PATCH v6 19/20 libxl: remove malloc failure handling from NEW_EVENT

In reply to Ian Jackson

Change to use libxl__zalloc, where allocation failure is fatal.

Also remove a spurious semicolon from the helper macro.

---
 tools/libxl/libxl.c          |    2 --
 tools/libxl/libxl_event.c    |    8 +-------
 tools/libxl/libxl_internal.h |    4 ++--
 3 files changed, 3 insertions(+), 11 deletions(-)
tools/libxl/libxl.c +0 -2
index 54f3813..b7f665a 100644
@@ -779,7 +779,6 @@ static void domain_death_occurred(libxl__egc *egc,
     *evg_upd = evg_next;
 
     libxl_event *ev = NEW_EVENT(egc, DOMAIN_DEATH, evg->domid);
-    if (!ev) return;
 
     libxl__event_occurred(egc, ev);
 
@@ -866,7 +865,6 @@ static void domain_death_xswatch_callback(libxl__egc *egc, libxl__ev_xswatch *w,
             if (!evg->shutdown_reported &&
                 (got->flags & XEN_DOMINF_shutdown)) {
                 libxl_event *ev = NEW_EVENT(egc, DOMAIN_SHUTDOWN, got->domain);
-                if (!ev) goto out;
                 
                 LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, " shutdown reporting");
 
tools/libxl/libxl_event.c +1 -7
index f355e3d..7600aa4 100644
@@ -980,13 +980,7 @@ libxl_event *libxl__event_new(libxl__egc *egc,
 {
     libxl_event *ev;
 
-    ev = malloc(sizeof(*ev));
-    if (!ev) {
-        LIBXL__EVENT_DISASTER(egc, "allocate new event", errno, type);
-        return NULL;
-    }
-
-    memset(ev, 0, sizeof(*ev));
+    ev = libxl__zalloc(0,sizeof(*ev));
     ev->type = type;
     ev->domid = domid;
 
tools/libxl/libxl_internal.h +2 -2
index 4e97f5e..977db2a 100644
@@ -641,10 +641,10 @@ _hidden libxl_event *libxl__event_new(libxl__egc*, libxl_event_type,
                                       uint32_t domid);
   /* Convenience function.
    * Allocates a new libxl_event, fills in domid and type.
-   * If allocation fails, calls _disaster, and returns NULL. */
+   * Cannot fail. */
 
 #define NEW_EVENT(egc, type, domid)                              \
-    libxl__event_new((egc), LIBXL_EVENT_TYPE_##type, (domid));
+    libxl__event_new((egc), LIBXL_EVENT_TYPE_##type, (domid))
     /* Convenience macro. */
 
 /*
Signature · 1 line
1.7.2.5
#35 lore raw

PATCH v6 20/20 libxl: convert console callback to libxl_asyncprogress_how

In reply to Ian Jackson

Remove the old console callback. (Its reentrancy properties were
troublesome: in principle, the event loop might be reentered during the callback, and the libxl__domain_create_state swept out from under the feet of the domain creation.)

As a side effect of the new code arrangements, the console callback for non-bootloader-using PV guests now occurs near the end of domain creation, in the same place as for HVM guests, rather than near the start.

The new arrangements should in principle mean that by the time the console is described as ready by the callback, the xenstore key is indeed ready. So in the future the timeout might be removed from the console client.

Cc Stefano Stabellini <[email protected]>
---
 tools/libxl/libxl.h            |   17 ++++++-----
 tools/libxl/libxl_bootloader.c |    3 ++
 tools/libxl/libxl_create.c     |   59 +++++++++++++++++++++++-----------------
 tools/libxl/libxl_internal.h   |    6 +++-
 tools/libxl/libxl_types.idl    |    2 +
 tools/libxl/xl_cmdimpl.c       |   25 ++++++++++-------
 6 files changed, 67 insertions(+), 45 deletions(-)
tools/libxl/libxl.h +9 -8
index 1bfe684..de8d1872 100644
@@ -509,18 +509,19 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
 int libxl_ctx_free(libxl_ctx *ctx /* 0 is OK */);
 
 /* domain related functions */
-typedef int (*libxl_console_ready)(libxl_ctx *ctx, uint32_t domid, void *priv);
-  /* fixme-ao   Need to review this API.  If we keep it, the reentrancy
-   * properties need to be documented but they may turn out to be too
-   * awkward */
 
 int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config,
-                            libxl_console_ready cb, void *priv, uint32_t *domid,
-                            const libxl_asyncop_how *ao_how);
+                            uint32_t *domid,
+                            const libxl_asyncop_how *ao_how,
+                            const libxl_asyncprogress_how *aop_console_how);
 int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
-                                libxl_console_ready cb, void *priv,
                                 uint32_t *domid, int restore_fd,
-                                const libxl_asyncop_how *ao_how);
+                                const libxl_asyncop_how *ao_how,
+                                const libxl_asyncprogress_how *aop_console_how);
+  /* A progress report will be made via ao_console_how, of type
+   * domain_create_console_available, when the domain's primary
+   * console is available and can be connected to.
+   */
 
 void libxl_domain_config_dispose(libxl_domain_config *d_config);
 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
tools/libxl/libxl_bootloader.c +3 -0
index eaec46a..3f6f129 100644
@@ -375,6 +375,9 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
         goto out;
     }
 
+    if (bl->console_available)
+        bl->console_available(egc, bl);
+
     int bootloader_master = libxl__carefd_fd(bl->ptys[0].master);
     int xenconsole_master = libxl__carefd_fd(bl->ptys[1].master);
 
tools/libxl/libxl_create.c +34 -25

131 lines not on this page · raw message

tools/libxl/libxl_internal.h +4 -2
index 977db2a..b2eefd6 100644
@@ -1651,11 +1651,14 @@ int libxl__openptys(libxl__openpty_state *op,
 typedef struct libxl__bootloader_state libxl__bootloader_state;
 typedef void libxl__run_bootloader_callback(libxl__egc*,
                                 libxl__bootloader_state*, int rc);
+typedef void libxl__bootloader_console_callback(libxl__egc*,
+                                libxl__bootloader_state*);
 
 struct libxl__bootloader_state {
     /* caller must fill these in, and they must all remain valid */
     libxl__ao *ao;
     libxl__run_bootloader_callback *callback;
+    libxl__bootloader_console_callback *console_available;
     libxl_domain_build_info *info; /* u.pv.{kernel,ramdisk,cmdline} updated */
     libxl_device_disk *disk;
     uint32_t domid;
@@ -1691,8 +1694,7 @@ struct libxl__domain_create_state {
     libxl__ao *ao;
     libxl_domain_config *guest_config;
     int restore_fd;
-    libxl_console_ready console_cb;
-    void *console_cb_priv;
+    libxl_asyncprogress_how aop_console_how;
     libxl__domain_create_cb *callback;
     /* private to domain_create */
     int guest_domid;
tools/libxl/libxl_types.idl +2 -0
index 5cf9708..f28d785 100644
@@ -444,6 +444,7 @@ libxl_event_type = Enumeration("event_type", [
     (2, "DOMAIN_DEATH"),
     (3, "DISK_EJECT"),
     (4, "OPERATION_COMPLETE"),
+    (5, "DOMAIN_CREATE_CONSOLE_AVAILABLE"),
     ])
 
 libxl_ev_user = UInt(64)
@@ -469,4 +470,5 @@ libxl_event = Struct("event",[
            ("operation_complete", Struct(None, [
                                         ("rc", integer),
                                  ])),
+           ("domain_create_console_available", Struct(None, [])),
            ]))])
tools/libxl/xl_cmdimpl.c +15 -10
index 9e66536..ce52599 100644
@@ -1430,16 +1430,18 @@ static int freemem(libxl_domain_build_info *b_info)
     return ERROR_NOMEM;
 }
 
-static int autoconnect_console(libxl_ctx *ctx, uint32_t domid, void *priv)
+static void autoconnect_console(libxl_ctx *ctx, libxl_event *ev, void *priv)
 {
     pid_t *pid = priv;
 
+    libxl_event_free(ctx, ev);
+
     *pid = fork();
     if (*pid < 0) {
         perror("unable to fork xenconsole");
-        return ERROR_FAIL;
+        return;
     } else if (*pid > 0)
-        return 0;
+        return;
 
     postfork();
 
@@ -1506,7 +1508,7 @@ static int create_domain(struct domain_create *dom_info)
     int config_len = 0;
     int restore_fd = -1;
     int status = 0;
-    libxl_console_ready cb;
+    const libxl_asyncprogress_how *autoconnect_console_how;
     pid_t child_console_pid = -1;
     struct save_file_header hdr;
 
@@ -1660,24 +1662,27 @@ start:
         goto error_out;
     }
 
+    libxl_asyncprogress_how autoconnect_console_how_buf;
     if ( dom_info->console_autoconnect ) {
-        cb = autoconnect_console;
+        autoconnect_console_how_buf.callback = autoconnect_console;
+        autoconnect_console_how_buf.for_callback = &child_console_pid;
+        autoconnect_console_how = &autoconnect_console_how_buf;
     }else{
-        cb = NULL;
+        autoconnect_console_how = 0;
     }
 
     if ( restore_file ) {
         ret = libxl_domain_create_restore(ctx, &d_config,
-                                          cb, &child_console_pid,
-                                          &domid, restore_fd, 0);
+                                          &domid, restore_fd,
+                                          0, autoconnect_console_how);
         /*
          * On subsequent reboot etc we should create the domain, not
          * restore/migrate-receive it again.
          */
         restore_file = NULL;
     }else{
-        ret = libxl_domain_create_new(ctx, &d_config,
-                                      cb, &child_console_pid, &domid, 0);
+        ret = libxl_domain_create_new(ctx, &d_config, &domid,
+                                      0, autoconnect_console_how);
     }
     if ( ret )
         goto error_out;
Signature · 1 line
1.7.2.5