Skip to contentSkip to conversation

libxl: event API

PATCH Superseded Ian Jackson 3 participants 43 messages Jan 6, 2012 – Jan 13, 2012

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover libxl: event API

This series is the latest revision of my event handling API. It includes changes in response to a number of comments made. It also includes a few new consequential patches and unrelated fixes/improvements, as usual.

The most significant difference is the new "libxl__egc" type which is used instead of libxl__gc by event-generating functions. This prevents accidental violation of the callback reentrancy rules.

I'm still working on this series. At the moment I'm working on providing asynchronous calls for long-running operations; this requires more support machinery which is still not finalised.

Of the resulting patches in this series,

These should perhaps go in soon:
01/10 libxl: make LIBXL_INIT_GC a statement, not an initialiser 03/10 libxl: move a lot more includes into libxl_internal.h 04/10 libxl: Provide more formal libxl__ctx_lock and _unlock 05/10 libxl: Fix leaks on context init failure
09/10 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

This one has now been tested and can go in IMO:
02/10 xenstore: New function xs_path_is_subpath

These are the meat:
07/10 libxl: New API for providing OS events to libxl 08/10 libxl: New event generation API
10/10 libxl: Permit multithreaded event waiting

And this one has become rendered obsolete. If we are to retain the new libxl__egc structure, I will drop it. Otherwise it may still be needed, and it's disruptive, which is why I haven't dropped it from my series yet:
06/10 DROP: libxl: rename libxl__free_all

#1 lore raw

PATCH v6 1/10 libxl: make LIBXL_INIT_GC a statement, not an initialiser

Previously LIBXL_INIT_GC was an initialiser, which you were expected to use like this:

    libxl__gc gc = LIBXL_INIT_GC(ctx);

But we are going to want to put things in the gc which are to be initialised using other macros. That means that LIBXL_INIT_GC has to become a statement too. So instead, we make it so that it's used like this:

    libxl_gc gc;
    LIBXL_INIT_GC(gc,ctx);

In fact there are only a couple of callers now,
including GC_INIT which uses this trick:

    libxl_gc gc[1];
    LIBXL_INIT_GC(gc[0],ctx);
---
 tools/libxl/libxl_internal.h |    9 +++++++--
 tools/libxl/libxl_qmp.c      |    2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)
tools/libxl/libxl_internal.h +7 -2
index fa7fb16..1bca869 100644
@@ -147,7 +147,12 @@ typedef struct {
     libxl_ctx *owner;
 } libxl__gc;
 
-#define LIBXL_INIT_GC(ctx) (libxl__gc){ .alloc_maxsize = 0, .alloc_ptrs = 0, .owner = ctx }
+#define LIBXL_INIT_GC(gc,ctx) do{               \
+        (gc).alloc_maxsize = 0;                 \
+        (gc).alloc_ptrs = 0;                    \
+        (gc).owner = (ctx);                     \
+    } while(0)
+
 static inline libxl_ctx *libxl__gc_owner(libxl__gc *gc)
 {
     return gc->owner;
@@ -721,7 +726,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
  * as a local variable.
  */
 
-#define GC_INIT(ctx)  libxl__gc gc[1] = { LIBXL_INIT_GC(ctx) }
+#define GC_INIT(ctx)  libxl__gc gc[1]; LIBXL_INIT_GC(gc[0],ctx)
 #define GC_FREE       libxl__free_all(gc)
 #define CTX           libxl__gc_owner(gc)
 
tools/libxl/libxl_qmp.c +1 -1
index 1777e44..3dfa43a 100644
@@ -513,7 +513,7 @@ static int qmp_send(libxl__qmp_handler *qmp,
 {
     char *buf = NULL;
     int rc = -1;
-    libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
+    libxl__gc gc; LIBXL_INIT_GC(gc,qmp->ctx);
 
     buf = qmp_send_prepare(&gc, qmp, cmd, args, callback, opaque, context);
 
Signature · 1 line
1.7.2.5
#2 lore raw

PATCH v6 2/10 xenstore: New function xs_path_is_subpath

In reply to Ian Jackson

This utility function compares two paths, textually and reports whether one is a subpath (a child path) of the other.

---
 tools/xenstore/xs.c |   17 +++++++++++++++++
 tools/xenstore/xs.h |    7 +++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
tools/xenstore/xs.c +17 -0
index 8e54fe0..0a01675 100644
@@ -950,6 +950,23 @@ char *xs_get_domain_path(struct xs_handle *h, unsigned int domid)
 	return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL);
 }
 
+bool xs_path_is_subpath(const char *parent, const char *child)
+{
+        size_t childlen = strlen(child);
+        size_t parentlen = strlen(parent);
+
+	if (childlen < parentlen)
+		return false;
+
+	if (memcmp(child, parent, parentlen))
+		return false;
+
+	if (childlen > parentlen && child[parentlen] != '/')
+		return false;
+
+	return true;
+}
+
 bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid)
 {
 	char *domain = single_with_domid(h, XS_IS_DOMAIN_INTRODUCED, domid);
tools/xenstore/xs.h +7 -0
index 63f535d..8d49e50 100644
@@ -207,6 +207,13 @@ bool xs_release_domain(struct xs_handle *h, unsigned int domid);
  */
 char *xs_get_domain_path(struct xs_handle *h, unsigned int domid);
 
+/* Returns true if child is either equal to parent, or a node underneath
+ * parent; or false otherwise.  Done by string comparison, so relative and
+ * absolute pathnames never in a parent/child relationship by this
+ * definition.  Cannot fail.
+ */
+bool xs_path_is_subpath(const char *parent, const char *child);
+
 /* Return whether the domain specified has been introduced to xenstored.
  */
 bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid);
Signature · 1 line
1.7.2.5
Acked-by on v6 2/10
#3 lore raw

Re: [PATCH 02/10] xenstore: New function xs_path_is_subpath

Quoted 4 lines · Ian Jackson

This utility function compares two paths, textually and reports whether one is a subpath (a child path) of the other.

Quoted 51 lines
---
 tools/xenstore/xs.c |   17 +++++++++++++++++
 tools/xenstore/xs.h |    7 +++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
tools/xenstore/xs.c +17 -0
index 8e54fe0..0a01675 100644
@@ -950,6 +950,23 @@ char *xs_get_domain_path(struct xs_handle *h, unsigned int domid)
 	return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL);
 }
 
+bool xs_path_is_subpath(const char *parent, const char *child)
+{
+        size_t childlen = strlen(child);
+        size_t parentlen = strlen(parent);
+
+	if (childlen < parentlen)
+		return false;
+
+	if (memcmp(child, parent, parentlen))
+		return false;
+
+	if (childlen > parentlen && child[parentlen] != '/')
+		return false;
+
+	return true;
+}
+
 bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid)
 {
 	char *domain = single_with_domid(h, XS_IS_DOMAIN_INTRODUCED, domid);
tools/xenstore/xs.h +7 -0
index 63f535d..8d49e50 100644
@@ -207,6 +207,13 @@ bool xs_release_domain(struct xs_handle *h, unsigned int domid);
  */
 char *xs_get_domain_path(struct xs_handle *h, unsigned int domid);
 
+/* Returns true if child is either equal to parent, or a node underneath
+ * parent; or false otherwise.  Done by string comparison, so relative and
+ * absolute pathnames never in a parent/child relationship by this
+ * definition.  Cannot fail.
+ */
+bool xs_path_is_subpath(const char *parent, const char *child);
+
 /* Return whether the domain specified has been introduced to xenstored.
  */
 bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid);
#4 lore raw

PATCH v6 3/10 libxl: move a lot more includes into libxl_internal.h

In reply to Ian Jackson

Move a lot of
#include <stdfoo.h>
from individual files into libxl_internal.h. This helps avoid portability mistakes where necessary system headers are omitted from individual files, and is also of course a convenience when developing.

Also add
#include "libxl_osdeps.h" /* must come before any other headers */
to the top of most libxl*.c files, so that anyone who adds any headers before libxl_internal.h will put the in the right place.

---
 tools/libxl/libxl.c            |   15 ---------------
 tools/libxl/libxl_blktap2.c    |    4 +---
 tools/libxl/libxl_bootloader.c |    7 +------
 tools/libxl/libxl_cpuid.c      |    2 ++
 tools/libxl/libxl_create.c     |   13 +++----------
 tools/libxl/libxl_device.c     |   10 +---------
 tools/libxl/libxl_dm.c         |   10 +---------
 tools/libxl/libxl_dom.c        |   11 +----------
 tools/libxl/libxl_exec.c       |   13 +------------
 tools/libxl/libxl_flask.c      |    8 +-------
 tools/libxl/libxl_internal.c   |   10 +---------
 tools/libxl/libxl_internal.h   |   22 +++++++++++++++++++---
 tools/libxl/libxl_json.c       |    4 +---
 tools/libxl/libxl_linux.c      |    2 +-
 tools/libxl/libxl_netbsd.c     |    2 +-
 tools/libxl/libxl_noblktap2.c  |    2 ++
 tools/libxl/libxl_nocpuid.c    |    2 ++
 tools/libxl/libxl_paths.c      |    1 +
 tools/libxl/libxl_pci.c        |   16 +---------------
 tools/libxl/libxl_qmp.c        |    4 +---
 tools/libxl/libxl_utils.c      |   13 +------------
 tools/libxl/libxl_uuid.c       |    2 +-
 tools/libxl/libxl_xshelp.c     |    8 +-------
 tools/libxl/libxlu_cfg.c       |    2 ++
 tools/libxl/libxlu_cfg_i.h     |    1 +
 tools/libxl/libxlu_disk.c      |    1 +
 tools/libxl/libxlu_disk_i.h    |    2 ++
 27 files changed, 51 insertions(+), 136 deletions(-)
tools/libxl/libxl.c +0 -15
index 2b8f8f4..2d3e8cd 100644
@@ -16,21 +16,6 @@
 
 #include "libxl_osdeps.h"
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/select.h>
-#include <sys/wait.h>
-#include <sys/time.h>
-#include <signal.h>
-#include <unistd.h> /* for write, unlink and close */
-#include <stdint.h>
-#include <inttypes.h>
-#include <assert.h>
-
 #include "libxl_internal.h"
 
 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
tools/libxl/libxl_blktap2.c +1 -3
index acf4110..2c40182 100644
@@ -12,13 +12,11 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxl_internal.h"
 
 #include "tap-ctl.h"
 
-#include <string.h>
-
 int libxl__blktap_enabled(libxl__gc *gc)
 {
     const char *msg;
tools/libxl/libxl_bootloader.c +1 -6
index ce83b8e..2da1d90 100644
@@ -12,15 +12,10 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <unistd.h>
-#include <fcntl.h>
 #include <termios.h>
 
-#include <sys/stat.h>
-#include <sys/types.h>
-
 #include "libxl_internal.h"
 
 #define XENCONSOLED_BUF_SIZE 16
tools/libxl/libxl_cpuid.c +2 -0
index 56a00cd..dcdb9d02 100644
@@ -10,6 +10,8 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxl_internal.h"
 
 void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list)
