ReactOS  0.4.14-dev-98-gb0d4763
service.c File Reference
#include <kmt_test.h>
#include "kmtest.h"
#include <assert.h>
Include dependency graph for service.c:

Go to the source code of this file.

Macros

#define SERVICE_ACCESS   (SERVICE_QUERY_STATUS | SERVICE_START | SERVICE_STOP | DELETE)
 

Functions

DWORD KmtpCreateService (IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, IN DWORD ServiceType, OUT SC_HANDLE *ServiceHandle)
 
KmtServiceInit

Initialize service management routines (by opening the service control manager)

Returns
Win32 error code
DWORD KmtServiceInit (VOID)
 
KmtServiceCleanup

Clean up resources used by service management routines.

Parameters
IgnoreErrorsIf TRUE, the function will never set ErrorLineAndFile, and always return ERROR_SUCCESS
Returns
Win32 error code
DWORD KmtServiceCleanup (BOOLEAN IgnoreErrors)
 
KmtCreateService

Create the specified driver service and return a handle to it

Parameters
ServiceNameName of the service to create
ServicePathFile name of the driver, relative to the current directory
DisplayNameService display name
ServiceHandlePointer to a variable to receive the handle to the service
Returns
Win32 error code
DWORD KmtCreateService (IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, OUT SC_HANDLE *ServiceHandle)
 
KmtGetServiceStateAsString
Parameters
ServiceStateService state as a number
Returns
Service state as a string
static PCSTR KmtGetServiceStateAsString (IN DWORD ServiceState)
 
KmtEnsureServiceState
Parameters
ServiceNameName of the service to check, or NULL
ServiceHandleHandle to the service
ExpectedServiceStateState which the service should be in
Returns
Win32 error code
static DWORD KmtEnsureServiceState (IN PCWSTR ServiceName OPTIONAL, IN SC_HANDLE ServiceHandle, IN DWORD ExpectedServiceState)
 
KmtStartService

Start the specified driver service by handle or name (and return a handle to it)

