131bf1e
diff -rup libvirt-0.6.0.orig/qemud/event.c libvirt-0.6.0.new/qemud/event.c
131bf1e
--- libvirt-0.6.0.orig/qemud/event.c	2008-12-22 13:02:54.000000000 +0000
131bf1e
+++ libvirt-0.6.0.new/qemud/event.c	2009-02-06 19:29:28.000000000 +0000
131bf1e
@@ -68,6 +68,7 @@ struct virEventTimeout {
131bf1e
 /* State for the main event loop */
131bf1e
 struct virEventLoop {
131bf1e
     pthread_mutex_t lock;
131bf1e
+    int running;
131bf1e
     pthread_t leader;
131bf1e
     int wakeupfd[2];
131bf1e
     int handlesCount;
131bf1e
@@ -521,6 +522,7 @@ int virEventRunOnce(void) {
131bf1e
     int ret, timeout, nfds;
131bf1e
 
131bf1e
     virEventLock();
131bf1e
+    eventLoop.running = 1;
131bf1e
     eventLoop.leader = pthread_self();
131bf1e
     if ((nfds = virEventMakePollFDs(&fds)) < 0) {
131bf1e
         virEventUnlock();
131bf1e
@@ -572,7 +574,7 @@ int virEventRunOnce(void) {
131bf1e
         return -1;
131bf1e
     }
131bf1e
 
131bf1e
-    eventLoop.leader = 0;
131bf1e
+    eventLoop.running = 0;
131bf1e
     virEventUnlock();
131bf1e
     return 0;
131bf1e
 }
131bf1e
@@ -611,7 +613,9 @@ int virEventInit(void)
131bf1e
 static int virEventInterruptLocked(void)
131bf1e
 {
131bf1e
     char c = '\0';
131bf1e
-    if (pthread_self() == eventLoop.leader)
131bf1e
+
131bf1e
+    if (!eventLoop.running ||
131bf1e
+        pthread_self() == eventLoop.leader)
131bf1e
         return 0;
131bf1e
 
131bf1e
     if (safewrite(eventLoop.wakeupfd[1], &c, sizeof(c)) != sizeof(c))
131bf1e
diff -rup libvirt-0.6.0.orig/qemud/qemud.c libvirt-0.6.0.new/qemud/qemud.c
131bf1e
--- libvirt-0.6.0.orig/qemud/qemud.c	2009-01-31 09:04:17.000000000 +0000
131bf1e
+++ libvirt-0.6.0.new/qemud/qemud.c	2009-02-06 19:29:28.000000000 +0000
131bf1e
@@ -2013,11 +2013,15 @@ static int qemudOneLoop(void) {
131bf1e
     return 0;
131bf1e
 }
131bf1e
 
131bf1e
-static void qemudInactiveTimer(int timer ATTRIBUTE_UNUSED, void *data) {
131bf1e
+static void qemudInactiveTimer(int timerid, void *data) {
131bf1e
     struct qemud_server *server = (struct qemud_server *)data;
131bf1e
-    DEBUG0("Got inactive timer expiry");
131bf1e
-    if (!virStateActive()) {
131bf1e
-        DEBUG0("No state active, shutting down");
131bf1e
+
131bf1e
+    if (virStateActive() ||
131bf1e
+        server->clients) {
131bf1e
+        DEBUG0("Timer expired but still active, not shutting down");
131bf1e
+        virEventUpdateTimeoutImpl(timerid, -1);
131bf1e
+    } else {
131bf1e
+        DEBUG0("Timer expired and inactive, shutting down");
131bf1e
         server->shutdown = 1;
131bf1e
     }
131bf1e
 }
131bf1e
@@ -2048,9 +2052,18 @@ static void qemudFreeClient(struct qemud
131bf1e
 static int qemudRunLoop(struct qemud_server *server) {
131bf1e
     int timerid = -1;
131bf1e
     int ret = -1, i;
131bf1e
+    int timerActive = 0;
131bf1e
 
131bf1e
     virMutexLock(&server->lock);
131bf1e
 
131bf1e
+    if (timeout > 0 &&
131bf1e
+        (timerid = virEventAddTimeoutImpl(-1,
131bf1e
+                                          qemudInactiveTimer,
131bf1e
+                                          server, NULL)) < 0) {
131bf1e
+        VIR_ERROR0(_("Failed to register shutdown timeout"));
131bf1e
+        return -1;
131bf1e
+    }
131bf1e
+
131bf1e
     if (min_workers > max_workers)
131bf1e
         max_workers = min_workers;
131bf1e
 
131bf1e
@@ -2071,11 +2084,21 @@ static int qemudRunLoop(struct qemud_ser
131bf1e
          * if any drivers have active state, if not
131bf1e
          * shutdown after timeout seconds
131bf1e
          */
131bf1e
-        if (timeout > 0 && !virStateActive() && !server->clients) {
131bf1e
-            timerid = virEventAddTimeoutImpl(timeout*1000,
131bf1e
-                                             qemudInactiveTimer,
131bf1e
-                                             server, NULL);
131bf1e
-            DEBUG("Scheduling shutdown timer %d", timerid);
131bf1e
+        if (timeout > 0) {
131bf1e
+            if (timerActive) {
131bf1e
+                if (server->clients) {
131bf1e
+                    DEBUG("Deactivating shutdown timer %d", timerid);
131bf1e
+                    virEventUpdateTimeoutImpl(timerid, -1);
131bf1e
+                    timerActive = 0;
131bf1e
+                }
131bf1e
+            } else {
131bf1e
+                if (!virStateActive() &&
131bf1e
+                    !server->clients) {
131bf1e
+                    DEBUG("Activating shutdown timer %d", timerid);
131bf1e
+                    virEventUpdateTimeoutImpl(timerid, timeout * 1000);
131bf1e
+                    timerActive = 1;
131bf1e
+                }
131bf1e
+            }
131bf1e
         }
131bf1e
 
131bf1e
         virMutexUnlock(&server->lock);
131bf1e
@@ -2129,15 +2152,6 @@ static int qemudRunLoop(struct qemud_ser
131bf1e
             }
131bf1e
         }
131bf1e
 
131bf1e
-        /* Unregister any timeout that's active, since we
131bf1e
-         * just had an event processed
131bf1e
-         */
131bf1e
-        if (timerid != -1) {
131bf1e
-            DEBUG("Removing shutdown timer %d", timerid);
131bf1e
-            virEventRemoveTimeoutImpl(timerid);
131bf1e
-            timerid = -1;
131bf1e
-        }
131bf1e
-
131bf1e
         if (server->shutdown) {
131bf1e
             ret = 0;
131bf1e
             break;