ReactOS 0.4.16-dev-297-gc569aee
ServiceMain.c File Reference
#include "myservice.h"
Include dependency graph for ServiceMain.c:

Go to the source code of this file.

Classes

struct  _ServiceInfo
 
struct  _ServiceData
 

Typedefs

typedef struct _ServiceInfo SERVICEINFO
 
typedef struct _ServiceInfoPSERVICEINFO
 
typedef struct _ServiceData SERVICEDATA
 
typedef struct _ServiceDataPSERVICEDATA
 

Functions

DWORD WINAPI ThreadProc (LPVOID lpParam)
 
static VOID UpdateStatus (PSERVICEINFO pServInfo, DWORD NewStatus, DWORD Check)
 
static BOOL CreateServiceThread (PSERVICEINFO pServInfo)
 
DWORD WINAPI ServerCtrlHandler (DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
 
VOID WINAPI ServiceMain (DWORD argc, LPTSTR argv[])
 
int _tmain (int argc, LPTSTR argv[])
 

Variables

volatile BOOL bShutDown = FALSE
 
volatile BOOL bPause = FALSE
 
LPTSTR ServiceName = _T("skel_service")
 

Typedef Documentation

◆ PSERVICEDATA

◆ PSERVICEINFO

◆ SERVICEDATA

◆ SERVICEINFO

Function Documentation

◆ _tmain()

int _tmain ( int  argc,
LPTSTR  argv[] 
)

Definition at line 189 of file ServiceMain.c.

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}
VOID WINAPI ServiceMain(DWORD argc, LPTSTR argv[])
Definition: ServiceMain.c:154
LPTSTR ServiceName
Definition: ServiceMain.c:15
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 NULL
Definition: types.h:112
#define LOG_ALL
Definition: tcpsvcs.h:17
#define _T(x)
Definition: vfdio.h:22
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define StartServiceCtrlDispatcher
Definition: winsvc.h:586

◆ CreateServiceThread()

static BOOL CreateServiceThread ( PSERVICEINFO  pServInfo)
static

Definition at line 67 of file ServiceMain.c.

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}
static VOID UpdateStatus(PSERVICEINFO pServInfo, DWORD NewStatus, DWORD Check)
Definition: ServiceMain.c:41
DWORD WINAPI ThreadProc(LPVOID lpParam)
Definition: ServiceMain.c:30
#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
HANDLE hThread
Definition: wizard.c:28
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define SERVICE_RUNNING
Definition: winsvc.h:24

Referenced by ServiceMain().

◆ ServerCtrlHandler()

DWORD WINAPI ServerCtrlHandler ( DWORD  dwControl,
DWORD  dwEventType,
LPVOID  lpEventData,
LPVOID  lpContext 
)

Definition at line 104 of file ServiceMain.c.

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}
volatile BOOL bShutDown
Definition: ServiceMain.c:12
struct _ServiceInfo * PSERVICEINFO
volatile BOOL bPause
Definition: ServiceMain.c:13
#define InterlockedExchange
Definition: armddk.h:54
#define ERROR_SUCCESS
Definition: deptool.c:10
long LONG
Definition: pedump.c:60
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwWaitHint
Definition: winsvc.h:105
SERVICE_STATUS servStatus
Definition: tcpsvcs.c:21
#define ERROR_INVALID_SERVICE_CONTROL
Definition: winerror.h:603
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define SERVICE_PAUSED
Definition: winsvc.h:27
#define SERVICE_CONTROL_CONTINUE
Definition: winsvc.h:38
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_CONTROL_PAUSE
Definition: winsvc.h:37
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39

Referenced by ServiceMain().

◆ ServiceMain()

VOID WINAPI ServiceMain ( DWORD  argc,
LPTSTR  argv[] 
)

Definition at line 154 of file ServiceMain.c.

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}
static BOOL CreateServiceThread(PSERVICEINFO pServInfo)
Definition: ServiceMain.c:67
DWORD WINAPI ServerCtrlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: ServiceMain.c:104
DWORD dwServiceType
Definition: winsvc.h:99
DWORD dwControlsAccepted
Definition: winsvc.h:101
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
#define ERROR_SERVICE_SPECIFIC_ERROR
Definition: winerror.h:617
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define RegisterServiceCtrlHandlerEx
Definition: winsvc.h:584
#define SERVICE_START_PENDING
Definition: winsvc.h:22
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:962

Referenced by _tmain().

◆ ThreadProc()

DWORD WINAPI ThreadProc ( LPVOID  lpParam)

Definition at line 30 of file ServiceMain.c.

31{
32 while (!bShutDown)
33 Sleep(1000);
34
35 return 0;
36}
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790

Referenced by CreateServiceThread(), and CreateUI().

◆ UpdateStatus()

static VOID UpdateStatus ( PSERVICEINFO  pServInfo,
DWORD  NewStatus,
DWORD  Check 
)
static

Definition at line 41 of file ServiceMain.c.

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}
int Check()
Definition: movefile.cpp:196
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
char TCHAR
Definition: xmlstorage.h:189
#define _sntprintf
Definition: xmlstorage.h:201

Referenced by CreateServiceThread(), ServerCtrlHandler(), and ServiceMain().

Variable Documentation

◆ bPause

volatile BOOL bPause = FALSE

Definition at line 13 of file ServiceMain.c.

Referenced by ServerCtrlHandler().

◆ bShutDown

volatile BOOL bShutDown = FALSE

Definition at line 12 of file ServiceMain.c.

Referenced by ServerCtrlHandler(), and ThreadProc().

◆ ServiceName

LPTSTR ServiceName = _T("skel_service")

Definition at line 15 of file ServiceMain.c.

Referenced by _tmain(), and ServiceMain().