ReactOS  0.4.14-dev-55-g2da92ac
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 
12 volatile BOOL bShutDown = FALSE;
13 volatile BOOL bPause = FALSE;
14 
15 LPTSTR ServiceName = _T("skel_service");
16 
17 typedef struct _ServiceInfo
18 {
22 
23 /********* To be moved to new file **********/
24 typedef struct _ServiceData
25 {
29 
31 {
32  while (!bShutDown)
33  Sleep(1000);
34 
35  return 0;
36 }
37 /*******************************************/
38 
39 
40 static VOID
42  DWORD NewStatus,
43  DWORD Check)
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 
66 static 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,
78  ThreadProc,
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 
153 VOID WINAPI
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 
189 int _tmain(int argc, LPTSTR argv[])
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 int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define RegisterServiceCtrlHandlerEx
Definition: winsvc.h:584
#define LOG_ALL
Definition: tcpsvcs.h:17
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD dwCurrentState
Definition: winsvc.h:100
BOOL InitLogging()
Definition: log.c:210
#define SERVICE_PAUSED
Definition: winsvc.h:27
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define ERROR_SERVICE_SPECIFIC_ERROR
Definition: winerror.h:617
SERVICE_STATUS servStatus
Definition: tcpsvcs.c:21
struct _ServiceData * PSERVICEDATA
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define SERVICE_START_PENDING
Definition: winsvc.h:22
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
SERVICE_STATUS_HANDLE hStatus
Definition: tcpsvcs.c:22
struct _ServiceInfo SERVICEINFO
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:56
CHAR * LPTSTR
Definition: xmlstorage.h:192
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:995
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
DWORD dwCheckPoint
Definition: winsvc.h:104
VOID WINAPI ServiceMain(DWORD argc, LPTSTR argv[])
Definition: ServiceMain.c:154
#define SERVICE_STOPPED
Definition: winsvc.h:21
int Check()
Definition: movefile.cpp:196
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
#define StartServiceCtrlDispatcher
Definition: winsvc.h:586
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define ERROR_INVALID_SERVICE_CONTROL
Definition: winerror.h:603
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:136
smooth NULL
Definition: ftsmooth.c:416
struct _ServiceData SERVICEDATA
static VOID UpdateStatus(PSERVICEINFO pServInfo, DWORD NewStatus, DWORD Check)
Definition: ServiceMain.c:41
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
volatile BOOL bShutDown
Definition: ServiceMain.c:12
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
char TCHAR
Definition: xmlstorage.h:189
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define _T(x)
Definition: vfdio.h:22
LPTSTR ServiceName
Definition: ServiceMain.c:15
DWORD dwWaitHint
Definition: winsvc.h:105
#define WINAPI
Definition: msvc.h:8
DWORD dwWin32ExitCode
Definition: winsvc.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwServiceType
Definition: winsvc.h:99
#define SERVICE_CONTROL_PAUSE
Definition: winsvc.h:37
#define _sntprintf
Definition: xmlstorage.h:201
int _tmain(int argc, LPTSTR argv[])
Definition: ServiceMain.c:189
#define InterlockedExchange
Definition: armddk.h:54
DWORD WINAPI ServerCtrlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: ServiceMain.c:104
DWORD dwControlsAccepted
Definition: winsvc.h:101
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
struct _ServiceInfo * PSERVICEINFO
#define SERVICE_CONTROL_CONTINUE
Definition: winsvc.h:38
volatile BOOL bPause
Definition: ServiceMain.c:13
HANDLE hThread
Definition: wizard.c:27
DWORD WINAPI ThreadProc(LPVOID lpParam)
Definition: ServiceMain.c:30
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
static BOOL CreateServiceThread(PSERVICEINFO pServInfo)
Definition: ServiceMain.c:67
#define INFINITE
Definition: serial.h:102
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
SERVICE_TABLE_ENTRYW ServiceTable[]
Definition: service.c:18
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40