Parameters
ServiceNameIf *ServiceHandle is NULL, name of the service to start
ServiceHandlePointer to a variable containing the service handle, or NULL (in which case it will be filled with a handle to the service)
Returns
Win32 error code
DWORD KmtStartService (IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
 
KmtCreateAndStartService

Create and start the specified driver service and return a handle to it

Parameters
ServiceNameName of the service to create
ServicePathFile name of the driver, relative to the current directory
DisplayNameService display name
ServiceHandlePointer to a variable to receive the handle to the service
RestartIfRunningTRUE to stop and restart the service if it is already running
Returns
Win32 error code
DWORD KmtCreateAndStartService (IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, OUT SC_HANDLE *ServiceHandle, IN BOOLEAN RestartIfRunning)
 
KmtStopService

Stop the specified driver service by handle or name (and return a handle to it)

Parameters
ServiceNameIf *ServiceHandle is NULL, name of the service to stop
ServiceHandlePointer to a variable containing the service handle, or NULL (in which case it will be filled with a handle to the service)
Returns
Win32 error code
DWORD KmtStopService (IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
 
KmtDeleteService

Delete the specified driver service by handle or name (and return a handle to it)

Parameters
ServiceNameIf *ServiceHandle is NULL, name of the service to delete
ServiceHandlePointer to a variable containing the service handle. Will be set to NULL on success
Returns
Win32 error code
DWORD KmtDeleteService (IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
 
KmtCloseService

Close the specified driver service handle

Parameters
ServiceHandlePointer to a variable containing the service handle. Will be set to NULL on success
Returns
Win32 error code
DWORD KmtCloseService (IN OUT SC_HANDLE *ServiceHandle)
 

Variables

static SC_HANDLE ScmHandle
 

Macro Definition Documentation

◆ SERVICE_ACCESS

Definition at line 15 of file service.c.

Function Documentation

◆ KmtCloseService()

DWORD KmtCloseService ( IN OUT SC_HANDLE *  ServiceHandle)

Definition at line 392 of file service.c.

394 {
396 
397  assert(ServiceHandle);
398 
399  if (*ServiceHandle && !CloseServiceHandle(*ServiceHandle))
401 
402  *ServiceHandle = NULL;
403 
404 cleanup:
405  return Error;
406 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error_goto(Error, label)
Definition: kmtest.h:21
#define assert(x)
Definition: debug.h:53
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
smooth NULL
Definition: ftsmooth.c:416
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
char * cleanup(char *str)
Definition: wpickclick.c:99

Referenced by KmtFltCloseService(), and main().

◆ KmtCreateAndStartService()

DWORD KmtCreateAndStartService ( IN PCWSTR  ServiceName,
IN PCWSTR  ServicePath,
IN PCWSTR DisplayName  OPTIONAL,
OUT SC_HANDLE *  ServiceHandle,
IN BOOLEAN  RestartIfRunning 
)

Definition at line 262 of file service.c.

268 {
270 
271  assert(ServiceHandle);
272 
273  Error = KmtCreateService(ServiceName, ServicePath, DisplayName, ServiceHandle);
274 
275  if (Error && Error != ERROR_SERVICE_EXISTS)
276  goto cleanup;
277 
278  Error = KmtStartService(ServiceName, ServiceHandle);
279 
281  goto cleanup;
282 
284 
285  if (!RestartIfRunning)
286  goto cleanup;
287 
288  Error = KmtStopService(ServiceName, ServiceHandle);
289  if (Error)
290  goto cleanup;
291 
292  Error = KmtStartService(ServiceName, ServiceHandle);
293  if (Error)
294  goto cleanup;
295 
296 cleanup:
297  assert(Error || *ServiceHandle);
298  return Error;
299 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define assert(x)
Definition: debug.h:53
DWORD KmtStartService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:217
LPTSTR ServiceName
Definition: ServiceMain.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
#define ERROR_SERVICE_EXISTS
Definition: winerror.h:624
DWORD KmtStopService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:315
DWORD KmtCreateService(IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, OUT SC_HANDLE *ServiceHandle)
Definition: service.c:92
char * cleanup(char *str)
Definition: wpickclick.c:99
#define ERROR_SERVICE_ALREADY_RUNNING
Definition: winerror.h:607

Referenced by KmtLoadDriver(), and main().

◆ KmtCreateService()

DWORD KmtCreateService ( IN PCWSTR  ServiceName,
IN PCWSTR  ServicePath,
IN PCWSTR DisplayName  OPTIONAL,
OUT SC_HANDLE *  ServiceHandle 
)

Definition at line 92 of file service.c.

97 {
99  ServicePath,
100  DisplayName,
102  ServiceHandle);
103 }
DWORD KmtpCreateService(IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, IN DWORD ServiceType, OUT SC_HANDLE *ServiceHandle)
Definition: service.c:414
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define SERVICE_KERNEL_DRIVER
Definition: cmtypes.h:951

Referenced by KmtCreateAndStartService(), and main().

◆ KmtDeleteService()

DWORD KmtDeleteService ( IN PCWSTR ServiceName  OPTIONAL,
IN OUT SC_HANDLE *  ServiceHandle 
)

Definition at line 356 of file service.c.

359 {
361 
362  assert(ServiceHandle);
363  assert(ServiceName || *ServiceHandle);
364 
365  if (!*ServiceHandle)
366  *ServiceHandle = OpenService(ScmHandle, ServiceName, SERVICE_ACCESS);
367 
368  if (!*ServiceHandle)
370 
371  if (!DeleteService(*ServiceHandle))
373 
374  if (*ServiceHandle)
375  CloseServiceHandle(*ServiceHandle);
376 
377 cleanup:
378  return Error;
379 }
#define SERVICE_ACCESS
Definition: service.c:15
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error_goto(Error, label)
Definition: kmtest.h:21
#define assert(x)
Definition: debug.h:53
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:916
#define OpenService
Definition: winsvc.h:576
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
LPTSTR ServiceName
Definition: ServiceMain.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
char * cleanup(char *str)
Definition: wpickclick.c:99
static SC_HANDLE ScmHandle
Definition: service.c:30

Referenced by KmtFltDeleteService(), and main().

◆ KmtEnsureServiceState()

static DWORD KmtEnsureServiceState ( IN PCWSTR ServiceName  OPTIONAL,
IN SC_HANDLE  ServiceHandle,
IN DWORD  ExpectedServiceState 
)
static

Definition at line 155 of file service.c.

159 {
163  DWORD Timeout = 10 * 1000;
164  PCWSTR ServiceNameOut = ServiceName ? ServiceName : L"(handle only, no name)";
165 
166  assert(ServiceHandle);
167  assert(ExpectedServiceState);
168 
169  if (!QueryServiceStatus(ServiceHandle, &ServiceStatus))
171 
172  while (ServiceStatus.dwCurrentState != ExpectedServiceState)
173  {
174  // NB: ServiceStatus.dwWaitHint and ServiceStatus.dwCheckPoint logic could be added, if need be.
175 
176  Sleep(1 * 1000);
177 
178  if (!QueryServiceStatus(ServiceHandle, &ServiceStatus))
180 
181  if (GetTickCount() - StartTime >= Timeout)
182  break;
183  }
184 
185  if (ServiceStatus.dwCurrentState != ExpectedServiceState)
186  {
187  ok(FALSE, "Service = %ls, state = %lu %s (!= %lu %s), waitHint = %lu, checkPoint = %lu\n",
188  ServiceNameOut,
190  ExpectedServiceState, KmtGetServiceStateAsString(ExpectedServiceState),
192  goto cleanup;
193  }
194 
195  trace("Service = %ls, state = %lu %s\n",
196  ServiceNameOut,
197  ExpectedServiceState, KmtGetServiceStateAsString(ExpectedServiceState));
198 
199 cleanup:
200  return Error;
201 }
const uint16_t * PCWSTR
Definition: typedefs.h:55
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD dwCurrentState
Definition: winsvc.h:100
#define error_goto(Error, label)
Definition: kmtest.h:21
static SERVICE_STATUS ServiceStatus
Definition: dcomlaunch.c:27
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
#define assert(x)
Definition: debug.h:53
static LARGE_INTEGER StartTime
Definition: sys_arch.c:18
DWORD dwCheckPoint
Definition: winsvc.h:104
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define trace
Definition: atltest.h:70
DWORD dwWaitHint
Definition: winsvc.h:105
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
BOOL WINAPI QueryServiceStatus(SC_HANDLE hService, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:2788
static const WCHAR L[]
Definition: oid.c:1250
static ULONG Timeout
Definition: ping.c:61
#define ok(value,...)
Definition: atltest.h:57
char * cleanup(char *str)
Definition: wpickclick.c:99
static PCSTR KmtGetServiceStateAsString(IN DWORD ServiceState)
Definition: service.c:115

Referenced by KmtStartService(), and KmtStopService().

◆ KmtGetServiceStateAsString()

static PCSTR KmtGetServiceStateAsString ( IN DWORD  ServiceState)
static

Definition at line 115 of file service.c.

117 {
118  switch(ServiceState)
119  {
120  case SERVICE_STOPPED:
121  return "STOPPED";
123  return "START_PENDING";
125  return "STOP_PENDING";
126  case SERVICE_RUNNING:
127  return "RUNNING";
129  return "CONTINUE_PENDING";
131  return "PAUSE_PENDING";
132  case SERVICE_PAUSED:
133  return "PAUSED";
134  default:
135  ok(FALSE, "Unknown service state = %lu\n", ServiceState);
136  return "(Unknown)";
137  }
138 }
#define SERVICE_PAUSE_PENDING
Definition: winsvc.h:26
#define SERVICE_PAUSED
Definition: winsvc.h:27
#define SERVICE_START_PENDING
Definition: winsvc.h:22
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define SERVICE_CONTINUE_PENDING
Definition: winsvc.h:25
#define ok(value,...)
Definition: atltest.h:57
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23

Referenced by KmtEnsureServiceState().

◆ KmtpCreateService()

DWORD KmtpCreateService ( IN PCWSTR  ServiceName,
IN PCWSTR  ServicePath,
IN PCWSTR DisplayName  OPTIONAL,
IN DWORD  ServiceType,
OUT SC_HANDLE *  ServiceHandle 
)

Definition at line 414 of file service.c.

420 {
422  WCHAR DriverPath[MAX_PATH];
423  HRESULT result = S_OK;
424 
425  assert(ServiceHandle);
426  assert(ServiceName && ServicePath);
427 
428  if (!GetModuleFileName(NULL, DriverPath, sizeof DriverPath / sizeof DriverPath[0]))
430 
431  assert(wcsrchr(DriverPath, L'\\') != NULL);
432  wcsrchr(DriverPath, L'\\')[1] = L'\0';
433 
434  result = StringCbCat(DriverPath, sizeof DriverPath, ServicePath);
435  if (FAILED(result))
437 
438  if (GetFileAttributes(DriverPath) == INVALID_FILE_ATTRIBUTES)
440 
441  *ServiceHandle = CreateService(ScmHandle, ServiceName, DisplayName,
442  SERVICE_ACCESS, ServiceType, SERVICE_DEMAND_START,
443  SERVICE_ERROR_NORMAL, DriverPath, NULL, NULL, NULL, NULL, NULL);
444 
445  if (!*ServiceHandle)
447 
448 cleanup:
449  return Error;
450 }
#define SERVICE_ACCESS
Definition: service.c:15
#define SERVICE_ERROR_NORMAL
Definition: cmtypes.h:980
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error_goto(Error, label)
Definition: kmtest.h:21
#define assert(x)
Definition: debug.h:53
#define CreateService
Definition: winsvc.h:569
smooth NULL
Definition: ftsmooth.c:416
_Check_return_ _CRTIMP _CONST_RETURN wchar_t *__cdecl wcsrchr(_In_z_ const wchar_t *_Str, _In_ wchar_t _Ch)
#define StringCbCat
Definition: strsafe.h:334
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
#define GetModuleFileName
Definition: winbase.h:3655
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
static const WCHAR L[]
Definition: oid.c:1250
#define GetFileAttributes
Definition: winbase.h:3639
#define S_OK
Definition: intsafe.h:59
#define error_value_goto(Error, value, label)
Definition: kmtest.h:22
char * cleanup(char *str)
Definition: wpickclick.c:99
#define SERVICE_DEMAND_START
Definition: cmtypes.h:976
GLuint64EXT * result
Definition: glext.h:11304
static SC_HANDLE ScmHandle
Definition: service.c:30

Referenced by KmtCreateService(), and KmtFltCreateService().

◆ KmtServiceCleanup()

DWORD KmtServiceCleanup ( BOOLEAN  IgnoreErrors)

Definition at line 64 of file service.c.

66 {
68 
69  if (ScmHandle && !CloseServiceHandle(ScmHandle) && !IgnoreErrors)
70  error(Error);
71 
72  return Error;
73 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error(str)
Definition: mkdosfs.c:1605
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
static SC_HANDLE ScmHandle
Definition: service.c:30

Referenced by main().

◆ KmtServiceInit()

DWORD KmtServiceInit ( VOID  )

Definition at line 40 of file service.c.

41 {
43 
44  assert(!ScmHandle);
45 
47  if (!ScmHandle)
48  error(Error);
49 
50  return Error;
51 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error(str)
Definition: mkdosfs.c:1605
#define assert(x)
Definition: debug.h:53
smooth NULL
Definition: ftsmooth.c:416
#define SC_MANAGER_CREATE_SERVICE
Definition: winsvc.h:15
#define OpenSCManager
Definition: winsvc.h:575
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
static SC_HANDLE ScmHandle
Definition: service.c:30

Referenced by main().

◆ KmtStartService()

DWORD KmtStartService ( IN PCWSTR ServiceName  OPTIONAL,
IN OUT SC_HANDLE *  ServiceHandle 
)

Definition at line 217 of file service.c.

220 {
222 
223  assert(ServiceHandle);
224  assert(ServiceName || *ServiceHandle);
225 
226  if (!*ServiceHandle)
227  *ServiceHandle = OpenService(ScmHandle, ServiceName, SERVICE_ACCESS);
228 
229  if (!*ServiceHandle)
231 
232  if (!StartService(*ServiceHandle, 0, NULL))
234 
236  if (Error)
237  goto cleanup;
238 
239 cleanup:
240  return Error;
241 }
#define StartService
Definition: winsvc.h:585
#define SERVICE_ACCESS
Definition: service.c:15
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error_goto(Error, label)
Definition: kmtest.h:21
#define assert(x)
Definition: debug.h:53
#define OpenService
Definition: winsvc.h:576
#define SERVICE_RUNNING
Definition: winsvc.h:24
smooth NULL
Definition: ftsmooth.c:416
LPTSTR ServiceName
Definition: ServiceMain.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
static DWORD KmtEnsureServiceState(IN PCWSTR ServiceName OPTIONAL, IN SC_HANDLE ServiceHandle, IN DWORD ExpectedServiceState)
Definition: service.c:155
char * cleanup(char *str)
Definition: wpickclick.c:99
static SC_HANDLE ScmHandle
Definition: service.c:30

Referenced by KmtCreateAndStartService(), main(), and START_TEST().

◆ KmtStopService()

DWORD KmtStopService ( IN PCWSTR ServiceName  OPTIONAL,
IN OUT SC_HANDLE *  ServiceHandle 
)

Definition at line 315 of file service.c.

318 {
321 
322  assert(ServiceHandle);
323  assert(ServiceName || *ServiceHandle);
324 
325  if (!*ServiceHandle)
326  *ServiceHandle = OpenService(ScmHandle, ServiceName, SERVICE_ACCESS);
327 
328  if (!*ServiceHandle)
330 
331  if (!ControlService(*ServiceHandle, SERVICE_CONTROL_STOP, &ServiceStatus))
333 
335  if (Error)
336  goto cleanup;
337 
338 cleanup:
339  return Error;
340 }
#define SERVICE_ACCESS
Definition: service.c:15
#define ERROR_SUCCESS
Definition: deptool.c:10
#define error_goto(Error, label)
Definition: kmtest.h:21
static SERVICE_STATUS ServiceStatus
Definition: dcomlaunch.c:27
#define assert(x)
Definition: debug.h:53
#define OpenService
Definition: winsvc.h:576
#define SERVICE_STOPPED
Definition: winsvc.h:21
LPTSTR ServiceName
Definition: ServiceMain.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
static DWORD KmtEnsureServiceState(IN PCWSTR ServiceName OPTIONAL, IN SC_HANDLE ServiceHandle, IN DWORD ExpectedServiceState)
Definition: service.c:155
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:619
char * cleanup(char *str)
Definition: wpickclick.c:99
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
static SC_HANDLE ScmHandle
Definition: service.c:30

Referenced by KmtCreateAndStartService(), KmtUnloadDriver(), and main().

Variable Documentation

◆ ScmHandle

SC_HANDLE ScmHandle
static