[Libguestfs] [PATCH libnbd v2 3/3] api: Add nbd_clear_debug_callback.

Richard W.M. Jones rjones at redhat.com
Tue Aug 13 15:12:34 UTC 2019


Commit 0904dd113dfa36485623b3c1756dc1aeab3dddeb removed the
theoretical possibility of clearing the debug callback from the handle
by setting it to NULL (although that was dubious from an API point of
view).

This commit adds an explicit way to do this.  Another advantage of
this is we can internally call this API from other places when we want
to clear/free the debug callback.
---
 generator/generator | 11 +++++++++++
 lib/debug.c         | 17 ++++++++++++++---
 lib/handle.c        |  4 +---
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/generator/generator b/generator/generator
index 6ccfc5f..d76eeea 100755
--- a/generator/generator
+++ b/generator/generator
@@ -1013,6 +1013,17 @@ a handle then messages are printed on C<stderr>.
 
 The callback should not call C<nbd_*> APIs on the same handle since it can
 be called while holding the handle lock and will cause a deadlock.";
+};
+
+  "clear_debug_callback", {
+    default_call with
+    args = [];
+    ret = RErr;
+    shortdesc = "clear the debug callback";
+    longdesc = "\
+Remove the debug callback if one was previously associated
+with the handle (with C<nbd_set_debug_callback>).  If not
+callback was associated this does nothing.";
 };
 
   "set_handle_name", {
diff --git a/lib/debug.c b/lib/debug.c
index ad4d9cb..c1decb2 100644
--- a/lib/debug.c
+++ b/lib/debug.c
@@ -38,13 +38,24 @@ nbd_unlocked_get_debug (struct nbd_handle *h)
   return h->debug;
 }
 
+int
+nbd_unlocked_clear_debug_callback (struct nbd_handle *h)
+{
+  if (h->debug_callback)
+    /* ignore return value */
+    h->debug_callback (LIBNBD_CALLBACK_FREE, h->debug_data, NULL, NULL);
+  h->debug_callback = NULL;
+  h->debug_data = NULL;
+  return 0;
+}
+
 int
 nbd_unlocked_set_debug_callback (struct nbd_handle *h,
                                  nbd_debug_callback debug_callback, void *data)
 {
-  if (h->debug_callback)
-    /* ignore return value */
-    h->debug_callback (LIBNBD_CALLBACK_FREE, h->debug_data, NULL, NULL);
+  /* This can't fail at the moment - see implementation above. */
+  nbd_unlocked_clear_debug_callback (h);
+
   h->debug_callback = debug_callback;
   h->debug_data = data;
   return 0;
diff --git a/lib/handle.c b/lib/handle.c
index 054c8a7..d599eef 100644
--- a/lib/handle.c
+++ b/lib/handle.c
@@ -113,9 +113,7 @@ nbd_close (struct nbd_handle *h)
     return;
 
   /* Free user callbacks first. */
-  if (h->debug_callback)
-    h->debug_callback (LIBNBD_CALLBACK_FREE, h->debug_data, NULL, NULL);
-  h->debug_callback = NULL;
+  nbd_unlocked_clear_debug_callback (h);
 
   free (h->bs_entries);
   for (m = h->meta_contexts; m != NULL; m = m_next) {
-- 
2.22.0




More information about the Libguestfs mailing list