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
;
32 /* FUNCTIONS *****************************************************************/
35 PrintString(LPCSTR fmt
, ...)
42 vsprintf(buffer
, fmt
, ap
);
45 OutputDebugStringA(buffer
);
51 ScmCreateStartEvent(PHANDLE StartEvent
)
55 hEvent
= CreateEvent(NULL
,
58 TEXT("SvcctrlStartEvent_A3752DX"));
61 if (GetLastError() == ERROR_ALREADY_EXISTS
)
63 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
65 TEXT("SvcctrlStartEvent_A3752DX"));
84 ScmNamedPipeHandleRequest(PVOID Request
,
89 DbgPrint("SCM READ: %s\n", Request
);
97 ScmNamedPipeThread(LPVOID Context
)
99 CHAR chRequest
[PIPE_BUFSIZE
];
100 CHAR chReply
[PIPE_BUFSIZE
];
107 hPipe
= (HANDLE
)Context
;
109 DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe
);
113 bSuccess
= ReadFile(hPipe
,
118 if (!bSuccess
|| cbBytesRead
== 0)
123 if (ScmNamedPipeHandleRequest(&chRequest
, cbBytesRead
, &chReply
, &cbReplyBytes
))
125 bSuccess
= WriteFile(hPipe
,
130 if (!bSuccess
|| cbReplyBytes
!= cbWritten
)
137 DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe
);
139 FlushFileBuffers(hPipe
);
140 DisconnectNamedPipe(hPipe
);
143 DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe
);
145 return ERROR_SUCCESS
;
150 ScmCreateNamedPipe(VOID
)
157 DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
159 hPipe
= CreateNamedPipe(TEXT("\\\\.\\pipe\\Ntsvcs"),
161 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
162 PIPE_UNLIMITED_INSTANCES
,
167 if (hPipe
== INVALID_HANDLE_VALUE
)
169 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
173 DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe
);
174 bConnected
= ConnectNamedPipe(hPipe
,
175 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
176 DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", bConnected
);
180 DPRINT("Pipe connected\n");
181 hThread
= CreateThread(NULL
,
189 DPRINT("Could not create thread (%d)\n", GetLastError());
190 DisconnectNamedPipe(hPipe
);
192 DPRINT("CreateNamedPipe() - returning FALSE\n");
198 DPRINT("Pipe not connected\n");
200 DPRINT("CreateNamedPipe() - returning FALSE\n");
203 DPRINT("CreateNamedPipe() - returning TRUE\n");
209 ScmNamedPipeListenerThread(LPVOID Context
)
212 DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context
);
214 // hPipe = (HANDLE)Context;
217 DPRINT("SCM: Waiting for new connection on named pipe...\n");
218 /* Create named pipe */
219 if (!ScmCreateNamedPipe())
221 DPRINT1("\nSCM: Failed to create named pipe\n");
225 DPRINT("\nSCM: named pipe session created.\n");
228 DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context
);
229 return ERROR_SUCCESS
;
234 StartScmNamedPipeThreadListener(VOID
)
239 hThread
= CreateThread(NULL
,
241 ScmNamedPipeListenerThread
,
242 NULL
, /*(LPVOID)hPipe,*/
247 DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
256 AcquireLoadDriverPrivilege(VOID
)
259 TOKEN_PRIVILEGES tkp
;
261 /* Get a token for this process */
262 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
264 /* Get the LUID for the debug privilege */
265 LookupPrivilegeValue(NULL
, SE_LOAD_DRIVER_NAME
, &tkp
.Privileges
[0].Luid
);
267 /* One privilege to set */
268 tkp
.PrivilegeCount
= 1;
269 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
271 /* Get the debug privilege for this process */
272 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
278 ShutdownHandlerRoutine(DWORD dwCtrlType
)
280 DPRINT1("ShutdownHandlerRoutine() called\n");
282 if (dwCtrlType
& (CTRL_SHUTDOWN_EVENT
| CTRL_LOGOFF_EVENT
))
284 DPRINT1("Shutdown event received!\n");
287 ScmAutoShutdownServices();
288 ScmShutdownServiceDatabase();
296 wWinMain(HINSTANCE hInstance
,
297 HINSTANCE hPrevInstance
,
301 HANDLE hScmStartEvent
;
305 DPRINT("SERVICES: Service Control Manager\n");
307 /* Acquire privileges to load drivers */
308 AcquireLoadDriverPrivilege();
310 /* Create start event */
311 if (!ScmCreateStartEvent(&hScmStartEvent
))
313 DPRINT1("SERVICES: Failed to create start event\n");
317 DPRINT("SERVICES: created start event with handle %x.\n", hScmStartEvent
);
319 // ScmInitThreadManager();
321 /* FIXME: more initialization */
324 /* Create the service database */
325 dwError
= ScmCreateServiceDatabase();
326 if (dwError
!= ERROR_SUCCESS
)
328 DPRINT1("SERVICES: failed to create SCM database (Error %lu)\n", dwError
);
332 /* Update service database */
333 ScmGetBootAndSystemDriverState();
335 /* Start the RPC server */
338 /* Register service process with CSRSS */
339 RegisterServicesProcess(GetCurrentProcessId());
341 DPRINT("SERVICES: Initialized.\n");
343 /* Signal start event */
344 SetEvent(hScmStartEvent
);
346 /* Register event handler (used for system shutdown) */
347 SetConsoleCtrlHandler(ShutdownHandlerRoutine
, TRUE
);
349 /* Start auto-start services */
350 ScmAutoStartServices();
352 /* FIXME: more to do ? */
355 DPRINT("SERVICES: Running.\n");
358 hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
360 WaitForSingleObject(hEvent
, INFINITE
);
368 CloseHandle(hScmStartEvent
);
370 DPRINT("SERVICES: Finished.\n");