tools/libxl/libxl_create.c +4 -11
index ebf2ed7..9a6a94a 100644
@@ -15,20 +15,13 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <xenctrl.h>
-#include <xc_dom.h>
-#include <xenguest.h>
-#include <assert.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
+#include <xc_dom.h>
+#include <xenguest.h>
+
 void libxl_domain_config_dispose(libxl_domain_config *d_config)
 {
     int i;
tools/libxl/libxl_device.c +1 -9
index 9b1fc57..5d05e90 100644
@@ -14,15 +14,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <string.h>
-#include <stdio.h>
-#include <sys/time.h> /* for struct timeval */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_dm.c +1 -9
index 97d91b4..f0bf014 100644
@@ -15,15 +15,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <assert.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_dom.c +1 -10
index c898d89..b2259f8 100644
@@ -13,22 +13,13 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <stdio.h>
-#include <assert.h>
 #include <glob.h>
-#include <inttypes.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <sys/time.h> /* for struct timeval */
-#include <sys/stat.h> /* for stat */
-#include <unistd.h> /* for sleep(2) */
 
 #include <xenctrl.h>
 #include <xc_dom.h>
 #include <xenguest.h>
-#include <fcntl.h>
 
 #include <xen/hvm/hvm_info_table.h>
 
tools/libxl/libxl_exec.c +1 -12
index 52d40d1..b10e79f 100644
@@ -15,18 +15,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <assert.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <signal.h> /* for SIGKILL */
-#include <fcntl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_flask.c +1 -7
index 6b548dd..23f2476 100644
@@ -7,13 +7,7 @@
  *  as published by the Free Software Foundation.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <xenctrl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_internal.c +1 -9
index cfa8c61..49b0dab 100644
@@ -13,15 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <unistd.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_internal.h +19 -3
index 1bca869..d681d73 100644
@@ -17,17 +17,33 @@
 #ifndef LIBXL_INTERNAL_H
 #define LIBXL_INTERNAL_H
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <stdint.h>
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <signal.h>
 #include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <pthread.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/select.h>
+#include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include <xs.h>
 #include <xenctrl.h>
+
 #include "xentoollog.h"
 
 #include <xen/io/xenbus.h>
tools/libxl/libxl_json.c +1 -3
index c0f869e..6ff2910 100644
@@ -12,10 +12,8 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <assert.h>
-#include <string.h>
 #include <math.h>
 
 #include <yajl/yajl_parse.h>
tools/libxl/libxl_linux.c +1 -1
index 786c6b5..925248b 100644
@@ -13,7 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
  
-#include <sys/stat.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
  
tools/libxl/libxl_netbsd.c +1 -1
index 1e8d622..9e0ed6d 100644
@@ -13,7 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
  
-#include <sys/stat.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_noblktap2.c +2 -0
index 3307551..246b0de 100644
@@ -12,6 +12,8 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxl_internal.h"
 
 int libxl__blktap_enabled(libxl__gc *gc)
tools/libxl/libxl_nocpuid.c +2 -0
index 2e9490c..9e52f8d 100644
@@ -10,6 +10,8 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxl_internal.h"
 
 void libxl_cpuid_destroy(libxl_cpuid_policy_list *p_cpuid_list)
tools/libxl/libxl_paths.c +1 -0
index e7bd1a2..a95d29f 100644
@@ -12,6 +12,7 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxl_internal.h"
 #include "_libxl_paths.h"
 
tools/libxl/libxl_pci.c +1 -15
index 8b2a1c5..c3828f6 100644
@@ -14,21 +14,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/select.h>
-#include <sys/mman.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <signal.h>
-#include <unistd.h> /* for write, unlink and close */
-#include <inttypes.h>
-#include <dirent.h>
-#include <assert.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_qmp.c +1 -3
index 3dfa43a..61d9769 100644
@@ -18,12 +18,10 @@
  * Specification, see in the QEMU repository.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <unistd.h>
 #include <sys/un.h>
 #include <sys/queue.h>
-#include <fcntl.h>
 
 #include <yajl/yajl_gen.h>
 
tools/libxl/libxl_utils.c +2 -13
index d36c737..dbe8891 100644
@@ -13,20 +13,9 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <xs.h>
-#include <xenctrl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include <ctype.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <assert.h>
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_uuid.c +1 -1
index 80ab789..7c18d71 100644
@@ -12,7 +12,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include <libxl_uuid.h>
 
tools/libxl/libxl_xshelp.c +1 -7
index ea835e2..6958d21 100644
@@ -13,13 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <string.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <inttypes.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxlu_cfg.c +2 -0
index 0d1c5d3..e3659c7 100644
@@ -16,6 +16,8 @@
  */
 
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include <limits.h>
 
 #include "libxlu_internal.h"
tools/libxl/libxlu_cfg_i.h +1 -0
index ea6a326..54d033c 100644
@@ -18,6 +18,7 @@
 #ifndef LIBXLU_CFG_I_H
 #define LIBXLU_CFG_I_H
 
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxlu_internal.h"
 #include "libxlu_cfg_y.h"
 
tools/libxl/libxlu_disk.c +1 -0
index 88b79ac..6cd86e9 100644
@@ -1,3 +1,4 @@
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxlu_internal.h"
 #include "libxlu_disk_l.h"
 #include "libxlu_disk_i.h"
tools/libxl/libxlu_disk_i.h +2 -0
index 4fccd4a..37246f2 100644
@@ -1,6 +1,8 @@
 #ifndef LIBXLU_DISK_I_H
 #define LIBXLU_DISK_I_H
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxlu_internal.h"
 
 
Signature · 1 line
1.7.2.5
Acked-by on v6 3/10
#5 lore raw

Re: [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h

Quoted 10 lines · Ian Jackson

Move a lot of
#include <stdfoo.h>
from individual files into libxl_internal.h. This helps avoid portability mistakes where necessary system headers are omitted from individual files, and is also of course a convenience when developing.

Also add
#include "libxl_osdeps.h" /* must come before any other headers */
to the top of most libxl*.c files, so that anyone who adds any headers before libxl_internal.h will put the in the right place.

Could we put this first in libxl_internal.h and apply that constraint to that header instead?

Either way I don't mind this patch so:

Quoted 553 lines
---
 tools/libxl/libxl.c            |   15 ---------------
 tools/libxl/libxl_blktap2.c    |    4 +---
 tools/libxl/libxl_bootloader.c |    7 +------
 tools/libxl/libxl_cpuid.c      |    2 ++
 tools/libxl/libxl_create.c     |   13 +++----------
 tools/libxl/libxl_device.c     |   10 +---------
 tools/libxl/libxl_dm.c         |   10 +---------
 tools/libxl/libxl_dom.c        |   11 +----------
 tools/libxl/libxl_exec.c       |   13 +------------
 tools/libxl/libxl_flask.c      |    8 +-------
 tools/libxl/libxl_internal.c   |   10 +---------
 tools/libxl/libxl_internal.h   |   22 +++++++++++++++++++---
 tools/libxl/libxl_json.c       |    4 +---
 tools/libxl/libxl_linux.c      |    2 +-
 tools/libxl/libxl_netbsd.c     |    2 +-
 tools/libxl/libxl_noblktap2.c  |    2 ++
 tools/libxl/libxl_nocpuid.c    |    2 ++
 tools/libxl/libxl_paths.c      |    1 +
 tools/libxl/libxl_pci.c        |   16 +---------------
 tools/libxl/libxl_qmp.c        |    4 +---
 tools/libxl/libxl_utils.c      |   13 +------------
 tools/libxl/libxl_uuid.c       |    2 +-
 tools/libxl/libxl_xshelp.c     |    8 +-------
 tools/libxl/libxlu_cfg.c       |    2 ++
 tools/libxl/libxlu_cfg_i.h     |    1 +
 tools/libxl/libxlu_disk.c      |    1 +
 tools/libxl/libxlu_disk_i.h    |    2 ++
 27 files changed, 51 insertions(+), 136 deletions(-)
tools/libxl/libxl.c +0 -15
index 2b8f8f4..2d3e8cd 100644
@@ -16,21 +16,6 @@
 
 #include "libxl_osdeps.h"
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/select.h>
-#include <sys/wait.h>
-#include <sys/time.h>
-#include <signal.h>
-#include <unistd.h> /* for write, unlink and close */
-#include <stdint.h>
-#include <inttypes.h>
-#include <assert.h>
-
 #include "libxl_internal.h"
 
 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
tools/libxl/libxl_blktap2.c +1 -3
index acf4110..2c40182 100644
@@ -12,13 +12,11 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxl_internal.h"
 
 #include "tap-ctl.h"
 
-#include <string.h>
-
 int libxl__blktap_enabled(libxl__gc *gc)
 {
     const char *msg;
tools/libxl/libxl_bootloader.c +1 -6
index ce83b8e..2da1d90 100644
@@ -12,15 +12,10 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <unistd.h>
-#include <fcntl.h>
 #include <termios.h>
 
-#include <sys/stat.h>
-#include <sys/types.h>
-
 #include "libxl_internal.h"
 
 #define XENCONSOLED_BUF_SIZE 16
tools/libxl/libxl_cpuid.c +2 -0
index 56a00cd..dcdb9d02 100644
@@ -10,6 +10,8 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxl_internal.h"
 
 void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list)
tools/libxl/libxl_create.c +4 -11
index ebf2ed7..9a6a94a 100644
@@ -15,20 +15,13 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <xenctrl.h>
-#include <xc_dom.h>
-#include <xenguest.h>
-#include <assert.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
+#include <xc_dom.h>
+#include <xenguest.h>
+
 void libxl_domain_config_dispose(libxl_domain_config *d_config)
 {
     int i;
tools/libxl/libxl_device.c +1 -9
index 9b1fc57..5d05e90 100644
@@ -14,15 +14,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <string.h>
-#include <stdio.h>
-#include <sys/time.h> /* for struct timeval */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_dm.c +1 -9
index 97d91b4..f0bf014 100644
@@ -15,15 +15,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <assert.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_dom.c +1 -10
index c898d89..b2259f8 100644
@@ -13,22 +13,13 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <stdio.h>
-#include <assert.h>
 #include <glob.h>
-#include <inttypes.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <sys/time.h> /* for struct timeval */
-#include <sys/stat.h> /* for stat */
-#include <unistd.h> /* for sleep(2) */
 
 #include <xenctrl.h>
 #include <xc_dom.h>
 #include <xenguest.h>
-#include <fcntl.h>
 
 #include <xen/hvm/hvm_info_table.h>
 
tools/libxl/libxl_exec.c +1 -12
index 52d40d1..b10e79f 100644
@@ -15,18 +15,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <assert.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <signal.h> /* for SIGKILL */
-#include <fcntl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_flask.c +1 -7
index 6b548dd..23f2476 100644
@@ -7,13 +7,7 @@
  *  as published by the Free Software Foundation.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <xenctrl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_internal.c +1 -9
index cfa8c61..49b0dab 100644
@@ -13,15 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <unistd.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_internal.h +19 -3
index 1bca869..d681d73 100644
@@ -17,17 +17,33 @@
 #ifndef LIBXL_INTERNAL_H
 #define LIBXL_INTERNAL_H
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <stdint.h>
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <signal.h>
 #include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <pthread.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/select.h>
+#include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include <xs.h>
 #include <xenctrl.h>
+
 #include "xentoollog.h"
 
 #include <xen/io/xenbus.h>
tools/libxl/libxl_json.c +1 -3
index c0f869e..6ff2910 100644
@@ -12,10 +12,8 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <assert.h>
-#include <string.h>
 #include <math.h>
 
 #include <yajl/yajl_parse.h>
tools/libxl/libxl_linux.c +1 -1
index 786c6b5..925248b 100644
@@ -13,7 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include <sys/stat.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_netbsd.c +1 -1
index 1e8d622..9e0ed6d 100644
@@ -13,7 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include <sys/stat.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_noblktap2.c +2 -0
index 3307551..246b0de 100644
@@ -12,6 +12,8 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxl_internal.h"
 
 int libxl__blktap_enabled(libxl__gc *gc)
tools/libxl/libxl_nocpuid.c +2 -0
index 2e9490c..9e52f8d 100644
@@ -10,6 +10,8 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxl_internal.h"
 
 void libxl_cpuid_destroy(libxl_cpuid_policy_list *p_cpuid_list)
tools/libxl/libxl_paths.c +1 -0
index e7bd1a2..a95d29f 100644
@@ -12,6 +12,7 @@
  * GNU Lesser General Public License for more details.
  */
 
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxl_internal.h"
 #include "_libxl_paths.h"
 
tools/libxl/libxl_pci.c +1 -15
index 8b2a1c5..c3828f6 100644
@@ -14,21 +14,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/select.h>
-#include <sys/mman.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <signal.h>
-#include <unistd.h> /* for write, unlink and close */
-#include <inttypes.h>
-#include <dirent.h>
-#include <assert.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_qmp.c +1 -3
index 3dfa43a..61d9769 100644
@@ -18,12 +18,10 @@
  * Specification, see in the QEMU repository.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <unistd.h>
 #include <sys/un.h>
 #include <sys/queue.h>
-#include <fcntl.h>
 
 #include <yajl/yajl_gen.h>
 
tools/libxl/libxl_utils.c +2 -13
index d36c737..dbe8891 100644
@@ -13,20 +13,9 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <xs.h>
-#include <xenctrl.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include <ctype.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <assert.h>
 
 #include "libxl_internal.h"
 
tools/libxl/libxl_uuid.c +1 -1
index 80ab789..7c18d71 100644
@@ -12,7 +12,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include <libxl_uuid.h>
 
tools/libxl/libxl_xshelp.c +1 -7
index ea835e2..6958d21 100644
@@ -13,13 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include "libxl_osdeps.h"
-
-#include <string.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <inttypes.h>
+#include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
 
tools/libxl/libxlu_cfg.c +2 -0
index 0d1c5d3..e3659c7 100644
@@ -16,6 +16,8 @@
  */
 
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include <limits.h>
 
 #include "libxlu_internal.h"
tools/libxl/libxlu_cfg_i.h +1 -0
index ea6a326..54d033c 100644
@@ -18,6 +18,7 @@
 #ifndef LIBXLU_CFG_I_H
 #define LIBXLU_CFG_I_H
 
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxlu_internal.h"
 #include "libxlu_cfg_y.h"
 
tools/libxl/libxlu_disk.c +1 -0
index 88b79ac..6cd86e9 100644
@@ -1,3 +1,4 @@
+#include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxlu_internal.h"
 #include "libxlu_disk_l.h"
 #include "libxlu_disk_i.h"
tools/libxl/libxlu_disk_i.h +2 -1
index 4fccd4a..37246f2 100644
@@ -1,6 +1,8 @@
 #ifndef LIBXLU_DISK_I_H
 #define LIBXLU_DISK_I_H
 
+#include "libxl_osdeps.h" /* must come before any other headers */
+
 #include "libxlu_internal.h"
 
 
--

1.7.2.5

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#6 lore raw

Re: [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h

Ian Campbell writes ("Re: [Xen-devel] [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h"):

Quoted 2 lines

Could we put this first in libxl_internal.h and apply that constraint to that header instead?

In theory, yes. But in practice people very much seem to like to add new system headers to individual files. And if you forget and add them in the wrong place it can lead to subtle portability bugs which you might not notice.

So I think this is a more obvious way to do it, even though it's an extra #include line in each file.

Ian.

#7 lore raw

Re: [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h

Quoted 11 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h"):

Quoted 2 lines

Could we put this first in libxl_internal.h and apply that constraint to that header instead?

In theory, yes. But in practice people very much seem to like to add new system headers to individual files. And if you forget and add them in the wrong place it can lead to subtle portability bugs which you might not notice.

So I think this is a more obvious way to do it, even though it's an extra #include line in each file.

Sure, that fine.

I suppose using -include (or whatever its real name is) on the compiler command line is too skanky?

Ian.

#8 lore raw

Re: [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h

Ian Campbell writes ("Re: [Xen-devel] [PATCH 03/10] libxl: move a lot more includes into libxl_internal.h"):

Quoted 8 lines · Ian Jackson
Quoted 2 lines

So I think this is a more obvious way to do it, even though it's an extra #include line in each file.

Sure, that fine.

I suppose using -include (or whatever its real name is) on the compiler command line is too skanky?

Personally I think that's a very annoying hack and should be avoided.

Ian.

#9 lore raw

PATCH v6 4/10 libxl: Provide more formal libxl__ctx_lock and _unlock

In reply to Ian Jackson

Previously the only official interface for the ctx lock was the CTX_LOCK and CTX_UNLOCK convenience macros, which assume and use "ctx" from the surrounding scope.

Instead, provide libxl__ctx_lock and _unlock functions which can be used by these convenience macros, and other callers who have nonstandard requirements.

---
 tools/libxl/libxl_internal.h |   23 +++++++++++++----------
 1 files changed, 13 insertions(+), 10 deletions(-)
tools/libxl/libxl_internal.h +13 -10
index d681d73..1dadf15 100644
@@ -115,7 +115,8 @@ struct libxl__ctx {
     struct xs_handle *xsh;
 
     pthread_mutex_t lock; /* protects data structures hanging off the ctx */
-      /* Always use CTX_LOCK and CTX_UNLOCK to manipulate this.
+      /* Always use libxl__ctx_lock and _unlock (or the convenience
+       * macors CTX_LOCK and CTX_UNLOCK) to manipulate this.
        *
        * You may acquire this mutex recursively if it is convenient to
        * do so.  You may not acquire this lock at the same time as any
@@ -749,16 +750,18 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
 
 /* Locking functions.  See comment for "lock" member of libxl__ctx. */
 
-#define CTX_LOCK do {                                   \
-        int mutex_r = pthread_mutex_lock(&CTX->lock);   \
-        assert(!mutex_r);                               \
-    } while(0)
+static inline void libxl__ctx_lock(libxl_ctx *ctx) {
+    int r = pthread_mutex_lock(&ctx->lock);
+    assert(!r);
+}
 
-#define CTX_UNLOCK do {                                 \
-        int mutex_r = pthread_mutex_unlock(&CTX->lock); \
-        assert(!mutex_r);                               \
-    } while(0)
-        
+static inline void libxl__ctx_unlock(libxl_ctx *ctx) {
+    int r = pthread_mutex_unlock(&ctx->lock);
+    assert(!r);
+}
+
+#define CTX_LOCK (libxl__ctx_lock(CTX))
+#define CTX_UNLOCK (libxl__ctx_unlock(CTX))
 
 
 /*
Signature · 1 line
1.7.2.5
Acked-by on v6 4/10
#10 lore raw

Re: [PATCH 04/10] libxl: Provide more formal libxl__ctx_lock and _unlock

Quoted 9 lines · Ian Jackson

Previously the only official interface for the ctx lock was the CTX_LOCK and CTX_UNLOCK convenience macros, which assume and use "ctx" from the surrounding scope.

Instead, provide libxl__ctx_lock and _unlock functions which can be used by these convenience macros, and other callers who have nonstandard requirements.

Quoted 46 lines
---
 tools/libxl/libxl_internal.h |   23 +++++++++++++----------
 1 files changed, 13 insertions(+), 10 deletions(-)
tools/libxl/libxl_internal.h +13 -10
index d681d73..1dadf15 100644
@@ -115,7 +115,8 @@ struct libxl__ctx {
     struct xs_handle *xsh;
 
     pthread_mutex_t lock; /* protects data structures hanging off the ctx */
-      /* Always use CTX_LOCK and CTX_UNLOCK to manipulate this.
+      /* Always use libxl__ctx_lock and _unlock (or the convenience
+       * macors CTX_LOCK and CTX_UNLOCK) to manipulate this.
        *
        * You may acquire this mutex recursively if it is convenient to
        * do so.  You may not acquire this lock at the same time as any
@@ -749,16 +750,18 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
 
 /* Locking functions.  See comment for "lock" member of libxl__ctx. */
 
-#define CTX_LOCK do {                                   \
-        int mutex_r = pthread_mutex_lock(&CTX->lock);   \
-        assert(!mutex_r);                               \
-    } while(0)
+static inline void libxl__ctx_lock(libxl_ctx *ctx) {
+    int r = pthread_mutex_lock(&ctx->lock);
+    assert(!r);
+}
 
-#define CTX_UNLOCK do {                                 \
-        int mutex_r = pthread_mutex_unlock(&CTX->lock); \
-        assert(!mutex_r);                               \
-    } while(0)
-        
+static inline void libxl__ctx_unlock(libxl_ctx *ctx) {
+    int r = pthread_mutex_unlock(&ctx->lock);
+    assert(!r);
+}
+
+#define CTX_LOCK (libxl__ctx_lock(CTX))
+#define CTX_UNLOCK (libxl__ctx_unlock(CTX))
 
 
 /*
#11 lore raw

PATCH v6 5/10 libxl: Fix leaks on context init failure

In reply to Ian Jackson

Several of the error exits from libxl_ctx_alloc leaked the context struct itself and sometimes other resources too.

Fix this by using the standard "rc = ERROR_FOO; goto out" error handling style throughout.

---
 tools/libxl/libxl.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
tools/libxl/libxl.c +12 -8
index 2d3e8cd..8ecce26 100644
@@ -24,17 +24,17 @@
 int libxl_ctx_alloc(libxl_ctx **pctx, int version,
                     unsigned flags, xentoollog_logger * lg)
 {
-    libxl_ctx *ctx;
+    libxl_ctx *ctx = NULL;
     struct stat stat_buf;
     const pthread_mutex_t mutex_value = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+    int rc;
 
-    if (version != LIBXL_VERSION)
-        return ERROR_VERSION;
+    if (version != LIBXL_VERSION) { rc = ERROR_VERSION; goto out; }
 
     ctx = malloc(sizeof(*ctx));
     if (!ctx) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Failed to allocate context\n");
-        return ERROR_NOMEM;
+        rc = ERROR_NOMEM; goto out;
     }
 
     memset(ctx, 0, sizeof(libxl_ctx));
@@ -48,14 +48,14 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
                      "failed to stat %s", XENSTORE_PID_FILE);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL; goto out;
     }
 
     ctx->xch = xc_interface_open(lg,lg,0);
     if (!ctx->xch) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
                         "cannot open libxc handle");
-        return ERROR_FAIL;
+        rc = ERROR_FAIL; goto out;
     }
 
     ctx->xsh = xs_daemon_open();
@@ -64,12 +64,16 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     if (!ctx->xsh) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
                         "cannot connect to xenstore");
-        xc_interface_close(ctx->xch);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL; goto out;
     }
 
     *pctx = ctx;
     return 0;
+
+ out:
+    libxl_ctx_free(ctx);
+    *pctx = NULL;
+    return rc;
 }
 
 int libxl_ctx_free(libxl_ctx *ctx)
Signature · 1 line
1.7.2.5
Acked-by on v6 5/10
#12 lore raw

Re: [PATCH 05/10] libxl: Fix leaks on context init failure

Quoted 7 lines · Ian Jackson

Several of the error exits from libxl_ctx_alloc leaked the context struct itself and sometimes other resources too.

Fix this by using the standard "rc = ERROR_FOO; goto out" error handling style throughout.

Quoted 11 lines
---
 tools/libxl/libxl.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
tools/libxl/libxl.c +0 -0
index 2d3e8cd..8ecce26 100644
@@ -24,17 +24,17 @@
 int libxl_ctx_alloc(libxl_ctx **pctx, int version,
                     unsigned flags, xentoollog_logger * lg)

tools/libxl/libxl.c line 32

Quoted 10 lines
tools/libxl/libxl.c +3 -3
 {
-    libxl_ctx *ctx;
+    libxl_ctx *ctx = NULL;
     struct stat stat_buf;
     const pthread_mutex_t mutex_value = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+    int rc;
 
-    if (version != LIBXL_VERSION)
-        return ERROR_VERSION;
+    if (version != LIBXL_VERSION) { rc = ERROR_VERSION; goto out; }

Aside: we really ought to have been cranking this number and probably the SONAME too. 4.2 should definitely differ from 4.1 in at least one of those, if not both.

Ian.

Quoted 45 lines
tools/libxl/libxl.c +9 -5
     ctx = malloc(sizeof(*ctx));
     if (!ctx) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Failed to allocate context\n");
-        return ERROR_NOMEM;
+        rc = ERROR_NOMEM; goto out;
     }
 
     memset(ctx, 0, sizeof(libxl_ctx));
@@ -48,14 +48,14 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
                      "failed to stat %s", XENSTORE_PID_FILE);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL; goto out;
     }
 
     ctx->xch = xc_interface_open(lg,lg,0);
     if (!ctx->xch) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
                         "cannot open libxc handle");
-        return ERROR_FAIL;
+        rc = ERROR_FAIL; goto out;
     }
 
     ctx->xsh = xs_daemon_open();
@@ -64,12 +64,16 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     if (!ctx->xsh) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
                         "cannot connect to xenstore");
-        xc_interface_close(ctx->xch);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL; goto out;
     }
 
     *pctx = ctx;
     return 0;
+
+ out:
+    libxl_ctx_free(ctx);
+    *pctx = NULL;
+    return rc;
 }
 
 int libxl_ctx_free(libxl_ctx *ctx)
#13 lore raw

Re: [PATCH 05/10] libxl: Fix leaks on context init failure

Ian Campbell writes ("Re: [Xen-devel] [PATCH 05/10] libxl: Fix leaks on context init failure"):

Quoted 3 lines

Aside: we really ought to have been cranking this number and probably the SONAME too. 4.2 should definitely differ from 4.1 in at least one of those, if not both.

Makefile's MAJOR has already been updated AFAICT:

commit 8d3cedd3b6719d1a1f4a962f4ffece1355d77642
Author: Ian Campbell <[email protected]>
Date: Fri Apr 8 16:17:18 2011 +0100

libxl: bump SONAME after binary incompatible change.

mariner:libxl> git-describe --tags 8d3cedd3
4.1.0-branched-212-g8d3cedd
mariner:libxl>

I'm not sure the version checking stuff in libxl_init serves any useful function.

Ian.

#14 lore raw

PATCH v6 6/10 DROP: libxl: rename libxl__free_all

In reply to Ian Jackson

*** THIS PATCH WILL PROBABLY BE DROPPED as it is no longer needed
in the current version of later patches. ***

libxl__free_all is going to gain some extra functionality, which will mean that the name is no longer accurate. Rename it to libxl__gc_cleanup.

---
 tools/libxl/libxl.h          |    2 +-
 tools/libxl/libxl_internal.c |    2 +-
 tools/libxl/libxl_internal.h |    4 ++--
 tools/libxl/libxl_qmp.c      |    2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
tools/libxl/libxl.h +1 -1
index 723eac2..9ae7cea 100644
@@ -119,7 +119,7 @@
  *
  * No temporary objects allocated from the pool may be explicitly freed.
  * Therefore public functions which initialize a libxl__gc MUST call
- * libxl__free_all() before returning.
+ * libxl__gc_cleanup() before returning.
  */
 #ifndef LIBXL_H
 #define LIBXL_H
tools/libxl/libxl_internal.c +1 -1
index 49b0dab..cc98d86 100644
@@ -53,7 +53,7 @@ int libxl__ptr_add(libxl__gc *gc, void *ptr)
     return 0;
 }
 
-void libxl__free_all(libxl__gc *gc)
+void libxl__gc_cleanup(libxl__gc *gc)
 {
     void *ptr;
     int i;
tools/libxl/libxl_internal.h +2 -2
index 1dadf15..df1dfde 100644
@@ -190,7 +190,7 @@ static inline libxl_ctx *libxl__gc_owner(libxl__gc *gc)
 /* register @ptr in @gc for free on exit from outermost libxl callframe. */
 _hidden int libxl__ptr_add(libxl__gc *gc, void *ptr);
 /* if this is the outermost libxl callframe then free all pointers in @gc */
-_hidden void libxl__free_all(libxl__gc *gc);
+_hidden void libxl__gc_cleanup(libxl__gc *gc);
 /* allocate and zero @bytes. (similar to a gc'd malloc(3)+memzero()) */
 _hidden void *libxl__zalloc(libxl__gc *gc, int bytes);
 /* allocate and zero memory for an array of @nmemb members of @size each.
@@ -744,7 +744,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
  */
 
 #define GC_INIT(ctx)  libxl__gc gc[1]; LIBXL_INIT_GC(gc[0],ctx)
-#define GC_FREE       libxl__free_all(gc)
+#define GC_FREE       libxl__gc_cleanup(gc)
 #define CTX           libxl__gc_owner(gc)
 
 
tools/libxl/libxl_qmp.c +1 -1
index 61d9769..58f5283 100644
@@ -528,7 +528,7 @@ static int qmp_send(libxl__qmp_handler *qmp,
 
     rc = qmp->last_id_used;
 out:
-    libxl__free_all(&gc);
+    libxl__gc_cleanup(&gc);
     return rc;
 }
 
Signature · 1 line
1.7.2.5
#15 lore raw

PATCH v6 7/10 libxl: New API for providing OS events to libxl

In reply to Ian Jackson

We provide a new set of functions and related structures
libxl_osevent_*
which are to be used by event-driven applications to receive information from libxl about which fds libxl is interested in, and what timeouts libxl is waiting for, and to pass back to libxl information about which fds are readable/writeable etc., and which timeouts have occurred. Ie, low-level events.

In this patch, this new machinery is still all unused. Callers will appear in the next patch in the series, which introduces a new API for applications to receive high-level events about actual domains etc.

---
 tools/libxl/Makefile         |    2 +-
 tools/libxl/libxl.c          |   30 ++
 tools/libxl/libxl.h          |    6 +
 tools/libxl/libxl_event.c    |  748 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_event.h    |  204 ++++++++++++
 tools/libxl/libxl_internal.h |  266 +++++++++++++++-
 6 files changed, 1252 insertions(+), 4 deletions(-)
 create mode 100644 tools/libxl/libxl_event.c
 create mode 100644 tools/libxl/libxl_event.h
tools/libxl/Makefile +1 -1
index 78bc589..8bdc308 100644
@@ -47,7 +47,7 @@ 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_qmp.o $(LIBXL_OBJS-y)
+			libxl_qmp.o libxl_event.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl.c +30 -0
index 8ecce26..c0024af 100644
@@ -45,6 +45,16 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
      * only as an initialiser, not as an expression. */
     memcpy(&ctx->lock, &mutex_value, sizeof(ctx->lock));
 
+    ctx->osevent_hooks = 0;
+
+    ctx->fd_rindex = 0;
+    LIBXL_LIST_INIT(&ctx->efds);
+    LIBXL_TAILQ_INIT(&ctx->etimes);
+
+    ctx->watch_slots = 0;
+    LIBXL_SLIST_INIT(&ctx->watch_freeslots);
+    libxl__ev_fd_init(&ctx->watch_efd);
+
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
                      "failed to stat %s", XENSTORE_PID_FILE);
@@ -79,9 +89,29 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
 int libxl_ctx_free(libxl_ctx *ctx)
 {
     if (!ctx) return 0;
+
+    int i;
+    GC_INIT(ctx);
+
+    /* Deregister all libxl__ev_KINDs: */
+
+    for (i = 0; i < ctx->watch_nslots; i++)
+        assert(!libxl__watch_slot_contents(gc, i));
+    libxl__ev_fd_deregister(gc, &ctx->watch_efd);
+
+    /* Now there should be no more events requested from the application: */
+
+    assert(LIBXL_LIST_EMPTY(&ctx->efds));
+    assert(LIBXL_TAILQ_EMPTY(&ctx->etimes));
+
     if (ctx->xch) xc_interface_close(ctx->xch);
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
+
+    free(ctx->fd_rindex);
+    free(ctx->watch_slots);
+
+    GC_FREE;
     free(ctx);
     return 0;
 }
tools/libxl/libxl.h +6 -0
index 9ae7cea..c0a4373 100644
@@ -137,6 +137,7 @@
 #include <xen/sysctl.h>
 
 #include <libxl_uuid.h>
+#include <_libxl_list.h>
 
 typedef uint8_t libxl_mac[6];
 #define LIBXL_MAC_FMT "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"
@@ -223,6 +224,9 @@ enum {
     ERROR_GUEST_TIMEDOUT = -8,
     ERROR_TIMEDOUT = -9,
     ERROR_NOPARAVIRT = -10,
+    ERROR_NOT_READY = -11,
+    ERROR_OSEVENT_REG_FAIL = -12,
+    ERROR_BUFFERFULL = -13,
 };
 
 #define LIBXL_VERSION 0
@@ -648,6 +652,8 @@ const char *libxl_xenpaging_dir_path(void);
 /* misc */
 int libxl_fd_set_cloexec(int fd);
 
+#include <libxl_event.h>
+
 #endif /* LIBXL_H */
 
 /*
tools/libxl/libxl_event.c new file +748 -0

748 lines not on this page · raw message

tools/libxl/libxl_event.h new file +204 -0

204 lines not on this page · raw message

tools/libxl/libxl_internal.h +263 -3

314 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#16 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

Quoted 4 lines · Ian Jackson
tools/libxl/libxl_internal.h +4 -0
+struct libxl__egc {
+    /* for event-generating functions only */
+    struct libxl__gc gc;
+};

[...]

Quoted 31 lines

/*
+ * Calling context and GC for event-generating functions:
+ *
+ * These are for use by parts of libxl which directly or indirectly
+ * call libxl__event_occurred. These contain a gc but also a list of
+ * deferred events.
+ *
+ * Most code in libxlshould not need to initialise their own egc.
+ * Even functions which generate specific kinds of events don't need
+ * to - rather, they will be passed an egc into their own callback
+ * function and should just use the one they're given.
+ *
+ * A handy idiom for functions taking an egc is:
+ * libxl__gc *gc = &egc->gc;
+ *
+ * Functions using LIBXL__INIT_EGC may *not* generally be called from
+ * within libxl, because libxl__egc_cleanup may call back into the
+ * application. This should be documented near the function
+ * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.
+ *
+ * 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.
+ */

tools/libxl/libxl_internal.h +7 -0
+
+/* egc initialisation and destruction: */
+
+#define LIBXL_INIT_EGC(egc,ctx) do{             \
+        LIBXL_INIT_GC((egc).gc,ctx);            \
+        /* list of occurred events tbd */       \
+    } while(0)

