From: Yang Hongyang Subject: [PATCH V9 01/12] introduce an API to async exec scripts Date: Tue, 15 Apr 2014 13:38:09 +0800 Message-ID: <1397540297-32184-2-git-send-email-yanghy@cn.fujitsu.com> References: <1397540297-32184-1-git-send-email-yanghy@cn.fujitsu.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1397540297-32184-1-git-send-email-yanghy@cn.fujitsu.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: ian.campbell@citrix.com, wency@cn.fujitsu.com, stefano.stabellini@eu.citrix.com, andrew.cooper3@citrix.com, yunhong.jiang@intel.com, ian.jackson@eu.citrix.com, eddie.dong@intel.com, rshriram@cs.ubc.ca, roger.pau@citrix.com, laijs@cn.fujitsu.com List-Id: xen-devel@lists.xenproject.org introduce an API to async exec scripts.it will be used for both device and netbuffer. Signed-off-by: Lai Jiangshan Signed-off-by: Wen Congyang --- tools/libxl/libxl_internal.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 21 ++++++++++++ 2 files changed, 103 insertions(+) diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 6c94d3e..e47eb90 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -375,6 +375,88 @@ out: return rc; } +static void libxl_async_exec_timeout(libxl__egc *egc, + libxl__ev_time *ev, + const struct timeval *requested_abs) +{ + libxl_async_exec *async_exec = CONTAINER_OF(ev, *async_exec, time); + + STATE_AO_GC(async_exec->ao); + + libxl__ev_time_deregister(gc, &async_exec->time); + assert(libxl__ev_child_inuse(&async_exec->child)); + + LOG(DEBUG, "killing hotplug script %s because of timeout", + async_exec->args[0]); + + if (kill(async_exec->child.pid, SIGKILL)) { + LOGEV(ERROR, errno, "unable to kill hotplug script %s [%ld]", + async_exec->args[0], + (unsigned long)async_exec->child.pid); + } + + return; +} + +static void libxl_async_exec_done(libxl__egc *egc, + libxl__ev_child *child, + pid_t pid, int status) +{ + libxl_async_exec *async_exec = CONTAINER_OF(child, *async_exec, child); + + STATE_AO_GC(async_exec->ao); + + libxl__ev_time_deregister(gc, &async_exec->time); + + if (status && !async_exec->allow_fail) { + libxl_report_child_exitstatus(CTX, LIBXL__LOG_ERROR, + async_exec->args[0], + pid, status); + } + + async_exec->finish_cb(async_exec->opaque, status); +} + +int libxl_async_exec_script(libxl__gc *gc, libxl_async_exec *async_exec) +{ + pid_t pid; + + /* Convenience aliases */ + libxl__ev_child *const child = &async_exec->child; + char * const *args = async_exec->args; + char * const *env = async_exec->env; + const int stdinfd = async_exec->stdinfd; + const int stdoutfd = async_exec->stdoutfd; + const int stderrfd = async_exec->stderrfd; + + /* Set hotplug timeout */ + if (libxl__ev_time_register_rel(gc, &async_exec->time, + libxl_async_exec_timeout, + async_exec->timeout * 1000)) { + LOG(ERROR, "unable to register timeout for " + "script %s", args[0]); + return ERROR_FAIL; + } + + LOG(DEBUG, "Calling script: %s ", args[0]); + + /* Fork and exec netbuf script */ + pid = libxl__ev_child_fork(gc, child, libxl_async_exec_done); + if (pid == -1) { + LOG(ERROR, "unable to fork for script %s", args[0]); + return ERROR_FAIL; + } + + if (!pid) { + /* child: Launch netbuf script */ + libxl__exec(gc, stdinfd, stdoutfd, stderrfd, args[0], args, env); + /* notreached */ + abort(); + } + + return 0; +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index c2b73c4..eddafaf 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -2030,6 +2030,27 @@ _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); +/*----- asynchronous function -----*/ +typedef struct libxl_async_exec { + char **env; + char **args; + void *opaque; + void (*finish_cb)(void *opaque, int status); + /* unit: second */ + int timeout; + bool allow_fail; + int stdinfd; + int stdoutfd; + int stderrfd; + + libxl__ev_time time; + libxl__ev_child child; + libxl__ao *ao; +} libxl_async_exec; + +_hidden extern int libxl_async_exec_script(libxl__gc *gc, + libxl_async_exec *async_exec); + /*----- device addition/removal -----*/ typedef struct libxl__ao_device libxl__ao_device; -- 1.8.3.2