/* 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;
}
+/************************
+ * 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
*
/* 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;
}
/* 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));
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 {
static SERVICE_STATUS service_status;
static SERVICE_STATUS_HANDLE service_status_handle;
-static HANDLE hShutdownEvent = NULL;
void WINAPI
service_ctrl_handler(DWORD Opcode)
case SERVICE_CONTROL_STOP:
logerror("Service stopping");
- SetEvent(hShutdownEvent);
+ empth_request_shutdown();
return;
case SERVICE_CONTROL_INTERROGATE:
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. */
finish_server();
}
-void
-service_stopped(void)
-{
- if (hShutdownEvent != NULL) {
- WaitForSingleObject(hShutdownEvent,INFINITE);
- }
-}
-
void
stop_service(void)
{