tools/libxl/libxl_internal.h libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); · line 992

Quoted 10 lines
tools/libxl/libxl_internal.h +10 -0
+
+_hidden void libxl__egc_cleanup(libxl__egc *egc);
+
+/* convenience macros: */
+
+#define EGC_INIT(ctx)                       \
+    libxl__egc egc[1]; LIBXL_INIT_EGC(egc[0],ctx);      \
+    libxl__gc *gc = &egc->gc
+
+#define EGC_FREE           libxl__egc_cleanup(egc)

I still prefer the approach prototyped in
http://marc.info/?l=xen-devel&m=132371797519877: avoid introducing restrictions on how libxl functions have to be called, unify egc and gc, make it possible to take the lock recursively and use the nested counter to figure out when to call the callbacks.
It would make my head hurt less when I have to read/write this code, however others might react differently.

I don't think we'll ever get an agreement on this so I'll just step back and let other people decide what is the right approach.

Only one more thing: I would kindly ask to move all these event related functions to a different source file, to make it easier for people to understand which ones are different from the rest of the library.

#17 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

Quoted 44 lines · Stefano Stabellini
Quoted 4 lines · Ian Jackson
tools/libxl/libxl_internal.h +4 -0
+struct libxl__egc {
+    /* for event-generating functions only */
+    struct libxl__gc gc;
+};

[...]

Quoted 36 lines

/*
+ * Calling context and GC for event-generating functions:
+ *
+ * These are for use by parts of libxl which directly or indirectly
+ * call libxl__event_occurred. These contain a gc but also a list of
+ * deferred events.
+ *
+ * Most code in libxlshould not need to initialise their own egc.
+ * Even functions which generate specific kinds of events don't need
+ * to - rather, they will be passed an egc into their own callback
+ * function and should just use the one they're given.
+ *
+ * A handy idiom for functions taking an egc is:
+ * libxl__gc *gc = &egc->gc;
+ *
+ * Functions using LIBXL__INIT_EGC may *not* generally be called from
+ * within libxl, because libxl__egc_cleanup may call back into the
+ * application. This should be documented near the function
+ * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.
+ *
+ * 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.
+ */

tools/libxl/libxl_internal.h +12 -0
+
+/* egc initialisation and destruction: */
+
+#define LIBXL_INIT_EGC(egc,ctx) do{             \
+        LIBXL_INIT_GC((egc).gc,ctx);            \
+        /* list of occurred events tbd */       \
+    } while(0)
+
+_hidden void libxl__egc_cleanup(libxl__egc *egc);
+
+/* convenience macros: */
+
Quoted 8 lines

tools/libxl/libxl_internal.h libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); · line 992

Quoted 5 lines
tools/libxl/libxl_internal.h +5 -0
+#define EGC_INIT(ctx)                       \
+    libxl__egc egc[1]; LIBXL_INIT_EGC(egc[0],ctx);      \
+    libxl__gc *gc = &egc->gc
+
+#define EGC_FREE           libxl__egc_cleanup(egc)

I still prefer the approach prototyped in
http://marc.info/?l=xen-devel&m=132371797519877

I pointed out some issues with this in
http://marc.info/?l=xen-devel&m=132378496608354&w=2 which have gone unanswered.

In particular error handling for the pthread_setspecific in libxl__free_all seems pretty nasty to me since you would be introducing a new failure path at the end of pretty much every libxl function. Worse it is after the actual action of each function is otherwise complete so you may end up having to undo stuff. I think it is going to be a lot of work to fix all of those up properly.

Having frequently used pro/epilogue code (especially epilogue) which cannot fail seems much simpler in general to me.

It occurred to me just now that having a different type of this type of gc has the benefit that since libxl__event_occurred takes one it will necessarily filter back up the call chain and help enforce/highlight the requirement that functions which directly or indirectly call libxl__event_occurred use an egc.

Ian.

Quoted 13 lines

: avoid introducing
restrictions on how libxl functions have to be called, unify egc and gc, make it possible to take the lock recursively and use the nested counter to figure out when to call the callbacks.
It would make my head hurt less when I have to read/write this code, however others might react differently.

I don't think we'll ever get an agreement on this so I'll just step back and let other people decide what is the right approach.

Only one more thing: I would kindly ask to move all these event related functions to a different source file, to make it easier for people to understand which ones are different from the rest of the library.

#18 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

Quoted 6 lines · Ian Campbell
Quoted 2 lines

I still prefer the approach prototyped in
http://marc.info/?l=xen-devel&m=132371797519877

I pointed out some issues with this in
http://marc.info/?l=xen-devel&m=132378496608354&w=2 which have gone unanswered.

