Skip to contentSkip to conversation

libxl: introduce asynchronous execution API

PATCH Superseded Yang Hongyang 3 participants 3 messages Apr 25, 2014 – May 2, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: introduce asynchronous execution API

1.introduce asynchronous execution API:
libxl__async_exec_init
libxl__async_exec_start
libxl__async_exec_inuse
2.use the async exec API to execute device hotplug scripts

---
 tools/libxl/libxl_aoutils.c  | 89 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_device.c   | 78 +++++++++++---------------------------
 tools/libxl/libxl_internal.h | 32 ++++++++++++++--
 3 files changed, 139 insertions(+), 60 deletions(-)
tools/libxl/libxl_aoutils.c +89 -0
index 1c9eb9e..6215a3d 100644
@@ -451,3 +451,92 @@ int libxl__openptys(libxl__openpty_state *op,
     return rc;
 }
 
+static void async_exec_timeout(libxl__egc *egc,
+                               libxl__ev_time *ev,
+                               const struct timeval *requested_abs)
+{
+    libxl__async_exec_state *aes = CONTAINER_OF(ev, *aes, time);
+    STATE_AO_GC(aes->ao);
+
+    libxl__ev_time_deregister(gc, &aes->time);
+
+    assert(libxl__ev_child_inuse(&aes->child));
+    LOG(DEBUG, "killing execution of %s because of timeout", aes->what);
+
+    if (kill(aes->child.pid, SIGKILL)) {
+        LOGEV(ERROR, errno, "unable to kill %s [%ld]",
+              aes->what, (unsigned long)aes->child.pid);
+    }
+
+    return;
+}
+
+static void async_exec_done(libxl__egc *egc,
+                            libxl__ev_child *child,
+                            pid_t pid, int status)
+{
+    libxl__async_exec_state *aes = CONTAINER_OF(child, *aes, child);
+    STATE_AO_GC(aes->ao);
+
+    libxl__ev_time_deregister(gc, &aes->time);
+
+    if (status) {
+        libxl_report_child_exitstatus(CTX, LIBXL__LOG_ERROR,
+                                      aes->what, pid, status);
+    }
+
+    aes->callback(egc, aes, status);
+}
+
+void libxl__async_exec_init(libxl__async_exec_state *aes)
+{
+    libxl__ev_time_init(&aes->time);
+    libxl__ev_child_init(&aes->child);
+}
+
+int libxl__async_exec_start(libxl__gc *gc, libxl__async_exec_state *aes)
+{
+    pid_t pid;
+
+    /* Convenience aliases */
+    libxl__ev_child *const child = &aes->child;
+    char ** const args = aes->args;
+
+    /* Set execution timeout */
+    if (libxl__ev_time_register_rel(gc, &aes->time,
+                                    async_exec_timeout,
+                                    aes->timeout_ms)) {
+        LOG(ERROR, "unable to register timeout for executing: %s", aes->what);
+        goto errout;
+    }
+
+    LOG(DEBUG, "asynchronously execute: %s ", aes->what);
+
+    /* Fork and exec */
+    pid = libxl__ev_child_fork(gc, child, async_exec_done);
+    if (pid == -1) {
+        LOG(ERROR, "unable to fork");
+        goto errout;
+    }
+
+    if (!pid) {
+        /* child */
+        libxl__exec(gc, aes->stdfds[0], aes->stdfds[1],
+                    aes->stdfds[2], args[0], args, aes->env);
+        /* notreached */
+        abort();
+    }
+
+    return 0;
+
+errout:
+    return ERROR_FAIL;
+}
+
+bool libxl__async_exec_inuse(const libxl__async_exec_state *aes)
+{
+    bool time_inuse = libxl__ev_time_isregistered(&aes->time);
+    bool child_inuse = libxl__ev_child_inuse(&aes->child);
+    assert(time_inuse == child_inuse);
+    return child_inuse;
+}
tools/libxl/libxl_device.c +22 -56

142 lines not on this page · raw message

tools/libxl/libxl_internal.h +28 -4
index c2b73c4..3b9acc5 100644
@@ -2030,6 +2030,31 @@ _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_state libxl__async_exec_state;
+
+typedef void libxl__async_exec_callback(libxl__egc *egc,
+                        libxl__async_exec_state *aes, int status);
+
+struct libxl__async_exec_state {
+    /* caller must fill these in */
+    libxl__ao *ao;
+    const char *what; /* for error msgs, what we're execute */
+    char **env; /* execution environment */
+    char **args; /* execution arguments */
+    libxl__async_exec_callback *callback;
+    int timeout_ms;
+    int stdfds[3];
+
+    /* private */
+    libxl__ev_time time;
+    libxl__ev_child child;
+};
+
+void libxl__async_exec_init(libxl__async_exec_state *aes);
+int libxl__async_exec_start(libxl__gc *gc, libxl__async_exec_state *aes);
+bool libxl__async_exec_inuse(const libxl__async_exec_state *aes);
+
 /*----- device addition/removal -----*/
 
 typedef struct libxl__ao_device libxl__ao_device;
