]> git.pond.sub.org Git - empserver/commitdiff
(service_stoppped,stop_service,loc_Ctrl_C_Handler,
authorRon Koenderink <rkoenderink@yahoo.ca>
Tue, 15 Mar 2005 18:19:07 +0000 (18:19 +0000)
committerRon Koenderink <rkoenderink@yahoo.ca>
Tue, 15 Mar 2005 18:19:07 +0000 (18:19 +0000)
empth_request_shutdown,loc_BlockMainThread,empth_init,
service_ctrl_handler) [_WIN32]: Remove the Windows Build UI and
replace with Ctrl-c signal trap.  The shutdown event that was used for
STOPPING the service is now used for both foreground and background
(service) modes of operation. Also move the shutdown event from service
code to empth_t library.

include/empthread.h
include/prototypes.h
src/lib/empthread/ntthread.c
src/lib/gen/service.c

index a59acdb1900f4fdc4b421e29719fdfd54ab1e93c..800f220859bbd7bfdd2d9f8269f3da25d59d9254 100644 (file)
@@ -105,6 +105,7 @@ typedef struct {
 typedef struct loc_Thread_t empth_t;
 typedef struct loc_Sem_t empth_sem_t;
 
+void empth_request_shutdown(void);
 #endif
 
 int empth_init(char **ctx, int flags);
index a3d1b848b52dfdc34ada48726b0cdad06e108094..6940f1ab052d69d597b185cc4c233b15aa1dbb38 100644 (file)
@@ -445,7 +445,7 @@ extern int command(void);
 extern int recvclient(s_char *, int);
 
 /* service.c */
-extern int service_stopped(void);
+extern void stop_service(void);
 /* more in service.h */
 
 /*
index cb34d6db1ed8d68c518ca9dcafe8721baffbc6ba..41af885205c3b61257045d082fbaa7823ad304d2 100644 (file)
@@ -126,6 +126,10 @@ static struct {
     /* We use this to lockstep when we are starting up threads. */
     HANDLE hThreadStartEvent;
 
+    /* This is an event used to wakeup the main thread */
+    /* to start the shutdown sequence. */
+    HANDLE hShutdownEvent;
+
     /* The Thread Local Storage index.  We store the pThread pointer */
     /* for each thread at this index. */
     DWORD dwTLSIndex;
@@ -281,6 +285,58 @@ loc_SleepThisThread(unsigned long ulMillisecs)
 }
 
 
+/************************
+ * loc_Ctrl_C_Handler
+ *
+ * Ctrl-C will initiate a shutdown. This is done by calling
+ * empth_request_shutdown()
+ */
+static BOOL
+loc_Ctrl_C_Handler(DWORD fdwCtrlType)
+{
+    switch (fdwCtrlType) 
+    { 
+        // Handle the CTRL+C signal. 
+        case CTRL_C_EVENT:
+           empth_request_shutdown();
+            return TRUE; 
+        case CTRL_CLOSE_EVENT:
+        case CTRL_BREAK_EVENT: 
+        case CTRL_LOGOFF_EVENT: 
+        case CTRL_SHUTDOWN_EVENT: 
+        default: 
+            return FALSE; 
+    } 
+}
+
+/************************
+ * empth_request_shutdown
+ *
+ * This wakes up the main thread so shutdown can proceed.
+ * This is done by signalling hShutdownEvent.
+ */
+void
+empth_request_shutdown(void)\r
+{
+    SetEvent(loc_GVAR.hShutdownEvent);
+}
+
+/************************
+ * loc_BlockMainThread
+ *
+ * This blocks up the main thread.
+ * loc_WakeupMainThread() is used
+ * wakeup the main so shutdown can
+ * proceed.
+ */
+static void
+loc_BlockMainThread(void)
+{
+    /* Get the MUTEX semaphore, wait the number of MS */
+    WaitForSingleObject(loc_GVAR.hShutdownEvent, INFINITE);
+}
+
 /************************
  * empth_threadMain
  *
@@ -344,7 +400,7 @@ empth_init(char **ctx_ptr, int flags)
     /* Initally unowned. */
     loc_GVAR.hThreadMutex = CreateMutex(NULL, FALSE, NULL);
     if (!loc_GVAR.hThreadMutex) {
-       logerror("Failed to create mutex");
+       logerror("Failed to create mutex %d", GetLastError());
        return 0;
     }
 
@@ -352,9 +408,18 @@ empth_init(char **ctx_ptr, int flags)
     /* Automatic state reset. */
     loc_GVAR.hThreadStartEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
     if (!loc_GVAR.hThreadStartEvent) {
-       logerror("Failed to create mutex");
+       logerror("Failed to create start event %d", GetLastError());
+       return 0;
+    }
+
+    /* Create the shutdown event for the main thread. */
+    /* Manual reset */
+    loc_GVAR.hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+    if (!loc_GVAR.hShutdownEvent) {
+        logerror("Failed to create shutdown event %d", GetLastError());
        return 0;
     }
+    SetConsoleCtrlHandler((PHANDLER_ROUTINE)loc_Ctrl_C_Handler, TRUE);
 
     /* Create the global Thread context. */
     pThread = (loc_Thread_t *)malloc(sizeof(*pThread));
@@ -469,17 +534,7 @@ empth_exit(void)
     loc_debug("empth_exit");
 
     if (pThread->bMainThread) {
-       if (daemonize)
-           service_stopped(); /* Wait until the service is stopped */
-       else {
-           while (1) { /* server UI - Wait forever. */
-               char buf[20];
-               printf("\nEmpire Server>");
-               fgets(buf, sizeof(buf), stdin);
-               if (!strnicmp(buf, "quit", 4))
-                   break;
-           }
-       }
+       loc_BlockMainThread();
        loc_RunThisThread();
        shutdwn(0);
     } else {
index a33dd41da83b1c0b29594b01f66dc425aaae03fe..a9652c820822a814962050b66a6659af744147d8 100644 (file)
@@ -149,7 +149,6 @@ remove_service(char *service_name)
 
 static SERVICE_STATUS          service_status; 
 static SERVICE_STATUS_HANDLE   service_status_handle;
-static HANDLE                  hShutdownEvent = NULL;
 
 void WINAPI
 service_ctrl_handler(DWORD Opcode) 
@@ -168,7 +167,7 @@ service_ctrl_handler(DWORD Opcode)
  
         case SERVICE_CONTROL_STOP:
            logerror("Service stopping");
-           SetEvent(hShutdownEvent);
+           empth_request_shutdown();
             return; 
  
         case SERVICE_CONTROL_INTERROGATE: 
@@ -206,12 +205,6 @@ service_main(DWORD argc, LPTSTR *argv)
         return;
     }
  
-    if ((hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
-        logerror("CreateEvent for Shutdown failed %d\n", GetLastError());
-       finish_server();
-       return;
-    }
-
     start_server(0);
  
     /* Initialization complete - report running status. */
@@ -229,14 +222,6 @@ service_main(DWORD argc, LPTSTR *argv)
     finish_server();
 }
 
-void
-service_stopped(void)
-{
-    if (hShutdownEvent != NULL) {
-       WaitForSingleObject(hShutdownEvent,INFINITE);
-    }
-}
-
 void
 stop_service(void)
 {