I was asked by Ian to wait for his next version and so I did.

Quoted 9 lines

In particular error handling for the pthread_setspecific in libxl__free_all seems pretty nasty to me since you would be introducing a new failure path at the end of pretty much every libxl function. Worse it is after the actual action of each function is otherwise complete so you may end up having to undo stuff. I think it is going to be a lot of work to fix all of those up properly.

Having frequently used pro/epilogue code (especially epilogue) which cannot fail seems much simpler in general to me.

The epilogue is not a problem because pthread_setspecific could only fail if the key is invalid so we don't need to handle that error. However the prologue still needs error handling.
Personally I don't think there is anything wrong with a GC_INIT that can fail. We can decide whether to handle the error within the macro (like it is done now in the new updated patch) or outside the macro. Both are fine by me.

tools/libxl/libxl.c +8 -0
diff -r 5b2676ac1321 tools/libxl/libxl.c
@@ -60,6 +60,12 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in
      * only as an initialiser, not as an expression. */
     memcpy(&ctx->lock, &mutex_value, sizeof(ctx->lock));
 
+    if(pthread_key_create(&ctx->tls_key, NULL) < 0) {
+        LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
+            "cannot create tls key");
+        return ERROR_FAIL;
+    }
+
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
                      "failed to stat %s", XENSTORE_PID_FILE);
@@ -93,6 +99,8 @@ int libxl_ctx_free(libxl_ctx *ctx)
     if (ctx->xch) xc_interface_close(ctx->xch);
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
+    if (pthread_key_delete(ctx->tls_key) < 0)
+        return ERROR_FAIL;
     free(ctx);
     return 0;
 }
tools/libxl/libxl_internal.c +30 -0
diff -r 5b2676ac1321 tools/libxl/libxl_internal.c
@@ -17,6 +17,7 @@
 
 #include <stdio.h>
 
+#include <pthread.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -61,11 +62,38 @@ int libxl__ptr_add(libxl__gc *gc, void *
     return 0;
 }
 
+libxl__gc *libxl__init_gc(libxl_ctx *ctx)
+{
+    libxl__gc *gc = (libxl__gc *) pthread_getspecific(ctx->tls_key);
+    if (gc == NULL) {
+        gc = (libxl__gc *) malloc(sizeof(libxl__gc));
+        if (gc == NULL)
+            return NULL;
+        gc->alloc_maxsize = 0;
+        gc->alloc_ptrs = 0;
+        gc->nested = 0;
+        gc->owner = ctx;
+        gc->nested = 1;
+        if (pthread_setspecific(ctx->tls_key, gc) < 0) {
+            free(gc);
+            return NULL;
+        }
+    } else {
+        gc->nested++;
+    }
+    return gc;
+}
+
+
 void libxl__free_all(libxl__gc *gc)
 {
     void *ptr;
     int i;
 
+    gc->nested--;
+    if (gc->nested > 0)
+        return;
+
     for (i = 0; i < gc->alloc_maxsize; i++) {
         ptr = gc->alloc_ptrs[i];
         gc->alloc_ptrs[i] = NULL;
@@ -74,6 +102,8 @@ void libxl__free_all(libxl__gc *gc)
     free(gc->alloc_ptrs);
     gc->alloc_ptrs = 0;
     gc->alloc_maxsize = 0;
+    pthread_setspecific(CTX->tls_key, NULL);
+    free(gc);
 }
 
 void *libxl__zalloc(libxl__gc *gc, int bytes)
tools/libxl/libxl_internal.h +5 -2
diff -r 5b2676ac1321 tools/libxl/libxl_internal.h
@@ -98,6 +98,8 @@ struct libxl__ctx {
     xc_interface *xch;
     struct xs_handle *xsh;
 
+    pthread_key_t tls_key;
+
     pthread_mutex_t lock; /* protects data structures hanging off the ctx */
       /* Always use CTX_LOCK and CTX_UNLOCK to manipulate this.
        *
@@ -145,9 +147,9 @@ typedef struct {
     int alloc_maxsize;
     void **alloc_ptrs;
     libxl_ctx *owner;
+    int nested;
 } libxl__gc;
 
-#define LIBXL_INIT_GC(ctx) (libxl__gc){ .alloc_maxsize = 0, .alloc_ptrs = 0, .owner = ctx }
 static inline libxl_ctx *libxl__gc_owner(libxl__gc *gc)
 {
     return gc->owner;
@@ -721,7 +723,8 @@ libxl__device_model_version_running(libx
  * as a local variable.
  */
 
-#define GC_INIT(ctx)  libxl__gc gc[1] = { LIBXL_INIT_GC(ctx) }
+_hidden libxl__gc *libxl__init_gc(struct libxl__ctx *ctx);
+#define GC_INIT(ctx)  libxl__gc *gc = libxl__init_gc(ctx); if (gc == NULL) return ERROR_NOMEM;
 #define GC_FREE       libxl__free_all(gc)
 #define CTX           libxl__gc_owner(gc)
#19 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

In reply to Ian Campbell

Ian Campbell writes ("Re: [Xen-devel] [PATCH 07/10] libxl: New API for providing OS events to libxl"):

Quoted 5 lines

It occurred to me just now that having a different type of this type of gc has the benefit that since libxl__event_occurred takes one it will necessarily filter back up the call chain and help enforce/highlight the requirement that functions which directly or indirectly call libxl__event_occurred use an egc.

Yes, exactly. This is a very helpful consequence of using a different type.

Ian.

#20 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

In reply to Stefano Stabellini

Stefano Stabellini writes ("Re: [Xen-devel] [PATCH 07/10] libxl: New API for providing OS events to libxl"):

Quoted 7 lines

I still prefer the approach prototyped in
http://marc.info/?l=xen-devel&m=132371797519877: avoid introducing restrictions on how libxl functions have to be called, unify egc and gc, make it possible to take the lock recursively and use the nested counter to figure out when to call the callbacks.
It would make my head hurt less when I have to read/write this code, however others might react differently.

I would agree with you if it were the case that editing general code in libxl might involve adding new event-generating functions, or indeed might involve turning existing functions into event-generating ones.

But this is not the case. The event-generating portions of the code end up completely separate in any correct implementation - and someone using this machinery doesn't even need to be very aware of this.

Quoted 3 lines

Only one more thing: I would kindly ask to move all these event related functions to a different source file, to make it easier for people to understand which ones are different from the rest of the library.

They are, in libxl_event.c. Unless by "event related" you meant "event generating", in which case I disagree, because I don't think that's the most relevant distinction.

Ian.

#21 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

In reply to Ian Jackson
Quoted 8 lines · Ian Jackson

We provide a new set of functions and related structures
libxl_osevent_*
which are to be used by event-driven applications to receive information from libxl about which fds libxl is interested in, and what timeouts libxl is waiting for, and to pass back to libxl information about which fds are readable/writeable etc., and which timeouts have occurred. Ie, low-level events.

Quoted 5 lines

In this patch, this new machinery is still all unused. Callers will appear in the next patch in the series, which introduces a new API for applications to receive high-level events about actual domains etc.

I think I've mostly looked at this already so I only glanced through it this time.

tools/libxl/libxl_event.h line 64

Quoted 10 lines

[...]

tools/libxl/libxl_event.h new file +8 -0
+int libxl_osevent_beforepoll(libxl_ctx *ctx, int *nfds_io,
+                             struct pollfd *fds, int *timeout_upd,
+                             struct timeval now);
+  /* The caller should provide beforepoll with some space for libxl's
+   * fds, and tell libxl how much space is available by setting *nfds_io.
+   * fds points to the start of this space (and fds may be a pointer into
+   * a larger array, for example, if the application has some fds of
+   * its own that it is interested in).

I think you were going to move the comments (sorry).

tools/libxl/libxl_internal.h libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); · line 959

Quoted 12 lines

[...]

tools/libxl/libxl_internal.h +7 -0
@@ -733,6 +950,49 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
 
 
 /*
+ * Calling context and GC for event-generating functions:
+ *
+ * These are for use by parts of libxl which directly or indirectly
+ * call libxl__event_occurred.  These contain a gc but also a list of
+ * deferred events.
+ *
+ * Most code in libxlshould not need to initialise their own egc.

Missing space. ^

Will any function even need to initialise their own egc and/or is it normally provided by infrastructure code? Otherwise when should a function initialise their own egc? Is it simply that you know if you need to initialise one because you are calling a function which takes one?

tools/libxl/libxl_internal.h libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); · line 965

Quoted 6 lines
tools/libxl/libxl_internal.h +6 -0
+ * Even functions which generate specific kinds of events don't need
+ * to - rather, they will be passed an egc into their own callback
+ * function and should just use the one they're given.
+ *
+ * A handy idiom for functions taking an egc is:
+ *     libxl__gc *gc = &egc->gc;

You've provided lots of handy macros for other things like this, perhaps EGC by comparison with CTX. Using GC seems better on the face of it but since it will only work in a context with an egc that is actually confusing.

tools/libxl/libxl_internal.h libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); · line 970

Quoted 4 lines
tools/libxl/libxl_internal.h +4 -0
+ * Functions using LIBXL__INIT_EGC may *not* generally be called from
+ * within libxl, because libxl__egc_cleanup may call back into the
+ * application.  This should be documented near the function
+ * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.

Can we also enforce/check this with a flag in the ctx? I guess not since multiple threads might call into libxl with the same ctx and each may legitimately setup an egc.?

Quoted 36 lines
tools/libxl/libxl_internal.h +25 -1
+ *
+ * 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.
+ */
+
+/* egc initialisation and destruction: */
+
+#define LIBXL_INIT_EGC(egc,ctx) do{             \
+        LIBXL_INIT_GC((egc).gc,ctx);            \
+        /* list of occurred events tbd */       \
+    } while(0)
+
+_hidden void libxl__egc_cleanup(libxl__egc *egc);
+
+/* convenience macros: */
+
+#define EGC_INIT(ctx)                       \
+    libxl__egc egc[1]; LIBXL_INIT_EGC(egc[0],ctx);      \
+    libxl__gc *gc = &egc->gc
+
+#define EGC_FREE           libxl__egc_cleanup(egc)
+
+
+/*
  * Convenience macros.
  */
 
--

1.7.2.5

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#22 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

Ian Campbell writes ("Re: [Xen-devel] [PATCH 07/10] libxl: New API for providing OS events to libxl"):

Quoted 11 lines · Ian Jackson

tools/libxl/libxl_event.h line 64

Quoted 8 lines
tools/libxl/libxl_event.h new file +8 -0
+int libxl_osevent_beforepoll(libxl_ctx *ctx, int *nfds_io,
+                             struct pollfd *fds, int *timeout_upd,
+                             struct timeval now);
+  /* The caller should provide beforepoll with some space for libxl's
+   * fds, and tell libxl how much space is available by setting *nfds_io.
+   * fds points to the start of this space (and fds may be a pointer into
+   * a larger array, for example, if the application has some fds of
+   * its own that it is interested in).

I think you were going to move the comments (sorry).

*sigh* OK, I will do that.

Quoted 3 lines
Quoted 1 line

+ * Most code in libxlshould not need to initialise their own egc.

Missing space. ^

Fixed.

Quoted 5 lines

Will any function even need to initialise their own egc and/or is it normally provided by infrastructure code? Otherwise when should a function initialise their own egc? Is it simply that you know if you need to initialise one because you are calling a function which takes one?

You don't need to because you should never end up calling a function that takes an egc unless you've got one already from the event machinery.

Quoted 11 lines
Quoted 6 lines

+ * Even functions which generate specific kinds of events don't need
+ * to - rather, they will be passed an egc into their own callback
+ * function and should just use the one they're given.
+ *
+ * A handy idiom for functions taking an egc is:
+ * libxl__gc *gc = &egc->gc;

You've provided lots of handy macros for other things like this, perhaps EGC by comparison with CTX. Using GC seems better on the face of it but since it will only work in a context with an egc that is actually confusing.

How about:
#define EGC_GC libxl__gc *gc = &egc->gc
?

Quoted 8 lines
Quoted 4 lines

+ * Functions using LIBXL__INIT_EGC may *not* generally be called from
+ * within libxl, because libxl__egc_cleanup may call back into the
+ * application. This should be documented near the function
+ * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.

Can we also enforce/check this with a flag in the ctx? I guess not since multiple threads might call into libxl with the same ctx and each may legitimately setup an egc.?

Exactly. This is the same disagreement I'm having with Stefano.

But this restriction is not difficult in practice because functions that want to generate events are almost[1] invariably functions which were themselves called in response to an OS event, and they get an egc provided by the event machinery.

See for example disk_eject_xswatch_callback, which is the thing which generates events of type DISK_EJECT. It's a xenstore watch callback function, so it gets an egc provided by the event machinery and uses that for NEW_EVENT etc.

The code which sets up the callback, libxl_evenable_disk_eject, doesn't need an egc because setting up a callback only needs a gc. So you never need to construct an egc yourself.

The functions which make their own egc, and which are therefore forbidden for other callers inside libxl, are things like
libxl_osevent_beforepoll
libxl_event_wait
and if you're calling those from within libxl you are definitely doing the whole thing entirely wrong.

Ian.

[1] libxl__ao_inprogress is a counterexample, but it's part of the event machinery, not a normal function implementing some actual domain-related functionality.

#23 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

Quoted 11 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 07/10] libxl: New API for providing OS events to libxl"):

Quoted 6 lines · Ian Jackson

Will any function even need to initialise their own egc and/or is it normally provided by infrastructure code? Otherwise when should a function initialise their own egc? Is it simply that you know if you need to initialise one because you are calling a function which takes one?

You don't need to because you should never end up calling a function that takes an egc unless you've got one already from the event machinery.

I think it is worth stating explicitly. e.g. "You should never need to initialise an egc unless you are part of the event machinery itself, Otherwise you will always be given an egc if you need one". I might even go as far as s/should never/will never/.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

+ * Even functions which generate specific kinds of events don't need
+ * to - rather, they will be passed an egc into their own callback
+ * function and should just use the one they're given.
+ *
+ * A handy idiom for functions taking an egc is:
+ * libxl__gc *gc = &egc->gc;

Quoted 8 lines
Quoted 4 lines

You've provided lots of handy macros for other things like this, perhaps EGC by comparison with CTX. Using GC seems better on the face of it but since it will only work in a context with an egc that is actually confusing.

How about:
#define EGC_GC libxl__gc *gc = &egc->gc
?

Works for me.

Quoted 33 lines
Quoted 9 lines
Quoted 5 lines

+ * Functions using LIBXL__INIT_EGC may *not* generally be called from
+ * within libxl, because libxl__egc_cleanup may call back into the
+ * application. This should be documented near the function
+ * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.

Can we also enforce/check this with a flag in the ctx? I guess not since multiple threads might call into libxl with the same ctx and each may legitimately setup an egc.?

Exactly. This is the same disagreement I'm having with Stefano.

But this restriction is not difficult in practice because functions that want to generate events are almost[1] invariably functions which were themselves called in response to an OS event, and they get an egc provided by the event machinery.

See for example disk_eject_xswatch_callback, which is the thing which generates events of type DISK_EJECT. It's a xenstore watch callback function, so it gets an egc provided by the event machinery and uses that for NEW_EVENT etc.

The code which sets up the callback, libxl_evenable_disk_eject, doesn't need an egc because setting up a callback only needs a gc. So you never need to construct an egc yourself.

The functions which make their own egc, and which are therefore forbidden for other callers inside libxl, are things like
libxl_osevent_beforepoll
libxl_event_wait
and if you're calling those from within libxl you are definitely doing the whole thing entirely wrong.

Quoted 5 lines

Ian.

[1] libxl__ao_inprogress is a counterexample, but it's part of the event machinery, not a normal function implementing some actual domain-related functionality.

Makes sense, I think the wording I proposed above describes this?

Ian.

#24 lore raw

Re: [PATCH 07/10] libxl: New API for providing OS events to libxl

Ian Campbell writes ("Re: [Xen-devel] [PATCH 07/10] libxl: New API for providing OS events to libxl"):

Quoted 4 lines

I think it is worth stating explicitly. e.g. "You should never need to initialise an egc unless you are part of the event machinery itself, Otherwise you will always be given an egc if you need one". I might even go as far as s/should never/will never/.

Right, that's good wording, thanks.

Quoted 5 lines
Quoted 3 lines

How about:
#define EGC_GC libxl__gc *gc = &egc->gc
?

Works for me.

I will do this.

Quoted 6 lines
Quoted 6 lines

The functions which make their own egc, and which are therefore forbidden for other callers inside libxl, are things like
libxl_osevent_beforepoll
libxl_event_wait
and if you're calling those from within libxl you are definitely doing the whole thing entirely wrong.

...

Quoted 1 line

Makes sense, I think the wording I proposed above describes this?

Right.

Ian.

tools/libxl/libxl_internal.h +9 -5
index e03a5d6..c50c058 100644
@@ -967,10 +967,12 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
  * call libxl__event_occurred.  These contain a gc but also a list of
  * deferred events.
  *
- * Most code in libxl should not need to initialise their own egc.
- * Even functions which generate specific kinds of events don't need
- * to - rather, they will be passed an egc into their own callback
- * function and should just use the one they're given.
+ * You should never need to initialise an egc unless you are part of
+ * the event machinery itself.  Otherwise you will always be given an
+ * egc if you need one.  Even functions which generate specific kinds
+ * of events don't need to - rather, they will be passed an egc into
+ * their own callback function and should just use the one they're
+ * given.
  *
  * A handy idiom for functions taking an egc is:
  *     libxl__gc *gc = &egc->gc;
@@ -978,7 +980,9 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
  * Functions using LIBXL__INIT_EGC may *not* generally be called from
  * within libxl, because libxl__egc_cleanup may call back into the
  * application.  This should be documented near the function
- * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.
+ * prototype(s) for callers of LIBXL__INIT_EGC and EGC_INIT.  You
+ * should in any case not find it necessary to call egc-creators from
+ * within libxl.
  *
  * For the same reason libxl__egc_cleanup (or EGC_FREE) must be called
  * with the ctx *unlocked*.  So the right pattern has the EGC_...
#25 lore raw

PATCH v6 8/10 libxl: New event generation API

In reply to Ian Jackson

Replace the existing API for retrieving high-level events (events about domains, etc.) from libxl with a new one.

This changes the definition and semantics of the `libxl_event' structure, and replaces the calls for obtaining information about domain death and disk eject events.

This is an incompatible change, sorry. The alternative was to try to provide both the previous horrid API and the new one, and would also involve never using the name `libxl_event' for the new interface.

---
 tools/libxl/libxl.c          |  327 ++++++++++++++++++++++++++++++-----------
 tools/libxl/libxl.h          |   55 ++------
 tools/libxl/libxl_event.c    |  239 ++++++++++++++++++++++++++++---
 tools/libxl/libxl_event.h    |  184 +++++++++++++++++++++++-
 tools/libxl/libxl_internal.h |   82 ++++++++++-
 tools/libxl/libxl_types.idl  |   34 ++++-
 tools/libxl/xl_cmdimpl.c     |  270 ++++++++++++++++++++---------------
 7 files changed, 912 insertions(+), 279 deletions(-)
tools/libxl/libxl.c +239 -88
index c0024af..f6370d4 100644
@@ -45,8 +45,11 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
      * only as an initialiser, not as an expression. */
     memcpy(&ctx->lock, &mutex_value, sizeof(ctx->lock));
 
+    LIBXL_TAILQ_INIT(&ctx->occurred);
+
     ctx->osevent_hooks = 0;
 
+    ctx->fd_polls = 0;
     ctx->fd_rindex = 0;
     LIBXL_LIST_INIT(&ctx->efds);
     LIBXL_TAILQ_INIT(&ctx->etimes);
@@ -55,6 +58,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     LIBXL_SLIST_INIT(&ctx->watch_freeslots);
     libxl__ev_fd_init(&ctx->watch_efd);
 
+    LIBXL_TAILQ_INIT(&ctx->death_list);
+    libxl__ev_xswatch_init(&ctx->death_watch);
+
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
                      "failed to stat %s", XENSTORE_PID_FILE);
