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
43 int WINAPI
RegisterServicesProcess(DWORD ServicesProcessId
);
45 /* GLOBALS ******************************************************************/
47 #define PIPE_BUFSIZE 1024
48 #define PIPE_TIMEOUT 1000
51 /* FUNCTIONS *****************************************************************/
54 PrintString(LPCSTR fmt
, ...)
61 vsprintf(buffer
, fmt
, ap
);
64 OutputDebugStringA(buffer
);
70 ScmCreateStartEvent(PHANDLE StartEvent
)
74 hEvent
= CreateEvent(NULL
,
77 _T("SvcctrlStartEvent_A3725DX"));
80 if (GetLastError() == ERROR_ALREADY_EXISTS
)
82 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
84 _T("SvcctrlStartEvent_A3725DX"));
103 ScmNamedPipeHandleRequest(PVOID Request
,
108 DbgPrint("SCM READ: %s\n", Request
);
116 ScmNamedPipeThread(LPVOID Context
)
118 CHAR chRequest
[PIPE_BUFSIZE
];
119 CHAR chReply
[PIPE_BUFSIZE
];
126 hPipe
= (HANDLE
)Context
;
128 DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe
);
131 fSuccess
= ReadFile(hPipe
,
136 if (!fSuccess
|| cbBytesRead
== 0) {
139 if (ScmNamedPipeHandleRequest(&chRequest
, cbBytesRead
, &chReply
, &cbReplyBytes
)) {
140 fSuccess
= WriteFile(hPipe
,
145 if (!fSuccess
|| cbReplyBytes
!= cbWritten
) {
150 DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe
);
151 FlushFileBuffers(hPipe
);
152 DisconnectNamedPipe(hPipe
);
154 DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe
);
155 return ERROR_SUCCESS
;
160 ScmCreateNamedPipe(VOID
)
167 DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
169 hPipe
= CreateNamedPipe(_T("\\\\.\\pipe\\Ntsvcs"),
171 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
172 PIPE_UNLIMITED_INSTANCES
,
177 if (hPipe
== INVALID_HANDLE_VALUE
) {
178 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
182 DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe
);
183 fConnected
= ConnectNamedPipe(hPipe
,
184 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
185 DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", fConnected
);
188 DPRINT("Pipe connected\n");
189 hThread
= CreateThread(NULL
,
196 DPRINT("Could not create thread (%d)\n", GetLastError());
197 DisconnectNamedPipe(hPipe
);
199 DPRINT("CreateNamedPipe() - returning FALSE\n");
203 DPRINT("Pipe not connected\n");
205 DPRINT("CreateNamedPipe() - returning FALSE\n");
208 DPRINT("CreateNamedPipe() - returning TRUE\n");
214 ScmNamedPipeListenerThread(LPVOID Context
)
217 DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context
);
219 // hPipe = (HANDLE)Context;
221 DPRINT("SCM: Waiting for new connection on named pipe...\n");
222 /* Create named pipe */
223 if (!ScmCreateNamedPipe()) {
224 DPRINT1("\nSCM: Failed to create named pipe\n");
228 DPRINT("\nSCM: named pipe session created.\n");
231 DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context
);
232 return ERROR_SUCCESS
;
237 StartScmNamedPipeThreadListener(VOID
)
242 hThread
= CreateThread(NULL
,
244 ScmNamedPipeListenerThread
,
245 NULL
, /*(LPVOID)hPipe,*/
250 DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
259 AcquireLoadDriverPrivilege(VOID
)
262 TOKEN_PRIVILEGES tkp
;
264 /* Get a token for this process */
265 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
267 /* Get the LUID for the debug privilege */
268 LookupPrivilegeValue(NULL
, SE_LOAD_DRIVER_NAME
, &tkp
.Privileges
[0].Luid
);
270 /* One privilege to set */
271 tkp
.PrivilegeCount
= 1;
272 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
274 /* Get the debug privilege for this process */
275 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
281 WinMain(HINSTANCE hInstance
,
282 HINSTANCE hPrevInstance
,
286 HANDLE hScmStartEvent
;
290 DPRINT("SERVICES: Service Control Manager\n");
292 /* Acquire privileges to load drivers */
293 AcquireLoadDriverPrivilege();
295 /* Create start event */
296 if (!ScmCreateStartEvent(&hScmStartEvent
))
298 DPRINT1("SERVICES: Failed to create start event\n");
302 DPRINT("SERVICES: created start event with handle %x.\n", hScmStartEvent
);
304 // ScmInitThreadManager();
306 /* FIXME: more initialization */
309 /* Create the service database */
310 Status
= ScmCreateServiceDataBase();
311 if (!NT_SUCCESS(Status
))
313 DPRINT1("SERVICES: failed to create SCM database (Status %lx)\n", Status
);
317 /* Update service database */
318 ScmGetBootAndSystemDriverState();
320 /* Start the RPC server */
323 /* Register service process with CSRSS */
324 // RegisterServicesProcess(GetCurrentProcessId());
326 DPRINT("SERVICES: Initialized.\n");
328 /* Signal start event */
329 SetEvent(hScmStartEvent
);
332 /* FIXME: register event handler (used for system shutdown) */
333 SetConsoleCtrlHandler(...);
336 /* Start auto-start services */
337 ScmAutoStartServices();
339 /* FIXME: more to do ? */
342 DPRINT("SERVICES: Running.\n");
345 hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
346 WaitForSingleObject(hEvent
, INFINITE
);
354 DPRINT("SERVICES: Finished.\n");