(service_stoppped,stop_service,loc_Ctrl_C_Handler,
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.
This commit is contained in:
parent
d8938a1605
commit
9227a314c5
4 changed files with 71 additions and 30 deletions
|
@ -105,6 +105,7 @@ typedef struct {
|
||||||
typedef struct loc_Thread_t empth_t;
|
typedef struct loc_Thread_t empth_t;
|
||||||
typedef struct loc_Sem_t empth_sem_t;
|
typedef struct loc_Sem_t empth_sem_t;
|
||||||
|
|
||||||
|
void empth_request_shutdown(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int empth_init(char **ctx, int flags);
|
int empth_init(char **ctx, int flags);
|
||||||
|
|
|
@ -445,7 +445,7 @@ extern int command(void);
|
||||||
extern int recvclient(s_char *, int);
|
extern int recvclient(s_char *, int);
|
||||||
|
|
||||||
/* service.c */
|
/* service.c */
|
||||||
extern int service_stopped(void);
|
extern void stop_service(void);
|
||||||
/* more in service.h */
|
/* more in service.h */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -126,6 +126,10 @@ static struct {
|
||||||
/* We use this to lockstep when we are starting up threads. */
|
/* We use this to lockstep when we are starting up threads. */
|
||||||
HANDLE hThreadStartEvent;
|
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 */
|
/* The Thread Local Storage index. We store the pThread pointer */
|
||||||
/* for each thread at this index. */
|
/* for each thread at this index. */
|
||||||
DWORD dwTLSIndex;
|
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)
|
||||||
|
{
|
||||||
|
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
|
* empth_threadMain
|
||||||
*
|
*
|
||||||
|
@ -344,7 +400,7 @@ empth_init(char **ctx_ptr, int flags)
|
||||||
/* Initally unowned. */
|
/* Initally unowned. */
|
||||||
loc_GVAR.hThreadMutex = CreateMutex(NULL, FALSE, NULL);
|
loc_GVAR.hThreadMutex = CreateMutex(NULL, FALSE, NULL);
|
||||||
if (!loc_GVAR.hThreadMutex) {
|
if (!loc_GVAR.hThreadMutex) {
|
||||||
logerror("Failed to create mutex");
|
logerror("Failed to create mutex %d", GetLastError());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,10 +408,19 @@ empth_init(char **ctx_ptr, int flags)
|
||||||
/* Automatic state reset. */
|
/* Automatic state reset. */
|
||||||
loc_GVAR.hThreadStartEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
loc_GVAR.hThreadStartEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||||
if (!loc_GVAR.hThreadStartEvent) {
|
if (!loc_GVAR.hThreadStartEvent) {
|
||||||
logerror("Failed to create mutex");
|
logerror("Failed to create start event %d", GetLastError());
|
||||||
return 0;
|
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. */
|
/* Create the global Thread context. */
|
||||||
pThread = (loc_Thread_t *)malloc(sizeof(*pThread));
|
pThread = (loc_Thread_t *)malloc(sizeof(*pThread));
|
||||||
if (!pThread) {
|
if (!pThread) {
|
||||||
|
@ -469,17 +534,7 @@ empth_exit(void)
|
||||||
loc_debug("empth_exit");
|
loc_debug("empth_exit");
|
||||||
|
|
||||||
if (pThread->bMainThread) {
|
if (pThread->bMainThread) {
|
||||||
if (daemonize)
|
loc_BlockMainThread();
|
||||||
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_RunThisThread();
|
loc_RunThisThread();
|
||||||
shutdwn(0);
|
shutdwn(0);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -149,7 +149,6 @@ remove_service(char *service_name)
|
||||||
|
|
||||||
static SERVICE_STATUS service_status;
|
static SERVICE_STATUS service_status;
|
||||||
static SERVICE_STATUS_HANDLE service_status_handle;
|
static SERVICE_STATUS_HANDLE service_status_handle;
|
||||||
static HANDLE hShutdownEvent = NULL;
|
|
||||||
|
|
||||||
void WINAPI
|
void WINAPI
|
||||||
service_ctrl_handler(DWORD Opcode)
|
service_ctrl_handler(DWORD Opcode)
|
||||||
|
@ -168,7 +167,7 @@ service_ctrl_handler(DWORD Opcode)
|
||||||
|
|
||||||
case SERVICE_CONTROL_STOP:
|
case SERVICE_CONTROL_STOP:
|
||||||
logerror("Service stopping");
|
logerror("Service stopping");
|
||||||
SetEvent(hShutdownEvent);
|
empth_request_shutdown();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case SERVICE_CONTROL_INTERROGATE:
|
case SERVICE_CONTROL_INTERROGATE:
|
||||||
|
@ -206,12 +205,6 @@ service_main(DWORD argc, LPTSTR *argv)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
|
|
||||||
logerror("CreateEvent for Shutdown failed %d\n", GetLastError());
|
|
||||||
finish_server();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
start_server(0);
|
start_server(0);
|
||||||
|
|
||||||
/* Initialization complete - report running status. */
|
/* Initialization complete - report running status. */
|
||||||
|
@ -229,14 +222,6 @@ service_main(DWORD argc, LPTSTR *argv)
|
||||||
finish_server();
|
finish_server();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
service_stopped(void)
|
|
||||||
{
|
|
||||||
if (hShutdownEvent != NULL) {
|
|
||||||
WaitForSingleObject(hShutdownEvent,INFINITE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
stop_service(void)
|
stop_service(void)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue