ReactOS 0.4.15-dev-5874-gc762234
ServiceMain.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE:
5 * PURPOSE: skeleton service
6 * COPYRIGHT: Copyright 2008 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10#include "myservice.h"
11
12volatile BOOL bShutDown = FALSE;
13volatile BOOL bPause = FALSE;
14
15LPTSTR ServiceName = _T("skel_service");
16
17typedef struct _ServiceInfo
18{
22
23/********* To be moved to new file **********/
24typedef struct _ServiceData
25{
29
31{
32 while (!bShutDown)
33 Sleep(1000);
34
35 return 0;
36}
37/*******************************************/
38
39
40static VOID
42 DWORD NewStatus,
44{
45 TCHAR szSet[50];
46
47 if (Check > 0)
48 pServInfo->servStatus.dwCheckPoint += Check;
49 else
50 pServInfo->servStatus.dwCheckPoint = Check;
51
52 if (NewStatus > 0)
53 pServInfo->servStatus.dwCurrentState = NewStatus;
54
55 _sntprintf(szSet,
56 49,
57 _T("Service state 0x%lx, CheckPoint %lu"),
58 pServInfo->servStatus.dwCurrentState,
59 pServInfo->servStatus.dwCheckPoint);
60 LogEvent(szSet, 0, 0, LOG_FILE);
61
62 if (!SetServiceStatus(pServInfo->hStatus, &pServInfo->servStatus))
63 LogEvent(_T("Cannot set service status"), GetLastError(), 0, LOG_ALL);
64}
65
66static BOOL
68{
70 PSERVICEDATA servData;
71
72 UpdateStatus(pServInfo, 0, 1);
73
74 LogEvent(_T("Creating service thread"), 0, 0, LOG_FILE);
75
77 0,
79 &servData,
80 0,
81 NULL);
82
83 if (!hThread)
84 {
85 LogEvent(_T("Failed to start service thread"), GetLastError(), 101, LOG_ALL);
86 return FALSE;
87 }
88
89 UpdateStatus(pServInfo, 0, 1);
90
91 LogEvent(_T("setting service status to running"), 0, 0, LOG_FILE);
92 UpdateStatus(pServInfo, SERVICE_RUNNING, 0);
93
95
96 if (hThread)
98
99 return TRUE;
100}
101
102
105 DWORD dwEventType,
106 LPVOID lpEventData,
107 LPVOID lpContext)
108
109{
110 PSERVICEINFO pServInfo = (PSERVICEINFO)lpContext;
111
112 switch (dwControl)
113 {
116 LogEvent(_T("\nSetting the service to SERVICE_STOP_PENDING"), 0, 0, LOG_FILE);
118 pServInfo->servStatus.dwWin32ExitCode = 0;
119 pServInfo->servStatus.dwWaitHint = 0;
120 UpdateStatus(pServInfo, SERVICE_STOP_PENDING, 1);
121 return ERROR_SUCCESS;
122
124 LogEvent(_T("Setting the service to SERVICE_PAUSED"), 0, 0, LOG_FILE);
126 UpdateStatus(pServInfo, SERVICE_PAUSED, 0);
127 return ERROR_SUCCESS;
128
130 LogEvent(_T("Setting the service to SERVICE_RUNNING"), 0, 0, LOG_FILE);
132 UpdateStatus(pServInfo, SERVICE_RUNNING, 0);
133 return ERROR_SUCCESS;
134
136 return ERROR_SUCCESS;
137
138 default:
139 if (dwControl >= 128 && dwControl <= 255) /* User defined */
140 {
141 LogEvent(_T("User defined control code"), 0, 0, LOG_FILE);
142 return ERROR_SUCCESS;
143 }
144 else
145 {
146 LogEvent(_T("ERROR: Bad control code"), 0, 0, LOG_FILE);
148 }
149 }
150}
151
152
155{
156 SERVICEINFO servInfo;
157
158 LogEvent(_T("Entering ServiceMain"), 0, 0, LOG_FILE);
159
165 servInfo.servStatus.dwCheckPoint = 0;
166 servInfo.servStatus.dwWaitHint = 1000;
167
168 LogEvent(_T("Registering service control handler"), 0, 0, LOG_FILE);
170 if (!servInfo.hStatus)
171 LogEvent(_T("Failed to register service"), GetLastError(), 100, LOG_ALL);
172
173 UpdateStatus(&servInfo, SERVICE_START_PENDING, 1);
174
175 if (!CreateServiceThread(&servInfo))
176 {
178 UpdateStatus(&servInfo, SERVICE_STOPPED, 0);
179 return;
180 }
181
182 LogEvent(_T("Service thread shut down. Set SERVICE_STOPPED status"), 0, 0, LOG_FILE);
183 UpdateStatus(&servInfo, SERVICE_STOPPED, 0);
184
185 LogEvent(_T("Leaving ServiceMain"), 0, 0, LOG_FILE);
186}
187
188
190{
192 {
194 {NULL, NULL }
195 };
196
197 InitLogging();
198
200 LogEvent(_T("failed to start the service control dispatcher"), GetLastError(), 101, LOG_ALL);
201
202 return 0;
203}
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
static int argc
Definition: ServiceArgs.c:12
VOID WINAPI ServiceMain(DWORD argc, LPTSTR argv[])
Definition: ServiceMain.c:154
struct _ServiceData SERVICEDATA
struct _ServiceData * PSERVICEDATA
struct _ServiceInfo SERVICEINFO
static VOID UpdateStatus(PSERVICEINFO pServInfo, DWORD NewStatus, DWORD Check)
Definition: ServiceMain.c:41
static BOOL CreateServiceThread(PSERVICEINFO pServInfo)
Definition: ServiceMain.c:67
volatile BOOL bShutDown
Definition: ServiceMain.c:12
LPTSTR ServiceName
Definition: ServiceMain.c:15
struct _ServiceInfo * PSERVICEINFO
DWORD WINAPI ThreadProc(LPVOID lpParam)
Definition: ServiceMain.c:30
DWORD WINAPI ServerCtrlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: ServiceMain.c:104
volatile BOOL bPause
Definition: ServiceMain.c:13
#define InterlockedExchange
Definition: armddk.h:54
static SERVICE_TABLE_ENTRYW ServiceTable[2]
Definition: eventlog.c:24
BOOL InitLogging()
Definition: log.c:210
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
#define INFINITE
Definition: serial.h:102
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _tmain
Definition: tchar.h:497
int Check()
Definition: movefile.cpp:196
#define argv
Definition: mplay32.c:18
HANDLE hThread
Definition: wizard.c:28
long LONG
Definition: pedump.c:60
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
DWORD dwServiceType
Definition: winsvc.h:99
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwControlsAccepted
Definition: winsvc.h:101
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwCurrentState
Definition: winsvc.h:100
DWORD dwCheckPoint
Definition: winsvc.h:104
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
SERVICE_STATUS_HANDLE hStatus
Definition: tcpsvcs.c:22
SERVICE_STATUS servStatus
Definition: tcpsvcs.c:21
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define LOG_ALL
Definition: tcpsvcs.h:17
int32_t INT
Definition: typedefs.h:58
#define _T(x)
Definition: vfdio.h:22
DWORD WINAPI GetLastError(void)
Definition: except.c:1040
#define WINAPI
Definition: msvc.h:6
#define ERROR_SERVICE_SPECIFIC_ERROR
Definition: winerror.h:617
#define ERROR_INVALID_SERVICE_CONTROL
Definition: winerror.h:603
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define RegisterServiceCtrlHandlerEx
Definition: winsvc.h:584
#define SERVICE_PAUSED
Definition: winsvc.h:27
#define StartServiceCtrlDispatcher
Definition: winsvc.h:586
#define SERVICE_START_PENDING
Definition: winsvc.h:22
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define SERVICE_CONTROL_CONTINUE
Definition: winsvc.h:38
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define SERVICE_CONTROL_PAUSE
Definition: winsvc.h:37
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:962
char TCHAR
Definition: xmlstorage.h:189
#define _sntprintf
Definition: xmlstorage.h:201
CHAR * LPTSTR
Definition: xmlstorage.h:192