2 * service control manager
4 * ReactOS Operating System
6 * --------------------------------------------------------------------
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this software; see the file COPYING.LIB. If not, write
20 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
26 * - Services.exe is NOT a native application, it is a GUI app.
29 /* INCLUDES *****************************************************************/
36 int WINAPI
RegisterServicesProcess(DWORD ServicesProcessId
);
38 /* GLOBALS ******************************************************************/
40 #define PIPE_BUFSIZE 1024
41 #define PIPE_TIMEOUT 1000
43 BOOL ScmShutdown
= FALSE
;
46 /* FUNCTIONS *****************************************************************/
49 PrintString(LPCSTR fmt
, ...)
56 vsprintf(buffer
, fmt
, ap
);
59 OutputDebugStringA(buffer
);
65 ScmCreateStartEvent(PHANDLE StartEvent
)
69 hEvent
= CreateEvent(NULL
,
72 TEXT("SvcctrlStartEvent_A3725DX"));
75 if (GetLastError() == ERROR_ALREADY_EXISTS
)
77 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
79 TEXT("SvcctrlStartEvent_A3725DX"));
98 ScmNamedPipeHandleRequest(PVOID Request
,
103 DbgPrint("SCM READ: %s\n", Request
);
111 ScmNamedPipeThread(LPVOID Context
)
113 CHAR chRequest
[PIPE_BUFSIZE
];
114 CHAR chReply
[PIPE_BUFSIZE
];
121 hPipe
= (HANDLE
)Context
;
123 DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe
);
127 bSuccess
= ReadFile(hPipe
,
132 if (!bSuccess
|| cbBytesRead
== 0)
137 if (ScmNamedPipeHandleRequest(&chRequest
, cbBytesRead
, &chReply
, &cbReplyBytes
))
139 bSuccess
= WriteFile(hPipe
,
144 if (!bSuccess
|| cbReplyBytes
!= cbWritten
)
151 DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe
);
153 FlushFileBuffers(hPipe
);
154 DisconnectNamedPipe(hPipe
);
157 DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe
);
159 return ERROR_SUCCESS
;
164 ScmCreateNamedPipe(VOID
)
171 DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
173 hPipe
= CreateNamedPipe(TEXT("\\\\.\\pipe\\Ntsvcs"),
175 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
176 PIPE_UNLIMITED_INSTANCES
,
181 if (hPipe
== INVALID_HANDLE_VALUE
)
183 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
187 DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe
);
188 bConnected
= ConnectNamedPipe(hPipe
,
189 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
190 DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", bConnected
);
194 DPRINT("Pipe connected\n");
195 hThread
= CreateThread(NULL
,
203 DPRINT("Could not create thread (%d)\n", GetLastError());
204 DisconnectNamedPipe(hPipe
);
206 DPRINT("CreateNamedPipe() - returning FALSE\n");
212 DPRINT("Pipe not connected\n");
214 DPRINT("CreateNamedPipe() - returning FALSE\n");
217 DPRINT("CreateNamedPipe() - returning TRUE\n");
223 ScmNamedPipeListenerThread(LPVOID Context
)
226 DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context
);
228 // hPipe = (HANDLE)Context;
231 DPRINT("SCM: Waiting for new connection on named pipe...\n");
232 /* Create named pipe */
233 if (!ScmCreateNamedPipe())
235 DPRINT1("\nSCM: Failed to create named pipe\n");
239 DPRINT("\nSCM: named pipe session created.\n");
242 DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context
);
243 return ERROR_SUCCESS
;
248 StartScmNamedPipeThreadListener(VOID
)
253 hThread
= CreateThread(NULL
,
255 ScmNamedPipeListenerThread
,
256 NULL
, /*(LPVOID)hPipe,*/
261 DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
270 AcquireLoadDriverPrivilege(VOID
)
273 TOKEN_PRIVILEGES tkp
;
275 /* Get a token for this process */
276 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
278 /* Get the LUID for the debug privilege */
279 LookupPrivilegeValue(NULL
, SE_LOAD_DRIVER_NAME
, &tkp
.Privileges
[0].Luid
);
281 /* One privilege to set */
282 tkp
.PrivilegeCount
= 1;
283 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
285 /* Get the debug privilege for this process */
286 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
292 ShutdownHandlerRoutine(DWORD dwCtrlType
)
294 DPRINT1("ShutdownHandlerRoutine() called\n");
296 if (dwCtrlType
== CTRL_SHUTDOWN_EVENT
)
298 DPRINT1("Shutdown event received!\n");
301 /* FIXME: Shut all services down */
309 WinMain(HINSTANCE hInstance
,
310 HINSTANCE hPrevInstance
,
314 HANDLE hScmStartEvent
;
318 DPRINT("SERVICES: Service Control Manager\n");
320 /* Acquire privileges to load drivers */
321 AcquireLoadDriverPrivilege();
323 /* Create start event */
324 if (!ScmCreateStartEvent(&hScmStartEvent
))
326 DPRINT1("SERVICES: Failed to create start event\n");
330 DPRINT("SERVICES: created start event with handle %x.\n", hScmStartEvent
);
332 // ScmInitThreadManager();
334 /* FIXME: more initialization */
337 /* Create the service database */
338 dwError
= ScmCreateServiceDatabase();
339 if (dwError
!= ERROR_SUCCESS
)
341 DPRINT1("SERVICES: failed to create SCM database (Error %lu)\n", dwError
);
345 /* Update service database */
346 ScmGetBootAndSystemDriverState();
348 /* Start the RPC server */
351 /* Register service process with CSRSS */
352 RegisterServicesProcess(GetCurrentProcessId());
354 DPRINT("SERVICES: Initialized.\n");
356 /* Signal start event */
357 SetEvent(hScmStartEvent
);
359 /* Register event handler (used for system shutdown) */
360 SetConsoleCtrlHandler(ShutdownHandlerRoutine
, TRUE
);
362 /* Start auto-start services */
363 ScmAutoStartServices();
365 /* FIXME: more to do ? */
368 DPRINT("SERVICES: Running.\n");
371 hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
372 WaitForSingleObject(hEvent
, INFINITE
);
380 DPRINT("SERVICES: Finished.\n");