@@ -86,6 +92,20 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     return rc;
 }
 
+static void free_disable_deaths(libxl__gc *gc,
+                                struct libxl__evgen_domain_death_list *l) {
+    libxl_evgen_domain_death *death;
+    while ((death = LIBXL_TAILQ_FIRST(l)))
+        libxl__evdisable_domain_death(gc, death);
+}
+
+static void discard_events(struct libxl__event_list *l) {
+    /* doesn't bother unlinking from the list, so l is corrupt on return */
+    libxl_event *ev;
+    LIBXL_TAILQ_FOREACH(ev, l, link)
+        libxl_event_free(0, ev);
+}
+
 int libxl_ctx_free(libxl_ctx *ctx)
 {
     if (!ctx) return 0;
@@ -95,6 +115,13 @@ int libxl_ctx_free(libxl_ctx *ctx)
 
     /* Deregister all libxl__ev_KINDs: */
 
+    free_disable_deaths(gc, &CTX->death_list);
+    free_disable_deaths(gc, &CTX->death_reported);
+
+    libxl_evgen_disk_eject *eject;
+    while ((eject = LIBXL_LIST_FIRST(&CTX->disk_eject_evgens)))
+        libxl__evdisable_disk_eject(gc, eject);
+
     for (i = 0; i < ctx->watch_nslots; i++)
         assert(!libxl__watch_slot_contents(gc, i));
     libxl__ev_fd_deregister(gc, &ctx->watch_efd);
@@ -108,9 +135,12 @@ int libxl_ctx_free(libxl_ctx *ctx)
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
 
+    free(ctx->fd_polls);
     free(ctx->fd_rindex);
     free(ctx->watch_slots);
 
+    discard_events(&ctx->occurred);
+
     GC_FREE;
     free(ctx);
     return 0;
@@ -646,117 +676,175 @@ int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid)
     return ret;
 }
 
