[libvirt] [snmp PATCH 2/2] Stop event thread on server stop

Martin Kletzander mkletzan at redhat.com
Wed May 30 13:29:42 UTC 2012


When C-c is sent to the process, the main thread (server) receives it
and sets keep_running to 0, but the second thread is still
running. Also, the second thread is not cleaned up after
pthread_join() or pthread_destroy().
This patch adds the cleanup function before all exit points of the
program and fixes the cleansing of the second thread.
---
 src/libvirtMib_subagent.c |    4 +++-
 src/libvirtSnmp.c         |   20 +++++++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/libvirtMib_subagent.c b/src/libvirtMib_subagent.c
index 7c038fc..53be07a 100644
--- a/src/libvirtMib_subagent.c
+++ b/src/libvirtMib_subagent.c
@@ -80,6 +80,8 @@ main (int argc, char **argv) {
       init_snmp("libvirtGuestTable");
       fprintf(stderr, "Configuration directives understood:\n");
       read_config_print_usage("  ");
+      snmp_shutdown("libvirtGuestTable");
+      shutdown_libvirtGuestTable();
       exit(0);
     case 'M':
       agentx_subagent = 0;
@@ -176,7 +178,7 @@ main (int argc, char **argv) {

   /* at shutdown time */
   snmp_shutdown("libvirtGuestTable");
+  shutdown_libvirtGuestTable();
   SOCK_CLEANUP;
   exit(0);
 }
-
diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c
index 7e56737..a9fa185 100644
--- a/src/libvirtSnmp.c
+++ b/src/libvirtSnmp.c
@@ -296,7 +296,6 @@ libvirtUnregisterEvents(virConnectPtr conn)
 {
     void *status;

-    pthread_join(poll_thread, &status);
     virConnectDomainEventDeregisterAny(conn, callbackRet);
     callbackRet = -1;
     return 0;
@@ -344,8 +343,27 @@ int libvirtSnmpInit(void)
     return 0;
 }

+static void
+libvirtDeinitTimer(int timer ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED)
+{
+    /* nothing to be done here */
+}
+
 void libvirtSnmpShutdown(void)
 {
+    int timer;
+
+    /* in case server is being stopped, run is still 1, so let's
+     * shutdown the thread in a clean way */
+    run = 0;
+
+    /* HACK: Add a dummy timeout to break event loop */
+    timer = virEventAddTimeout(0, libvirtDeinitTimer, NULL, NULL);
+    pthread_join(poll_thread, NULL);
+
+    if (timer != -1)
+        virEventRemoveTimeout(timer);
+
     if (libvirtUnregisterEvents(conn)) {
         printf("Failed to unregister domain events\n");
     }
-- 
1.7.8.6




More information about the libvir-list mailing list