summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-07-23 21:26:18 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-07-23 21:26:18 +0100
commit941ec968b8a1d6f891d30631a8ddb1e23e427b99 (patch)
tree738bc815255cfa77ac443007761fe3ed5d870e84
parentbf00be0e17c03f46075b3276fade7bd23d380557 (diff)
downloadlibguestfs-941ec968b8a1d6f891d30631a8ddb1e23e427b99.tar.gz
libguestfs-941ec968b8a1d6f891d30631a8ddb1e23e427b99.tar.xz
libguestfs-941ec968b8a1d6f891d30631a8ddb1e23e427b99.zip
lib: Initialize libvirt and libxml2 once when the library loads.
-rw-r--r--examples/copy_over.c4
-rw-r--r--src/guestfs.c36
-rw-r--r--src/libvirtdomain.c11
3 files changed, 36 insertions, 15 deletions
diff --git a/examples/copy_over.c b/examples/copy_over.c
index 45b8fa6d..ec026168 100644
--- a/examples/copy_over.c
+++ b/examples/copy_over.c
@@ -21,7 +21,6 @@
#include <pthread.h>
#include <guestfs.h>
-#include <libvirt/libvirt.h>
struct threaddata {
const char *src;
@@ -66,9 +65,6 @@ main (int argc, char *argv[])
struct timeval start_t, end_t;
int64_t ms;
- /* This is required when using libvirt from multiple threads. */
- virInitialize ();
-
if (argc != 5) {
usage ();
exit (EXIT_FAILURE);
diff --git a/src/guestfs.c b/src/guestfs.c
index efdf254a..b5874a88 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"
@@ -80,6 +89,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)
{