-int libxl_get_wait_fd(libxl_ctx *ctx, int *fd)
-{
-    *fd = xs_fileno(ctx->xsh);
-    return 0;
-}
+static void domain_death_xswatch_callback(libxl__egc *egc, libxl__ev_xswatch *w,
+                                        const char *wpath, const char *epath) {
+    libxl__gc *gc = &egc->gc;
+    libxl_evgen_domain_death *evg;
+    uint32_t domid;
+    int rc;
 
-int libxl_wait_for_domain_death(libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
-{
-    waiter->path = strdup("@releaseDomain");
-    if (asprintf(&(waiter->token), "%d", LIBXL_EVENT_TYPE_DOMAIN_DEATH) < 0)
-        return -1;
-    if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
-        return -1;
-    return 0;
-}
+    CTX_LOCK;
 
-int libxl_wait_for_disk_ejects(libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
-{
-    GC_INIT(ctx);
-    int i, rc = -1;
-    uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
+    evg = LIBXL_TAILQ_FIRST(&CTX->death_list);
+    if (!evg) goto out;
 
-    if (!domid)
-        domid = guest_domid;
+    domid = evg->domid;
 
-    for (i = 0; i < num_disks; i++) {
-        if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
-                     libxl__xs_get_dompath(gc, domid),
-                     libxl__device_disk_dev_number(disks[i].vdev,
-                                                   NULL, NULL)) < 0)
-            goto out;
-        if (asprintf(&(waiter[i].token), "%d", LIBXL_EVENT_TYPE_DISK_EJECT) < 0)
+    for (;;) {
+        int nentries = LIBXL_TAILQ_NEXT(evg, entry) ? 200 : 1;
+        xc_domaininfo_t domaininfos[nentries];
+        const xc_domaininfo_t *got = domaininfos, *gotend;
+
+        rc = xc_domain_getinfolist(CTX->xch, domid, nentries, domaininfos);
+        if (rc == -1) {
+            LIBXL__EVENT_DISASTER(egc, "xc_domain_getinfolist failed while"
+                                  " processing @releaseDomain watch event",
+                                  errno, 0);
             goto out;
-        xs_watch(ctx->xsh, waiter[i].path, waiter[i].token);
+        }
+        gotend = &domaininfos[rc];
+
+        for (;;) {
+            if (!evg)
+                goto all_reported;
+
+            if (!rc || got->domain > evg->domid) {
+                /* ie, the list doesn't contain evg->domid any more so
+                 * the domain has been destroyed */
+                libxl_evgen_domain_death *evg_next;
+
+                libxl_event *ev = NEW_EVENT(egc, DOMAIN_DESTROY, evg->domid);
+                if (!ev) goto out;
+
+                libxl__event_occurred(egc, ev);
+
+                evg->death_reported = 1;
+                evg_next = LIBXL_TAILQ_NEXT(evg, entry);
+                LIBXL_TAILQ_REMOVE(&CTX->death_list, evg, entry);
+                LIBXL_TAILQ_INSERT_HEAD(&CTX->death_reported, evg, entry);
+                evg = evg_next;
+
+                continue;
+            }
+            
+            if (got == gotend)
+                break;
+
+            if (got->domain < evg->domid) {
+                got++;
+                continue;
+            }
+
+            assert(evg->domid == got->domain);
+
+            if (!evg->shutdown_reported &&
+                (got->flags & XEN_DOMINF_shutdown)) {
+                libxl_event *ev = NEW_EVENT(egc, DOMAIN_SHUTDOWN, got->domain);
+                if (!ev) goto out;
+                
+                ev->u.domain_shutdown.shutdown_reason =
+                    (got->flags >> XEN_DOMINF_shutdownshift) &
+                    XEN_DOMINF_shutdownmask;
+                libxl__event_occurred(egc, ev);
+
+                evg->shutdown_reported = 1;
+            }
+            evg = LIBXL_TAILQ_NEXT(evg, entry);
+        }
+
+        assert(rc); /* rc==0 results in us eating all evgs and quitting */
+        domid = gotend[-1].domain;
     }
-    rc = 0;
-out:
-    GC_FREE;
-    return rc;
+ all_reported:
+ out:
+
+    CTX_UNLOCK;
 }
 
-int libxl_get_event(libxl_ctx *ctx, libxl_event *event)
-{
-    unsigned int num;
-    char **events = xs_read_watch(ctx->xsh, &num);
-    if (num != 2) {
-        free(events);
-        return ERROR_FAIL;
+int libxl_evenable_domain_death(libxl_ctx *ctx, uint32_t domid,
+                libxl_ev_user user, libxl_evgen_domain_death **evgen_out) {
+    GC_INIT(ctx);
+    libxl_evgen_domain_death *evg, *evg_search;
+    int rc;
+    
+    CTX_LOCK;
+
+    evg = malloc(sizeof(*evg));  if (!evg) { rc = ERROR_NOMEM; goto out; }
+    memset(evg, 0, sizeof(*evg));
+    evg->domid = domid;
+    evg->user = user;
+
+    LIBXL_TAILQ_INSERT_SORTED(&ctx->death_list, entry, evg, evg_search, ,
+                              evg->domid > evg_search->domid);
+
+    if (!libxl__ev_xswatch_isregistered(&ctx->death_watch)) {
+        rc = libxl__ev_xswatch_register(gc, &ctx->death_watch,
+                        domain_death_xswatch_callback, "@releaseDomain");
+        if (rc) { libxl__evdisable_domain_death(gc, evg); goto out; }
     }
-    event->path = strdup(events[XS_WATCH_PATH]);
-    event->token = strdup(events[XS_WATCH_TOKEN]);
-    event->type = atoi(event->token);
-    free(events);
-    return 0;
-}
 
-int libxl_stop_waiting(libxl_ctx *ctx, libxl_waiter *waiter)
-{
-    if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
-        return ERROR_FAIL;
-    else
-        return 0;
-}
+    rc = 0;
 
-int libxl_free_event(libxl_event *event)
-{
-    free(event->path);
-    free(event->token);
-    return 0;
-}
+ out:
+    CTX_UNLOCK;
+    return rc;
+};
 
-int libxl_free_waiter(libxl_waiter *waiter)
-{
-    free(waiter->path);
-    free(waiter->token);
-    return 0;
-}
+void libxl__evdisable_domain_death(libxl__gc *gc,
+                                   libxl_evgen_domain_death *evg) {
+    CTX_LOCK;
 
-int libxl_event_get_domain_death_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_dominfo *info)
-{
-    if (libxl_domain_info(ctx, info, domid) < 0)
-        return 0;
+    if (!evg->death_reported)
+        LIBXL_TAILQ_REMOVE(&CTX->death_list, evg, entry);
+    else
+        LIBXL_TAILQ_REMOVE(&CTX->death_reported, evg, entry);
 
-    if (info->running || (!info->shutdown && !info->dying))
-        return ERROR_INVAL;
+    free(evg);
 
-    return 1;
+    if (!LIBXL_TAILQ_FIRST(&CTX->death_list) &&
+        libxl__ev_xswatch_isregistered(&CTX->death_watch))
+        libxl__ev_xswatch_deregister(gc, &CTX->death_watch);
+
+    CTX_UNLOCK;
 }
 
-int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
-{
+void libxl_evdisable_domain_death(libxl_ctx *ctx,
+                                  libxl_evgen_domain_death *evg) {
     GC_INIT(ctx);
-    char *path;
+    libxl__evdisable_domain_death(gc, evg);
+    GC_FREE;
+}
+
+static void disk_eject_xswatch_callback(libxl__egc *egc, libxl__ev_xswatch *w,
+                                        const char *wpath, const char *epath) {
+    libxl__gc *gc = &egc->gc;
+    libxl_evgen_disk_eject *evg = (void*)w;
     char *backend;
     char *value;
     char backend_type[BACKEND_STRING_SIZE+1];
 
-    value = libxl__xs_read(gc, XBT_NULL, event->path);
+    value = libxl__xs_read(gc, XBT_NULL, wpath);
 
-    if (!value || strcmp(value,  "eject")) {
-        GC_FREE;
-        return 0;
+    if (!value || strcmp(value,  "eject"))
+        return;
+
+    if (libxl__xs_write(gc, XBT_NULL, wpath, "")) {
+        LIBXL__EVENT_DISASTER(egc, "xs_write failed acknowledging eject",
+                              errno, LIBXL_EVENT_TYPE_DISK_EJECT);
+        return;
     }
 
-    path = strdup(event->path);
-    path[strlen(path) - 6] = '\0';
-    backend = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/backend", path));
+    libxl_event *ev = NEW_EVENT(egc, DISK_EJECT, evg->domid);
+    libxl_device_disk *disk = &ev->u.disk_eject.disk;
+    
+    backend = libxl__xs_read(gc, XBT_NULL,
+                             libxl__sprintf(gc, "%.*s/backend",
+                                            (int)strlen(wpath)-6, wpath));
 
     sscanf(backend,
-            "/local/domain/%d/backend/%" TOSTRING(BACKEND_STRING_SIZE) "[a-z]/%*d/%*d",
-            &disk->backend_domid, backend_type);
+            "/local/domain/%d/backend/%" TOSTRING(BACKEND_STRING_SIZE)
+           "[a-z]/%*d/%*d",
+           &disk->backend_domid, backend_type);
     if (!strcmp(backend_type, "tap") || !strcmp(backend_type, "vbd")) {
         disk->backend = LIBXL_DISK_BACKEND_TAP;
     } else if (!strcmp(backend_type, "qdisk")) {
@@ -765,19 +853,82 @@ int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event
         disk->backend = LIBXL_DISK_BACKEND_UNKNOWN;
     }
 
-    disk->pdev_path = strdup("");
+    disk->pdev_path = strdup(""); /* xxx fixme malloc failure */
     disk->format = LIBXL_DISK_FORMAT_EMPTY;
     /* this value is returned to the user: do not free right away */
-    disk->vdev = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(gc, "%s/dev", backend), NULL);
+    disk->vdev = xs_read(CTX->xsh, XBT_NULL,
+                         libxl__sprintf(gc, "%s/dev", backend), NULL);
     disk->removable = 1;
     disk->readwrite = 0;
     disk->is_cdrom = 1;
 
-    free(path);
+    libxl__event_occurred(egc, ev);
+}
+
+int libxl_evenable_disk_eject(libxl_ctx *ctx, uint32_t guest_domid,
+                              const char *vdev, libxl_ev_user user,
+                              libxl_evgen_disk_eject **evgen_out) {
+    GC_INIT(ctx);
+    CTX_LOCK;
+    int rc;
+    char *path;
+    libxl_evgen_disk_eject *evg = NULL;
+
+    evg = malloc(sizeof(*evg));  if (!evg) { rc = ERROR_NOMEM; goto out; }
+    memset(evg, 0, sizeof(*evg));
+    evg->user = user;
+    evg->domid = guest_domid;
+    LIBXL_LIST_INSERT_HEAD(&CTX->disk_eject_evgens, evg, entry);
+
+    evg->vdev = strdup(vdev);
+    if (!evg->vdev) { rc = ERROR_NOMEM; goto out; }
+
+    uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
+
+    if (!domid)
+        domid = guest_domid;
+
+    path = libxl__sprintf(gc, "%s/device/vbd/%d/eject",
+                 libxl__xs_get_dompath(gc, domid),
+                 libxl__device_disk_dev_number(vdev, NULL, NULL));
+    if (!path) { rc = ERROR_NOMEM; goto out; }
+
+    rc = libxl__ev_xswatch_register(gc, &evg->watch,
+                                    disk_eject_xswatch_callback, path);
+    if (rc) goto out;
+
+    CTX_UNLOCK;
     GC_FREE;
-    return 1;
+    return 0;
+
+ out:
+    if (evg)
+        libxl__evdisable_disk_eject(gc, evg);
+    CTX_UNLOCK;
+    GC_FREE;
+    return rc;
+}
+
+void libxl__evdisable_disk_eject(libxl__gc *gc, libxl_evgen_disk_eject *evg) {
+    CTX_LOCK;
+
+    LIBXL_LIST_REMOVE(evg, entry);
+
+    if (libxl__ev_xswatch_isregistered(&evg->watch))
+        libxl__ev_xswatch_deregister(gc, &evg->watch);
+
+    free(evg->vdev);
+    free(evg);
+
+    CTX_UNLOCK;
 }
 
+void libxl_evdisable_disk_eject(libxl_ctx *ctx, libxl_evgen_disk_eject *evg) {
+    GC_INIT(ctx);
+    libxl__evdisable_disk_eject(gc, evg);
+    GC_FREE;
+}    
+
 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid)
 {
     GC_INIT(ctx);
tools/libxl/libxl.h +9 -46

79 lines not on this page · raw message

tools/libxl/libxl_event.c +220 -19

296 lines not on this page · raw message

tools/libxl/libxl_event.h +180 -4

202 lines not on this page · raw message

tools/libxl/libxl_internal.h +78 -4

150 lines not on this page · raw message

tools/libxl/libxl_types.idl +29 -5
index 78a1cc6..6728479 100644
@@ -75,11 +75,6 @@ libxl_action_on_shutdown = Enumeration("action_on_shutdown", [
     (6, "COREDUMP_RESTART"),
     ])
 
-libxl_event_type = Enumeration("event_type", [
-    (1, "DOMAIN_DEATH"),
-    (2, "DISK_EJECT"),
-    ])
-
 libxl_button = Enumeration("button", [
     (1, "POWER"),
     (2, "SLEEP"),
@@ -394,3 +389,32 @@ libxl_sched_sedf = Struct("sched_sedf", [
     ("extratime", integer),
     ("weight", integer),
     ], dispose_fn=None)
+
+libxl_event_type = Enumeration("event_type", [
+    (1, "DOMAIN_SHUTDOWN"),
+    (2, "DOMAIN_DESTROY"),
+    (3, "DISK_EJECT"),
+    ])
+
+libxl_ev_user = Number("libxl_ev_user")
+
+libxl_ev_link = Builtin("ev_link", passby=PASS_BY_REFERENCE, private=True)
+
+libxl_event = Struct("event",[
+    ("link",     libxl_ev_link,0,
+     "for use by libxl; caller may use this once the event has been"
+     " returned by libxl_event_{check,wait}"),
+    ("domid",    libxl_domid),
+    ("domuuid",  libxl_uuid),
+    ("for_user", libxl_ev_user),
+    ("type",     libxl_event_type),
+    ("u", KeyedUnion(None, libxl_event_type, "type",
+          [("domain_shutdown", Struct(None, [
+                                             ("shutdown_reason", uint8),
+                                      ])),
+           ("domain_destroy", Struct(None, [])),
+           ("disk_eject", Struct(None, [
+                                        ("vdev", string),
+                                        ("disk", libxl_device_disk),
+                                 ])),
+           ]))])
tools/libxl/xl_cmdimpl.c +157 -113

354 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#26 lore raw

Re: [PATCH 08/10] libxl: New event generation API

Quoted 7 lines · Ian Jackson

Replace the existing API for retrieving high-level events (events about domains, etc.) from libxl with a new one.

This changes the definition and semantics of the `libxl_event' structure, and replaces the calls for obtaining information about domain death and disk eject events.

Quoted 5 lines

This is an incompatible change, sorry. The alternative was to try to provide both the previous horrid API and the new one, and would also involve never using the name `libxl_event' for the new interface.

I think I've seen most of this before too so again I only skimmed it. Nothing much jumped out but I must admit I'm suffering from Friday afternoon review fatigue...

Quoted 18 lines
tools/libxl/libxl_types.idl +0 -5
index 78a1cc6..6728479 100644
@@ -75,11 +75,6 @@ libxl_action_on_shutdown = Enumeration("action_on_shutdown", [
     (6, "COREDUMP_RESTART"),
     ])
 
-libxl_event_type = Enumeration("event_type", [
-    (1, "DOMAIN_DEATH"),
-    (2, "DISK_EJECT"),
-    ])
-
 libxl_button = Enumeration("button", [
     (1, "POWER"),
     (2, "SLEEP"),
@@ -394,3 +389,32 @@ libxl_sched_sedf = Struct("sched_sedf", [
     ("extratime", integer),

tools/libxl/libxl_types.idl libxl_sched_sedf = Struct("sched_sedf", [ · line 399

Quoted 10 lines
tools/libxl/libxl_types.idl +8 -0
     ("weight", integer),
     ], dispose_fn=None)
+
+libxl_event_type = Enumeration("event_type", [
+    (1, "DOMAIN_SHUTDOWN"),
+    (2, "DOMAIN_DESTROY"),
+    (3, "DISK_EJECT"),
+    ])
+
+libxl_ev_user = Number("libxl_ev_user")

This doesn't turn into libxl_libxl_ev_user in the code, does it?

No, I checked, It's right, Good ;-)

tools/libxl/libxl_types.idl libxl_sched_sedf = Struct("sched_sedf", [ · line 406

Quoted 7 lines
tools/libxl/libxl_types.idl +7 -0
+
+libxl_ev_link = Builtin("ev_link", passby=PASS_BY_REFERENCE, private=True)
+
+libxl_event = Struct("event",[
+    ("link",     libxl_ev_link,0,
+     "for use by libxl; caller may use this once the event has been"
+     " returned by libxl_event_{check,wait}"),

I've got a pending patch which does away with comments in the IDL syntax in favour of source level comments (e.g. "# for use by...."). I don't think anyone reads the comments in generated code in preference to the comments in the IDL source...

Either we race here or you can just go straight for the source level comments.

Quoted 18 lines
tools/libxl/libxl_types.idl +14 -0
+    ("domid",    libxl_domid),
+    ("domuuid",  libxl_uuid),
+    ("for_user", libxl_ev_user),
+    ("type",     libxl_event_type),
+    ("u", KeyedUnion(None, libxl_event_type, "type",
+          [("domain_shutdown", Struct(None, [
+                                             ("shutdown_reason", uint8),
+                                      ])),
+           ("domain_destroy", Struct(None, [])),
+           ("disk_eject", Struct(None, [
+                                        ("vdev", string),
+                                        ("disk", libxl_device_disk),
+                                 ])),
+           ]))])
tools/libxl/xl_cmdimpl.c +0 -0
index 8270f34..a18c6b2 100644

[...]

tools/libxl/xl_cmdimpl.c start: · line 1813

Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2 -0
+        case LIBXL_EVENT_TYPE_DISK_EJECT:
+            /* XXX what is this for? */

This is signalled by qemu when the guest opens the CD-ROM drive.

(not sure if this is your comment or an existing one which got reindented.)

Ian.

#27 lore raw

Re: [PATCH 08/10] libxl: New event generation API

Ian Campbell writes ("Re: [Xen-devel] [PATCH 08/10] libxl: New event generation API"):

Quoted 3 lines

I think I've seen most of this before too so again I only skimmed it. Nothing much jumped out but I must admit I'm suffering from Friday afternoon review fatigue...

Heh.

Quoted 3 lines · Ian Jackson
Quoted 2 lines
tools/libxl/libxl_types.idl +0 -0
index 78a1cc6..6728479 100644

...

Quoted 11 lines

tools/libxl/libxl_types.idl libxl_sched_sedf = Struct("sched_sedf", [ · line 399

Quoted 7 lines
tools/libxl/libxl_types.idl +7 -0
+libxl_event_type = Enumeration("event_type", [
+    (1, "DOMAIN_SHUTDOWN"),
+    (2, "DOMAIN_DESTROY"),
+    (3, "DISK_EJECT"),
+    ])
+
+libxl_ev_user = Number("libxl_ev_user")

This doesn't turn into libxl_libxl_ev_user in the code, does it?

No, I checked, It's right, Good ;-)

In fact to make the ocaml binding work it is necessary to write this as
libxl_ev_user = UInt(64);
but we can keep the typedef. This is because the ocaml idl generator wants to know the size of the integer. I think this is not an unreasonable requirement so I will keep that change even though I'm about to disable the generation of an ocaml binding for libxl_event for other reasons (mainly, the lack of KeyedUnion).

Quoted 12 lines

tools/libxl/libxl_types.idl libxl_sched_sedf = Struct("sched_sedf", [ · line 406

Quoted 6 lines
tools/libxl/libxl_types.idl +6 -0
+libxl_ev_link = Builtin("ev_link", passby=PASS_BY_REFERENCE, private=True)
+
+libxl_event = Struct("event",[
+    ("link",     libxl_ev_link,0,
+     "for use by libxl; caller may use this once the event has been"
+     " returned by libxl_event_{check,wait}"),

I've got a pending patch which does away with comments in the IDL syntax in favour of source level comments (e.g. "# for use by...."). I don't think anyone reads the comments in generated code in preference to the comments in the IDL source...

Quoted 2 lines

Either we race here or you can just go straight for the source level comments.

I'll do the latter.

Quoted 5 lines
Quoted 4 lines
tools/libxl/xl_cmdimpl.c +0 -0
index 8270f34..a18c6b2 100644

[...]

Quoted 7 lines

tools/libxl/xl_cmdimpl.c start: · line 1813

Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2 -0
+        case LIBXL_EVENT_TYPE_DISK_EJECT:
+            /* XXX what is this for? */

This is signalled by qemu when the guest opens the CD-ROM drive.

(not sure if this is your comment or an existing one which got reindented.)

It's an existing comment, which I didn't feel confident to remove. I guess if we're happy with all the code here I could delete it.

Ian.

#28 lore raw

PATCH v6 9/10 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

In reply to Ian Jackson

We want a function for setting fds to nonblocking, so introduce one.

This is a very similar requirement to that for libxl_fd_set_cloexec, so make it common with that.

While we're at it, fix a few deficiences that make this latter function less desirable than it could be:
* Change the return from 0/-1 (like a syscall) to a libxl error code
* Take a boolean parameter for turning the flag on and off
* Log on error (and so, take a ctx for this purpose)

Change callers of libxl_fd_set_cloexec to notice errors. (Although, such errors are highly unlikely.)

---
 tools/libxl/libxl.c      |   33 ++++++++++++++++++++++++++-------
 tools/libxl/libxl.h      |    6 +++++-
 tools/libxl/libxl_qmp.c  |    3 ++-
 tools/libxl/xl_cmdimpl.c |    3 ++-
 4 files changed, 35 insertions(+), 10 deletions(-)
tools/libxl/libxl.c +26 -7
index f6370d4..42b64d8 100644
@@ -3647,19 +3647,38 @@ int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
     return 0;
 }
 
-int libxl_fd_set_cloexec(int fd)
+static int fd_set_flags(libxl_ctx *ctx, int fd,
+                        int fcntlgetop, int fcntlsetop, const char *fl,
+                        int flagmask, int set_p)
 {
-    int flags = 0;
+    int flags, r;
 
-    if ((flags = fcntl(fd, F_GETFD)) == -1) {
-        flags = 0;
+    flags = fcntl(fd, fcntlgetop);
+    if (flags == -1) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "fcntl(,F_GET%s) failed",fl);
+        return ERROR_FAIL;
     }
-    if ((flags & FD_CLOEXEC)) {
-        return 0;
+
+    if (set_p)
+        flags |= flagmask;
+    else
+        flags &= ~flagmask;
+
+    r = fcntl(fd, fcntlsetop, flags);
+    if (r == -1) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "fcntl(,F_SET%s) failed",fl);
+        return ERROR_FAIL;
     }
-    return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+
+    return 0;
 }
 
+int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec)
+  { return fd_set_flags(ctx,fd, F_GETFD,F_SETFD,"FD", FD_CLOEXEC, cloexec); }
+
+int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock)
+  { return fd_set_flags(ctx,fd, F_GETFL,F_SETFL,"FL", O_NONBLOCK, nonblock); }
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl.h +5 -1
index 97ca25e..5396ba8 100644
@@ -613,7 +613,11 @@ const char *libxl_run_dir_path(void);
 const char *libxl_xenpaging_dir_path(void);
 
 /* misc */
-int libxl_fd_set_cloexec(int fd);
+int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec);
+int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock);
+  /* Each of these sets or clears the flag according to whether the
+   * 2nd parameter is nonzero.  On failure, they log, and
+   * return ERROR_FAIL, but also leave errno valid. */
 
 #include <libxl_event.h>
 
tools/libxl/libxl_qmp.c +2 -1
index 58f5283..05df4d7 100644
@@ -324,7 +324,8 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
     if (fcntl(qmp->qmp_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
         return -1;
     }
-    libxl_fd_set_cloexec(qmp->qmp_fd);
+    ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);
+    if (ret) return -1;
 
     memset(&qmp->addr, 0, sizeof (&qmp->addr));
     qmp->addr.sun_family = AF_UNIX;
tools/libxl/xl_cmdimpl.c +2 -1
index a18c6b2..b88fc8a 100644
@@ -1490,7 +1490,8 @@ static int create_domain(struct domain_create *dom_info)
             restore_fd = migrate_fd;
         } else {
             restore_fd = open(restore_file, O_RDONLY);
-            libxl_fd_set_cloexec(restore_fd);
+            rc = libxl_fd_set_cloexec(ctx, restore_fd, 1);
+            if (rc) return rc;
         }
 
         CHK_ERRNO( libxl_read_exactly(ctx, restore_fd, &hdr,
Signature · 1 line
1.7.2.5
Acked-by on v6 9/10
#29 lore raw

Re: [PATCH 09/10] libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

Quoted 11 lines · Ian Jackson

We want a function for setting fds to nonblocking, so introduce one.

This is a very similar requirement to that for libxl_fd_set_cloexec, so make it common with that.

While we're at it, fix a few deficiences that make this latter function less desirable than it could be:
* Change the return from 0/-1 (like a syscall) to a libxl error code
* Take a boolean parameter for turning the flag on and off
* Log on error (and so, take a ctx for this purpose)

Quoted 4 lines

Change callers of libxl_fd_set_cloexec to notice errors. (Although, such errors are highly unlikely.)

(although I notice that you've slipped back into the comment-follows-prototype style...)

Quoted 102 lines
---
 tools/libxl/libxl.c      |   33 ++++++++++++++++++++++++++-------
 tools/libxl/libxl.h      |    6 +++++-
 tools/libxl/libxl_qmp.c  |    3 ++-
 tools/libxl/xl_cmdimpl.c |    3 ++-
 4 files changed, 35 insertions(+), 10 deletions(-)
tools/libxl/libxl.c +26 -7
index f6370d4..42b64d8 100644
@@ -3647,19 +3647,38 @@ int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
     return 0;
 }
 
-int libxl_fd_set_cloexec(int fd)
+static int fd_set_flags(libxl_ctx *ctx, int fd,
+                        int fcntlgetop, int fcntlsetop, const char *fl,
+                        int flagmask, int set_p)
 {
-    int flags = 0;
+    int flags, r;
 
-    if ((flags = fcntl(fd, F_GETFD)) == -1) {
-        flags = 0;
+    flags = fcntl(fd, fcntlgetop);
+    if (flags == -1) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "fcntl(,F_GET%s) failed",fl);
+        return ERROR_FAIL;
     }
-    if ((flags & FD_CLOEXEC)) {
-        return 0;
+
+    if (set_p)
+        flags |= flagmask;
+    else
+        flags &= ~flagmask;
+
+    r = fcntl(fd, fcntlsetop, flags);
+    if (r == -1) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "fcntl(,F_SET%s) failed",fl);
+        return ERROR_FAIL;
     }
-    return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+
+    return 0;
 }
 
+int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec)
+  { return fd_set_flags(ctx,fd, F_GETFD,F_SETFD,"FD", FD_CLOEXEC, cloexec); }
+
+int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock)
+  { return fd_set_flags(ctx,fd, F_GETFL,F_SETFL,"FL", O_NONBLOCK, nonblock); }
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl.h +5 -1
index 97ca25e..5396ba8 100644
@@ -613,7 +613,11 @@ const char *libxl_run_dir_path(void);
 const char *libxl_xenpaging_dir_path(void);
 
 /* misc */
-int libxl_fd_set_cloexec(int fd);
+int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec);
+int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock);
+  /* Each of these sets or clears the flag according to whether the
+   * 2nd parameter is nonzero.  On failure, they log, and
+   * return ERROR_FAIL, but also leave errno valid. */
 
 #include <libxl_event.h>
 
tools/libxl/libxl_qmp.c +2 -1
index 58f5283..05df4d7 100644
@@ -324,7 +324,8 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
     if (fcntl(qmp->qmp_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
         return -1;
     }
-    libxl_fd_set_cloexec(qmp->qmp_fd);
+    ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);
+    if (ret) return -1;
 
     memset(&qmp->addr, 0, sizeof (&qmp->addr));
     qmp->addr.sun_family = AF_UNIX;
tools/libxl/xl_cmdimpl.c +2 -1
index a18c6b2..b88fc8a 100644
@@ -1490,7 +1490,8 @@ static int create_domain(struct domain_create *dom_info)
             restore_fd = migrate_fd;
         } else {
             restore_fd = open(restore_file, O_RDONLY);
-            libxl_fd_set_cloexec(restore_fd);
+            rc = libxl_fd_set_cloexec(ctx, restore_fd, 1);
+            if (rc) return rc;
         }
 
         CHK_ERRNO( libxl_read_exactly(ctx, restore_fd, &hdr,
#30 lore raw

Re: [PATCH 09/10] libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

Ian Campbell writes ("Re: [Xen-devel] [PATCH 09/10] libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec"):

Quoted 2 lines · Ian Jackson
Quoted 1 line

We want a function for setting fds to nonblocking, so introduce one.

...

Quoted 2 lines

(although I notice that you've slipped back into the comment-follows-prototype style...)

Fixed.

(I still think it's ridiculous.)

Ian.

#31 lore raw

PATCH v6 10/10 libxl: Permit multithreaded event waiting

In reply to Ian Jackson

Previously, the context would be locked whenever we were waiting in libxl's own call to poll (waiting for operating system events).

This would mean that multiple simultaneous calls to libxl_event_wait in different threads with different parameters would not work properly.

If we simply unlock the context, it would be possible for another thread to discover the occurrence of the event we were waiting for, without us even waking up, and we would remain in poll. So we need a way to wake up other threads: a pipe, one for each thread in poll.

We also need to move some variables from globals in the ctx to be per-polling-thread.

---
 tools/libxl/libxl.c          |   18 +++-
 tools/libxl/libxl_event.c    |  196 ++++++++++++++++++++++++++++++++++--------
 tools/libxl/libxl_internal.h |   51 ++++++++++-
 3 files changed, 219 insertions(+), 46 deletions(-)
tools/libxl/libxl.c +14 -4
index 42b64d8..b8167b3 100644
@@ -49,8 +49,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
 
     ctx->osevent_hooks = 0;
 
-    ctx->fd_polls = 0;
-    ctx->fd_rindex = 0;
+    LIBXL_LIST_INIT(&ctx->pollers_event);
+    LIBXL_LIST_INIT(&ctx->pollers_idle);
+
     LIBXL_LIST_INIT(&ctx->efds);
     LIBXL_TAILQ_INIT(&ctx->etimes);
 
@@ -61,6 +62,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     LIBXL_TAILQ_INIT(&ctx->death_list);
     libxl__ev_xswatch_init(&ctx->death_watch);
 
+    rc = libxl__poller_init(ctx, &ctx->poller_app);
+    if (rc) goto out;
+
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
                      "failed to stat %s", XENSTORE_PID_FILE);
@@ -135,8 +139,14 @@ int libxl_ctx_free(libxl_ctx *ctx)
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
 
-    free(ctx->fd_polls);
-    free(ctx->fd_rindex);
+    libxl__poller_dispose(&ctx->poller_app);
+    assert(LIBXL_LIST_EMPTY(&ctx->pollers_event));
+    libxl__poller *poller, *poller_tmp;
+    LIBXL_LIST_FOREACH_SAFE(poller, &ctx->pollers_idle, entry, poller_tmp) {
+        libxl__poller_dispose(poller);
+        free(poller);
+    }
+
     free(ctx->watch_slots);
 
     discard_events(&ctx->occurred);
tools/libxl/libxl_event.c +158 -38

320 lines not on this page · raw message

tools/libxl/libxl_internal.h +47 -4

69 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#32 lore raw

RFC PATCH 1/1 libxl: asynchronous operations

In reply to Ian Jackson

Here are four more patches to go onto the end of my previous 10-patch event series posting:

11/10 libxl: Asynchronous/long-running operation infrastructure 12/10 libxl: New convenience macro CONTAINING_STRUCT 13/10 libxl: Introduce libxl__ev_devstate
14/10 libxl: Convert to asynchronous: device removal

This introduces a standard way for the same libxl function to be useable both by simple-minded callers who want to wait synchronously, and by advanced callers who want to the function to return immediately and arrange a callback or event later.

#33 lore raw

[PATCH 11/10] libxl: Asynchronous/long-running operation infrastructure

In reply to Ian Jackson

Provide a new set of machinery for writing public libxl functions which may take a long time. The application gets to decide whether they want the function to be synchronous, or whether they'd prefer to get a callback, or an event, when the operation is complete.

User(s) of this machinery will be introduced in later patch(es).

---
 tools/libxl/libxl.h          |   50 ++++++++++++
 tools/libxl/libxl_event.c    |  183 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |  105 ++++++++++++++++++++++++
 tools/libxl/libxl_types.idl  |    4 +
 4 files changed, 342 insertions(+), 0 deletions(-)
tools/libxl/libxl.h +50 -0
index 5396ba8..e8fed73 100644
@@ -235,8 +235,58 @@ enum {
     ERROR_NOT_READY = -11,
     ERROR_OSEVENT_REG_FAIL = -12,
     ERROR_BUFFERFULL = -13,
+    ERROR_ASYNC_INPROGRESS = -14,
 };
 
+
+/*
+ * Some libxl operations can take a long time.  These functions take a
+ * parameter to control their concurrency:
+ *     libxl_asyncop_how *ao_how
+ *
+ * If ao_how==NULL, the function will be synchronous.
+ *
+ * If ao_how!=NULL, the function will set the operation going, and
+ * if this is successful will return ERROR_ASYNCH_INPROGRESS.
+ *
+ * If ao_how->callback!=NULL, the callback will be called when the
+ * operation completes.  The same rules as for libxl_event_hooks
+ * apply, including the reentrancy rules and the possibility of
+ * "disaster", except that libxl calls ao_how->callback instead of
+ * libxl_event_hooks.event_occurs.
+ *
+ * If ao_how->callback==NULL, a libxl_event will be generated which
+ * can be obtained from libxl_event_wait or libxl_event_check.  The
+ * event will have type OPERATION_COMPLETE (which is not used
+ * elsewhere).
+ *
+ * Note that it is possible for an asynchronous operation which is to
+ * result in a callback to complete during its initiating function
+ * call.  In this case the initating function will return
+ * ERROR_ASYNCH_INPROGRESS, even though by the time it returns the
+ * operation is complete and the callback has already happened.
+ *
+ * The application must set and use ao_how->for_event (which will be
+ * copied into libxl_event.for_user) or ao_how->for_callback (passed
+ * to the callback) to determine which operation finished, and it must
+ * of course check the rc value for errors.
+ *
+ * *ao_how does not need to remain valid after the initiating function
+ * returns.
+ *
+ * Callbacks may occur on any thread in which the application calls
+ * libxl.
+ */
+
+typedef struct {
+    void (*callback)(libxl_ctx *ctx, int rc, void *for_callback);
+    union {
+        libxl_ev_user for_event; /* used if callback==NULL */
+        void *for_callback; /* passed to callback */
+    } u;
+} libxl_asyncop_how;
+
+
 #define LIBXL_VERSION 0
 
 typedef struct {
tools/libxl/libxl_event.c +183 -0

199 lines not on this page · raw message

tools/libxl/libxl_internal.h +105 -0

129 lines not on this page · raw message

tools/libxl/libxl_types.idl +4 -0
index 6728479..eff4d43 100644
@@ -394,6 +394,7 @@ libxl_event_type = Enumeration("event_type", [
     (1, "DOMAIN_SHUTDOWN"),
     (2, "DOMAIN_DESTROY"),
     (3, "DISK_EJECT"),
+    (4, "OPERATION_COMPLETE"),
     ])
 
 libxl_ev_user = Number("libxl_ev_user")
@@ -417,4 +418,7 @@ libxl_event = Struct("event",[
                                         ("vdev", string),
                                         ("disk", libxl_device_disk),
                                  ])),
+           ("operation_complete", Struct(None, [
+                                        ("rc", integer),
+                                 ])),
            ]))])
Signature · 1 line
1.7.2.5
#34 lore raw

[PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT

In reply to Ian Jackson

Provide a convenient and type-safe wrapper which does the correct dance to subtract offsetof.

---
 tools/libxl/libxl_internal.h |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +35 -0
index e0ff15c..e0c2ad6 100644
@@ -1224,6 +1224,41 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);
  * Convenience macros.
  */
 
+/*
+ * [GET_]CONTAINING_STRUCT work like this.  Given:
+ *    typedef struct {
+ *      ...
+ *      member_type member_name;
+ *      ...
+ *    } outer_type;
+ * Then:
+ *    void GET_CONTAINING_STRUCT(outer_type *outer_var [NB:UPDATED],
+ *                               some_type *inner_ptr,
+ *                               member_name);
+ *    outer_type *CONTAINING_STRUCT(outer_type,
+ *                                  some_type *inner_ptr,
+ *                                  member_name);
+ * The semantics are that after:
+ *    outer_type outer, *outer_var;
+ *    member_type *inner_ptr = &outer->member_name;
+ *    GET_CONTAINING_STRUCT(outer_var, &outer_ptr->member_name, member_name)
+ * The following hold:
+ *    CONTAINING_STRUCT(inner_ptr, outer_type, member_name) == outer_ptr
+ *    outer_var == &outer
+ */
+#define GET_CONTAINING_STRUCT(outer_var, inner_ptr, member_name)        \
+    ((outer_var) = (void*)((char*)(inner_ptr) -                         \
+                           offsetof(typeof(*(outer_var)), member_name)), \
+     (void)(&(outer_var)->member_name ==                                \
+           (typeof(inner_ptr))0) /* type check */,                      \
+     (void)0)
+#define CONTAINING_STRUCT(outer_type, inner_ptr, member_name)           \
+    ({                                                                  \
+        typeof(outer_type) *containing_struct;                          \
+        GET_CONTAINING_STRUCT(containing_struct, inner_ptr, member_name); \
+        containing_struct;                                              \
+    })
+
 
 /*
  * All of these assume (or define)
Signature · 1 line
1.7.2.5
#35 lore raw

Re: [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT

Quoted 26 lines · Ian Jackson

Provide a convenient and type-safe wrapper which does the correct dance to subtract offsetof.

---
 tools/libxl/libxl_internal.h |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +10 -0
index e0ff15c..e0c2ad6 100644
@@ -1224,6 +1224,41 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);
  * Convenience macros.
  */
 
+/*
+ * [GET_]CONTAINING_STRUCT work like this.  Given:
+ *    typedef struct {
+ *      ...
+ *      member_type member_name;
+ *      ...
+ *    } outer_type;
+ * Then:
+ *    void GET_CONTAINING_STRUCT(outer_type *outer_var [NB:UPDATED],
+ *                               some_type *inner_ptr,

tools/libxl/libxl_internal.h _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao); · line 1246

Quoted 10 lines
tools/libxl/libxl_internal.h +10 -0
+ *                               member_name);
+ *    outer_type *CONTAINING_STRUCT(outer_type,
+ *                                  some_type *inner_ptr,
+ *                                  member_name);
+ * The semantics are that after:
+ *    outer_type outer, *outer_var;
+ *    member_type *inner_ptr = &outer->member_name;
+ *    GET_CONTAINING_STRUCT(outer_var, &outer_ptr->member_name, member_name)
+ * The following hold:
+ *    CONTAINING_STRUCT(inner_ptr, outer_type, member_name) == outer_ptr

There is no outer_ptr in the givens, did you mean outer_var or something else?

It's not entirely clear to me what the distinction between the GET_ and non GET_ variant is (just that one returns the thing and the other updates a variable?) and/or why we would need both. The common operation is to go from inner_ptr to outer_ptr I think and CONTAINING_STRUCT seems to fill that niche just fine.

BTW, in Linux this is called container_of, which is maybe more familiar to people?

Quoted 18 lines
tools/libxl/libxl_internal.h +15 -0
+ *    outer_var == &outer
+ */
+#define GET_CONTAINING_STRUCT(outer_var, inner_ptr, member_name)        \
+    ((outer_var) = (void*)((char*)(inner_ptr) -                         \
+                           offsetof(typeof(*(outer_var)), member_name)), \
+     (void)(&(outer_var)->member_name ==                                \
+           (typeof(inner_ptr))0) /* type check */,                      \
+     (void)0)
+#define CONTAINING_STRUCT(outer_type, inner_ptr, member_name)           \
+    ({                                                                  \
+        typeof(outer_type) *containing_struct;                          \
+        GET_CONTAINING_STRUCT(containing_struct, inner_ptr, member_name); \
+        containing_struct;                                              \
+    })
+
 
 /*
  * All of these assume (or define)
#36 lore raw

Re: [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT

Ian Campbell writes ("Re: [Xen-devel] [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

Provide a convenient and type-safe wrapper which does the correct dance to subtract offsetof.

...

Quoted 13 lines
Quoted 7 lines

+ * void GET_CONTAINING_STRUCT(outer_type *outer_var [NB:UPDATED],
+ * some_type *inner_ptr,
+ * member_name);
+ * outer_type *CONTAINING_STRUCT(outer_type,
+ * some_type *inner_ptr,
+ * member_name);
+ * The semantics are that after:

Quoted 5 lines
+ *    outer_type outer, *outer_var;
+ *    member_type *inner_ptr = &outer->member_name;
+ *    GET_CONTAINING_STRUCT(outer_var, &outer_ptr->member_name, member_name)

+ * The following hold:
+ * CONTAINING_STRUCT(inner_ptr, outer_type, member_name) == outer_ptr

Quoted 2 lines

There is no outer_ptr in the givens, did you mean outer_var or something else?

I meant &outer. Fixed.

Quoted 3 lines

It's not entirely clear to me what the distinction between the GET_ and non GET_ variant is (just that one returns the thing and the other updates a variable?) and/or why we would need both.

That's exactly the difference.

Quoted 2 lines

The common operation is to go from inner_ptr to outer_ptr I think and CONTAINING_STRUCT seems to fill that niche just fine.

The reason we need GET_CONTAINING_STRUCT is because we want to write this:

    libxl__ev_devstate *ds;
    GET_CONTAINING_STRUCT(ds, watch, watch);

If we have to use CONTAINING_STRUCT, we have to write:

libxl__ev_devstate *ds =
CONTAINING_STRUCT(libxl__ev_devstate, watch, watch);

which is really unnecessarily verbose, because of the repetition of libxl__ev_devstate.

Quoted 2 lines

BTW, in Linux this is called container_of, which is maybe more familiar to people?

I'm not attached to the name. If we pick Linux's name we should make sure its semantics are identical, so the argument order should change.

Should it be called "container_of" or "CONTAINER_OF" ?

I still think we need something like the GET_... version, or some other construct that allows us to avoid writing out the type name twice.

Hmm, actually, in our version, you can write,

    libxl__ev_devstate *ds = CONTAINING_STRUCT(ds, watch, watch);

since we call typeof on the type argument.

So how about I change everything to use that pattern, rename it CONTAINER_OF, and remove the GET_ version ?

Ian.

#37 lore raw

Re: [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT

Quoted 58 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

Provide a convenient and type-safe wrapper which does the correct dance to subtract offsetof.

...

Quoted 13 lines
Quoted 7 lines

+ * void GET_CONTAINING_STRUCT(outer_type *outer_var [NB:UPDATED],
+ * some_type *inner_ptr,
+ * member_name);
+ * outer_type *CONTAINING_STRUCT(outer_type,
+ * some_type *inner_ptr,
+ * member_name);
+ * The semantics are that after:

Quoted 5 lines
+ *    outer_type outer, *outer_var;
+ *    member_type *inner_ptr = &outer->member_name;
+ *    GET_CONTAINING_STRUCT(outer_var, &outer_ptr->member_name, member_name)

+ * The following hold:
+ * CONTAINING_STRUCT(inner_ptr, outer_type, member_name) == outer_ptr

Quoted 2 lines

There is no outer_ptr in the givens, did you mean outer_var or something else?

I meant &outer. Fixed.

Quoted 3 lines

It's not entirely clear to me what the distinction between the GET_ and non GET_ variant is (just that one returns the thing and the other updates a variable?) and/or why we would need both.

That's exactly the difference.

Quoted 2 lines

The common operation is to go from inner_ptr to outer_ptr I think and CONTAINING_STRUCT seems to fill that niche just fine.

The reason we need GET_CONTAINING_STRUCT is because we want to write this:

    libxl__ev_devstate *ds;
    GET_CONTAINING_STRUCT(ds, watch, watch);

If we have to use CONTAINING_STRUCT, we have to write:

libxl__ev_devstate *ds =
CONTAINING_STRUCT(libxl__ev_devstate, watch, watch);

which is really unnecessarily verbose, because of the repetition of libxl__ev_devstate.

Quoted 2 lines

BTW, in Linux this is called container_of, which is maybe more familiar to people?

I'm not attached to the name. If we pick Linux's name we should make sure its semantics are identical, so the argument order should change.

Should it be called "container_of" or "CONTAINER_OF" ?

I still think we need something like the GET_... version, or some other construct that allows us to avoid writing out the type name twice.

Quoted 8 lines

Hmm, actually, in our version, you can write,

    libxl__ev_devstate *ds = CONTAINING_STRUCT(ds, watch, watch);

since we call typeof on the type argument.

So how about I change everything to use that pattern, rename it CONTAINER_OF, and remove the GET_ version ?

Sounds good.

RE "container_of" vs. "CONTAINER_OF" -- Linux uses lowercase but that is against the usual style of macros being caps. I don't mind which.

Ian.

#38 lore raw

[PATCH 13/10] libxl: Introduce libxl__ev_devstate

In reply to Ian Jackson

Provide a new-style asynchronous facility for waiting for device states on xenbus. This will replace libxl__wait_for_device_state, after the callers have been updated in later patches.

---
 tools/libxl/libxl_event.c    |   74 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   41 +++++++++++++++++++++++
 2 files changed, 115 insertions(+), 0 deletions(-)
tools/libxl/libxl_event.c +74 -0
index c594494..c26210b 100644
@@ -506,6 +506,80 @@ void libxl__ev_xswatch_deregister(libxl__gc *gc, libxl__ev_xswatch *w)
 }
 
 /*
+ * waiting for device state
+ */
+
+static void devstate_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,
+                                const char *watch_path, const char *event_path)
+{
+    libxl__ev_devstate *ds;
+    libxl__gc *gc = &egc->gc;
+    GET_CONTAINING_STRUCT(ds, watch, watch);
+    int rc;
+
+    char *sstate = libxl__xs_read(&egc->gc, XBT_NULL, watch_path);
+    if (!sstate) {
+        if (errno == ENOENT) {
+            LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d"
+                       " but it was removed", watch_path, ds->wanted);
+            rc = ERROR_INVAL;
+        } else {
+            LIBXL__LOG_ERRNO(CTX, LIBXL__LOG_ERROR, "backend %s wanted state"
+                             " %d but read failed", watch_path, ds->wanted);
+            rc = ERROR_FAIL;
+        }
+    } else {
+        int got = atoi(sstate);
+        if (got == ds->wanted) {
+            LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d ok",
+                       watch_path, ds->wanted);
+            rc = 0;
+        } else {
+            LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d "
+                       " still waiting state %d", watch_path, ds->wanted, got);
+            return;
+        }
+    }
+    libxl__ev_devstate_cancel(&egc->gc, ds);
+    ds->callback(egc, ds, rc);
+}
+
+static void devstate_timeout(libxl__egc *egc, libxl__ev_time *ev,
+                             const struct timeval *requested_abs)
+{
+    libxl__ev_devstate *ds;
+    libxl__gc *gc = &egc->gc;
+    GET_CONTAINING_STRUCT(ds, ev, timeout);
+    LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d "
+               " timed out", ds->watch.path, ds->wanted);
+    libxl__ev_devstate_cancel(&egc->gc, ds);
+    ds->callback(egc, ds, ERROR_TIMEDOUT);
+}
+
+int libxl__ev_devstate_wait(libxl__gc *gc, libxl__ev_devstate *ds,
+                            libxl__ev_devstate_callback cb,
+                            const char *state_path, int state, int milliseconds)
+{
+    int rc;
+
+    ds->wanted = state;
+
+    rc = libxl__ev_time_register_rel(gc, &ds->timeout, devstate_timeout,
+                                     milliseconds);
+    if (rc) goto out;
+
+    rc = libxl__ev_xswatch_register(gc, &ds->watch, devstate_watch_callback,
+                                    state_path);
+    if (rc) goto out;
+
+    return 0;
+
+ out:
+    libxl__ev_devstate_cancel(gc, ds);
+    return rc;
+}
+
+/*
  * osevent poll
  */
 
