[Libguestfs] [PATCH 4/7] close: Rearrange the order in which the handle is closed and freed.

Richard W.M. Jones rjones at redhat.com
Tue Jul 3 18:03:20 UTC 2012


From: "Richard W.M. Jones" <rjones at redhat.com>

The order is now:

 - remove the handle from the list of handles
 - send close trace message
 - sync and shutdown qemu
 - run user close callback
 - free temporary directory
 - free memory

This commit ought to be no functional change.
---
 src/guestfs.c |   64 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 31 insertions(+), 33 deletions(-)

diff --git a/src/guestfs.c b/src/guestfs.c
index 32bcbef..16d11da 100644
--- a/src/guestfs.c
+++ b/src/guestfs.c
@@ -178,6 +178,19 @@ guestfs_close (guestfs_h *g)
     return;
   }
 
+  /* Remove the handle from the handles list. */
+  gl_lock_lock (handles_lock);
+  if (handles == g)
+    handles = g->next;
+  else {
+    guestfs_h *gg;
+
+    for (gg = handles; gg->next != g; gg = gg->next)
+      ;
+    gg->next = g->next;
+  }
+  gl_lock_unlock (handles_lock);
+
   if (g->trace) {
     const char trace_msg[] = "close";
 
@@ -203,19 +216,6 @@ guestfs_close (guestfs_h *g)
     ignore_value (guestfs_kill_subprocess (g));
 #endif
 
-  /* Run user close callbacks. */
-  guestfs___call_callbacks_void (g, GUESTFS_EVENT_CLOSE);
-
-  /* Remove all other registered callbacks.  Since we've already
-   * called the close callbacks, we shouldn't call any others.
-   */
-  free (g->events);
-  g->nr_events = 0;
-  g->events = NULL;
-
-  guestfs___free_inspect_info (g);
-  guestfs___free_drives (&g->drives);
-
   /* Close sockets. */
   if (g->fd[0] >= 0)
     close (g->fd[0]);
@@ -231,9 +231,25 @@ guestfs_close (guestfs_h *g)
   if (g->pid > 0) waitpid (g->pid, NULL, 0);
   if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
 
+  /* Run user close callbacks. */
+  guestfs___call_callbacks_void (g, GUESTFS_EVENT_CLOSE);
+
   /* Remove whole temporary directory. */
   guestfs___remove_tmpdir (g->tmpdir);
-  free (g->tmpdir);
+
+  /* Mark the handle as dead and then free up all memory. */
+  g->state = NO_HANDLE;
+
+  free (g->events);
+  g->nr_events = 0;
+  g->events = NULL;
+
+#if HAVE_FUSE
+  guestfs___free_fuse (g);
+#endif
+
+  guestfs___free_inspect_info (g);
+  guestfs___free_drives (&g->drives);
 
   if (g->cmdline) {
     size_t i;
@@ -243,27 +259,9 @@ guestfs_close (guestfs_h *g)
     free (g->cmdline);
   }
 
-  /* Mark the handle as dead before freeing it. */
-  g->state = NO_HANDLE;
-
-  gl_lock_lock (handles_lock);
-  if (handles == g)
-    handles = g->next;
-  else {
-    guestfs_h *gg;
-
-    for (gg = handles; gg->next != g; gg = gg->next)
-      ;
-    gg->next = g->next;
-  }
-  gl_lock_unlock (handles_lock);
-
-#if HAVE_FUSE
-  guestfs___free_fuse (g);
-#endif
-
   if (g->pda)
     hash_free (g->pda);
+  free (g->tmpdir);
   free (g->last_error);
   free (g->path);
   free (g->qemu);
-- 
1.7.10.4




More information about the Libguestfs mailing list