Mainloop: handle SIGINT as well (and terminate)
authorGuido Trotter <ultrotter@google.com>
Fri, 21 May 2010 10:24:54 +0000 (11:24 +0100)
committerGuido Trotter <ultrotter@google.com>
Sat, 22 May 2010 06:58:30 +0000 (07:58 +0100)
This is needed if daemons are in the foreground, and get ctrl+c-ed by
the user. Also add unittests to make sure the correct signals terminate
the mainloop.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/daemon.py
test/ganeti.daemon_unittest.py

index c843f0a..06cd70c 100644 (file)
@@ -168,6 +168,7 @@ class Mainloop(object):
 
   @utils.SignalHandled([signal.SIGCHLD])
   @utils.SignalHandled([signal.SIGTERM])
+  @utils.SignalHandled([signal.SIGINT])
   def Run(self, signal_handlers=None):
     """Runs the mainloop.
 
@@ -194,7 +195,7 @@ class Mainloop(object):
         handler = signal_handlers[sig]
         if handler.called:
           self._CallSignalWaiters(sig)
-          running = (sig != signal.SIGTERM)
+          running = sig not in (signal.SIGTERM, signal.SIGINT)
           handler.Clear()
 
   def _CallSignalWaiters(self, signum):
index ad5a128..81912ee 100755 (executable)
@@ -56,6 +56,16 @@ class TestMainloop(testutils.GanetiTestCase):
     self.mainloop.Run() # terminates by _SendSig being scheduled
     self.assertEquals(self.sendsig_events, [signal.SIGTERM])
 
+  def testTerminatingSignals(self):
+    self.mainloop.scheduler.enter(0.1, 1, self._SendSig, [signal.SIGCHLD])
+    self.mainloop.scheduler.enter(0.2, 1, self._SendSig, [signal.SIGINT])
+    self.mainloop.Run()
+    self.assertEquals(self.sendsig_events, [signal.SIGCHLD, signal.SIGINT])
+    self.mainloop.scheduler.enter(0.1, 1, self._SendSig, [signal.SIGTERM])
+    self.mainloop.Run()
+    self.assertEquals(self.sendsig_events, [signal.SIGCHLD, signal.SIGINT,
+                                            signal.SIGTERM])
+
   def testSchedulerCancel(self):
     handle = self.mainloop.scheduler.enter(0.1, 1, self._SendSig,
                                            [signal.SIGTERM])