summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard Jones <rjones@trick.home.annexia.org>2009-07-10 11:41:43 +0100
committerRichard Jones <rjones@trick.home.annexia.org>2009-07-10 11:41:43 +0100
commit03e1f74ee08dc71bc09cc7655bf4732685f80b43 (patch)
tree29fbe8075da84949d6f8a98e39679eddee248b05 /src
parent67a3d3e3915a18a58702b7e68f514d9dc9a82a2e (diff)
downloadlibguestfs-03e1f74ee08dc71bc09cc7655bf4732685f80b43.tar.gz
libguestfs-03e1f74ee08dc71bc09cc7655bf4732685f80b43.tar.xz
libguestfs-03e1f74ee08dc71bc09cc7655bf4732685f80b43.zip
Properly close fds and unregister handlers in guestfs_close.
This caused a segfault if you tried to repeatedly open and close a guestfs handle in the same program. The reason is that the old handler remained registered (not always - it was racey). This adds proper cleanup to the guestfs_close path, also for file descriptors.
Diffstat (limited to 'src')
-rw-r--r--src/guestfs.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/guestfs.c b/src/guestfs.c
index 66ab12af..7ab72006 100644
--- a/src/guestfs.c
+++ b/src/guestfs.c
@@ -327,6 +327,25 @@ guestfs_close (guestfs_h *g)
if (g->state != CONFIG)
guestfs_kill_subprocess (g);
+ /* Close any sockets and deregister any handlers. */
+ if (g->stdout_watch >= 0)
+ g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
+ if (g->sock_watch >= 0)
+ g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
+ g->stdout_watch = -1;
+ g->sock_watch = -1;
+
+ if (g->fd[0] >= 0)
+ close (g->fd[0]);
+ if (g->fd[1] >= 0)
+ close (g->fd[1]);
+ if (g->sock >= 0)
+ close (g->sock);
+ g->fd[0] = -1;
+ g->fd[1] = -1;
+ g->sock = -1;
+
+ /* Remove tmpfiles. */
if (g->tmpdir) {
snprintf (filename, sizeof filename, "%s/sock", g->tmpdir);
unlink (filename);