Mark McLoughlin a6e23d
From e7be6cc841a5652b73ddd2ccd3769c7f8bbad13d Mon Sep 17 00:00:00 2001
Mark McLoughlin a6e23d
From: Daniel P. Berrange <berrange@redhat.com>
Mark McLoughlin a6e23d
Date: Tue, 12 May 2009 16:41:49 +0000
Mark McLoughlin a6e23d
Subject: [PATCH 1/2] Fix interrupting of main event thread & protect against accidental uniniitalized variables
Mark McLoughlin a6e23d
Mark McLoughlin a6e23d
---
Mark McLoughlin a6e23d
 qemud/event.c |   42 +++++++++++++++++++++++++++++++++++-------
Mark McLoughlin a6e23d
 1 files changed, 35 insertions(+), 7 deletions(-)
Mark McLoughlin a6e23d
Mark McLoughlin a6e23d
diff --git a/qemud/event.c b/qemud/event.c
Mark McLoughlin a6e23d
index 65f548e..754f2b1 100644
Mark McLoughlin a6e23d
--- a/qemud/event.c
Mark McLoughlin a6e23d
+++ b/qemud/event.c
Mark McLoughlin a6e23d
@@ -84,10 +84,10 @@ struct virEventLoop {
Mark McLoughlin a6e23d
 static struct virEventLoop eventLoop;
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
 /* Unique ID for the next FD watch to be registered */
Mark McLoughlin a6e23d
-static int nextWatch = 0;
Mark McLoughlin a6e23d
+static int nextWatch = 1;
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
 /* Unique ID for the next timer to be registered */
Mark McLoughlin a6e23d
-static int nextTimer = 0;
Mark McLoughlin a6e23d
+static int nextTimer = 1;
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
 static void virEventLock(void)
Mark McLoughlin a6e23d
 {
Mark McLoughlin a6e23d
@@ -143,15 +143,22 @@ int virEventAddHandleImpl(int fd, int events,
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
 void virEventUpdateHandleImpl(int watch, int events) {
Mark McLoughlin a6e23d
     int i;
Mark McLoughlin a6e23d
+    EVENT_DEBUG("Update handle w=%d e=%d", watch, events);
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
+    if (watch <= 0) {
Mark McLoughlin a6e23d
+        VIR_WARN("Ignoring invalid update watch %d", watch);
Mark McLoughlin a6e23d
+        return;
Mark McLoughlin a6e23d
+    }
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
     virEventLock();
Mark McLoughlin a6e23d
     for (i = 0 ; i < eventLoop.handlesCount ; i++) {
Mark McLoughlin a6e23d
         if (eventLoop.handles[i].watch == watch) {
Mark McLoughlin a6e23d
             eventLoop.handles[i].events =
Mark McLoughlin a6e23d
                     virEventHandleTypeToPollEvent(events);
Mark McLoughlin a6e23d
+            virEventInterruptLocked();
Mark McLoughlin a6e23d
             break;
Mark McLoughlin a6e23d
         }
Mark McLoughlin a6e23d
     }
Mark McLoughlin a6e23d
-    virEventInterruptLocked();
Mark McLoughlin a6e23d
     virEventUnlock();
Mark McLoughlin a6e23d
 }
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
@@ -164,6 +171,12 @@ void virEventUpdateHandleImpl(int watch, int events) {
Mark McLoughlin a6e23d
 int virEventRemoveHandleImpl(int watch) {
Mark McLoughlin a6e23d
     int i;
Mark McLoughlin a6e23d
     EVENT_DEBUG("Remove handle %d", watch);
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
+    if (watch <= 0) {
Mark McLoughlin a6e23d
+        VIR_WARN("Ignoring invalid remove watch %d", watch);
Mark McLoughlin a6e23d
+        return -1;
Mark McLoughlin a6e23d
+    }
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
     virEventLock();
Mark McLoughlin a6e23d
     for (i = 0 ; i < eventLoop.handlesCount ; i++) {
Mark McLoughlin a6e23d
         if (eventLoop.handles[i].deleted)
Mark McLoughlin a6e23d
@@ -172,11 +185,11 @@ int virEventRemoveHandleImpl(int watch) {
Mark McLoughlin a6e23d
         if (eventLoop.handles[i].watch == watch) {
Mark McLoughlin a6e23d
             EVENT_DEBUG("mark delete %d %d", i, eventLoop.handles[i].fd);
Mark McLoughlin a6e23d
             eventLoop.handles[i].deleted = 1;
Mark McLoughlin a6e23d
+            virEventInterruptLocked();
Mark McLoughlin a6e23d
             virEventUnlock();
Mark McLoughlin a6e23d
             return 0;
Mark McLoughlin a6e23d
         }
Mark McLoughlin a6e23d
     }
Mark McLoughlin a6e23d
-    virEventInterruptLocked();
Mark McLoughlin a6e23d
     virEventUnlock();
Mark McLoughlin a6e23d
     return -1;
Mark McLoughlin a6e23d
 }
Mark McLoughlin a6e23d
@@ -232,6 +245,12 @@ void virEventUpdateTimeoutImpl(int timer, int frequency) {
Mark McLoughlin a6e23d
     struct timeval tv;
Mark McLoughlin a6e23d
     int i;
Mark McLoughlin a6e23d
     EVENT_DEBUG("Updating timer %d timeout with %d ms freq", timer, frequency);
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
+    if (timer <= 0) {
Mark McLoughlin a6e23d
+        VIR_WARN("Ignoring invalid update timer %d", timer);
Mark McLoughlin a6e23d
+        return;
Mark McLoughlin a6e23d
+    }
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
     if (gettimeofday(&tv, NULL) < 0) {
Mark McLoughlin a6e23d
         return;
Mark McLoughlin a6e23d
     }
Mark McLoughlin a6e23d
@@ -244,10 +263,10 @@ void virEventUpdateTimeoutImpl(int timer, int frequency) {
Mark McLoughlin a6e23d
                 frequency >= 0 ? frequency +
Mark McLoughlin a6e23d
                 (((unsigned long long)tv.tv_sec)*1000) +
Mark McLoughlin a6e23d
                 (((unsigned long long)tv.tv_usec)/1000) : 0;
Mark McLoughlin a6e23d
+            virEventInterruptLocked();
Mark McLoughlin a6e23d
             break;
Mark McLoughlin a6e23d
         }
Mark McLoughlin a6e23d
     }
Mark McLoughlin a6e23d
-    virEventInterruptLocked();
Mark McLoughlin a6e23d
     virEventUnlock();
Mark McLoughlin a6e23d
 }
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
@@ -260,6 +279,12 @@ void virEventUpdateTimeoutImpl(int timer, int frequency) {
Mark McLoughlin a6e23d
 int virEventRemoveTimeoutImpl(int timer) {
Mark McLoughlin a6e23d
     int i;
Mark McLoughlin a6e23d
     EVENT_DEBUG("Remove timer %d", timer);
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
+    if (timer <= 0) {
Mark McLoughlin a6e23d
+        VIR_WARN("Ignoring invalid remove timer %d", timer);
Mark McLoughlin a6e23d
+        return -1;
Mark McLoughlin a6e23d
+    }
Mark McLoughlin a6e23d
+
Mark McLoughlin a6e23d
     virEventLock();
Mark McLoughlin a6e23d
     for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
Mark McLoughlin a6e23d
         if (eventLoop.timeouts[i].deleted)
Mark McLoughlin a6e23d
@@ -267,11 +292,11 @@ int virEventRemoveTimeoutImpl(int timer) {
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
         if (eventLoop.timeouts[i].timer == timer) {
Mark McLoughlin a6e23d
             eventLoop.timeouts[i].deleted = 1;
Mark McLoughlin a6e23d
+            virEventInterruptLocked();
Mark McLoughlin a6e23d
             virEventUnlock();
Mark McLoughlin a6e23d
             return 0;
Mark McLoughlin a6e23d
         }
Mark McLoughlin a6e23d
     }
Mark McLoughlin a6e23d
-    virEventInterruptLocked();
Mark McLoughlin a6e23d
     virEventUnlock();
Mark McLoughlin a6e23d
     return -1;
Mark McLoughlin a6e23d
 }
Mark McLoughlin a6e23d
@@ -617,9 +642,12 @@ static int virEventInterruptLocked(void)
Mark McLoughlin a6e23d
     char c = '\0';
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
     if (!eventLoop.running ||
Mark McLoughlin a6e23d
-        pthread_self() == eventLoop.leader)
Mark McLoughlin a6e23d
+        pthread_self() == eventLoop.leader) {
Mark McLoughlin a6e23d
+        VIR_DEBUG("Skip interrupt, %d %d", eventLoop.running, (int)eventLoop.leader);
Mark McLoughlin a6e23d
         return 0;
Mark McLoughlin a6e23d
+    }
Mark McLoughlin a6e23d
 
Mark McLoughlin a6e23d
+    VIR_DEBUG0("Interrupting");
Mark McLoughlin a6e23d
     if (safewrite(eventLoop.wakeupfd[1], &c, sizeof(c)) != sizeof(c))
Mark McLoughlin a6e23d
         return -1;
Mark McLoughlin a6e23d
     return 0;
Mark McLoughlin a6e23d
-- 
Mark McLoughlin a6e23d
1.6.0.6
Mark McLoughlin a6e23d