2008-03-31 14:41:06 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS services
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE:
|
|
|
|
* PURPOSE: skeleton service
|
|
|
|
* COPYRIGHT: Copyright 2008 Ged Murphy <gedmurphy@reactos.org>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "myservice.h"
|
|
|
|
|
|
|
|
volatile BOOL bShutDown = FALSE;
|
|
|
|
volatile BOOL bPause = FALSE;
|
|
|
|
|
|
|
|
LPTSTR ServiceName = _T("skel_service");
|
|
|
|
|
|
|
|
typedef struct _ServiceInfo
|
|
|
|
{
|
|
|
|
SERVICE_STATUS servStatus;
|
|
|
|
SERVICE_STATUS_HANDLE hStatus;
|
|
|
|
} SERVICEINFO, *PSERVICEINFO;
|
|
|
|
|
|
|
|
/********* To be moved to new file **********/
|
|
|
|
typedef struct _ServiceData
|
|
|
|
{
|
|
|
|
INT val1;
|
|
|
|
INT val2;
|
|
|
|
} SERVICEDATA, *PSERVICEDATA;
|
|
|
|
|
|
|
|
DWORD WINAPI ThreadProc(LPVOID lpParam)
|
|
|
|
{
|
|
|
|
while (!bShutDown)
|
|
|
|
Sleep(1000);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*******************************************/
|
|
|
|
|
|
|
|
|
2008-03-31 15:00:12 +00:00
|
|
|
static VOID
|
2008-03-31 14:41:06 +00:00
|
|
|
UpdateStatus(PSERVICEINFO pServInfo,
|
|
|
|
DWORD NewStatus,
|
|
|
|
DWORD Check)
|
|
|
|
{
|
|
|
|
TCHAR szSet[50];
|
|
|
|
|
|
|
|
if (Check > 0)
|
|
|
|
pServInfo->servStatus.dwCheckPoint += Check;
|
|
|
|
else
|
|
|
|
pServInfo->servStatus.dwCheckPoint = Check;
|
|
|
|
|
|
|
|
if (NewStatus > 0)
|
|
|
|
pServInfo->servStatus.dwCurrentState = NewStatus;
|
|
|
|
|
2008-04-02 08:54:50 +00:00
|
|
|
_sntprintf(szSet,
|
|
|
|
49,
|
2015-10-03 22:38:05 +00:00
|
|
|
_T("Service state 0x%lx, CheckPoint %lu"),
|
2008-04-02 08:54:50 +00:00
|
|
|
pServInfo->servStatus.dwCurrentState,
|
|
|
|
pServInfo->servStatus.dwCheckPoint);
|
2008-03-31 14:41:06 +00:00
|
|
|
LogEvent(szSet, 0, 0, LOG_FILE);
|
|
|
|
|
|
|
|
if (!SetServiceStatus(pServInfo->hStatus, &pServInfo->servStatus))
|
|
|
|
LogEvent(_T("Cannot set service status"), GetLastError(), 0, LOG_ALL);
|
|
|
|
}
|
|
|
|
|
2008-03-31 15:00:12 +00:00
|
|
|
static BOOL
|
2008-03-31 14:41:06 +00:00
|
|
|
CreateServiceThread(PSERVICEINFO pServInfo)
|
|
|
|
{
|
|
|
|
HANDLE hThread;
|
|
|
|
PSERVICEDATA servData;
|
|
|
|
|
|
|
|
UpdateStatus(pServInfo, 0, 1);
|
|
|
|
|
|
|
|
LogEvent(_T("Creating service thread"), 0, 0, LOG_FILE);
|
|
|
|
|
|
|
|
hThread = CreateThread(NULL,
|
|
|
|
0,
|
|
|
|
ThreadProc,
|
|
|
|
&servData,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!hThread)
|
|
|
|
{
|
|
|
|
LogEvent(_T("Failed to start service thread"), GetLastError(), 101, LOG_ALL);
|
2008-03-31 15:00:12 +00:00
|
|
|
return FALSE;
|
2008-03-31 14:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateStatus(pServInfo, 0, 1);
|
|
|
|
|
|
|
|
LogEvent(_T("setting service status to running"), 0, 0, LOG_FILE);
|
|
|
|
UpdateStatus(pServInfo, SERVICE_RUNNING, 0);
|
|
|
|
|
|
|
|
WaitForSingleObject(hThread, INFINITE);
|
|
|
|
|
|
|
|
if (hThread)
|
|
|
|
CloseHandle(hThread);
|
|
|
|
|
2008-03-31 15:00:12 +00:00
|
|
|
return TRUE;
|
2008-03-31 14:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-18 20:37:51 +00:00
|
|
|
DWORD WINAPI
|
2008-03-31 14:41:06 +00:00
|
|
|
ServerCtrlHandler(DWORD dwControl,
|
|
|
|
DWORD dwEventType,
|
|
|
|
LPVOID lpEventData,
|
|
|
|
LPVOID lpContext)
|
|
|
|
|
|
|
|
{
|
|
|
|
PSERVICEINFO pServInfo = (PSERVICEINFO)lpContext;
|
|
|
|
|
|
|
|
switch (dwControl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
LogEvent(_T("\nSetting the service to SERVICE_STOP_PENDING"), 0, 0, LOG_FILE);
|
|
|
|
InterlockedExchange((LONG *)&bShutDown, TRUE);
|
|
|
|
pServInfo->servStatus.dwWin32ExitCode = 0;
|
|
|
|
pServInfo->servStatus.dwWaitHint = 0;
|
|
|
|
UpdateStatus(pServInfo, SERVICE_STOP_PENDING, 1);
|
2012-09-18 20:37:51 +00:00
|
|
|
return ERROR_SUCCESS;
|
2008-03-31 14:41:06 +00:00
|
|
|
|
|
|
|
case SERVICE_CONTROL_PAUSE:
|
|
|
|
LogEvent(_T("Setting the service to SERVICE_PAUSED"), 0, 0, LOG_FILE);
|
|
|
|
InterlockedExchange((LONG *)&bPause, TRUE);
|
|
|
|
UpdateStatus(pServInfo, SERVICE_PAUSED, 0);
|
2012-09-18 20:37:51 +00:00
|
|
|
return ERROR_SUCCESS;
|
2008-03-31 14:41:06 +00:00
|
|
|
|
|
|
|
case SERVICE_CONTROL_CONTINUE:
|
|
|
|
LogEvent(_T("Setting the service to SERVICE_RUNNING"), 0, 0, LOG_FILE);
|
|
|
|
InterlockedExchange((LONG *)&bPause, FALSE);
|
|
|
|
UpdateStatus(pServInfo, SERVICE_RUNNING, 0);
|
2012-09-18 20:37:51 +00:00
|
|
|
return ERROR_SUCCESS;
|
2008-03-31 14:41:06 +00:00
|
|
|
|
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
2012-09-18 20:37:51 +00:00
|
|
|
return ERROR_SUCCESS;
|
2008-03-31 14:41:06 +00:00
|
|
|
|
|
|
|
default:
|
2012-09-18 20:37:51 +00:00
|
|
|
if (dwControl >= 128 && dwControl <= 255) /* User defined */
|
|
|
|
{
|
2008-03-31 14:41:06 +00:00
|
|
|
LogEvent(_T("User defined control code"), 0, 0, LOG_FILE);
|
2012-09-18 20:37:51 +00:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2008-03-31 14:41:06 +00:00
|
|
|
else
|
2012-09-18 20:37:51 +00:00
|
|
|
{
|
2008-03-31 14:41:06 +00:00
|
|
|
LogEvent(_T("ERROR: Bad control code"), 0, 0, LOG_FILE);
|
2012-09-18 20:37:51 +00:00
|
|
|
return ERROR_INVALID_SERVICE_CONTROL;
|
|
|
|
}
|
2008-03-31 14:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID WINAPI
|
|
|
|
ServiceMain(DWORD argc, LPTSTR argv[])
|
|
|
|
{
|
|
|
|
SERVICEINFO servInfo;
|
|
|
|
|
|
|
|
LogEvent(_T("Entering ServiceMain"), 0, 0, LOG_FILE);
|
|
|
|
|
|
|
|
servInfo.servStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
servInfo.servStatus.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
servInfo.servStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
|
|
|
|
servInfo.servStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
|
|
|
|
servInfo.servStatus.dwServiceSpecificExitCode = 0;
|
|
|
|
servInfo.servStatus.dwCheckPoint = 0;
|
|
|
|
servInfo.servStatus.dwWaitHint = 1000;
|
|
|
|
|
|
|
|
LogEvent(_T("Registering service control handler"), 0, 0, LOG_FILE);
|
|
|
|
servInfo.hStatus = RegisterServiceCtrlHandlerEx(ServiceName, ServerCtrlHandler, &servInfo);
|
|
|
|
if (!servInfo.hStatus)
|
|
|
|
LogEvent(_T("Failed to register service"), GetLastError(), 100, LOG_ALL);
|
|
|
|
|
|
|
|
UpdateStatus(&servInfo, SERVICE_START_PENDING, 1);
|
|
|
|
|
2008-03-31 15:00:12 +00:00
|
|
|
if (!CreateServiceThread(&servInfo))
|
2008-03-31 14:41:06 +00:00
|
|
|
{
|
|
|
|
servInfo.servStatus.dwServiceSpecificExitCode = 1;
|
|
|
|
UpdateStatus(&servInfo, SERVICE_STOPPED, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogEvent(_T("Service thread shut down. Set SERVICE_STOPPED status"), 0, 0, LOG_FILE);
|
|
|
|
UpdateStatus(&servInfo, SERVICE_STOPPED, 0);
|
|
|
|
|
|
|
|
LogEvent(_T("Leaving ServiceMain"), 0, 0, LOG_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int _tmain(int argc, LPTSTR argv[])
|
|
|
|
{
|
|
|
|
SERVICE_TABLE_ENTRY ServiceTable[] =
|
|
|
|
{
|
|
|
|
{ServiceName, ServiceMain},
|
|
|
|
{NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
InitLogging();
|
|
|
|
|
|
|
|
if (!StartServiceCtrlDispatcher(ServiceTable))
|
|
|
|
LogEvent(_T("failed to start the service control dispatcher"), GetLastError(), 101, LOG_ALL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|