ReactOS 0.4.16-dev-92-g0c2cdca
config.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/config.c
5 * PURPOSE: Query/Set the service configuration
6 * COPYRIGHT: Copyright 2016 Eric Kohl
7 *
8 */
9
10#include "sc.h"
11
13{
14 SC_HANDLE hManager = NULL;
15 SC_HANDLE hService = NULL;
16 BOOL bResult = TRUE;
17 DWORD cbBytesNeeded = 0;
18 LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
19 LPWSTR lpPtr;
20 SSIZE_T nLen, i;
21
22#ifdef SCDBG
23 _tprintf(_T("service to show configuration - %s\n\n"), ServiceName);
24#endif
25
26 hManager = OpenSCManager(NULL,
27 NULL,
29 if (hManager == NULL)
30 {
31 _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
32 bResult = FALSE;
33 goto done;
34 }
35
36 hService = OpenService(hManager, ServiceName, SERVICE_QUERY_CONFIG);
37 if (hService == NULL)
38 {
39 _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
40 bResult = FALSE;
41 goto done;
42 }
43
44 if (!QueryServiceConfig(hService,
45 NULL,
46 0,
47 &cbBytesNeeded))
48 {
49 if (cbBytesNeeded == 0)
50 {
51 _tprintf(_T("[SC] QueryServiceConfig FAILED %lu:\n\n"), GetLastError());
52 bResult = FALSE;
53 goto done;
54 }
55 }
56
57 pServiceConfig = HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded);
58 if (pServiceConfig == NULL)
59 {
61 _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
62 bResult = FALSE;
63 goto done;
64 }
65
66 if (!QueryServiceConfig(hService,
67 pServiceConfig,
68 cbBytesNeeded,
69 &cbBytesNeeded))
70 {
71 _tprintf(_T("[SC] QueryServiceConfig FAILED %lu:\n\n"), GetLastError());
72 bResult = FALSE;
73 goto done;
74 }
75
76 _tprintf(_T("[SC] QueryServiceConfig SUCCESS\n\n"));
77
78 _tprintf(_T("SERVICE_NAME: %s\n"), ServiceName);
79 _tprintf(_T(" TYPE : %-3lx "), pServiceConfig->dwServiceType);
80 switch (pServiceConfig->dwServiceType)
81 {
83 _tprintf(_T("KERNEL_DRIVER\n"));
84 break;
85
87 _tprintf(_T("FILE_SYSTEM_DRIVER\n"));
88 break;
89
91 _tprintf(_T("WIN32_OWN_PROCESS\n"));
92 break;
93
95 _tprintf(_T("WIN32_SHARE_PROCESS\n"));
96 break;
97
99 _tprintf(_T("WIN32_OWN_PROCESS (interactive)\n"));
100 break;
101
103 _tprintf(_T("WIN32_SHARE_PROCESS (interactive)\n"));
104 break;
105
106 default:
107 _tprintf(_T("\n"));
108 break;
109 }
110
111 _tprintf(_T(" START_TYPE : %-3lx "), pServiceConfig->dwStartType);
112 switch (pServiceConfig->dwStartType)
113 {
115 _tprintf(_T("BOOT_START\n"));
116 break;
117
119 _tprintf(_T("SYSTEM_START\n"));
120 break;
121
123 _tprintf(_T("AUTO_START\n"));
124 break;
125
127 _tprintf(_T("DEMAND_START\n"));
128 break;
129
130 case SERVICE_DISABLED:
131 _tprintf(_T("DISABLED\n"));
132 break;
133
134 default:
135 _tprintf(_T("\n"));
136 break;
137 }
138
139 _tprintf(_T(" ERROR_CONTROL : %-3lx "), pServiceConfig->dwErrorControl);
140 switch (pServiceConfig->dwErrorControl)
141 {
143 _tprintf(_T("IGNORE\n"));
144 break;
145
147 _tprintf(_T("NORMAL\n"));
148 break;
149
151 _tprintf(_T("SEVERE\n"));
152 break;
153
155 _tprintf(_T("CRITICAL\n"));
156 break;
157
158 default:
159 _tprintf(_T("\n"));
160 break;
161 }
162
163 _tprintf(_T(" BINARY_PATH_NAME : %s\n"), pServiceConfig->lpBinaryPathName);
164 _tprintf(_T(" LOAD_ORDER_GROUP : %s\n"), pServiceConfig->lpLoadOrderGroup);
165 _tprintf(_T(" TAG : %lu\n"), pServiceConfig->dwTagId);
166 _tprintf(_T(" DISPLAY_NAME : %s\n"), pServiceConfig->lpDisplayName);
167 _tprintf(_T(" DEPENDENCIES : "));
168 lpPtr = pServiceConfig->lpDependencies;
169 i = 0;
170 while (*lpPtr != _T('\0'))
171 {
172 nLen = _tcslen(lpPtr);
173 if (i != 0)
174 _tprintf(_T("\n : "));
175 _tprintf(_T("%s"), lpPtr);
176 lpPtr = lpPtr + nLen + 1;
177 i++;
178 }
179 _tprintf(_T("\n"));
180
181 _tprintf(_T(" SERVICE_START_NAME : %s\n"), pServiceConfig->lpServiceStartName);
182
183done:
184 if (bResult == FALSE)
186
187 if (pServiceConfig != NULL)
188 HeapFree(GetProcessHeap(), 0, pServiceConfig);
189
190 if (hService)
191 CloseServiceHandle(hService);
192
193 if (hManager)
194 CloseServiceHandle(hManager);
195
196 return bResult;
197}
198
199BOOL SetConfig(LPCTSTR *ServiceArgs, INT ArgCount)
200{
201 SC_HANDLE hManager = NULL;
202 SC_HANDLE hService = NULL;
203 BOOL bResult = TRUE;
205
206 if (!ParseCreateConfigArguments(ServiceArgs, ArgCount, TRUE, &ServiceInfo))
207 {
209 return FALSE;
210 }
211
212#ifdef SCDBG
213 _tprintf(_T("service name - %s\n"), ServiceInfo.lpServiceName);
214 _tprintf(_T("display name - %s\n"), ServiceInfo.lpDisplayName);
215 _tprintf(_T("service type - %lu\n"), ServiceInfo.dwServiceType);
216 _tprintf(_T("start type - %lu\n"), ServiceInfo.dwStartType);
217 _tprintf(_T("error control - %lu\n"), ServiceInfo.dwErrorControl);
218 _tprintf(_T("Binary path - %s\n"), ServiceInfo.lpBinaryPathName);
219 _tprintf(_T("load order group - %s\n"), ServiceInfo.lpLoadOrderGroup);
220 _tprintf(_T("tag - %lu\n"), ServiceInfo.dwTagId);
221 _tprintf(_T("dependencies - %s\n"), ServiceInfo.lpDependencies);
222 _tprintf(_T("account start name - %s\n"), ServiceInfo.lpServiceStartName);
223 _tprintf(_T("account password - %s\n"), ServiceInfo.lpPassword);
224#endif
225
226 hManager = OpenSCManager(NULL,
227 NULL,
229 if (hManager == NULL)
230 {
231 _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
232 bResult = FALSE;
233 goto done;
234 }
235
236 hService = OpenService(hManager,
237 ServiceInfo.lpServiceName,
239 if (hService == NULL)
240 {
241 _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
242 bResult = FALSE;
243 goto done;
244 }
245
246 if (!ChangeServiceConfig(hService,
247 ServiceInfo.dwServiceType,
248 ServiceInfo.dwStartType,
249 ServiceInfo.dwErrorControl,
250 ServiceInfo.lpBinaryPathName,
251 ServiceInfo.lpLoadOrderGroup,
252 ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
253 ServiceInfo.lpDependencies,
254 ServiceInfo.lpServiceStartName,
255 ServiceInfo.lpPassword,
256 ServiceInfo.lpDisplayName))
257 {
258 _tprintf(_T("[SC] ChangeServiceConfig FAILED %lu:\n\n"), GetLastError());
259 bResult = FALSE;
260 goto done;
261 }
262
263 _tprintf(_T("[SC] ChangeServiceConfig SUCCESS\n\n"));
264
265done:
266 if (bResult == FALSE)
268
269 if (hService)
270 CloseServiceHandle(hService);
271
272 if (hManager)
273 CloseServiceHandle(hManager);
274
275 return bResult;
276}
BOOL QueryConfig(LPCTSTR ServiceName)
Definition: config.c:12
BOOL SetConfig(LPCTSTR *ServiceArgs, INT ArgCount)
Definition: config.c:199
BOOL ParseCreateConfigArguments(LPCTSTR *ServiceArgs, INT ArgCount, BOOL bChangeService, OUT LPSERVICE_CREATE_INFO lpServiceInfo)
Definition: misc.c:53
static WCHAR ServiceName[]
Definition: browser.c:19
LONG_PTR SSIZE_T
Definition: basetsd.h:181
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define SetLastError(x)
Definition: compat.h:752
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
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
#define _tprintf
Definition: tchar.h:506
DWORD ReportLastError(void)
Definition: loadlib.c:67
VOID SetConfigUsage(VOID)
Definition: usage.c:229
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
LPSTR lpServiceStartName
Definition: winsvc.h:152
int32_t INT
Definition: typedefs.h:58
JS_SERVICE_STATE ServiceInfo(LPCTSTR ServiceName)
Return service status by service name.
Definition: user_lib.cpp:157
#define _T(x)
Definition: vfdio.h:22
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define QueryServiceConfig
Definition: winsvc.h:580
#define OpenSCManager
Definition: winsvc.h:575
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
#define SERVICE_CHANGE_CONFIG
Definition: winsvc.h:54
#define ChangeServiceConfig
Definition: winsvc.h:567
#define SERVICE_QUERY_CONFIG
Definition: winsvc.h:53
#define OpenService
Definition: winsvc.h:576
#define SERVICE_DEMAND_START
Definition: cmtypes.h:978
#define SERVICE_KERNEL_DRIVER
Definition: cmtypes.h:953
#define SERVICE_WIN32_SHARE_PROCESS
Definition: cmtypes.h:963
#define SERVICE_ERROR_SEVERE
Definition: cmtypes.h:983
#define SERVICE_INTERACTIVE_PROCESS
Definition: cmtypes.h:967
#define SERVICE_DISABLED
Definition: cmtypes.h:979
#define SERVICE_AUTO_START
Definition: cmtypes.h:977
#define SERVICE_BOOT_START
Definition: cmtypes.h:975
#define SERVICE_ERROR_CRITICAL
Definition: cmtypes.h:984
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:962
#define SERVICE_SYSTEM_START
Definition: cmtypes.h:976
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:954
#define SERVICE_ERROR_IGNORE
Definition: cmtypes.h:981
#define SERVICE_ERROR_NORMAL
Definition: cmtypes.h:982
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define _tcslen
Definition: xmlstorage.h:198