Command-line signal handling for Windows
authorConstantin Wenger <constantin.wenger@googlemail.com>
Sun, 6 Nov 2011 14:17:18 +0000 (15:17 +0100)
committerPerttu Ahola <celeron55@gmail.com>
Sun, 6 Nov 2011 17:24:44 +0000 (19:24 +0200)
src/porting.cpp

index f349c0ce5e8c877660b7beab497145b57eee76a5..f3a0ab07443fd726fd0df36a4727cacf04760b02 100644 (file)
@@ -75,10 +75,45 @@ void signal_handler_init(void)
 }
 
 #else // _WIN32
+       #include <signal.h>
+       #include <windows.h>
+       
+       BOOL WINAPI event_handler(DWORD sig)
+       {
+               switch(sig)
+               {
+               case CTRL_C_EVENT:
+               case CTRL_CLOSE_EVENT:
+               case CTRL_LOGOFF_EVENT:
+               case CTRL_SHUTDOWN_EVENT:
+
+                       if(g_killed == false)
+                       {
+                               dstream<<DTIME<<"INFO: event_handler(): "
+                                               <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
+                               dstream<<DTIME<<"INFO: event_handler(): "
+                                               <<"Printing debug stacks"<<std::endl;
+                               debug_stacks_print();
+
+                               g_killed = true;
+                       }
+                       else
+                       {
+                               (void)signal(SIGINT, SIG_DFL);
+                       }
 
+                       break;
+               case CTRL_BREAK_EVENT:
+                       break;
+               }
+               
+               return TRUE;
+       }
+       
 void signal_handler_init(void)
 {
-       // No-op
+       dstream<<"signal_handler_init()"<<std::endl;
+       SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
 }
 
 #endif