tools/libxl/libxl_internal.h +41 -0

47 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#39 lore raw

[PATCH 14/10] libxl: Convert to asynchronous: device removal

In reply to Ian Jackson

Convert libxl_FOO_device_remove, and the function which does the bulk of the work, libxl__device_remove, to the new async ops scheme.

Adjust all callers.

Also remove libxl__wait_for_device_state which is now obsolete.

---
 tools/libxl/libxl.c          |   60 +++++++++++++--------
 tools/libxl/libxl.h          |   16 ++++--
 tools/libxl/libxl_device.c   |  119 +++++++++++++-----------------------------
 tools/libxl/libxl_internal.h |   31 ++---------
 tools/libxl/xl_cmdimpl.c     |    4 +-
 5 files changed, 95 insertions(+), 135 deletions(-)
tools/libxl/libxl.c +38 -22
index b8167b3..ce9aef2 100644
@@ -1308,19 +1308,23 @@ out:
 }
 
 int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
-                             libxl_device_disk *disk)
+                             libxl_device_disk *disk,
+                             const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
+    AO_CREATE(ctx, domid, ao_how);
     libxl__device device;
     int rc;
 
     rc = libxl__device_from_disk(gc, domid, disk, &device);
     if (rc != 0) goto out;
 
-    rc = libxl__device_remove(gc, &device, 1);
+    rc = libxl__initiate_device_remove(ao, &device);
+    if (rc) goto out;
+
+    AO_INPROGRESS;
+
 out:
