ReactOS  0.4.13-dev-39-g8b6696f
failure.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: base/applications/sc/failure.c
5  * PURPOSE: Query/Set the service failure actions
6  * COPYRIGHT: Copyright 2016 Eric Kohl
7  */
8 
9 #include "sc.h"
10 
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 }
118 
119 BOOL
121  LPCTSTR *ServiceArgs,
122  INT ArgCount)
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
BOOL SetFailure(LPCTSTR *ServiceArgs, INT ArgCount)
Definition: failure.c:120
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)
int32_t INT
Definition: typedefs.h:56
#define OpenService
Definition: winsvc.h:576
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned char * LPBYTE
Definition: typedefs.h:52
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define QueryServiceConfig2
Definition: winsvc.h:581
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
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define SERVICE_CHANGE_CONFIG
Definition: winsvc.h:54
#define SERVICE_CONFIG_FAILURE_ACTIONS
Definition: winsvc.h:66
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
BOOL QueryFailure(LPCTSTR ServiceName)
Definition: failure.c:11
#define SERVICE_START
Definition: winsvc.h:57
VOID ReportLastError(VOID)
Definition: sc.c:15
#define SERVICE_QUERY_CONFIG
Definition: winsvc.h:53
#define ChangeServiceConfig2
Definition: winsvc.h:568
#define HeapFree(x, y, z)
Definition: compat.h:394
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13