dll: FspService: console mode improvements

This commit is contained in:
Bill Zissimopoulos 2016-05-08 17:46:09 -07:00
parent b77a749f93
commit 6da81be792

View File

@ -236,6 +236,8 @@ FSP_API NTSTATUS FspServiceLoop(FSP_SERVICE *Service)
if (!StartServiceCtrlDispatcherW(ServiceTable)) if (!StartServiceCtrlDispatcherW(ServiceTable))
{ {
HANDLE Thread; HANDLE Thread;
PWSTR *Argv;
DWORD Argc;
DWORD WaitResult; DWORD WaitResult;
DWORD LastError; DWORD LastError;
@ -246,8 +248,9 @@ FSP_API NTSTATUS FspServiceLoop(FSP_SERVICE *Service)
goto exit; goto exit;
} }
/* enter console mode! */ /* ENTER CONSOLE MODE! */
/* create/reset the console mode event */
if (0 == FspServiceConsoleModeEvent) if (0 == FspServiceConsoleModeEvent)
{ {
FspServiceConsoleModeEvent = CreateEventW(0, TRUE, FALSE, 0); FspServiceConsoleModeEvent = CreateEventW(0, TRUE, FALSE, 0);
@ -260,13 +263,25 @@ FSP_API NTSTATUS FspServiceLoop(FSP_SERVICE *Service)
else else
ResetEvent(FspServiceConsoleModeEvent); ResetEvent(FspServiceConsoleModeEvent);
/* create a thread to mimic what StartServiceCtrlDispatcherW does */ /* prepare the command line arguments */
Thread = CreateThread(0, 0, FspServiceConsoleModeThread, Service, 0, 0); Argv = CommandLineToArgvW(GetCommandLineW(), &Argc);
if (0 == Thread) if (0 == Argv)
{ {
Result = FspNtStatusFromWin32(GetLastError()); Result = FspNtStatusFromWin32(GetLastError());
goto exit; goto exit;
} }
Argv[0] = Service->ServiceName;
/* create a thread to mimic what StartServiceCtrlDispatcherW does; it takes ownership of Argv */
Thread = CreateThread(0, 0, FspServiceConsoleModeThread, Argv, 0, 0);
if (0 == Thread)
{
LocalFree(Argv);
Result = FspNtStatusFromWin32(GetLastError());
goto exit;
}
/* wait for the console mode startup thread to terminate */
WaitResult = WaitForSingleObject(Thread, INFINITE); WaitResult = WaitForSingleObject(Thread, INFINITE);
CloseHandle(Thread); CloseHandle(Thread);
if (WAIT_OBJECT_0 != WaitResult) if (WAIT_OBJECT_0 != WaitResult)
@ -275,12 +290,14 @@ FSP_API NTSTATUS FspServiceLoop(FSP_SERVICE *Service)
goto exit; goto exit;
} }
/* set up our console control handler */
if (!SetConsoleCtrlHandler(FspServiceConsoleCtrlHandler, TRUE)) if (!SetConsoleCtrlHandler(FspServiceConsoleCtrlHandler, TRUE))
{ {
Result = FspNtStatusFromWin32(GetLastError()); Result = FspNtStatusFromWin32(GetLastError());
goto exit; goto exit;
} }
/* wait until we get signaled by the console control handler */
WaitResult = WaitForSingleObject(FspServiceConsoleModeEvent, INFINITE); WaitResult = WaitForSingleObject(FspServiceConsoleModeEvent, INFINITE);
SetConsoleCtrlHandler(FspServiceConsoleCtrlHandler, FALSE); SetConsoleCtrlHandler(FspServiceConsoleCtrlHandler, FALSE);
if (WAIT_OBJECT_0 != WaitResult) if (WAIT_OBJECT_0 != WaitResult)
@ -445,30 +462,20 @@ static DWORD WINAPI FspServiceCtrlHandler(
static DWORD WINAPI FspServiceConsoleModeThread(PVOID Context) static DWORD WINAPI FspServiceConsoleModeThread(PVOID Context)
{ {
FSP_SERVICE *Service; FSP_SERVICE *Service;
PWSTR Args[2] = { 0, 0 }; PWSTR *Argv = Context;
PWSTR *Argv;
DWORD Argc; DWORD Argc;
for (Argc = 0; 0 != *Argv; Argc++)
;
Service = FspServiceFromTable(); Service = FspServiceFromTable();
if (0 == Service) if (0 == Service)
{
FspServiceLog(EVENTLOG_ERROR_TYPE, FspServiceLog(EVENTLOG_ERROR_TYPE,
L"" __FUNCTION__ ": internal error: FspServiceFromTable = 0"); L"" __FUNCTION__ ": internal error: FspServiceFromTable = 0");
return FALSE; else
} FspServiceMain(Service, Argc, Argv);
Argv = CommandLineToArgvW(GetCommandLineW(), &Argc); LocalFree(Argv);
if (0 == Argv)
{
Argv = Args;
Argc = 1;
}
Argv[0] = Service->ServiceName;
FspServiceMain(Service, Argc, Argv);
if (Args != Argv)
LocalFree(Argv);
return 0; return 0;
} }