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
11 BOOL
QueryFailure(LPCTSTR ServiceName
)
13 SC_HANDLE hManager
= NULL
;
14 SC_HANDLE hService
= NULL
;
16 DWORD cbBytesNeeded
= 0;
17 LPSERVICE_FAILURE_ACTIONS pServiceFailure
= NULL
;
21 _tprintf(_T("service to show failure action - %s\n\n"), ServiceName
);
24 hManager
= OpenSCManager(NULL
,
33 hService
= OpenService(hManager
, ServiceName
, SERVICE_QUERY_CONFIG
);
40 if (!QueryServiceConfig2(hService
,
41 SERVICE_CONFIG_FAILURE_ACTIONS
,
46 if (cbBytesNeeded
== 0)
53 pServiceFailure
= HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded
);
54 if (pServiceFailure
== NULL
)
56 SetLastError(ERROR_OUTOFMEMORY
);
61 if (!QueryServiceConfig2(hService
,
62 SERVICE_CONFIG_FAILURE_ACTIONS
,
63 (LPBYTE
)pServiceFailure
,
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
++)
80 switch (pServiceFailure
->lpsaActions
[i
].Type
)
85 case SC_ACTION_RESTART
:
86 _tprintf(_T("RESTART -- Delay = %lu milliseconds.\n"), pServiceFailure
->lpsaActions
[i
].Delay
);
89 case SC_ACTION_REBOOT
:
90 _tprintf(_T("REBOOT -- Delay = %lu milliseconds.\n"), pServiceFailure
->lpsaActions
[i
].Delay
);
93 case SC_ACTION_RUN_COMMAND
:
94 _tprintf(_T("RUN_COMMAND -- Delay = %lu milliseconds.\n"), pServiceFailure
->lpsaActions
[i
].Delay
);
104 if (bResult
== FALSE
)
107 if (pServiceFailure
!= NULL
)
108 HeapFree(GetProcessHeap(), 0, pServiceFailure
);
111 CloseServiceHandle(hService
);
114 CloseServiceHandle(hManager
);
121 LPCTSTR
*ServiceArgs
,
124 SC_HANDLE hManager
= NULL
;
125 SC_HANDLE hService
= NULL
;
127 SERVICE_FAILURE_ACTIONS FailureActions
;
128 LPCTSTR lpServiceName
= NULL
;
131 ZeroMemory(&FailureActions
, sizeof(SERVICE_FAILURE_ACTIONS
));
133 if (!ParseFailureArguments(ServiceArgs
, ArgCount
, &lpServiceName
, &FailureActions
))
139 hManager
= OpenSCManager(NULL
,
142 if (hManager
== NULL
)
144 _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
149 hService
= OpenService(hManager
,
151 SERVICE_CHANGE_CONFIG
| SERVICE_START
);
152 if (hService
== NULL
)
154 _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
159 RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE
, TRUE
, FALSE
, &Old
);
161 if (!ChangeServiceConfig2(hService
,
162 SERVICE_CONFIG_FAILURE_ACTIONS
,
163 (LPBYTE
)&FailureActions
))
165 _tprintf(_T("[SC] ChangeServiceConfig2 FAILED %lu:\n\n"), GetLastError());
170 _tprintf(_T("[SC] ChangeServiceConfig2 SUCCESS\n\n"));
173 RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE
, Old
, FALSE
, &Old
);
175 if (bResult
== FALSE
)
178 if (FailureActions
.lpsaActions
!= NULL
)
179 HeapFree(GetProcessHeap(), 0, FailureActions
.lpsaActions
);
182 CloseServiceHandle(hService
);
185 CloseServiceHandle(hManager
);