@@ -2066,14 +2091,13 @@ struct libxl__ao_device {
     libxl__multidev *multidev; /* reference to the containing multidev */
     /* private for add/remove implementation */
     libxl__ev_devstate backend_ds;
-    /* Bodge for Qemu devices, also used for timeout of hotplug execution */
+    /* Bodge for Qemu devices */
     libxl__ev_time timeout;
     /* xenstore watch for backend path of driver domains */
     libxl__ev_xswatch xs_watch;
-    /* device hotplug execution */
-    const char *what;
     int num_exec;
-    libxl__ev_child child;
+    /* for calling hotplug scripts */
+    libxl__async_exec_state aes;
 };
 
 /*
Signature · 1 line
1.9.1
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Yang Hongyang

1.introduce asynchronous execution API:
libxl__async_exec_init
libxl__async_exec_start
libxl__async_exec_inuse
2.use the async exec API to execute device hotplug scripts

Thanks, the patch looks fine to me, just a couple of comments on style nits (but it can be applied as-is).

Quoted 19 lines
---
 tools/libxl/libxl_aoutils.c  | 89 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_device.c   | 78 +++++++++++---------------------------
 tools/libxl/libxl_internal.h | 32 ++++++++++++++--
 3 files changed, 139 insertions(+), 60 deletions(-)
tools/libxl/libxl_aoutils.c +1 -0
index 1c9eb9e..6215a3d 100644
@@ -451,3 +451,92 @@ int libxl__openptys(libxl__openpty_state *op,
     return rc;
 }
 
+static void async_exec_timeout(libxl__egc *egc,

tools/libxl/libxl_aoutils.c int libxl__openptys(libxl__openpty_state *op, · line 464

Quoted 10 lines
tools/libxl/libxl_aoutils.c +10 -0
+                               libxl__ev_time *ev,
+                               const struct timeval *requested_abs)
+{
+    libxl__async_exec_state *aes = CONTAINER_OF(ev, *aes, time);
+    STATE_AO_GC(aes->ao);
+
+    libxl__ev_time_deregister(gc, &aes->time);
+
+    assert(libxl__ev_child_inuse(&aes->child));
+    LOG(DEBUG, "killing execution of %s because of timeout", aes->what);

I would make the message above an ERROR, rather than DEBUG (I know it's a DEBUG message right now, but I think the message is relevant enough to be printed as an error).

Quoted 58 lines
tools/libxl/libxl_aoutils.c +58 -0
+
+    if (kill(aes->child.pid, SIGKILL)) {
+        LOGEV(ERROR, errno, "unable to kill %s [%ld]",
+              aes->what, (unsigned long)aes->child.pid);
+    }
+
+    return;
+}
+
+static void async_exec_done(libxl__egc *egc,
+                            libxl__ev_child *child,
+                            pid_t pid, int status)
+{
+    libxl__async_exec_state *aes = CONTAINER_OF(child, *aes, child);
+    STATE_AO_GC(aes->ao);
+
+    libxl__ev_time_deregister(gc, &aes->time);
+
+    if (status) {
+        libxl_report_child_exitstatus(CTX, LIBXL__LOG_ERROR,
+                                      aes->what, pid, status);
+    }
+
+    aes->callback(egc, aes, status);
+}
+
+void libxl__async_exec_init(libxl__async_exec_state *aes)
+{
+    libxl__ev_time_init(&aes->time);
+    libxl__ev_child_init(&aes->child);
+}
+
+int libxl__async_exec_start(libxl__gc *gc, libxl__async_exec_state *aes)
+{
+    pid_t pid;
+
+    /* Convenience aliases */
+    libxl__ev_child *const child = &aes->child;
+    char ** const args = aes->args;
+
+    /* Set execution timeout */
+    if (libxl__ev_time_register_rel(gc, &aes->time,
+                                    async_exec_timeout,
+                                    aes->timeout_ms)) {
+        LOG(ERROR, "unable to register timeout for executing: %s", aes->what);
+        goto errout;
+    }
+
+    LOG(DEBUG, "asynchronously execute: %s ", aes->what);
+
+    /* Fork and exec */
+    pid = libxl__ev_child_fork(gc, child, async_exec_done);
+    if (pid == -1) {
+        LOG(ERROR, "unable to fork");
+        goto errout;
+    }
+
+    if (!pid) {

tools/libxl/libxl_aoutils.c int libxl__openptys(libxl__openpty_state *op, · line 532

Quoted 10 lines
tools/libxl/libxl_aoutils.c +10 -0
+        /* child */
+        libxl__exec(gc, aes->stdfds[0], aes->stdfds[1],
+                    aes->stdfds[2], args[0], args, aes->env);
+        /* notreached */
+        abort();
+    }
+
+    return 0;
+
+errout:

Those kind of labels are usually just called "error" inside of libxl.

#2 lore raw

Re: [PATCH] libxl: introduce asynchronous execution API [and 1 more messages]

In reply to Yang Hongyang

Yang Hongyang writes ("[PATCH] libxl: introduce asynchronous execution API"):

Quoted 5 lines

1.introduce asynchronous execution API:
libxl__async_exec_init
libxl__async_exec_start
libxl__async_exec_inuse
2.use the async exec API to execute device hotplug scripts

Thanks, this is very good. Exactly what I meant.

I have only some very small nits to comment on:

Quoted 2 lines
(quoted diff) +2 -0
+int libxl__async_exec_start(libxl__gc *gc, libxl__async_exec_state *aes)
+{

...

Quoted 1 line

+ LOG(DEBUG, "asynchronously execute: %s ", aes->what);

I would write "forking to execute" in the message.

tools/libxl/libxl_internal.h _hidden const char *libxl__xen_script_dir_path(void); · line 2034

Quoted 11 lines

static void devices_remove_callback(libxl__egc *egc,

tools/libxl/libxl_internal.h +2 -0
index c2b73c4..3b9acc5 100644
@@ -2030,6 +2030,31 @@ _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_state libxl__async_exec_state;

I think this comment needs a blank line after it, and a better name. I suggest "subprocess execution with timeout".

tools/libxl/libxl_internal.h _hidden const char *libxl__xen_script_dir_path(void); · line 2042

Quoted 7 lines
tools/libxl/libxl_internal.h +7 -0
+typedef void libxl__async_exec_callback(libxl__egc *egc,
+                        libxl__async_exec_state *aes, int status);
+
+struct libxl__async_exec_state {
+    /* caller must fill these in */
+    libxl__ao *ao;
+    const char *what; /* for error msgs, what we're execute */

"what we're executing"

tools/libxl/libxl_internal.h _hidden const char *libxl__xen_script_dir_path(void); · line 2047

Quoted 5 lines
tools/libxl/libxl_internal.h +5 -0
+    char **env; /* execution environment */
+    char **args; /* execution arguments */
+    libxl__async_exec_callback *callback;
+    int timeout_ms;
+    int stdfds[3];

I would reorder these so that all the ones which are just the same as arguments to libxl__exec are together. And then add a comment

       /* caller must fill in; as for libxl__exec */

for that section.

Roger Pau Monné writes ("Re: [PATCH] libxl: introduce asynchronous execution API"):

Quoted 1 line

....

Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl_aoutils.c +0 -0
index 1c9eb9e..6215a3d 100644

...

Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl_aoutils.c +4 -0
+static void async_exec_timeout(libxl__egc *egc,
+                               libxl__ev_time *ev,
+                               const struct timeval *requested_abs)
+{

...

Quoted 6 lines

tools/libxl/libxl_aoutils.c int libxl__openptys(libxl__openpty_state *op, · line 464

Quoted 2 lines
tools/libxl/libxl_aoutils.c +2 -0
+    assert(libxl__ev_child_inuse(&aes->child));
+    LOG(DEBUG, "killing execution of %s because of timeout", aes->what);

I would make the message above an ERROR, rather than DEBUG (I know it's a DEBUG message right now, but I think the message is relevant enough to be printed as an error).

I think I agree.

Quoted 2 lines
Quoted 2 lines
tools/libxl/libxl_aoutils.c +2 -0
+int libxl__async_exec_start(libxl__gc *gc, libxl__async_exec_state *aes)
+{

...

Quoted 5 lines
Quoted 5 lines
tools/libxl/libxl_aoutils.c +5 -0
+    if (libxl__ev_time_register_rel(gc, &aes->time,
+                                    async_exec_timeout,
+                                    aes->timeout_ms)) {
+        LOG(ERROR, "unable to register timeout for executing: %s", aes->what);
+        goto errout;

...

Quoted 3 lines
Quoted 1 line

+errout:

Those kind of labels are usually just called "error" inside of libxl.

Actually, they are normally called "out". See eg libxl__datacopier_start, libxl__openptys, in that same file.

It should be called "out" here too.

Thanks,
Ian.