diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-07-23 21:26:18 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-08-05 22:48:52 +0100 |
commit | 5816608eab20daec7937cd83b83a5ef90cd9a7f1 (patch) | |
tree | 683e80c27e41aa02ab7f1e535aeeab4aef3c2b7d /src | |
parent | 7024a8a34a98b779130663ebaab1867bec188c72 (diff) | |
download | libguestfs-5816608eab20daec7937cd83b83a5ef90cd9a7f1.tar.gz libguestfs-5816608eab20daec7937cd83b83a5ef90cd9a7f1.tar.xz libguestfs-5816608eab20daec7937cd83b83a5ef90cd9a7f1.zip |
lib: Initialize libvirt and libxml2 once when the library loads.
(cherry picked from commit 941ec968b8a1d6f891d30631a8ddb1e23e427b99)
Diffstat (limited to 'src')
-rw-r--r-- | src/guestfs.c | 36 | ||||
-rw-r--r-- | src/libvirtdomain.c | 11 |
2 files changed, 36 insertions, 11 deletions
diff --git a/src/guestfs.c b/src/guestfs.c index 46326e48..0bffd0bb 100644 --- a/src/guestfs.c +++ b/src/guestfs.c @@ -61,6 +61,15 @@ #include <arpa/inet.h> #include <netinet/in.h> +#ifdef HAVE_LIBVIRT +#include <libvirt/libvirt.h> +#endif + +#ifdef HAVE_LIBXML2 +#include <libxml/parser.h> +#include <libxml/xmlversion.h> +#endif + #include "c-ctype.h" #include "glthread/lock.h" #include "hash.h" @@ -79,6 +88,33 @@ gl_lock_define_initialized (static, handles_lock); static guestfs_h *handles = NULL; static int atexit_handler_set = 0; +gl_lock_define_initialized (static, init_lock); + +/* No initialization is required by libguestfs, but libvirt and + * libxml2 require initialization if they might be called from + * multiple threads. Hence this constructor function which is called + * when libguestfs is first loaded. + */ +static void init_libguestfs (void) __attribute__((constructor)); + +static void +init_libguestfs (void) +{ +#if defined(HAVE_LIBVIRT) || defined(HAVE_LIBXML2) + gl_lock_lock (init_lock); +#endif +#ifdef HAVE_LIBVIRT + virInitialize (); +#endif +#ifdef HAVE_LIBXML2 + xmlInitParser (); + LIBXML_TEST_VERSION; +#endif +#if defined(HAVE_LIBVIRT) || defined(HAVE_LIBXML2) + gl_lock_unlock (init_lock); +#endif +} + guestfs_h * guestfs_create (void) { diff --git a/src/libvirtdomain.c b/src/libvirtdomain.c index 6be45289..fe871ed6 100644 --- a/src/libvirtdomain.c +++ b/src/libvirtdomain.c @@ -44,17 +44,6 @@ #if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2) -static void init_libxml2 (void) __attribute__((constructor)); - -static void -init_libxml2 (void) -{ - /* I am told that you don't really need to call virInitialize ... */ - - xmlInitParser (); - LIBXML_TEST_VERSION; -} - static void ignore_errors (void *ignore, virErrorPtr ignore2) { |