[Libguestfs] [PATCH threads v2 2/5] threads: Acquire and release the lock around each public guestfs_* API.

Richard W.M. Jones rjones at redhat.com
Tue Jun 16 16:02:21 UTC 2015


Acquire the per-handle lock on entering each public API function.

The lock is released by a cleanup handler, so we only need to use the
ACQUIRE_LOCK macro at the top of each function.  Although this looks a
bit odd, it makes it easy to write reliable code.

Note this means we require __attribute__((cleanup)).  On platforms
where this is not supported, the code will probably hang whenever a
libguestfs function is called.

The only definitive list of public APIs is found indirectly in the
generator (in generator/c.ml : globals).
---
 generator/c.ml                  |  4 ++++
 src/cleanup.c                   | 10 +++++++++-
 src/errors.c                    |  8 ++++++++
 src/events.c                    |  8 ++++++++
 src/guestfs-internal-frontend.h |  4 ++++
 src/guestfs-internal.h          |  8 ++++++++
 src/handle.c                    | 17 ++++++++++++++++-
 src/private-data.c              |  7 +++++++
 8 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/generator/c.ml b/generator/c.ml
index a2b9c94..d19e2b4 100644
--- a/generator/c.ml
+++ b/generator/c.ml
@@ -1565,6 +1565,7 @@ and generate_client_actions hash () =
         ~dll_public:true
         c_name style;
     pr "{\n";
+    pr "  ACQUIRE_LOCK (g);\n";
 
     handle_null_optargs optargs c_name;
 
@@ -1651,6 +1652,7 @@ and generate_client_actions hash () =
         c_name style;
 
     pr "{\n";
+    pr "  ACQUIRE_LOCK (g);\n";
 
     handle_null_optargs optargs c_name;
 
@@ -1998,6 +2000,7 @@ and generate_client_actions_variants () =
       ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
       c_name style;
     pr "{\n";
+    pr "  ACQUIRE_LOCK (g);\n";
     pr "  struct guestfs_%s_argv optargs_s;\n" c_name;
     pr "  struct guestfs_%s_argv *optargs = &optargs_s;\n" c_name;
     pr "  int i;\n";
@@ -2055,6 +2058,7 @@ and generate_client_actions_variants () =
       ~handle:"g" ~prefix:"guestfs_"
       name (ret, args, []);
     pr "{\n";
+    pr "  ACQUIRE_LOCK (g);\n";
     pr "  struct guestfs_%s_opts_argv optargs_s = { .bitmask = 0 };\n" name;
     pr "  struct guestfs_%s_opts_argv *optargs = &optargs_s;\n" name;
     pr "\n";