-    GC_FREE;
-    return rc;
+    AO_ABORT(rc);
 }
 
 int libxl_device_disk_destroy(libxl_ctx *ctx, uint32_t domid,
@@ -1534,11 +1538,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
 
     ret = 0;
 
-    libxl_device_disk_remove(ctx, domid, disks + i);
+    libxl_device_disk_remove(ctx, domid, disks + i, 0);
     libxl_device_disk_add(ctx, domid, disk);
     stubdomid = libxl_get_stubdom_id(ctx, domid);
     if (stubdomid) {
-        libxl_device_disk_remove(ctx, stubdomid, disks + i);
+        libxl_device_disk_remove(ctx, stubdomid, disks + i, 0);
         libxl_device_disk_add(ctx, stubdomid, disk);
     }
 out:
@@ -1751,19 +1755,23 @@ out:
 }
 
 int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
-                            libxl_device_nic *nic)
+                            libxl_device_nic *nic,
+                            const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
+    AO_CREATE(ctx, domid, ao_how);
     libxl__device device;
     int rc;
 
     rc = libxl__device_from_nic(gc, domid, nic, &device);
     if (rc != 0) goto out;
 
-    rc = libxl__device_remove(gc, &device, 1);
+    rc = libxl__initiate_device_remove(ao, &device);
+    if (rc) goto out;
+
+    AO_INPROGRESS;
+
 out:
-    GC_FREE;
-    return rc;
+    AO_ABORT(rc);
 }
 
 int libxl_device_nic_destroy(libxl_ctx *ctx, uint32_t domid,
@@ -2091,19 +2099,23 @@ out:
 }
 
 int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid,
-                            libxl_device_vkb *vkb)
+                            libxl_device_vkb *vkb,
+                            const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
+    AO_CREATE(ctx, domid, ao_how);
     libxl__device device;
     int rc;
 
     rc = libxl__device_from_vkb(gc, domid, vkb, &device);
     if (rc != 0) goto out;
 
-    rc = libxl__device_remove(gc, &device, 1);
+    rc = libxl__initiate_device_remove(ao, &device);
+    if (rc) goto out;
+
+    AO_INPROGRESS;
+
 out:
-    GC_FREE;
-    return rc;
+    AO_ABORT(rc);
 }
 
 int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid,
@@ -2208,19 +2220,23 @@ out:
 }
 
 int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid,
-                            libxl_device_vfb *vfb)
+                            libxl_device_vfb *vfb,
+                            const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
+    AO_CREATE(ctx, domid, ao_how);
     libxl__device device;
     int rc;
 
     rc = libxl__device_from_vfb(gc, domid, vfb, &device);
     if (rc != 0) goto out;
 
-    rc = libxl__device_remove(gc, &device, 1);
+    rc = libxl__initiate_device_remove(ao, &device);
+    if (rc) goto out;
+
+    AO_INPROGRESS;
+
 out:
-    GC_FREE;
-    return rc;
+    AO_ABORT(rc);
 }
 
 int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid,
tools/libxl/libxl.h +12 -4
index e8fed73..207288e 100644
@@ -464,7 +464,9 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm);
 /* Disks */
 int libxl_device_disk_init(libxl_ctx *ctx, libxl_device_disk *disk);
 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk);
-int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk);
+int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
+                             libxl_device_disk *disk,
+                             const libxl_asyncop_how *ao_how);
 int libxl_device_disk_destroy(libxl_ctx *ctx, uint32_t domid,
                               libxl_device_disk *disk);
 
@@ -488,7 +490,9 @@ int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk);
 /* Network Interfaces */
 int libxl_device_nic_init(libxl_ctx *ctx, libxl_device_nic *nic);
 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
-int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
+int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
+                            libxl_device_nic *nic,
+                            const libxl_asyncop_how *ao_how);
 int libxl_device_nic_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
 
 libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int *num);
@@ -498,13 +502,17 @@ int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
 /* Keyboard */
 int libxl_device_vkb_init(libxl_ctx *ctx, libxl_device_vkb *vkb);
 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb);
-int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb);
+int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid,
+                            libxl_device_vkb *vkb,
+                            const libxl_asyncop_how *ao_how);
 int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb);
 
 /* Framebuffer */
 int libxl_device_vfb_init(libxl_ctx *ctx, libxl_device_vfb *vfb);
 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb);
-int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb);
+int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid,
+                            libxl_device_vfb *vfb,
+                            const libxl_asyncop_how *ao_how);
 int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb);
 
 /* PCI Passthrough */
tools/libxl/libxl_device.c +37 -82

145 lines not on this page · raw message

tools/libxl/libxl_internal.h +6 -25
index 6c87290..94ab681 100644
@@ -652,35 +652,16 @@ _hidden char *libxl__device_backend_path(libxl__gc *gc, libxl__device *device);
 _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device *device);
 _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,
                                       libxl__device *dev);
-_hidden int libxl__device_remove(libxl__gc *gc, libxl__device *dev, int wait);
 _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev);
 _hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid);
 _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);
 
-/* Handler for the libxl__wait_for_device_state callback */
-/*
- * libxl__device_state_handler - Handler for the libxl__wait_for_device_state
- * gc: allocation pool
- * l1: array containing the path and token
- * state: string that contains the state of the device
- *
- * Returns 0 on success, and < 0 on error.
- */
-typedef int libxl__device_state_handler(libxl__gc *gc, char **l1, char *state);
-
-/*
- * libxl__wait_for_device_state - waits a given time for a device to
- * reach a given state
- * gc: allocation pool
- * tv: timeval struct containing the maximum time to wait
- * state: state to wait for (check xen/io/xenbus.h)
- * handler: callback function to execute when state is reached
- *
- * Returns 0 on success, and < 0 on error.
- */
-_hidden int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv,
-                                         XenbusState state,
-                                         libxl__device_state_handler handler);
+_hidden int libxl__initiate_device_remove(libxl__ao*, libxl__device *dev);
+    /* Arranges that dev will be removed from its guest.  When
+     * this is done, the ao will be completed.  An error
+     * return from libxl__device_remove means that the ao
+     * will _not_ be completed and the caller must do so.
+     */
 
 /*
  * libxl__ev_devstate - waits a given time for a device to
tools/libxl/xl_cmdimpl.c +2 -2
index b88fc8a..dda74b9 100644
@@ -4616,7 +4616,7 @@ int main_networkdetach(int argc, char **argv)
             return 1;
         }
     }
-    if (libxl_device_nic_remove(ctx, domid, &nic)) {
+    if (libxl_device_nic_remove(ctx, domid, &nic, 0)) {
         fprintf(stderr, "libxl_device_nic_del failed.\n");
         return 1;
     }
@@ -4711,7 +4711,7 @@ int main_blockdetach(int argc, char **argv)
         fprintf(stderr, "Error: Device %s not connected.\n", argv[optind+1]);
         return 1;
     }
-    if (libxl_device_disk_remove(ctx, domid, &disk)) {
+    if (libxl_device_disk_remove(ctx, domid, &disk, 0)) {
         fprintf(stderr, "libxl_device_disk_remove failed.\n");
     }
     return 0;
Signature · 1 line
1.7.2.5
#40 lore raw
In reply to Ian Jackson
Quoted 5 lines · Ian Jackson

This series is the latest revision of my event handling API. It includes changes in response to a number of comments made. It also includes a few new consequential patches and unrelated fixes/improvements, as usual.

Quoted 7 lines

The most significant difference is the new "libxl__egc" type which is used instead of libxl__gc by event-generating functions. This prevents accidental violation of the callback reentrancy rules.

I'm still working on this series. At the moment I'm working on providing asynchronous calls for long-running operations; this requires more support machinery which is still not finalised.

Do we require that the async stuff is complete before we can commit the event stuff? Obviously the former makes heavy use of the later so I guess there will be "knock-back" effects as you implement it but the series is becoming quite unwieldy (and long lived) so maybe it'd be better to push part 1 and fix it up as necessary when you do the async stuff?

Quoted 10 lines

Of the resulting patches in this series,

These should perhaps go in soon:
01/10 libxl: make LIBXL_INIT_GC a statement, not an initialiser 03/10 libxl: move a lot more includes into libxl_internal.h 04/10 libxl: Provide more formal libxl__ctx_lock and _unlock 05/10 libxl: Fix leaks on context init failure
09/10 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

Quoted 2 lines

This one has now been tested and can go in IMO:
02/10 xenstore: New function xs_path_is_subpath

I agree that all the above can go in. I think I've acked them all.

Quoted 15 lines

These are the meat:
07/10 libxl: New API for providing OS events to libxl 08/10 libxl: New event generation API
10/10 libxl: Permit multithreaded event waiting

And this one has become rendered obsolete. If we are to retain the new libxl__egc structure, I will drop it. Otherwise it may still be needed, and it's disruptive, which is why I haven't dropped it from my series yet:
06/10 DROP: libxl: rename libxl__free_all

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#41 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH v6 00/10] libxl: event API"):

Quoted 6 lines

Do we require that the async stuff is complete before we can commit the event stuff? Obviously the former makes heavy use of the later so I guess there will be "knock-back" effects as you implement it but the series is becoming quite unwieldy (and long lived) so maybe it'd be better to push part 1 and fix it up as necessary when you do the async stuff?

That's probably a good idea. I'm refreshing it and testing it.

Quoted 11 lines
Quoted 9 lines

These should perhaps go in soon:
01/10 libxl: make LIBXL_INIT_GC a statement, not an initialiser 03/10 libxl: move a lot more includes into libxl_internal.h 04/10 libxl: Provide more formal libxl__ctx_lock and _unlock 05/10 libxl: Fix leaks on context init failure
09/10 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

This one has now been tested and can go in IMO:
02/10 xenstore: New function xs_path_is_subpath

I agree that all the above can go in. I think I've acked them all.

Right.

Ian.

#42 lore raw

Ian Jackson writes ("Re: [Xen-devel] [PATCH v6 00/10] libxl: event API"):

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines

These should perhaps go in soon:
01/10 libxl: make LIBXL_INIT_GC a statement, not an initialiser 03/10 libxl: move a lot more includes into libxl_internal.h 04/10 libxl: Provide more formal libxl__ctx_lock and _unlock 05/10 libxl: Fix leaks on context init failure
09/10 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec

This one has now been tested and can go in IMO:
02/10 xenstore: New function xs_path_is_subpath

I agree that all the above can go in. I think I've acked them all.

I have applied 01-05 to xen-unstable. 09 has not yet been executed (by me at least) so I think it should wait.

Thanks,
Ian.