libxl: check for early failures of qemu-dm (v2)
This patch makes xl create check whether qemu-dm has started correctly, and causes it to fail immediately with appropriate errors if not. There are other bugfixes too.
More specifically:
* libxl_create_device_model forks twice rather than once so that the process which calls libxl does not end up being the actual parent of qemu. That avoids the need for the qemu-dm process to be reaped at some indefinite time in the future.
* The first fork generates an intermediate process which is responsible for writing the qemu-dm pid to xenstore and then merely waits to collect and report on qemu-dm's exit status during startup. New arguments to libxl_create_device_model allow the preservation of its pid so that a later call can check whether the startup is successful.
* xl.c's create_domain checks for errors in its libxl calls.
Consequential changes:
* libxl_wait_for_device_model now takes a callback function parameter which is called repeatedly in the loop iteration and allows the caller to abort the wait.
* libxl_exec no longer calls fork; there is a new libxl_fork.
* osdep.[ch] new use #define _GNU_SOURCE. The provided asprintf declarations are suppressed when not needed (currently, always).
* There is a hook to override waitpid, which will be necessary for some callers.
Remaining problems and other issues I noticed or we found:
* The error handling is rather inconsistent still and lacking in places.
* xl_logv is declared but not defined.
* _GNU_SOURCE should be used throughout. The asprintf implementation should be disabled in favour of the system one.
* XL_LOG_ERROR_ERRNO needs to actually print the errno value.
* destroy_device_model can kill random dom0 processes (!)
* struct libxl_ctx should be defined in libxl_internal.h.
(Changes since v1:
* Remove new error log level; should be in a future patch
* Properly fixed the asprintf problem
* Indentation no uses literal tabs)
tools/libxl/Makefile +1 -2
@@ -23,8 +23,7 @@ LIBCONFIG_SOURCE = libconfig-1.3.2 LIBCONFIG_OUTPUT = $(LIBCONFIG_SOURCE)/.libs -LIBXL_OBJS-y = -LIBXL_OBJS-$(CONFIG_Linux) += osdeps.o +LIBXL_OBJS-y = osdeps.o LIBXL_OBJS = flexarray.o libxl.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o xenguest.o libxl_utils.o $(LIBXL_OBJS-y) CLIENTS = xl
tools/libxl/libxl.c +129 -13
204 lines not on this page · raw message
tools/libxl/libxl.h +16 -1
@@ -42,6 +42,11 @@/* mini-GC */ int alloc_maxsize;void **alloc_ptrs; + +/* for callers who reap children willy-nilly; caller must only +* set this after libxl_init and before any other call - or +* may leave them untouched */ +int (*waitpid_instead)(pid_t pid,int *status,int flags); }; typedef struct {
@@ -193,9 +198,19 @@struct libxl_dominfo *libxl_domain_list (struct libxl_ctx *ctx,int *nb_domain);xc_dominfo_t *libxl_domain_infolist (struct libxl_ctx *ctx,int *nb_domain); +typedef struct libxl_device_model_starting libxl_device_model_starting ;int libxl_create_device_model (struct libxl_ctx *ctx,libxl_device_model_info *info, -libxl_device_nic *vifs,int num_vifs); +libxl_device_nic *vifs,int num_vifs, +libxl_device_model_starting **starting_r); +/* Caller must either: pass starting_r==0, or on successful +* return pass *starting_r to libxl_confirm_device_model +* or libxl_detach_device_model */ +int libxl_confirm_device_model_startup (struct libxl_ctx *ctx, +libxl_device_model_starting *starting); +int libxl_detach_device_model (struct libxl_ctx *ctx, +libxl_device_model_starting *starting); +/* DM is detached even if error is returned */ int libxl_device_disk_add (struct libxl_ctx *ctx,uint32_t domid,libxl_device_disk *disk);int libxl_device_disk_clean_shutdown (struct libxl_ctx *ctx,uint32_t domid);
tools/libxl/libxl_device.c +10 -1
@@ -260,12 +260,17 @@return -1 ; } -int libxl_wait_for_device_model (struct libxl_ctx *ctx,uint32_t domid,char *state) +int libxl_wait_for_device_model (struct libxl_ctx *ctx, +uint32_t domid,char *state, +int (*check_callback)(struct libxl_ctx *ctx, +void *userdata), +void *check_callback_userdata) {char path[50 ];char *p;int watchdog =100 ;unsigned int len; +int rc;snprintf (path,sizeof (path),"/local/domain/0/device-model/%d/state" , domid);while (watchdog >0 ) {
@@ -282,6 +287,10 @@usleep (100000 ); watchdog--; } + } +if (check_callback) { + rc =check_callback (ctx, check_callback_userdata); +if (rc)return rc; } }XL_LOG (ctx,XL_LOG_ERROR ,"Device Model not ready\n" );
tools/libxl/libxl_exec.c +64 -17
98 lines not on this page · raw message
tools/libxl/libxl_internal.h +12 -3
@@ -45,6 +45,7 @@#define XL_LOG_WARNING 1#define XL_LOG_ERROR 0 +void xl_logv (struct libxl_ctx *ctx,int loglevel,const char *file,int line,const char *func,char *fmt,va_list al);void xl_log (struct libxl_ctx *ctx,int loglevel,const char *file,int line,const char *func,char *fmt, ...); typedef struct {
@@ -126,7 +127,11 @@ char **bents, char **fents);int libxl_device_destroy (struct libxl_ctx *ctx,char *be_path,int force);int libxl_devices_destroy (struct libxl_ctx *ctx,uint32_t domid,int force); -int libxl_wait_for_device_model (struct libxl_ctx *ctx,uint32_t domid,char *state); +int libxl_wait_for_device_model (struct libxl_ctx *ctx, +uint32_t domid,char *state, +int (*check_callback)(struct libxl_ctx *ctx, +void *userdata), +void *check_callback_userdata);int libxl_wait_for_backend (struct libxl_ctx *ctx,char *be_path,char *state);int libxl_device_pci_flr (struct libxl_ctx *ctx,unsigned int domain,unsigned int bus,unsigned int dev,unsigned int func);
@@ -137,8 +142,12 @@ int vcpus, int store_evtchn, unsigned long *store_mfn);/* xl_exec */ -int libxl_exec (struct libxl_ctx *ctx,int stdinfd,int stdoutfd,int stderrfd, -char *arg0,char **args); +pid_t libxl_fork (struct libxl_ctx *ctx); +void libxl_exec (struct libxl_ctx *ctx,int stdinfd,int stdoutfd,int stderrfd, +char *arg0,char **args); +void libxl_report_child_exitstatus (struct libxl_ctx *ctx, +const char *what,pid_t pid,int status); +pid_t libxl_waitpid_instead_default (pid_t pid,int *status,int flags);#endif
tools/libxl/osdeps.c +6 -0
@@ -13,11 +13,15 @@* GNU Lesser General Public License for more details. */ +#include "osdeps.h" +#include <unistd.h> #include <stdarg.h> #include <stdio.h> #include <sys/time.h> #include <stdlib.h> + +#ifdef NEED_OWN_ASPRINTF int vasprintf (char **buffer,const char *fmt,va_list ap) {
@@ -60,3 +64,5 @@va_end (ap); return status; } + +#endif
tools/libxl/osdeps.h +3 -2
@@ -16,9 +16,10 @@#ifndef LIBXL_OSDEP #define LIBXL_OSDEP +#define _GNU_SOURCE + +#ifdef NEED_OWN_ASPRINTF #include <stdarg.h> - -#if defined(__linux__)int asprintf (char **buffer,char *fmt, ...);int vasprintf (char **buffer,const char *fmt,va_list ap);#endif
tools/libxl/xl.c +21 -7
@@ -572,6 +572,15 @@config_destroy (&config); } +#define MUST ( call ) ({ \ + int must_rc = (call); \ + if (must_rc) { \ + fprintf(stderr,"xl: fatal error: %s:%d, rc=%d: %s\n", \ + __FILE__,__LINE__, must_rc, #call); \ + exit(-must_rc); \ + } \ + }) +static void create_domain (int debug,const char *filename) {struct libxl_ctx ctx;
@@ -584,30 +593,35 @@libxl_device_pci *pcidevs =NULL ;int num_disks =0 , num_vifs =0 , num_pcidevs =0 ;int i; +libxl_device_model_starting *dm_starting;printf ("Parsing config file %s\n" , filename);parse_config_file (filename, &info1, &info2, &disks, &num_disks, &vifs, &num_vifs, &pcidevs, &num_pcidevs, &dm_info);if (debug)printf_info (&info1, &info2, disks, num_disks, vifs, num_vifs, pcidevs, num_pcidevs, &dm_info); -libxl_ctx_init (&ctx); -libxl_ctx_set_log (&ctx, log_callback,NULL ); -libxl_domain_make (&ctx, &info1, &domid); -libxl_domain_build (&ctx, &info2, domid); +MUST (libxl_ctx_init (&ctx) ); +MUST (libxl_ctx_set_log (&ctx, log_callback,NULL ) ); +MUST (libxl_domain_make (&ctx, &info1, &domid) ); +MUST (libxl_domain_build (&ctx, &info2, domid) );device_model_info_domid_fixup (&dm_info, domid);for (i =0 ; i < num_disks; i++) {disk_info_domid_fixup (disks + i, domid); -libxl_device_disk_add (&ctx, domid, &disks[i]); +MUST (libxl_device_disk_add (&ctx, domid, &disks[i]) ); }for (i =0 ; i < num_vifs; i++) {nic_info_domid_fixup (vifs + i, domid); -libxl_device_nic_add (&ctx, domid, &vifs[i]); +MUST (libxl_device_nic_add (&ctx, domid, &vifs[i]) ); } -libxl_create_device_model (&ctx, &dm_info, vifs, num_vifs); + +MUST (libxl_create_device_model (&ctx, &dm_info, vifs, num_vifs, + &dm_starting) );for (i =0 ; i < num_pcidevs; i++)libxl_device_pci_add (&ctx, domid, &pcidevs[i]); +MUST (libxl_confirm_device_model_startup (&ctx, dm_starting) ); +libxl_domain_unpause (&ctx, domid); }
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 11 lines · Ian Jackson
This patch makes xl create check whether qemu-dm has started correctly, and causes it to fail immediately with appropriate errors if not. There are other bugfixes too.
More specifically:
*
libxl_create_device_modelforks twice rather than once so that the process which calls libxl does not end up being the actual parent of qemu. That avoids the need for the qemu-dm process to be reaped at some indefinite time in the future.
Quoted 7 lines
* The first fork generates an intermediate process which is responsible for writing the qemu-dm pid to xenstore and then merely waits to collect and report on qemu-dm's exit status during startup. New arguments to
libxl_create_device_modelallow the preservation of its pid so that a later call can check whether the startup is successful.
Did you have the qemu-dm ready patch in qemu ?
Quoted 10 lines
* xl.c's
create_domainchecks for errors in its libxl calls.Consequential changes:
*
libxl_wait_for_device_modelnow takes a callback function parameter which is called repeatedly in the loop iteration and allows the caller to abort the wait.*
libxl_execno longer calls fork; there is a newlibxl_fork.
this is not libxl goal to provide wrapper for syscalls.
the exec should do the double fork+exec itself, no need to have a fork function.
Quoted 6 lines
* osdep.[ch] new use #define
_GNU_SOURCE. The provided asprintf declarations are suppressed when not needed (currently, always).* There is a hook to override waitpid, which will be necessary for some callers.
why ?
Quoted 9 lines
Remaining problems and other issues I noticed or we found:
* The error handling is rather inconsistent still and lacking in places.
*
xl_logvis declared but not defined.
*_GNU_SOURCEshould be used throughout. The asprintf implementation should be disabled in favour of the system one.
osdeps was just suppose to be available for netbsd. not sure why the contrary happens on christoph's patch.
Quoted 2 lines
*
XL_LOG_ERROR_ERRNOneeds to actually print the errno value.
there shouldn't be a LOG_ERROR_ERRNO value. what you want is the different log function
that will embedd an errno automatically in the fmt, not a different logging level. not sure what you meant later by "remove new error level, should be in future patch".
Quoted 4 lines
*
destroy_device_modelcan kill random dom0 processes (!)* struct
libxl_ctxshould be defined inlibxl_internal.h.
no, otherwise the init ctx need to allocate itself the memory of the context, instead of having the caller "allocate it" itself on the fct stack.
Quoted 6 lines
Signed-off-by Ian Jackson <[email protected]>(Changes since v1:
* Remove new error log level; should be in a future patch
* Properly fixed the asprintf problem
* Indentation no uses literal tabs
Signature · 1 line
Vincent
Vincent Hanquez writes ("Re: [Xen-devel] Re: [PATCH] libxl: check for early failures of qemu-dm (v2)"):
Quoted 4 lines
Quoted 3 lines · Ian Jackson
This patch makes xl create check whether qemu-dm has started correctly, and causes it to fail immediately with appropriate errors if not. There are other bugfixes too.
...
Quoted 8 lines
Quoted 6 lines
* The first fork generates an intermediate process which is responsible for writing the qemu-dm pid to xenstore and then merely waits to collect and report on qemu-dm's exit status during startup. New arguments to
libxl_create_device_modelallow the preservation of its pid so that a later call can check whether the startup is successful.Did you have the qemu-dm ready patch in qemu ?
This is not relevant because my qemu currently dies before it gets to that point. Stefano tested v1 of my patch and it worked for him.
Quoted 5 lines
Quoted 1 line
*
libxl_execno longer calls fork; there is a newlibxl_fork.this is not libxl goal to provide wrapper for syscalls.
the exec should do the double fork+exec itself, no need to have a fork function.
No, it can't, because there is actual functionality in the intermediate process. The libxl_fork function is not provided for the benefit of "providing wrapper for syscalls". It is there to factor out the common error handling for the two instances of fork in libxl.c.
Quoted 4 lines
Quoted 2 lines
* There is a hook to override waitpid, which will be necessary for some callers.
why ?
Why is it necessary ? Some applications have an event loop or SIGCHLD handler which automatically reaps all children. In such an application in order to collect a child process exit status we need to allow the application to look the process up in its own table of reaped processes.
Quoted 5 lines
Quoted 3 lines
*
_GNU_SOURCEshould be used throughout. The asprintf implementation should be disabled in favour of the system one.osdeps was just suppose to be available for netbsd. not sure why the contrary happens on christoph's patch.
The osdeps arrangements were broken and backwards. We were compiling them in on Linux, which isn't necessary. It's not necessary on BSD either, but BSD presumably barfed on it because it declared the system asprintf without special handling.
Quoted 5 lines
there shouldn't be a
LOG_ERROR_ERRNOvalue. what you want is the different log function
that will embedd an errno automatically in the fmt, not a different logging level. not sure what you meant later by "remove new error level, should be in future patch".
I mean that I'm going to do exactly that in a followup patch. The comment about LOG_ERROR_ERRNO was left over in my changeset comment by mistake.
Quoted 7 lines
Quoted 3 lines
*
destroy_device_modelcan kill random dom0 processes (!)* struct
libxl_ctxshould be defined inlibxl_internal.h.no, otherwise the init ctx need to allocate itself the memory of the context, instead of having
the caller "allocate it" itself on the fct stack.
If you do that then libxl can't be linked dynamically. We should talk about this.
Ian.
Quoted 7 lines · Ian Jackson
Quoted 3 lines
Did you have the qemu-dm ready patch in qemu ?
This is not relevant because my qemu currently dies before it gets to that point. Stefano tested v1 of my patch and it worked for him.
it's not irrelevant. as heavy discuss before, fixing the ready problem fix the qemu died in the meantime problem too (not ideally since you end-up waiting timeout, but it does). what you did is just an optimisation of the qemu died problem, which leave one problem open.
Quoted 6 lines
No, it can't, because there is actual functionality in the intermediate process. The
libxl_forkfunction is not provided for the benefit of "providing wrapper for syscalls". It is there to factor out the common error handling for the two instances of fork inlibxl.c.
you can have a middle process callback without any problem within the libxl_exec call.
Quoted 6 lines
Why is it necessary ? Some applications have an event loop or SIGCHLD handler which automatically reaps all children. In such an application in order to collect a child process exit status we need to allow the application to look the process up in its own table of reaped processes.
sounds a bit premature, since we don't even have such an application yet nor that we ever had in the past either.
Quoted 5 lines
The osdeps arrangements were broken and backwards. We were compiling them in on Linux, which isn't necessary. It's not necessary on BSD either, but BSD presumably barfed on it because it declared the system asprintf without special handling.
yep
Quoted 7 lines
Quoted 3 lines
no, otherwise the init ctx need to allocate itself the memory of the context, instead of having the caller "allocate it" itself on the fct stack.
If you do that then libxl can't be linked dynamically. We should talk about this.
it *can* be linked dynamically. the only "shortcoming" is the context structure can't grow dynamically. the same happens to all structures that we use for argument passing.
Signature · 1 line
Vincent