libxl: child process handling
v9 cover libxl: child process handling
All of these patches have now been acked and tested. Thanks are due to to Ian Campbell and Roger Pau Monne. I am posting them in their final form and will apply them very shortly.
01/27 libxl: handle POLLERR, POLLHUP, POLLNVAL properly 02/27 libxl: support multiple libxl__ev_fds for the same fd 03/27 libxl: event API: new facilities for waiting for subprocesses 04/27 autoconf: trim the configure script; use autoheader 05/27 autoconf: New test for openpty et al.
06/27 libxl: provide libxl__remove_file et al.
07/27 libxl: Introduce libxl__sendmsg_fds and libxl__recvmsg_fds 08/27 libxl: Clean up setdefault in do_domain_create 09/27 libxl: provide libxl__datacopier_*
10/27 libxl: provide libxl__openpty_*
11/27 libxl: ao: Convert libxl_run_bootloader
12/27 libxl: make libxl_create_logfile const-correct 13/27 libxl: log bootloader output
14/27 libxl: Allow AO_GC and EGC_GC even if not used 15/27 libxl: add a dummy ao_how to libxl_domain_core_dump 16/27 libxl: remove ctx->waitpid_instead
17/27 libxl: change some structures to unit arrays
18/27 libxl: ao: convert libxl__spawn_*
19/27 libxl: set guest_domid even if libxl__domain_make fails 20/27 libxl: make libxl_create run bootloader via callback 21/27 libxl: Fix an ao completion bug; document locking policy 22/27 libxl: provide progress reporting for long-running operations 23/27 libxl: remove malloc failure handling from NEW_EVENT 24/27 libxl: convert console callback to libxl_asyncprogress_how 25/27 libxl: clarify definition of "slow" operation 26/27 libxl: child processes cleanups
27/27 libxl: abort bootloader invocation when domain dies
PATCH v9 1/27 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
@@ -339,6 +339,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) {
@@ -743,7 +746,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
@@ -128,6 +128,11 @@ _hidden 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
PATCH v9 2/27 libxl: support multiple libxl__ev_fds for the same fd
In reply to Ian JacksonWe 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.
Changes since v6:
* Fix typo
--- 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
@@ -639,10 +639,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 element in the rindices is three indices +* into the fd array (for POLLIN, POLLPRI and POLLOUT). */ if (*nfds_io) {
@@ -663,14 +664,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; } }
@@ -681,8 +684,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++; });
@@ -710,7 +715,6 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller, *timeout_upd = our_timeout; } - out:return rc; }
@@ -732,24 +736,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; }
@@ -1013,7 +1021,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) {
@@ -1040,7 +1048,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
@@ -132,7 +132,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: */
@@ -248,8 +252,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
PATCH v9 3/27 libxl: event API: new facilities for waiting for subprocesses
In reply to Ian JacksonThe 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
@@ -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
@@ -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
PATCH v9 4/27 autoconf: trim the configure script; use autoheader
In reply to Ian JacksonRemove a lot of unnecessary tests. Specifically, we no longer test for standard POSIX facilities which we expect to be provided everywhere and which we don't in any case have any alternative for.
Switch to generating config.h.in with autoheader.
Changes since v7:
* Removed AX_CHECK_PTYFUNCS (snuck in from previous patch)
--- autogen.sh | 1 + tools/config.h.in | 73 +- tools/configure | 8825 +++++++++++++++++----------------------------------- tools/configure.ac | 60 +- 4 files changed, 2981 insertions(+), 5978 deletions(-)
autogen.sh +1 -0
@@ -1,3 +1,4 @@ #!/bin/sh -e cd tools autoconf +autoheader
tools/config.h.in +59 -14
78 lines not on this page · raw message
tools/configure +3003 -5988
2,918 lines not on this page · raw message
tools/configure.ac +1 -59
60 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 7 lines
1.7.2.5 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Part of the diff is not shown here. View the raw message.
PATCH v9 5/27 autoconf: New test for openpty et al.
In reply to Ian JacksonWe 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.
Changes since v7:
* Actually include the call to AX_CHECK_PTYFUNCS in this patch, not the previous one, and regenerate configure accordingly.
Changes since v6:
* Put failure macro call in correct place so it might actually happen.
* Try both with -lutil and without.
* Patch now contains update for config.h.in.
--- config/Tools.mk.in | 2 + tools/config.h.in | 3 ++ tools/configure | 61 ++++++++++++++++++++++++++++++++++++++++ tools/configure.ac | 2 + tools/libxl/Makefile | 2 +- tools/libxl/libxl_bootloader.c | 4 ++ tools/m4/ptyfuncs.m4 | 28 ++++++++++++++++++ 7 files changed, 101 insertions(+), 1 deletions(-) create mode 100644 tools/m4/ptyfuncs.m4
config/Tools.mk.in +2 -0
@@ -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/config.h.in +3 -0
@@ -42,6 +42,9 @@ /* Define curses header to use */ #undef INCLUDE_CURSES_H +/* libutil header file name */ +#undef INCLUDE_LIBUTIL_H + /* Define to the address where bug reports for this package should be sent. */ #undef PACKAGE_BUGREPORT
tools/configure +61 -0
79 lines not on this page · raw message
tools/configure.ac +2 -0
@@ -32,6 +32,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([yajl], [yajl_alloc], [], [AC_MSG_ERROR([Could not find yajl])]) AC_CHECK_LIB([z], [deflateCopy], [], [AC_MSG_ERROR([Could not find zlib])])
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) CFLAGS += $(PTHREAD_CFLAGS) LDFLAGS += $(PTHREAD_LDFLAGS)
tools/libxl/libxl_bootloader.c +4 -0
@@ -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 +28 -0
@@ -0,0 +1,28 @@ +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], [ + for ax_cv_ptyfuncs_libs in -lutil "" NOT_FOUND; do + if test "x$ax_cv_ptyfuncs_libs" = "xNOT_FOUND"; then + AC_MSG_FAILURE([Unable to find library for openpty and login_tty]) + fi + 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); +} +],[ + break + ],[]) + AX_SAVEVAR_RESTORE(LIBS) + done + ]) + PTYFUNCS_LIBS="$ax_cv_ptyfuncs_libs" + AC_SUBST(PTYFUNCS_LIBS) +])
Signature · 1 line
1.7.2.5
PATCH v9 6/27 libxl: provide libxl__remove_file et al.
In reply to Ian JacksonThese 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
@@ -433,6 +433,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
PATCH v9 7/27 libxl: Introduce libxl__sendmsg_fds and libxl__recvmsg_fds
In reply to Ian JacksonProvide functions for sending and receiving fds.
There used to be fd-sending functionality in libxl_qmp.c before 25298:84ae90427c54, which this implementation is partially derived from.
--- tools/libxl/libxl_internal.h | 11 ++++ tools/libxl/libxl_utils.c | 104 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +11 -0
@@ -1135,6 +1135,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_utils.c +104 -0
110 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v9 8/27 libxl: Clean up setdefault in do_domain_create
In reply to Ian Jacksondo_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
@@ -573,9 +573,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
PATCH v9 9/27 libxl: provide libxl__datacopier_*
In reply to Ian JacksonGeneral facility for ao operations to shovel data between fds.
This will be used by the bootloader machinery.
Changes in v7:
* assert that the ao is non-null on _init.
--- tools/libxl/Makefile | 3 +- tools/libxl/libxl_aoutils.c | 189 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 40 +++++++++ 3 files changed, 231 insertions(+), 1 deletions(-) create mode 100644 tools/libxl/libxl_aoutils.c
tools/libxl/Makefile +2 -1
@@ -64,7 +64,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 +189 -0
189 lines not on this page · raw message
tools/libxl/libxl_internal.h +40 -0
@@ -840,6 +840,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);
@@ -1473,6 +1474,45 @@ _hidden const char *libxl__xen_script_dir_path(void); _hidden const char *libxl__lock_dir_path(void); _hidden const char *libxl__run_dir_path(void); +/*----- 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
PATCH v9 10/27 libxl: provide libxl__openpty_*
In reply to Ian JacksonGeneral 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
@@ -187,3 +187,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
@@ -1513,6 +1513,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
PATCH v9 11/27 libxl: ao: Convert libxl_run_bootloader
In reply to Ian JacksonConvert 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.
Changes since v6:
* Use libxl__ev_child_inuse rather than testing pid directly.
* Fix a code style error.
* Properly initialise the sub-operations' aos in _init.
* Bugfixes.
--- tools/libxl/libxl.c | 4 + tools/libxl/libxl.h | 3 +- tools/libxl/libxl_bootloader.c | 713 +++++++++++++++++++++------------------- tools/libxl/libxl_create.c | 8 +- tools/libxl/libxl_internal.h | 34 ++ 5 files changed, 419 insertions(+), 343 deletions(-)
tools/libxl/libxl.c +4 -0
@@ -1750,6 +1750,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
@@ -495,7 +495,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 +373 -340
832 lines not on this page · raw message
tools/libxl/libxl_create.c +6 -2
@@ -593,8 +593,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
@@ -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>
@@ -440,6 +441,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,
@@ -1549,6 +1551,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
PATCH v9 12/27 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
@@ -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
@@ -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
PATCH v9 13/27 libxl: log bootloader output
In reply to Ian JacksonThis 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
@@ -118,6 +118,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
@@ -236,6 +236,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)
@@ -258,6 +262,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);
@@ -269,6 +275,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 ;
@@ -308,6 +330,7 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl) out:assert (rc); out_ok: +free (logfile_tmp);bootloader_callback (egc, bl, rc); }
@@ -465,6 +488,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
@@ -1503,6 +1503,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;
@@ -1565,7 +1566,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
PATCH v9 14/27 libxl: Allow AO_GC and EGC_GC even if not used
In reply to Ian JacksonMark 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
@@ -1287,7 +1287,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: */
@@ -1390,7 +1390,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
PATCH v9 15/27 libxl: add a dummy ao_how to libxl_domain_core_dump
In reply to Ian JacksonFrom: Ian Campbell <[email protected]>
Although this function is not currently slow it may become so in the future (this also depends somewhat on the size of the guest). Therefore arrange for it to take an ao_how which it completes immediately. This will allow us to make it asynchronous in the future without breaking API compatibility.
--- tools/libxl/libxl.c | 18 ++++++++++++++---- tools/libxl/libxl.h | 4 +++- tools/libxl/xl_cmdimpl.c | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-)
tools/libxl/libxl.c +14 -4
@@ -642,16 +642,26 @@ int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid) }int libxl_domain_core_dump (libxl_ctx *ctx,uint32_t domid, -const char *filename) +const char *filename, +const libxl_asyncop_how *ao_how) { -int ret; +AO_CREATE (ctx, domid, ao_how); +int ret, rc; + ret =xc_domain_dumpcore (ctx->xch, domid, filename);if (ret<0 ) {LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"core dumping domain %d to %s" , domid, filename); -return ERROR_FAIL ; + rc =ERROR_FAIL ; + goto out; } -return 0 ; + + rc =0 ; +out: + +libxl__ao_complete (egc, ao, rc); + +return AO_INPROGRESS ; }int libxl_domain_unpause (libxl_ctx *ctx,uint32_t domid)
tools/libxl/libxl.h +3 -1
@@ -511,7 +511,9 @@ int libxl_domain_rename(libxl_ctx *ctx, uint32_t domid,int libxl_domain_pause (libxl_ctx *ctx,uint32_t domid);int libxl_domain_unpause (libxl_ctx *ctx,uint32_t domid); -int libxl_domain_core_dump (libxl_ctx *ctx,uint32_t domid,const char *filename); +int libxl_domain_core_dump (libxl_ctx *ctx,uint32_t domid, +const char *filename, +const libxl_asyncop_how *ao_how);int libxl_domain_setmaxmem (libxl_ctx *ctx,uint32_t domid,uint32_t target_memkb);int libxl_set_memory_target (libxl_ctx *ctx,uint32_t domid,int32_t target_memkb,int relative,int enforce);
tools/libxl/xl_cmdimpl.c +2 -2
@@ -1306,7 +1306,7 @@ static int handle_domain_death(libxl_ctx *ctx, uint32_t domid,LOG ("failed to construct core dump path" ); }else {LOG ("dumping core to %s" , corefile); - rc=libxl_domain_core_dump (ctx, domid, corefile); + rc=libxl_domain_core_dump (ctx, domid, corefile,NULL );if (rc)LOG ("core dump failed (rc=%d)." , rc); }/* No point crying over spilled milk, continue on failure. */
@@ -2927,7 +2927,7 @@ static void core_dump_domain(const char *domain_spec, const char *filename) {int rc;find_domain (domain_spec); - rc=libxl_domain_core_dump (ctx, domid, filename); + rc=libxl_domain_core_dump (ctx, domid, filename,NULL );if (rc) {fprintf (stderr,"core dump failed (rc=%d)\n" ,rc);exit (-1 ); } }
Signature · 1 line
1.7.2.5
PATCH v9 16/27 libxl: remove ctx->waitpid_instead
In reply to Ian JacksonRemove this obsolete hook. Callers inside libxl which create and reap children should use the mechanisms provided by the event system.
(This has no functional difference since there is no way for ctx->waitpid_instead ever to become set.)
--- 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
@@ -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
@@ -325,9 +325,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
PATCH v9 17/27 libxl: change some structures to unit arrays
In reply to Ian JacksonIn 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
@@ -564,7 +564,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;
@@ -606,12 +606,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) {
@@ -649,7 +649,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);
@@ -657,7 +657,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);
@@ -683,11 +683,11 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config, d_config->num_vfbs, d_config->vfbs, d_config->num_disks, &d_config->disks[0]); -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 ; }
@@ -701,7 +701,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
PATCH v9 18/27 libxl: ao: convert libxl__spawn_*
In reply to Ian Jacksonlibxl__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.
Changes since v8:
* Make midproc_cb callback with correct pid (that of the grandchild, not "middle" which is zero). Reported by Roger Pau Monne.
Changes since v7:
* Rename libxl__spawn_stubdom to libxl__spawn_stub_dm (and ..._state); rename the state's stubdom_* members to dm_*.
* Eliminate the union between the two dm creation states in libxl__domain_create_state. Instead, the domain creation code simply uses libxl__stub_dm_spawn_state.dm directly, if we're taking the local dm path.
* Remove a spurious "break".
* In domain creation, move the PV non-qemu case into the switch.
* Code style fixes.
* Constify some convenience aliases.
* Improve comments (including typo fixes).
--- tools/libxl/libxl.h | 14 ++- tools/libxl/libxl_create.c | 206 +++++++++++++++++++----- 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, 683 insertions(+), 402 deletions(-)
tools/libxl/libxl.h +12 -2
@@ -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_init (libxl_domain_config *d_config);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_create.c +162 -44
272 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
@@ -1695,8 +1695,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.
@@ -1704,7 +1704,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
PATCH v9 19/27 libxl: set guest_domid even if libxl__domain_make fails
In reply to Ian JacksonFrom: Roger Pau Monne <[email protected]>
This is needed in order to perform the domain destruction if libxl__domain_make fails.
Also move doc comment about error behaviour of libxl__domain_make from implementation to declaration.
--- tools/libxl/libxl_create.c | 3 +-- tools/libxl/libxl_internal.h | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-)
tools/libxl/libxl_create.c +1 -2
@@ -396,8 +396,6 @@ out:int libxl__domain_make (libxl__gc *gc,libxl_domain_create_info *info,uint32_t *domid) -/* on entry, libxl_domid_valid_guest(domid) must be false; -* on exit (even error exit), domid may be valid and refer to a domain */ {libxl_ctx *ctx =libxl__gc_owner (gc);int flags, ret, rc;
@@ -600,6 +598,7 @@ static void initiate_domain_create(libxl__egc *egc, ret =libxl__domain_make (gc, &d_config->c_info, &domid);if (ret) {LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot make domain: %d" , ret); + dcs->guest_domid = domid; ret =ERROR_FAIL ; goto error_out; }
tools/libxl/libxl_internal.h +4 -0
@@ -1078,9 +1078,13 @@ _hidden void libxl__exec(int stdinfd, int stdoutfd, int stderrfd, const char *arg0, char **args); // logs errors, never returns/* from xl_create */ + +/* on entry, libxl_domid_valid_guest(domid) must be false; +* on exit (even error exit), domid may be valid and refer to a domain */ _hidden int libxl__domain_make(libxl__gc *gc,libxl_domain_create_info *info,uint32_t *domid); + _hidden int libxl__domain_build(libxl__gc *gc, libxl_domain_build_info *info, uint32_t domid,
Signature · 1 line
1.7.2.5
PATCH v9 20/27 libxl: make libxl_create run bootloader via callback
In reply to Ian JacksonChange initiate_domain_create to properly use libxl__bootloader_run rather than improperly calling libxl_run_bootloader with NULL ao_how.
Changes since v7:
* constify convenience aliases.
Changes since v6:
* Bugfixes from testing.
--- 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
@@ -569,6 +569,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. */
@@ -589,6 +592,7 @@ static void initiate_domain_create(libxl__egc *egc,const int restore_fd = dcs->restore_fd;const libxl_console_ready cb = dcs->console_cb;void *const priv = dcs->console_cb_priv; +memset (&dcs->build_state,0 ,sizeof (dcs->build_state)); domid =0 ;
@@ -619,21 +623,43 @@ static void initiate_domain_create(libxl__egc *egc,if (ret) goto error_out; } + dcs->bl.ao = ao;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; - } + dcs->bl.callback = domcreate_bootloader_done; + dcs->bl.info = &d_config->b_info, + dcs->bl.disk = bootdisk; + dcs->bl.domid = dcs->guest_domid; + +libxl__bootloader_run (egc, &dcs->bl); + }else { +domcreate_bootloader_done (egc, &dcs->bl,0 ); } +return ; -memset (&dcs->build_state,0 ,sizeof (dcs->build_state)); -libxl__domain_build_state *state = &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 */ +const uint32_t domid = dcs->guest_domid; +libxl_domain_config *const d_config = dcs->guest_config; +const int restore_fd = dcs->restore_fd; +libxl__domain_build_state *const state = &dcs->build_state; +libxl_ctx *const ctx =CTX ; + +if (ret) goto error_out;/* We might be going to call libxl__spawn_local_dm, or _spawn_stub_dm. * Fill in any field required by either, including both relevant
@@ -784,12 +810,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
@@ -1689,6 +1689,7 @@ struct libxl__domain_create_state {/* private to domain_create */ int guest_domid;libxl__domain_build_state build_state; +libxl__bootloader_state bl;libxl__stub_dm_spawn_state dmss;/* If we're not doing stubdom, we use only dmss.dm, * for the non-stubdom device model. */
Signature · 1 line
1.7.2.5
PATCH v9 21/27 libxl: Fix an ao completion bug; document locking policy
In reply to Ian JacksonDocument the concurrent access policies for libxl__ao and libxl__egc, and their corresponding gcs.
Fix a violation of the policy:
If an ao was submitted and a callback requested, and while the initiating function was still running on the original thread, the ao is completed on another thread, the completing thread would improperly concurrently access the ao with the initiating thread.
--- tools/libxl/libxl_event.c | 7 +++++++ tools/libxl/libxl_internal.h | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletions(-)
tools/libxl/libxl_event.c +7 -0
@@ -901,6 +901,11 @@ void libxl__event_disaster(libxl__egc *egc, const char *msg, int errnoval,static void egc_run_callbacks (libxl__egc *egc) { +/* +* The callbacks must happen with the ctx unlocked. +* See the comment near #define EGC_GC in libxl_internal.h and +* those in the definitions of libxl__egc and libxl__ao. +*/ EGC_GC ;libxl_event *ev, *ev_tmp;
@@ -914,9 +919,11 @@ static void egc_run_callbacks(libxl__egc *egc) entry_for_callback, ao_tmp) {LIBXL_TAILQ_REMOVE (&egc->aos_for_callback, ao, entry_for_callback); ao->how.callback (CTX , ao->rc, ao->how.u.for_callback); +CTX_LOCK ; ao->notified =1 ;if (!ao->in_initiator)libxl__ao__destroy (CTX , ao); +CTX_UNLOCK ; } }
tools/libxl/libxl_internal.h +22 -1
@@ -359,7 +359,8 @@ struct libxl__gc { };struct libxl__egc { -/* for event-generating functions only */ +/* For event-generating functions only. +* The egc and its gc may be accessed only on the creating thread. */ struct libxl__gc gc;struct libxl__event_list occurred_for_callback; LIBXL_TAILQ_HEAD(, libxl__ao) aos_for_callback;
@@ -369,6 +370,20 @@ struct libxl__egc {#define LIBXL__AO_MAGIC_DESTROYED 0xA0DEAD00ulstruct libxl__ao { +/* +* An ao and its gc may be accessed only with the ctx lock held. +* +* Special exception: If an ao has been added to +* egc->aos_for_callback, the thread owning the egc may remove the +* ao from that list and make the callback without holding the +* lock. +* +* Corresponding restriction: An ao may be added only to one +* egc->aos_for_callback, once; rc and how must already have been +* set and may not be subsequently modified. (This restriction is +* easily and obviously met since the ao is queued for callback +* only in libxl__ao_complete.) +*/ uint32_t magic;unsigned constructing:1 , in_initiator:1 , complete:1 , notified:1 ;int rc;
@@ -1365,6 +1380,12 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);* should in any case not find it necessary to call egc-creators from * within libxl. * +* The callbacks must all take place with the ctx unlocked because +* the application is entitled to reenter libxl from them. This +* would be bad not because the lock is not recursive (it is) but +* because the application might make blocking libxl calls which +* would hold the lock unreasonably long. +* * For the same reason libxl__egc_cleanup (or EGC_FREE) must be called * with the ctx *unlocked*. So the right pattern has the EGC_... * macro calls on the outside of the CTX_... ones.
Signature · 1 line
1.7.2.5
PATCH v9 22/27 libxl: provide progress reporting for long-running operations
In reply to Ian JacksonThis will be used for reporting, during domain creation, that the console is ready.
Changes since v7:
* If aop->how.callback, actually add the aop to the for_callback list (!)
* Document the threadsafety of aop's, and make appropriate cross-references.
* Allocate the actual aop from its thread's egc; do not free it.
* Remove pointless code motion of libxl__ao_create.
* Minor formatting fixes.
--- tools/libxl/libxl.h | 45 ++++++++++++++++++++++++++ tools/libxl/libxl_event.c | 72 ++++++++++++++++++++++++++++++++++++++++-- tools/libxl/libxl_internal.h | 35 ++++++++++++++++++++ 3 files changed, 149 insertions(+), 3 deletions(-)
tools/libxl/libxl.h +45 -0
@@ -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 +69 -3
111 lines not on this page · raw message
tools/libxl/libxl_internal.h +35 -0
@@ -119,6 +119,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 ; _hidden void libxl__alloc_failed(libxl_ctx *, const char *func,size_t nmemb,size_t size) __attribute__((noreturn));
@@ -364,6 +365,21 @@ 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 { +/* +* An aop belongs to, and may be accessed only on, the thread +* which created it. It normally lives in that thread's egc. +* +* While an aop exists, it corresponds to one refcount in +* ao->progress_reports_outstanding, preventing ao destruction. +*/ +LIBXL_TAILQ_ENTRY (libxl__aop_occurred) entry; +libxl__ao *ao; +libxl_event *ev; +const libxl_asyncprogress_how *how; };#define LIBXL__AO_MAGIC 0xA0FACE00ul
@@ -386,6 +402,7 @@ 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;
@@ -1402,6 +1419,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);
@@ -1448,6 +1466,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
@@ -1457,6 +1478,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
@@ -1512,8 +1537,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
PATCH v9 23/27 libxl: remove malloc failure handling from NEW_EVENT
In reply to Ian JacksonChange 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
@@ -789,7 +789,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);
@@ -876,7 +875,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
@@ -989,13 +989,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
@@ -654,10 +654,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
PATCH v9 24/27 libxl: convert console callback to libxl_asyncprogress_how
In reply to Ian JacksonRemove 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.
--- 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
@@ -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_init (libxl_domain_config *d_config);void libxl_domain_config_dispose (libxl_domain_config *d_config);
tools/libxl/libxl_bootloader.c +3 -0
@@ -377,6 +377,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
@@ -1699,11 +1699,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;
@@ -1739,9 +1742,8 @@ 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__domain_create_cb *callback; +libxl_asyncprogress_how aop_console_how;/* private to domain_create */ int guest_domid;libxl__domain_build_state build_state;
tools/libxl/libxl_types.idl +2 -0
@@ -459,6 +459,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)
@@ -484,4 +485,5 @@ libxl_event = Struct("event",[ ("operation_complete", Struct(None, [ ("rc", integer), ])), + ("domain_create_console_available", Struct(None, [])), ]))])
tools/libxl/xl_cmdimpl.c +15 -10
@@ -1457,16 +1457,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 ();
@@ -1533,7 +1535,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;
@@ -1687,24 +1689,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
PATCH v9 25/27 libxl: clarify definition of "slow" operation
In reply to Ian JacksonUpdate the comment in libxl_internal.h to be clearer about which application-facing libxl operations need to take an ao_how.
--- tools/libxl/libxl_internal.h | 29 +++++++++++++++++++++++------ 1 files changed, 23 insertions(+), 6 deletions(-)
tools/libxl/libxl_internal.h +23 -6
@@ -1439,17 +1439,34 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);/* * Machinery for asynchronous operations ("ao") * -* All "slow" functions (includes anything that might block on a -* guest or an external script) need to use the asynchronous -* operation ("ao") machinery. The function should take a parameter -* const libxl_asyncop_how *ao_how and must start with a call to -* AO_INITIATOR_ENTRY. These functions MAY NOT be called from -* inside libxl, because they can cause reentrancy callbacks. +* All "slow" functions (see below for the exact definition) need to +* use the asynchronous operation ("ao") machinery. The function +* should take a parameter const libxl_asyncop_how *ao_how and must +* start with a call to AO_INITIATOR_ENTRY. These functions MAY NOT +* be called from inside libxl, because they can cause reentrancy +* callbacks. * * For the same reason functions taking an ao_how may make themselves * an egc with EGC_INIT (and they will generally want to, to be able * to immediately complete an ao during its setup). * +* +* "Slow" functions includes any that might block on a guest or an +* external script. More broadly, it includes any operations which +* are sufficiently slow that an application might reasonably want to +* initiate them, and then carry on doing something else, while the +* operation completes. That is, a "fast" function must be fast +* enough that we do not mind blocking all other management operations +* on the same host while it completes. +* +* There are certain primitive functions which make a libxl operation +* necessarily "slow" for API reasons. These are: +* - awaiting xenstore watches (although read-modify-write xenstore +* transactions are OK for fast functions) +* - spawning subprocesses +* - anything with a timeout +* +* * Lifecycle of an ao: * * - Created by libxl__ao_create (or the AO_CREATE convenience macro).
Signature · 1 line
1.7.2.5
PATCH v9 26/27 libxl: child processes cleanups
In reply to Ian JacksonAbolish libxl_fork. Its only callers were in xl. Its functionality is now moved elsewhere, as follows:
* The "logging version of fork", which is what it was billed as, is now xl_fork, which also dies on failure.
* Closing the xenstore handle in the child is now done in libxl__ev_child_fork, which is the only remaining place where fork is called in libxl.
* We provide a new function libxl__ev_child_xenstore_reopen for in-libxl children to make the ctx useable for xenstore again.
* Consequently libxl__spawn_record_pid now no longer needs to mess about with its own xenstore handle. As a bonus it can now just use libxl__xs_write.
Also, since we have now converted all the forkers in libxl, we can always honour the fork_replacement childproc hook - so do so.
--- tools/libxl/libxl_exec.c | 35 ++++++++++++++++------------------- tools/libxl/libxl_fork.c | 25 ++++++++++++++++++++++++- tools/libxl/libxl_internal.h | 5 +++++ tools/libxl/libxl_utils.c | 21 --------------------- tools/libxl/libxl_utils.h | 3 +-- tools/libxl/xl.c | 12 ++++++++++++ tools/libxl/xl.h | 1 + tools/libxl/xl_cmdimpl.c | 5 ++--- 8 files changed, 61 insertions(+), 46 deletions(-)
tools/libxl/libxl_exec.c +16 -19
@@ -127,34 +127,23 @@ void libxl_report_child_exitstatus(libxl_ctx *ctx, } } -int libxl__spawn_record_pid (libxl__gc *gc,libxl__spawn_state *spawn, -pid_t innerchild) +int libxl__spawn_record_pid (libxl__gc *gc,libxl__spawn_state *spawn,pid_t pid) { -struct xs_handle *xsh =NULL ; -const char *pid =NULL ; -int rc, xsok; +int r, rc; - pid =GCSPRINTF ("%d" , innerchild); + rc =libxl__ev_child_xenstore_reopen (gc, spawn->what); +if (rc) goto out; -/* we mustn't use the parent's handle in the child */ - xsh =xs_daemon_open (); -if (!xsh) { -LOGE (ERROR ,"write %s = %s: xenstore reopen failed" , - spawn->pidpath, pid); - rc =ERROR_FAIL ; goto out; - } - - xsok =xs_write (xsh,XBT_NULL , spawn->pidpath, pid,strlen (pid)); -if (!xsok) { + r =libxl__xs_write (gc,XBT_NULL , spawn->pidpath,"%d" , pid); +if (r) {LOGE (ERROR , -"write %s = %s: xenstore write failed" , spawn->pidpath, pid); +"write %s = %d: xenstore write failed" , spawn->pidpath, pid); rc =ERROR_FAIL ; goto out; } rc =0 ; out: -if (xsh)xs_daemon_close (xsh);return rc ?SIGTERM :0 ; }
@@ -302,7 +291,15 @@ int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss)/* we are now the middle process */ - child =fork (); +pid_t (*fork_replacement)(void *) = +CTX ->childproc_hooks + ?CTX ->childproc_hooks->fork_replacement + :0 ; + child = + fork_replacement + ?fork_replacement (CTX ->childproc_user) + :fork (); +if (child ==-1 )exit (255 );if (!child) {
tools/libxl/libxl_fork.c +24 -1
@@ -347,7 +347,12 @@ pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *ch,if (!pid) {/* woohoo! */ -return 0 ;/* Yes, CTX is left locked in the child. */ +if (CTX ->xsh) { +xs_daemon_destroy_postfork (CTX ->xsh); +CTX ->xsh =NULL ;/* turns mistakes into crashes */ + } +/* Yes, CTX is left locked in the child. */ +return 0 ; } ch->pid = pid;
@@ -395,6 +400,24 @@ const libxl_childproc_hooks libxl__childproc_default_hooks = { libxl_sigchld_owner_libxl, 0, 0 }; +int libxl__ev_child_xenstore_reopen (libxl__gc *gc,const char *what) { +int rc; + +assert (!CTX ->xsh); +CTX ->xsh =xs_daemon_open (); +if (!CTX ->xsh) { +LOGE (ERROR ,"%s: xenstore reopen failed" , what); + rc =ERROR_FAIL ; goto out; + } + +libxl_fd_set_cloexec (CTX ,xs_fileno (CTX ->xsh),1 ); + +return 0 ; + + out: +return rc; +} +/* * Local variables: * mode: C
tools/libxl/libxl_internal.h +5 -0
@@ -640,6 +640,11 @@ static inline void libxl__ev_child_init(libxl__ev_child *childw_out)static inline int libxl__ev_child_inuse (libxl__ev_child *childw_out) {return childw_out->pid >=0 ; } +/* Useable (only) in the child to once more make the ctx useable for +* xenstore operations. logs failure in the form "what: <error +* message>". */ +_hidden int libxl__ev_child_xenstore_reopen(libxl__gc *gc, const char *what); +/* * Other event-handling support provided by the libxl event core to
tools/libxl/libxl_utils.c +0 -21
@@ -444,27 +444,6 @@ int libxl__remove_directory(libxl__gc *gc, const char *dirpath)return rc; } -pid_t libxl_fork (libxl_ctx *ctx) -{ -pid_t pid; - - pid =fork (); -if (pid ==-1 ) { -LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"fork failed" ); -return -1 ; - } - -if (!pid) { -if (ctx->xsh)xs_daemon_destroy_postfork (ctx->xsh); - ctx->xsh =0 ; -/* This ensures that anyone who forks but doesn't exec, -* and doesn't reinitialise the libxl_ctx, is OK. -* It also means they can safely call libxl_ctx_free. */ - } - -return pid; -} -int libxl_pipe (libxl_ctx *ctx,int pipes[2 ]) {if (pipe (pipes) <0 ) {
tools/libxl/libxl_utils.h +1 -2
@@ -47,9 +47,8 @@ int libxl_write_exactly(libxl_ctx *ctx, int fd, const void *data,* logged using filename (which is only used for logging) and what * (which may be 0). */ -pid_t libxl_fork (libxl_ctx *ctx);int libxl_pipe (libxl_ctx *ctx,int pipes[2 ]); -/* Just like fork(2), pipe(2), but log errors. */ +/* Just like pipe(2), but log errors. */ void libxl_report_child_exitstatus (libxl_ctx *ctx,xentoollog_level ,const char *what,pid_t pid,int status);
tools/libxl/xl.c +12 -0
@@ -105,6 +105,18 @@ void postfork(void) } } +pid_t xl_fork (libxl_ctx *ctx) { +pid_t pid; + + pid =fork (); +if (pid ==-1 ) { +perror ("fork failed" ); +exit (-1 ); + } + +return pid; +} +int main (int argc,char **argv) {int opt =0 ;
tools/libxl/xl.h +1 -0
@@ -104,6 +104,7 @@ struct cmd_spec *cmdtable_lookup(const char *s);extern libxl_ctx *ctx;extern xentoollog_logger_stdiostream *logger; +pid_t xl_fork (libxl_ctx *ctx);/* like fork, but prints and dies if it fails */ void postfork (void );/* global options */
tools/libxl/xl_cmdimpl.c +2 -3
@@ -1736,7 +1736,7 @@ start:pid_t child1, got_child;int nullfd; - child1 =libxl_fork (ctx); + child1 =xl_fork (ctx);if (child1) {printf ("Daemon running with PID %d\n" , child1);
@@ -2779,8 +2779,7 @@ static void migrate_domain(const char *domain_spec, const char *rune,MUST (libxl_pipe (ctx, sendpipe) );MUST (libxl_pipe (ctx, recvpipe) ); - child =libxl_fork (ctx); -if (child==-1 )exit (1 ); + child =xl_fork (ctx);if (!child) {dup2 (sendpipe[0 ],0 );
Signature · 1 line
1.7.2.5
PATCH v9 27/27 libxl: abort bootloader invocation when domain dies
In reply to Ian JacksonCancel the bootloader (specifically, by sending it a signal) if the domain is seen to disappear from xenstore.
We use a new utility event source libxl__domaindeathcheck which provides a convenient wrapper for the xenstore watch.
Changes since v8:
* Fixed the commit message summary line.
Changes since v7:
* Add a comment explaining why we use a watch on the domain's xenstore path rather than @releaseDomain.
* Fix typo in error message.
--- tools/libxl/libxl_bootloader.c | 43 ++++++++++++++++++++++++++++-------- tools/libxl/libxl_event.c | 47 ++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 29 +++++++++++++++++++++++- 3 files changed, 108 insertions(+), 11 deletions(-)
tools/libxl/libxl_bootloader.c +33 -10
@@ -31,6 +31,7 @@ static void bootloader_keystrokes_copyfail(libxl__egc *egc, libxl__datacopier_state *dc, int onwrite, int errnoval);static void bootloader_display_copyfail (libxl__egc *egc,libxl__datacopier_state *dc,int onwrite,int errnoval); +static void bootloader_domaindeath (libxl__egc *,libxl__domaindeathcheck *dc);static void bootloader_finished (libxl__egc *egc,libxl__ev_child *child,pid_t pid,int status);
@@ -213,6 +214,7 @@ void libxl__bootloader_init(libxl__bootloader_state *bl) bl->ptys[0 ].master = bl->ptys[0 ].slave =0 ; bl->ptys[1 ].master = bl->ptys[1 ].slave =0 ;libxl__ev_child_init (&bl->child); +libxl__domaindeathcheck_init (&bl->deathcheck); bl->keystrokes.ao = bl->ao;libxl__datacopier_init (&bl->keystrokes); bl->display.ao = bl->ao;libxl__datacopier_init (&bl->display); }
@@ -230,6 +232,7 @@ static void bootloader_cleanup(libxl__egc *egc, libxl__bootloader_state *bl)free (bl->diskpath); bl->diskpath =0 ; } +libxl__domaindeathcheck_stop (gc,&bl->deathcheck);libxl__datacopier_kill (&bl->keystrokes);libxl__datacopier_kill (&bl->display);for (i=0 ; i<2 ; i++) {
@@ -256,6 +259,23 @@ static void bootloader_callback(libxl__egc *egc, libxl__bootloader_state *bl, bl->callback (egc, bl, rc); } +/* might be called at any time, provided it's init'd */ +static void bootloader_abort (libxl__egc *egc, +libxl__bootloader_state *bl,int rc) +{ +STATE_AO_GC (bl->ao); +int r; + +libxl__datacopier_kill (&bl->keystrokes); +libxl__datacopier_kill (&bl->display); +if (libxl__ev_child_inuse (&bl->child)) { + r =kill (bl->child.pid,SIGTERM ); +if (r)LOGE (WARN ,"after failure, failed to kill bootloader [%lu]" , + (unsigned long )bl->child.pid); + } + bl->rc = rc; +} +/*----- main flow of control -----*/ void libxl__bootloader_run (libxl__egc *egc,libxl__bootloader_state *bl)
@@ -377,6 +397,12 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op) goto out; } + bl->deathcheck.what ="stopping bootloader" ; + bl->deathcheck.domid = bl->domid; + bl->deathcheck.callback = bootloader_domaindeath; + rc =libxl__domaindeathcheck_start (gc, &bl->deathcheck); +if (rc) goto out; +if (bl->console_available) bl->console_available (egc, bl);
@@ -454,18 +480,9 @@ static void bootloader_copyfail(libxl__egc *egc, const char *which, libxl__bootloader_state *bl, int onwrite, int errnoval) { STATE_AO_GC(bl->ao); -int r; -if (!onwrite && !errnoval)LOG (ERROR ,"unexpected eof copying %s" , which); -libxl__datacopier_kill (&bl->keystrokes); -libxl__datacopier_kill (&bl->display); -if (libxl__ev_child_inuse (&bl->child)) { - r =kill (bl->child.pid,SIGTERM ); -if (r)LOGE (WARN ,"after failure, failed to kill bootloader [%lu]" , - (unsigned long )bl->child.pid); - } - bl->rc =ERROR_FAIL ; +bootloader_abort (egc, bl,ERROR_FAIL ); }static void bootloader_keystrokes_copyfail (libxl__egc *egc,libxl__datacopier_state *dc,int onwrite,int errnoval)
@@ -480,6 +497,12 @@ static void bootloader_display_copyfail(libxl__egc *egc,bootloader_copyfail (egc,"bootloader output" , bl, onwrite, errnoval); } +static void bootloader_domaindeath (libxl__egc *egc,libxl__domaindeathcheck *dc) +{ +libxl__bootloader_state *bl =CONTAINER_OF (dc, *bl, deathcheck); +bootloader_abort (egc, bl,ERROR_FAIL ); +} +static void bootloader_finished (libxl__egc *egc,libxl__ev_child *child,pid_t pid,int status) {
tools/libxl/libxl_event.c +47 -0
53 lines not on this page · raw message
tools/libxl/libxl_internal.h +28 -1
@@ -866,6 +866,33 @@ _hidden int libxl__ev_devstate_wait(libxl__gc *gc, libxl__ev_devstate *ds,const char *state_path, int state, int milliseconds); +/* +* libxl__ev_domaindeathcheck_register - arranges to call back (once) +* if the domain is destroyed. If the domain dies, we log a message +* of the form "<what>: <explanation of the situation, including the domid>". +*/ + +typedef struct libxl__domaindeathcheck libxl__domaindeathcheck ; +typedef void libxl___domaindeathcheck_callback (libxl__egc *egc, +libxl__domaindeathcheck *); + +struct libxl__domaindeathcheck { +/* must be filled in by caller, and remain valid: */ +const char *what; +uint32_t domid; +libxl___domaindeathcheck_callback *callback; +/* private */ +libxl__ev_xswatch watch; +}; + +_hidden int libxl__domaindeathcheck_start(libxl__gc *gc, +libxl__domaindeathcheck *dc); + +static inline void libxl__domaindeathcheck_init + (libxl__domaindeathcheck *dc) {libxl__ev_xswatch_init (&dc->watch); } +static inline void libxl__domaindeathcheck_stop (libxl__gc *gc, +libxl__domaindeathcheck *dc) {libxl__ev_xswatch_deregister (gc,&dc->watch); } +/* * libxl__try_phy_backend - Check if there's support for the passed
@@ -1738,6 +1765,7 @@ struct libxl__bootloader_state {libxl__openpty_state openpty;libxl__openpty_result ptys[2 ];/* [0] is for bootloader */ libxl__ev_child child; +libxl__domaindeathcheck deathcheck;int nargs, argsspace;const char **args;libxl__datacopier_state keystrokes, display;
@@ -1750,7 +1778,6 @@ _hidden void libxl__bootloader_init(libxl__bootloader_state *bl);* If callback is passed rc==0, will have updated st->info appropriately */ _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st); -/*----- Domain creation -----*/ typedef struct libxl__domain_create_state libxl__domain_create_state ;
Signature · 1 line
1.7.2.5