diff --git a/src/cleanup.c b/src/cleanup.c
index 71c26ec..3230563 100644
--- a/src/cleanup.c
+++ b/src/cleanup.c
@@ -1,5 +1,5 @@
 /* libguestfs
- * Copyright (C) 2013 Red Hat Inc.
+ * Copyright (C) 2013-2015 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -28,6 +28,7 @@
 #include <libxml/xpath.h>
 #include <libxml/xmlwriter.h>
 
+#include "glthread/lock.h"
 #include "hash.h"
 
 #include "guestfs.h"
@@ -140,3 +141,10 @@ guestfs_int_cleanup_pclose (void *ptr)
   if (f)
     pclose (f);
 }
+
+void
+guestfs_int_cleanup_gl_recursive_lock_unlock (void *ptr)
+{
+  gl_recursive_lock_t *lockp = * (gl_recursive_lock_t **) ptr;
+  gl_recursive_lock_unlock (*lockp);
+}
diff --git a/src/errors.c b/src/errors.c
index 2d3ae84..629fb7c 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -32,12 +32,14 @@
 const char *
 guestfs_last_error (guestfs_h *g)
 {
+  ACQUIRE_LOCK (g);
   return g->last_error;
 }
 
 int
 guestfs_last_errno (guestfs_h *g)
 {
+  ACQUIRE_LOCK (g);
   return g->last_errnum;
 }
 
@@ -164,12 +166,14 @@ guestfs_int_perrorf (guestfs_h *g, const char *fs, ...)
 void
 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
 {
+  ACQUIRE_LOCK (g);
   g->abort_cb = cb;
 }
 
 guestfs_abort_cb
 guestfs_get_out_of_memory_handler (guestfs_h *g)
 {
+  ACQUIRE_LOCK (g);
   return g->abort_cb;
 }
 
@@ -177,6 +181,7 @@ void
 guestfs_set_error_handler (guestfs_h *g,
                            guestfs_error_handler_cb cb, void *data)
 {
+  ACQUIRE_LOCK (g);
   g->error_cb = cb;
   g->error_cb_data = data;
 }
@@ -184,6 +189,7 @@ guestfs_set_error_handler (guestfs_h *g,
 guestfs_error_handler_cb
 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
 {
+  ACQUIRE_LOCK (g);
   if (data_rtn) *data_rtn = g->error_cb_data;
   return g->error_cb;
 }
@@ -192,6 +198,7 @@ void
 guestfs_push_error_handler (guestfs_h *g,
                             guestfs_error_handler_cb cb, void *data)
 {
+  ACQUIRE_LOCK (g);
   struct error_cb_stack *old_stack;
 
   old_stack = g->error_cb_stack;
@@ -206,6 +213,7 @@ guestfs_push_error_handler (guestfs_h *g,
 void
 guestfs_pop_error_handler (guestfs_h *g)
 {
+  ACQUIRE_LOCK (g);
   struct error_cb_stack *next_stack;
 
   if (g->error_cb_stack) {
diff --git a/src/events.c b/src/events.c
index 51b9948..ba16bad 100644
--- a/src/events.c
+++ b/src/events.c
@@ -39,6 +39,7 @@ guestfs_set_event_callback (guestfs_h *g,
                             int flags,
                             void *opaque)
 {
+  ACQUIRE_LOCK (g);
   int event_handle;
 
   if (flags != 0) {
@@ -73,6 +74,8 @@ guestfs_set_event_callback (guestfs_h *g,
 void
 guestfs_delete_event_callback (guestfs_h *g, int event_handle)
 {
+  ACQUIRE_LOCK (g);
+
   if (event_handle < 0 || event_handle >= (int) g->nr_events)
     return;
 
@@ -295,6 +298,7 @@ void
 guestfs_set_log_message_callback (guestfs_h *g,
                                   guestfs_log_message_cb cb, void *opaque)
 {
+  ACQUIRE_LOCK (g);
   replace_old_style_event_callback (g, log_message_callback_wrapper,
                                     GUESTFS_EVENT_APPLIANCE,
                                     opaque, cb);
@@ -317,6 +321,7 @@ void
 guestfs_set_subprocess_quit_callback (guestfs_h *g,
                                       guestfs_subprocess_quit_cb cb, void *opaque)
 {
+  ACQUIRE_LOCK (g);
   replace_old_style_event_callback (g, subprocess_quit_callback_wrapper,
                                     GUESTFS_EVENT_SUBPROCESS_QUIT,
                                     opaque, cb);
@@ -339,6 +344,7 @@ void
 guestfs_set_launch_done_callback (guestfs_h *g,
                                   guestfs_launch_done_cb cb, void *opaque)
 {
+  ACQUIRE_LOCK (g);
   replace_old_style_event_callback (g, launch_done_callback_wrapper,
                                     GUESTFS_EVENT_LAUNCH_DONE,
                                     opaque, cb);
@@ -361,6 +367,7 @@ void
 guestfs_set_close_callback (guestfs_h *g,
                             guestfs_close_cb cb, void *opaque)
 {
+  ACQUIRE_LOCK (g);
   replace_old_style_event_callback (g, close_callback_wrapper,
                                     GUESTFS_EVENT_CLOSE,
                                     opaque, cb);
@@ -384,6 +391,7 @@ void
 guestfs_set_progress_callback (guestfs_h *g,
                                guestfs_progress_cb cb, void *opaque)
 {
+  ACQUIRE_LOCK (g);
   replace_old_style_event_callback (g, progress_callback_wrapper,
                                     GUESTFS_EVENT_PROGRESS,
                                     opaque, cb);
diff --git a/src/guestfs-internal-frontend.h b/src/guestfs-internal-frontend.h
index 9322201..295ccbe 100644
--- a/src/guestfs-internal-frontend.h
+++ b/src/guestfs-internal-frontend.h
@@ -57,6 +57,8 @@
   __attribute__((cleanup(guestfs_int_cleanup_xmlXPathFreeObject)))
 #define CLEANUP_FCLOSE __attribute__((cleanup(guestfs_int_cleanup_fclose)))
 #define CLEANUP_PCLOSE __attribute__((cleanup(guestfs_int_cleanup_pclose)))
+#define CLEANUP_GL_RECURSIVE_LOCK_UNLOCK \
+  __attribute__((cleanup(guestfs_int_cleanup_gl_recursive_lock_unlock)))
 #else
 #define CLEANUP_FREE
 #define CLEANUP_FREE_STRING_LIST
@@ -70,6 +72,7 @@
 #define CLEANUP_XMLXPATHFREEOBJECT
 #define CLEANUP_FCLOSE
 #define CLEANUP_PCLOSE
+/* XXX no safe equivalent to CLEANUP_GL_RECURSIVE_LOCK_UNLOCK */
 #endif
 
 /* NB: At some point we will stop exporting these safe_* allocation
@@ -122,6 +125,7 @@ extern void guestfs_int_cleanup_xmlXPathFreeContext (void *ptr);
 extern void guestfs_int_cleanup_xmlXPathFreeObject (void *ptr);
 extern void guestfs_int_cleanup_fclose (void *ptr);
 extern void guestfs_int_cleanup_pclose (void *ptr);
+extern void guestfs_int_cleanup_gl_recursive_lock_unlock (void *ptr);
 
 /* These are in a separate header so the header can be generated.
  * Don't include the following file directly:
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index b68942f..be77459 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -58,6 +58,14 @@
 #define TRACE4(name, arg1, arg2, arg3, arg4)
 #endif
 
+/* Acquire and release the per-handle lock.  Note the release happens
+ * in an __attribute__((cleanup)) handler, making it simple to write
+ * bug-free code.
+ */
+#define ACQUIRE_LOCK(g) \
+  CLEANUP_GL_RECURSIVE_LOCK_UNLOCK gl_recursive_lock_t *_lock = &(g)->lock; \
+  gl_recursive_lock_lock (*_lock)
+
 /* Default and minimum appliance memory size. */
 
 /* Needs to be larger on ppc64 because of the larger page size (64K).
diff --git a/src/handle.c b/src/handle.c
index a057475..424b130 100644
--- a/src/handle.c
+++ b/src/handle.c
@@ -316,6 +316,7 @@ guestfs_close (guestfs_h *g)
 {
   struct hv_param *hp, *hp_next;
   guestfs_h **gg;
+  int r;
 
   if (g->state == NO_HANDLE) {
     /* Not safe to call ANY callbacks here, so ... */
