2 * PROJECT: ReactOS Service Control Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/services/services.c
5 * PURPOSE: Main SCM controller
6 * COPYRIGHT: Copyright 2001-2005 Eric Kohl
7 * Copyright 2007 Ged Murphy <gedmurphy@reactos.org>
12 * - Services.exe is NOT a native application, it is a GUI app.
15 /* INCLUDES *****************************************************************/
22 int WINAPI
RegisterServicesProcess(DWORD ServicesProcessId
);
24 /* GLOBALS ******************************************************************/
26 #define PIPE_BUFSIZE 1024
27 #define PIPE_TIMEOUT 1000
29 BOOL ScmShutdown
= FALSE
;
30 static HANDLE hScmShutdownEvent
= NULL
;
33 /* FUNCTIONS *****************************************************************/
36 PrintString(LPCSTR fmt
, ...)
43 vsprintf(buffer
, fmt
, ap
);
46 OutputDebugStringA(buffer
);
52 ScmLogError(DWORD dwEventId
,
58 hLog
= RegisterEventSourceW(NULL
,
59 L
"Service Control Manager");
62 DPRINT1("ScmLogEvent: RegisterEventSourceW failed %d\n", GetLastError());
66 if (!ReportEventW(hLog
,
76 DPRINT1("ScmLogEvent: ReportEventW failed %d\n", GetLastError());
79 DeregisterEventSource(hLog
);
84 ScmCreateStartEvent(PHANDLE StartEvent
)
88 hEvent
= CreateEvent(NULL
,
91 TEXT("SvcctrlStartEvent_A3752DX"));
94 if (GetLastError() == ERROR_ALREADY_EXISTS
)
96 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
98 TEXT("SvcctrlStartEvent_A3752DX"));
110 *StartEvent
= hEvent
;
117 ScmWaitForLsass(VOID
)
122 hEvent
= CreateEventW(NULL
,
125 L
"LSA_RPC_SERVER_ACTIVE");
128 dwError
= GetLastError();
129 DPRINT("Failed to create the notication event (Error %lu)\n", dwError
);
131 if (dwError
== ERROR_ALREADY_EXISTS
)
133 hEvent
= OpenEventW(SYNCHRONIZE
,
135 L
"LSA_RPC_SERVER_ACTIVE");
138 DPRINT1("Could not open the notification event (Error %lu)\n", GetLastError());
144 DPRINT("Wait for the LSA server!\n");
145 WaitForSingleObject(hEvent
, INFINITE
);
146 DPRINT("LSA server running!\n");
153 ScmNamedPipeHandleRequest(PVOID Request
,
158 DbgPrint("SCM READ: %s\n", Request
);
166 ScmNamedPipeThread(LPVOID Context
)
168 CHAR chRequest
[PIPE_BUFSIZE
];
169 CHAR chReply
[PIPE_BUFSIZE
];
176 hPipe
= (HANDLE
)Context
;
178 DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe
);
182 bSuccess
= ReadFile(hPipe
,
187 if (!bSuccess
|| cbBytesRead
== 0)
192 if (ScmNamedPipeHandleRequest(&chRequest
, cbBytesRead
, &chReply
, &cbReplyBytes
))
194 bSuccess
= WriteFile(hPipe
,
199 if (!bSuccess
|| cbReplyBytes
!= cbWritten
)
206 DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe
);
208 FlushFileBuffers(hPipe
);
209 DisconnectNamedPipe(hPipe
);
212 DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe
);
214 return ERROR_SUCCESS
;
219 ScmCreateNamedPipe(VOID
)
226 DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
228 hPipe
= CreateNamedPipe(TEXT("\\\\.\\pipe\\Ntsvcs"),
230 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
231 PIPE_UNLIMITED_INSTANCES
,
236 if (hPipe
== INVALID_HANDLE_VALUE
)
238 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
242 DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe
);
243 bConnected
= ConnectNamedPipe(hPipe
,
244 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
245 DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", bConnected
);
249 DPRINT("Pipe connected\n");
250 hThread
= CreateThread(NULL
,
258 DPRINT("Could not create thread (%d)\n", GetLastError());
259 DisconnectNamedPipe(hPipe
);
261 DPRINT("CreateNamedPipe() - returning FALSE\n");
265 CloseHandle(hThread
);
269 DPRINT("Pipe not connected\n");
271 DPRINT("CreateNamedPipe() - returning FALSE\n");
274 DPRINT("CreateNamedPipe() - returning TRUE\n");
280 ScmNamedPipeListenerThread(LPVOID Context
)
283 DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context
);
285 // hPipe = (HANDLE)Context;
288 DPRINT("SCM: Waiting for new connection on named pipe...\n");
289 /* Create named pipe */
290 if (!ScmCreateNamedPipe())
292 DPRINT1("\nSCM: Failed to create named pipe\n");
296 DPRINT("\nSCM: named pipe session created.\n");
299 DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context
);
300 return ERROR_SUCCESS
;
305 StartScmNamedPipeThreadListener(VOID
)
310 hThread
= CreateThread(NULL
,
312 ScmNamedPipeListenerThread
,
313 NULL
, /*(LPVOID)hPipe,*/
318 DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
322 CloseHandle(hThread
);
329 AcquireLoadDriverPrivilege(VOID
)
332 TOKEN_PRIVILEGES tkp
;
334 /* Get a token for this process */
335 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
337 /* Get the LUID for the debug privilege */
338 LookupPrivilegeValue(NULL
, SE_LOAD_DRIVER_NAME
, &tkp
.Privileges
[0].Luid
);
340 /* One privilege to set */
341 tkp
.PrivilegeCount
= 1;
342 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
344 /* Get the debug privilege for this process */
345 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
351 ShutdownHandlerRoutine(DWORD dwCtrlType
)
353 DPRINT1("ShutdownHandlerRoutine() called\n");
355 if (dwCtrlType
& (CTRL_SHUTDOWN_EVENT
| CTRL_LOGOFF_EVENT
))
357 DPRINT1("Shutdown event received!\n");
360 ScmAutoShutdownServices();
361 ScmShutdownServiceDatabase();
363 /* Set the shutdwon event */
364 SetEvent(hScmShutdownEvent
);
372 wWinMain(HINSTANCE hInstance
,
373 HINSTANCE hPrevInstance
,
377 HANDLE hScmStartEvent
;
380 DPRINT("SERVICES: Service Control Manager\n");
382 /* Create start event */
383 if (!ScmCreateStartEvent(&hScmStartEvent
))
385 DPRINT1("SERVICES: Failed to create start event\n");
389 DPRINT("SERVICES: created start event with handle %p.\n", hScmStartEvent
);
391 // ScmInitThreadManager();
393 /* FIXME: more initialization */
396 /* Create the service database */
397 dwError
= ScmCreateServiceDatabase();
398 if (dwError
!= ERROR_SUCCESS
)
400 DPRINT1("SERVICES: failed to create SCM database (Error %lu)\n", dwError
);
401 CloseHandle(hScmStartEvent
);
405 /* Update service database */
406 ScmGetBootAndSystemDriverState();
408 /* Start the RPC server */
411 /* Register service process with CSRSS */
412 RegisterServicesProcess(GetCurrentProcessId());
414 DPRINT("SERVICES: Initialized.\n");
416 /* Signal start event */
417 SetEvent(hScmStartEvent
);
419 /* Register event handler (used for system shutdown) */
420 SetConsoleCtrlHandler(ShutdownHandlerRoutine
, TRUE
);
422 /* Wait for the LSA server */
425 /* Acquire privileges to load drivers */
426 AcquireLoadDriverPrivilege();
428 ScmInitNamedPipeCriticalSection();
430 /* Start auto-start services */
431 ScmAutoStartServices();
433 /* FIXME: more to do ? */
436 DPRINT("SERVICES: Running.\n");
438 /* Create the shutdown event and wait until it gets set */
439 hScmShutdownEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
440 if (hScmShutdownEvent
)
441 WaitForSingleObject(hScmShutdownEvent
, INFINITE
);
443 ScmDeleteNamedPipeCriticalSection();
445 /* Close the shutdown event */
446 CloseHandle(hScmShutdownEvent
);
448 /* Close the start event */
449 CloseHandle(hScmStartEvent
);
451 DPRINT("SERVICES: Finished.\n");