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 *****************************************************************/
37 int WINAPI
RegisterServicesProcess(DWORD ServicesProcessId
);
39 /* GLOBALS ******************************************************************/
41 #define PIPE_BUFSIZE 1024
42 #define PIPE_TIMEOUT 1000
45 /* FUNCTIONS *****************************************************************/
48 PrintString(LPCSTR fmt
, ...)
55 vsprintf(buffer
, fmt
, ap
);
58 OutputDebugStringA(buffer
);
64 ScmCreateStartEvent(PHANDLE StartEvent
)
68 hEvent
= CreateEvent(NULL
,
71 TEXT("SvcctrlStartEvent_A3725DX"));
74 if (GetLastError() == ERROR_ALREADY_EXISTS
)
76 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
78 TEXT("SvcctrlStartEvent_A3725DX"));
97 ScmNamedPipeHandleRequest(PVOID Request
,
102 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
;
154 ScmCreateNamedPipe(VOID
)
161 DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
163 hPipe
= CreateNamedPipe(TEXT("\\\\.\\pipe\\Ntsvcs"),
165 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
166 PIPE_UNLIMITED_INSTANCES
,
171 if (hPipe
== INVALID_HANDLE_VALUE
) {
172 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
176 DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe
);
177 fConnected
= ConnectNamedPipe(hPipe
,
178 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
179 DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", fConnected
);
182 DPRINT("Pipe connected\n");
183 hThread
= CreateThread(NULL
,
190 DPRINT("Could not create thread (%d)\n", GetLastError());
191 DisconnectNamedPipe(hPipe
);
193 DPRINT("CreateNamedPipe() - returning FALSE\n");
197 DPRINT("Pipe not connected\n");
199 DPRINT("CreateNamedPipe() - returning FALSE\n");
202 DPRINT("CreateNamedPipe() - returning TRUE\n");
208 ScmNamedPipeListenerThread(LPVOID Context
)
211 DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context
);
213 // hPipe = (HANDLE)Context;
215 DPRINT("SCM: Waiting for new connection on named pipe...\n");
216 /* Create named pipe */
217 if (!ScmCreateNamedPipe()) {
218 DPRINT1("\nSCM: Failed to create named pipe\n");
222 DPRINT("\nSCM: named pipe session created.\n");
225 DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context
);
226 return ERROR_SUCCESS
;
231 StartScmNamedPipeThreadListener(VOID
)
236 hThread
= CreateThread(NULL
,
238 ScmNamedPipeListenerThread
,
239 NULL
, /*(LPVOID)hPipe,*/
244 DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
253 AcquireLoadDriverPrivilege(VOID
)
256 TOKEN_PRIVILEGES tkp
;
258 /* Get a token for this process */
259 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
261 /* Get the LUID for the debug privilege */
262 LookupPrivilegeValue(NULL
, SE_LOAD_DRIVER_NAME
, &tkp
.Privileges
[0].Luid
);
264 /* One privilege to set */
265 tkp
.PrivilegeCount
= 1;
266 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
268 /* Get the debug privilege for this process */
269 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
275 WinMain(HINSTANCE hInstance
,
276 HINSTANCE hPrevInstance
,
280 HANDLE hScmStartEvent
;
284 DPRINT("SERVICES: Service Control Manager\n");
286 /* Acquire privileges to load drivers */
287 AcquireLoadDriverPrivilege();
289 /* Create start event */
290 if (!ScmCreateStartEvent(&hScmStartEvent
))
292 DPRINT1("SERVICES: Failed to create start event\n");
296 DPRINT("SERVICES: created start event with handle %x.\n", hScmStartEvent
);
298 // ScmInitThreadManager();
300 /* FIXME: more initialization */
303 /* Create the service database */
304 Status
= ScmCreateServiceDataBase();
305 if (!NT_SUCCESS(Status
))
307 DPRINT1("SERVICES: failed to create SCM database (Status %lx)\n", Status
);
311 /* Update service database */
312 ScmGetBootAndSystemDriverState();
314 /* Start the RPC server */
317 /* Register service process with CSRSS */
318 // RegisterServicesProcess(GetCurrentProcessId());
320 DPRINT("SERVICES: Initialized.\n");
322 /* Signal start event */
323 SetEvent(hScmStartEvent
);
326 /* FIXME: register event handler (used for system shutdown) */
327 SetConsoleCtrlHandler(...);
330 /* Start auto-start services */
331 ScmAutoStartServices();
333 /* FIXME: more to do ? */
336 DPRINT("SERVICES: Running.\n");
339 hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
340 WaitForSingleObject(hEvent
, INFINITE
);
348 DPRINT("SERVICES: Finished.\n");