1 /* $Id: services.c,v 1.9 2002/10/20 14:54:34 ekohl Exp $
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
44 /* GLOBALS ******************************************************************/
46 #define PIPE_BUFSIZE 1024
47 #define PIPE_TIMEOUT 1000
50 /* FUNCTIONS *****************************************************************/
53 PrintString(char* fmt
,...)
60 vsprintf(buffer
, fmt
, ap
);
63 OutputDebugStringA(buffer
);
69 ScmCreateStartEvent(PHANDLE StartEvent
)
73 hEvent
= CreateEvent(NULL
,
76 _T("SvcctrlStartEvent_A3725DX"));
79 if (GetLastError() == ERROR_ALREADY_EXISTS
)
81 hEvent
= OpenEvent(EVENT_ALL_ACCESS
,
83 _T("SvcctrlStartEvent_A3725DX"));
102 ScmNamedPipeHandleRequest(
108 DbgPrint("SCM READ: %s\n", Request
);
121 CHAR chRequest
[PIPE_BUFSIZE
];
122 CHAR chReply
[PIPE_BUFSIZE
];
129 DPRINT("Accepting SCM commands through named pipe\n");
131 hPipe
= (HANDLE
)Context
;
141 if (!fSuccess
|| cbBytesRead
== 0)
146 if (ScmNamedPipeHandleRequest(&chRequest
, cbBytesRead
, &chReply
, &cbReplyBytes
))
148 fSuccess
= WriteFile(
154 if (!fSuccess
|| cbReplyBytes
!= cbWritten
)
161 FlushFileBuffers(hPipe
);
162 DisconnectNamedPipe(hPipe
);
165 return ERROR_SUCCESS
;
169 BOOL
ScmCreateNamedPipe(VOID
)
176 hPipe
= CreateNamedPipe("\\\\.\\pipe\\Ntsvcs",
178 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
179 PIPE_UNLIMITED_INSTANCES
,
184 if (hPipe
== INVALID_HANDLE_VALUE
)
186 DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
190 fConnected
= ConnectNamedPipe(hPipe
,
191 NULL
) ? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED
);
194 DPRINT("Pipe connected\n");
196 hThread
= CreateThread(NULL
,
204 DPRINT("Could not create thread (%d)\n", GetLastError());
206 DisconnectNamedPipe(hPipe
);
213 DPRINT("Pipe not connected\n");
224 WinMain(HINSTANCE hInstance
,
225 HINSTANCE hPrevInstance
,
229 HANDLE hScmStartEvent
;
233 PrintString("Service Control Manager\n");
235 /* Create start event */
236 if (!ScmCreateStartEvent(&hScmStartEvent
))
238 PrintString("SERVICES: Failed to create start event\n");
243 /* FIXME: more initialization */
246 /* Create the service database */
247 Status
= ScmCreateServiceDataBase();
248 if (!NT_SUCCESS(Status
))
250 PrintString("ScmCreateServiceDataBase() failed (Status %lx)\n", Status
);
254 /* Update service database */
255 ScmGetBootAndSystemDriverState();
258 /* Create named pipe */
259 if (!ScmCreateNamedPipe())
261 PrintString("SERVICES: Failed to create named pipe\n");
265 /* FIXME: create listener thread for pipe */
268 /* Register service process with CSRSS */
269 RegisterServicesProcess(GetCurrentProcessId());
271 PrintString("SERVICES: Initialized.\n");
273 /* Signal start event */
274 SetEvent(hScmStartEvent
);
276 /* FIXME: register event handler (used for system shutdown) */
277 // SetConsoleCtrlHandler(...);
280 /* Start auto-start services */
281 ScmAutoStartServices();
283 /* FIXME: more to do ? */
286 PrintString("SERVICES: Running.\n");
288 hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
289 WaitForSingleObject(hEvent
, INFINITE
);
297 PrintString("SERVICES: Finished.\n");