@@ -392,7 +393,21 @@ guestfs_close (guestfs_h *g)
   free (g->backend_data);
   guestfs_int_free_string_list (g->backend_settings);
   free (g->append);
-  gl_recursive_lock_destroy (g->lock);
+  r = glthread_recursive_lock_destroy (&g->lock);
+  if (r != 0) {
+    /* If pthread_mutex_destroy returns 16 (EBUSY), this indicates
+     * that the lock is held somewhere.  That means a programming
+     * error if the main program is using threads.
+     */
+    errno = r;
+    perror ("guestfs_close: g->lock");
+    /* While we're debugging locks in libguestfs I want this to fail
+     * noisily.  Remove this later since there are valid times when
+     * this might fail such as if the program exits during a
+     * libguestfs operation.
+     */
+    abort ();
+  }
   free (g);
 }
 
diff --git a/src/private-data.c b/src/private-data.c
index 725b74b..c23dd86 100644
--- a/src/private-data.c
+++ b/src/private-data.c
@@ -66,6 +66,7 @@ freer (void *x)
 void
 guestfs_set_private (guestfs_h *g, const char *key, void *data)
 {
+  ACQUIRE_LOCK (g);
   struct pda_entry *new_entry, *old_entry, *entry;
 
   if (g->pda == NULL) {
@@ -90,6 +91,8 @@ guestfs_set_private (guestfs_h *g, const char *key, void *data)
 void *
 guestfs_get_private (guestfs_h *g, const char *key)
 {
+  ACQUIRE_LOCK (g);
+
   if (g->pda == NULL)
     return NULL;                /* no keys have been set */
 
@@ -105,6 +108,8 @@ guestfs_get_private (guestfs_h *g, const char *key)
 void *
 guestfs_first_private (guestfs_h *g, const char **key_rtn)
 {
+  ACQUIRE_LOCK (g);
+
   if (g->pda == NULL)
     return NULL;
 
@@ -124,6 +129,8 @@ guestfs_first_private (guestfs_h *g, const char **key_rtn)
 void *
 guestfs_next_private (guestfs_h *g, const char **key_rtn)
 {
+  ACQUIRE_LOCK (g);
+
   if (g->pda == NULL)
     return NULL;
 
-- 
2.3.1




More information about the Libguestfs mailing list