ReactOS  0.4.12-dev-43-g63b00d8
failure.c File Reference
#include "sc.h"
Include dependency graph for failure.c:

Go to the source code of this file.

Functions

BOOL QueryFailure (LPCTSTR ServiceName)
 
BOOL SetFailure (LPCTSTR *ServiceArgs, INT ArgCount)
 

Function Documentation

◆ QueryFailure()

BOOL QueryFailure ( LPCTSTR  ServiceName)

Definition at line 11 of file failure.c.

Referenced by ScControl().

12 {
13  SC_HANDLE hManager = NULL;
14  SC_HANDLE hService = NULL;
15  BOOL bResult = TRUE;
16  DWORD cbBytesNeeded = 0;
17  LPSERVICE_FAILURE_ACTIONS pServiceFailure = NULL;
18  INT i;
19 
20 #ifdef SCDBG
21  _tprintf(_T("service to show failure action - %s\n\n"), ServiceName);
22 #endif
23 
24  hManager = OpenSCManager(NULL,
25  NULL,
27  if (hManager == NULL)
28  {
29  bResult = FALSE;
30  goto done;
31  }
32 
33  hService = OpenService(hManager, ServiceName, SERVICE_QUERY_CONFIG);
34  if (hService == NULL)
35  {
36  bResult = FALSE;
37  goto done;
38  }
39 
40  if (!QueryServiceConfig2(hService,
42  NULL,
43  0,
44  &cbBytesNeeded))
45  {
46  if (cbBytesNeeded == 0)
47  {
48  bResult = FALSE;
49  goto done;
50  }
51  }
52 
53  pServiceFailure = HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded);
54  if (pServiceFailure == NULL)
55  {
57  bResult = FALSE;
58  goto done;
59  }
60 
61  if (!QueryServiceConfig2(hService,
63  (LPBYTE)pServiceFailure,
64  cbBytesNeeded,
65  &cbBytesNeeded))
66  {
67  bResult = FALSE;
68  goto done;
69  }
70 
71  _tprintf(_T("SERVICE_NAME: %s\n"), ServiceName);
72  _tprintf(_T(" RESET_PERIOD : %lu seconds\n"), pServiceFailure->dwResetPeriod);
73  _tprintf(_T(" REBOOT_MESSAGE : %s\n"), (pServiceFailure->lpRebootMsg) ? pServiceFailure->lpRebootMsg : _T(""));
74  _tprintf(_T(" COMMAND_LINE : %s\n"), (pServiceFailure->lpCommand) ? pServiceFailure->lpCommand : _T(""));
75  _tprintf(_T(" FAILURE_ACTIONS : "));
76  for (i = 0; i < pServiceFailure->cActions; i++)
77  {
78  if (i != 0)
79  _tprintf(_T(" "));
80  switch (pServiceFailure->lpsaActions[i].Type)
81  {
82  case SC_ACTION_NONE:
83  continue;
84 
85  case SC_ACTION_RESTART:
86  _tprintf(_T("RESTART -- Delay = %lu milliseconds.\n"), pServiceFailure->lpsaActions[i].Delay);
87  break;
88 
89  case SC_ACTION_REBOOT:
90  _tprintf(_T("REBOOT -- Delay = %lu milliseconds.\n"), pServiceFailure->lpsaActions[i].Delay);
91  break;
92 
94  _tprintf(_T("RUN_COMMAND -- Delay = %lu milliseconds.\n"), pServiceFailure->lpsaActions[i].Delay);
95  break;
96 
97  default:
98  _tprintf(_T("\n"));
99  break;
100  }
101  }
102 
103 done:
104  if (bResult == FALSE)
105  ReportLastError();
106 
107  if (pServiceFailure != NULL)
108  HeapFree(GetProcessHeap(), 0, pServiceFailure);
109 
110  if (hService)
111  CloseServiceHandle(hService);
112 
113  if (hManager)
114  CloseServiceHandle(hManager);
115 
116  return bResult;
117 }
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
int32_t INT
Definition: typedefs.h:56
#define OpenService
Definition: winsvc.h:576
GLenum GLclampf GLint i
Definition: glfuncs.h:14
unsigned char * LPBYTE
Definition: typedefs.h:52
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
#define QueryServiceConfig2
Definition: winsvc.h:581
#define OpenSCManager
Definition: winsvc.h:575
#define _T(x)
Definition: vfdio.h:22
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define SERVICE_CONFIG_FAILURE_ACTIONS
Definition: winsvc.h:66
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
VOID ReportLastError(VOID)
Definition: sc.c:15
#define SERVICE_QUERY_CONFIG
Definition: winsvc.h:53
#define HeapFree(x, y, z)
Definition: compat.h:394
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13

