2017-10-31 09:20:37 +00:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <tchar.h>
|
2017-10-31 11:09:57 +00:00
|
|
|
#include <common\debug.h>
|
2017-10-31 09:20:37 +00:00
|
|
|
|
2017-10-31 11:09:57 +00:00
|
|
|
#include "ivshmem.h"
|
2017-10-31 09:20:37 +00:00
|
|
|
|
2017-10-31 11:09:57 +00:00
|
|
|
#define SERVICE_NAME "kvm-ivshmem-host"
|
2017-10-31 09:20:37 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
struct App
|
|
|
|
{
|
|
|
|
SERVICE_STATUS serviceStatus;
|
|
|
|
SERVICE_STATUS_HANDLE statusHandle;
|
|
|
|
HANDLE serviceStopEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct App app =
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
{0},
|
|
|
|
NULL,
|
|
|
|
INVALID_HANDLE_VALUE
|
2017-10-31 09:20:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
|
|
|
|
VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode);
|
|
|
|
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int main(int argc, TCHAR *argv[])
|
|
|
|
{
|
|
|
|
SERVICE_TABLE_ENTRY ServiceTable[] =
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
{_T(SERVICE_NAME), (LPSERVICE_MAIN_FUNCTION)ServiceMain},
|
2017-10-31 09:20:37 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
|
|
|
|
{
|
|
|
|
DWORD lastError = GetLastError();
|
|
|
|
if (lastError == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
|
|
|
|
{
|
|
|
|
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
|
|
|
|
WaitForSingleObject(hThread, INFINITE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
app.statusHandle = RegisterServiceCtrlHandler(_T(SERVICE_NAME), ServiceCtrlHandler);
|
2017-10-31 09:20:37 +00:00
|
|
|
if (!app.statusHandle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ZeroMemory(&app.serviceStatus, sizeof(app.serviceStatus));
|
|
|
|
app.serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
app.serviceStatus.dwControlsAccepted = 0;
|
|
|
|
app.serviceStatus.dwCurrentState = SERVICE_START_PENDING;
|
|
|
|
app.serviceStatus.dwWin32ExitCode = 0;
|
|
|
|
app.serviceStatus.dwServiceSpecificExitCode = 0;
|
|
|
|
app.serviceStatus.dwCheckPoint = 0;
|
|
|
|
|
|
|
|
if (SetServiceStatus(app.statusHandle, &app.serviceStatus) == FALSE)
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
DEBUG_ERROR("SetServiceStatus failed");
|
2017-10-31 09:20:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.serviceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (app.serviceStopEvent == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
app.serviceStatus.dwControlsAccepted = 0;
|
|
|
|
app.serviceStatus.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
app.serviceStatus.dwWin32ExitCode = GetLastError();
|
|
|
|
app.serviceStatus.dwCheckPoint = 1;
|
|
|
|
if (SetServiceStatus(app.statusHandle, &app.serviceStatus) == FALSE)
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
DEBUG_ERROR("SetServiceStatus failed");
|
2017-10-31 09:20:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|
|
|
app.serviceStatus.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
app.serviceStatus.dwWin32ExitCode = 0;
|
|
|
|
app.serviceStatus.dwCheckPoint = 0;
|
|
|
|
if (SetServiceStatus(app.statusHandle, &app.serviceStatus) == FALSE)
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
DEBUG_ERROR("SetServiceStatus failed");
|
2017-10-31 09:20:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
|
|
|
|
WaitForSingleObject(hThread, INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(app.serviceStopEvent);
|
|
|
|
app.serviceStatus.dwControlsAccepted = 0;
|
|
|
|
app.serviceStatus.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
app.serviceStatus.dwWin32ExitCode = 0;
|
|
|
|
app.serviceStatus.dwCheckPoint = 3;
|
|
|
|
|
|
|
|
if (SetServiceStatus(app.statusHandle, &app.serviceStatus) == FALSE)
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
DEBUG_ERROR("SetServiceStatus failed");
|
2017-10-31 09:20:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
|
|
|
|
{
|
|
|
|
switch (CtrlCode)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
if (app.serviceStatus.dwCurrentState != SERVICE_RUNNING)
|
|
|
|
break;
|
|
|
|
|
|
|
|
app.serviceStatus.dwControlsAccepted = 0;
|
|
|
|
app.serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
app.serviceStatus.dwWin32ExitCode = 0;
|
|
|
|
app.serviceStatus.dwCheckPoint = 4;
|
|
|
|
|
|
|
|
if (SetServiceStatus(app.statusHandle, &app.serviceStatus) == FALSE)
|
2017-10-31 11:09:57 +00:00
|
|
|
DEBUG_ERROR("SetServiceStatus failed");
|
2017-10-31 09:20:37 +00:00
|
|
|
|
|
|
|
SetEvent(app.serviceStopEvent);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
|
|
|
|
{
|
2017-10-31 11:09:57 +00:00
|
|
|
IVSHMEM * ivshmem = IVSHMEM::Get();
|
|
|
|
if (!ivshmem->Initialize())
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize IVSHMEM");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-10-31 09:20:37 +00:00
|
|
|
while (WaitForSingleObject(app.serviceStopEvent, 0) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
Sleep(1000);
|
|
|
|
}
|
2017-10-31 11:09:57 +00:00
|
|
|
|
|
|
|
ivshmem->DeInitialize();
|
2017-10-31 09:20:37 +00:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|