3 * service control manager
5 * ReactOS Operating System
7 * --------------------------------------------------------------------
9 * This software is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This software is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this software; see the file COPYING.LIB. If not, write
21 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
27 * - Services.exe is NOT a native application, it is a GUI app.
30 /* INCLUDES *****************************************************************/
32 #define NTOS_MODE_USER
44 /* GLOBALS ******************************************************************/
46 #define PIPE_BUFSIZE 1024
47 #define PIPE_TIMEOUT 1000
50 /* FUNCTIONS *****************************************************************/
53 PrintString(char* fmt
,...)
60 vsprintf(buffer
, fmt
, ap
);
63 OutputDebugStringA(buffer
);
69 ScmCreateStartEvent(PHANDLE StartEvent
)
73 hEvent
= CreateEvent(NULL
,
76 _T("SvcctrlStartEvent_A3725DX"));
78 if (GetLastError() == ERROR_ALREADY_EXISTS
) {
79 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
81 _T("SvcctrlStartEvent_A3725DX"));
95 ScmNamedPipeHandleRequest(
101 DbgPrint("SCM READ: %s\n", Request
);
110 ScmNamedPipeThread(LPVOID Context
)
112 CHAR chRequest
[PIPE_BUFSIZE
];
113 CHAR chReply
[PIPE_BUFSIZE
];
120 hPipe
= (HANDLE
)Context
;
122 DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe
);
125 fSuccess
= ReadFile(hPipe
,
130 if (!fSuccess
|| cbBytesRead
== 0) {
133 if (ScmNamedPipeHandleRequest(&chRequest
, cbBytesRead
, &chReply
, &cbReplyBytes
)) {
134 fSuccess
= WriteFile(hPipe
,
139 if (!fSuccess
|| cbReplyBytes
!= cbWritten
) {
144 DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe
);
145 FlushFileBuffers(hPipe
);
146 DisconnectNamedPipe(hPipe
);
148 DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe
);
149 return ERROR_SUCCESS
;
152 BOOL
ScmCreateNamedPipe(VOID
)
159 DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
161 hPipe
= CreateNamedPipe("\\\\.\\pipe\\Ntsvcs",
163 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
164 PIPE_UNLIMITED_INSTANCES
,
169 if (hPipe
== INVALID_HANDLE_VALUE
) {
170 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
174 DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe
);
175 fConnected
= ConnectNamedPipe(hPipe
,
176 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
177 DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", fConnected
);
180 DPRINT("Pipe connected\n");
181 hThread
= CreateThread(NULL
,
188 DPRINT("Could not create thread (%d)\n", GetLastError());
189 DisconnectNamedPipe(hPipe
);
191 DPRINT("CreateNamedPipe() - returning FALSE\n");
195 DPRINT("Pipe not connected\n");
197 DPRINT("CreateNamedPipe() - returning FALSE\n");
200 DPRINT("CreateNamedPipe() - returning TRUE\n");
206 ScmNamedPipeListenerThread(LPVOID Context
)
209 DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context
);
211 // hPipe = (HANDLE)Context;
213 DPRINT("SCM: Waiting for new connection on named pipe...\n");
214 /* Create named pipe */
215 if (!ScmCreateNamedPipe()) {
216 DPRINT1("\nSCM: Failed to create named pipe\n");
220 DPRINT("\nSCM: named pipe session created.\n");
223 DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context
);
224 return ERROR_SUCCESS
;
227 BOOL
StartScmNamedPipeThreadListener(void)
232 hThread
= CreateThread(NULL
,
234 ScmNamedPipeListenerThread
,
235 NULL
, /*(LPVOID)hPipe,*/
240 DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
247 AcquireLoadDriverPrivilege(VOID
)
250 TOKEN_PRIVILEGES tkp
;
252 /* Get a token for this process. */
253 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
)) {
254 /* Get the LUID for the debug privilege. */
255 LookupPrivilegeValue(NULL
, SE_LOAD_DRIVER_NAME
, &tkp
.Privileges
[0].Luid
);
257 tkp
.PrivilegeCount
= 1; /* one privilege to set */
258 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
260 /* Get the debug privilege for this process. */
261 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
266 WinMain(HINSTANCE hInstance
,
267 HINSTANCE hPrevInstance
,
271 HANDLE hScmStartEvent
;
275 DPRINT("SERVICES: Service Control Manager\n");
277 /* Acquire privileges to load drivers */
278 AcquireLoadDriverPrivilege();
280 /* Create start event */
281 if (!ScmCreateStartEvent(&hScmStartEvent
))
283 DPRINT1("SERVICES: Failed to create start event\n");
287 DPRINT("SERVICES: created start event with handle %x.\n", hScmStartEvent
);
289 /* FIXME: more initialization */
292 /* Create the service database */
293 Status
= ScmCreateServiceDataBase();
294 if (!NT_SUCCESS(Status
))
296 DPRINT1("SERVICES: failed to create SCM database (Status %lx)\n", Status
);
300 /* Update service database */
301 ScmGetBootAndSystemDriverState();
304 DPRINT("SERVICES: Attempting to create named pipe...\n");
305 /* Create named pipe */
306 if (!ScmCreateNamedPipe()) {
307 DPRINT1("SERVICES: Failed to create named pipe\n");
310 DPRINT("SERVICES: named pipe created successfully.\n");
312 DPRINT("SERVICES: Attempting to create named pipe listener...\n");
313 if (!StartScmNamedPipeThreadListener()) {
314 DPRINT1("SERVICES: Failed to create named pipe listener thread.\n");
317 DPRINT("SERVICES: named pipe listener thread created.\n");
319 /* FIXME: create listener thread for pipe */
322 /* Register service process with CSRSS */
323 RegisterServicesProcess(GetCurrentProcessId());
325 DPRINT("SERVICES: Initialized.\n");
327 /* Signal start event */
328 SetEvent(hScmStartEvent
);
330 /* FIXME: register event handler (used for system shutdown) */
331 // SetConsoleCtrlHandler(...);
334 /* Start auto-start services */
335 ScmAutoStartServices();
337 /* FIXME: more to do ? */
340 DPRINT("SERVICES: Running.\n");
343 hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
344 WaitForSingleObject(hEvent
, INFINITE
);
352 DPRINT("SERVICES: Finished.\n");