◆ SetFailure()

BOOL SetFailure ( LPCTSTR ServiceArgs,
INT  ArgCount 
)

Definition at line 120 of file failure.c.

Referenced by ScControl().

123 {
124  SC_HANDLE hManager = NULL;
125  SC_HANDLE hService = NULL;
126  BOOL bResult = TRUE;
127  SERVICE_FAILURE_ACTIONS FailureActions;
128  LPCTSTR lpServiceName = NULL;
129  BOOLEAN Old = FALSE;
130 
131  ZeroMemory(&FailureActions, sizeof(SERVICE_FAILURE_ACTIONS));
132 
133  if (!ParseFailureArguments(ServiceArgs, ArgCount, &lpServiceName, &FailureActions))
134  {
135  SetFailureUsage();
136  return FALSE;
137  }
138 
139  hManager = OpenSCManager(NULL,
140  NULL,
142  if (hManager == NULL)
143  {
144  _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
145  bResult = FALSE;
146  goto done;
147  }
148 
149  hService = OpenService(hManager,
150  lpServiceName,
152  if (hService == NULL)
153  {
154  _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
155  bResult = FALSE;
156  goto done;
157  }
158 
160 
161  if (!ChangeServiceConfig2(hService,
163  (LPBYTE)&FailureActions))
164  {
165  _tprintf(_T("[SC] ChangeServiceConfig2 FAILED %lu:\n\n"), GetLastError());
166  bResult = FALSE;
167  goto done;
168  }
169 
170  _tprintf(_T("[SC] ChangeServiceConfig2 SUCCESS\n\n"));
171 
172 done:
174 
175  if (bResult == FALSE)
176  ReportLastError();
177 
178  if (FailureActions.lpsaActions != NULL)
179  HeapFree(GetProcessHeap(), 0, FailureActions.lpsaActions);
180 
181  if (hService)
182  CloseServiceHandle(hService);
183 
184  if (hManager)
185  CloseServiceHandle(hManager);
186 
187  return bResult;
188 }
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1635
NTSYSAPI NTSTATUS NTAPI RtlAdjustPrivilege(_In_ ULONG Privilege, _In_ BOOLEAN NewValue, _In_ BOOLEAN ForThread, _Out_ PBOOLEAN OldValue)
#define OpenService
Definition: winsvc.h:576
unsigned char * LPBYTE
Definition: typedefs.h:52
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
VOID SetFailureUsage(VOID)
Definition: usage.c:249
#define SE_SHUTDOWN_PRIVILEGE
Definition: security.c:673
BOOL ParseFailureArguments(IN LPCTSTR *ServiceArgs, IN INT ArgCount, OUT LPCTSTR *ppServiceName, OUT LPSERVICE_FAILURE_ACTIONS pFailureActions)
Definition: misc.c:259
#define OpenSCManager
Definition: winsvc.h:575
#define _T(x)
Definition: vfdio.h:22
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GetProcessHeap()
Definition: compat.h:395
#define SERVICE_CHANGE_CONFIG
Definition: winsvc.h:54
#define SERVICE_CONFIG_FAILURE_ACTIONS
Definition: winsvc.h:66
#define SERVICE_START
Definition: winsvc.h:57
VOID ReportLastError(VOID)
Definition: sc.c:15
#define ChangeServiceConfig2
Definition: winsvc.h:568
#define HeapFree(x, y, z)
Definition: compat.h:394