*
*/
-/* NOTE:
- * - Services.exe is NOT a native application, it is a GUI app.
- */
-
/* INCLUDES *****************************************************************/
#include "services.h"
+#include <wincon.h>
+
#define NDEBUG
#include <debug.h>
#define PIPE_BUFSIZE 1024
#define PIPE_TIMEOUT 1000
+/* Defined in include/reactos/services/services.h */
+// #define SCM_START_EVENT L"SvcctrlStartEvent_A3752DX"
+#define SCM_AUTOSTARTCOMPLETE_EVENT L"SC_AutoStartComplete"
+#define LSA_RPC_SERVER_ACTIVE L"LSA_RPC_SERVER_ACTIVE"
+
+BOOL ScmInitialize = FALSE;
BOOL ScmShutdown = FALSE;
static HANDLE hScmShutdownEvent = NULL;
L"Service Control Manager");
if (hLog == NULL)
{
- DPRINT1("ScmLogEvent: RegisterEventSourceW failed %d\n", GetLastError());
+ DPRINT1("ScmLogEvent: RegisterEventSourceW failed %lu\n", GetLastError());
return;
}
lpStrings,
NULL))
{
- DPRINT1("ScmLogEvent: ReportEventW failed %d\n", GetLastError());
+ DPRINT1("ScmLogEvent: ReportEventW failed %lu\n", GetLastError());
}
DeregisterEventSource(hLog);
}
-BOOL
-ScmCreateStartEvent(PHANDLE StartEvent)
-{
- HANDLE hEvent;
-
- hEvent = CreateEventW(NULL,
- TRUE,
- FALSE,
- L"SvcctrlStartEvent_A3752DX");
- if (hEvent == NULL)
- {
- if (GetLastError() == ERROR_ALREADY_EXISTS)
- {
- hEvent = OpenEventW(EVENT_ALL_ACCESS,
- FALSE,
- L"SvcctrlStartEvent_A3752DX");
- if (hEvent == NULL)
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- }
-
- *StartEvent = hEvent;
-
- return TRUE;
-}
-
-
VOID
ScmWaitForLsa(VOID)
{
- HANDLE hEvent;
- DWORD dwError;
-
- hEvent = CreateEventW(NULL,
- TRUE,
- FALSE,
- L"LSA_RPC_SERVER_ACTIVE");
+ HANDLE hEvent = CreateEventW(NULL, TRUE, FALSE, LSA_RPC_SERVER_ACTIVE);
if (hEvent == NULL)
{
- dwError = GetLastError();
- DPRINT1("Failed to create the notication event (Error %lu)\n", dwError);
-
- if (dwError == ERROR_ALREADY_EXISTS)
- {
- hEvent = OpenEventW(SYNCHRONIZE,
- FALSE,
- L"LSA_RPC_SERVER_ACTIVE");
- if (hEvent == NULL)
- {
- DPRINT1("Could not open the notification event (Error %lu)\n", GetLastError());
- return;
- }
- }
+ DPRINT1("Failed to create the notification event (Error %lu)\n", GetLastError());
+ }
+ else
+ {
+ DPRINT("Wait for the LSA server!\n");
+ WaitForSingleObject(hEvent, INFINITE);
+ DPRINT("LSA server running!\n");
+ CloseHandle(hEvent);
}
-
- DPRINT("Wait for the LSA server!\n");
- WaitForSingleObject(hEvent, INFINITE);
- DPRINT("LSA server running!\n");
-
- CloseHandle(hEvent);
DPRINT("ScmWaitForLsa() done\n");
}
PVOID Reply,
LPDWORD ReplySize)
{
- DbgPrint("SCM READ: %s\n", Request);
+ DbgPrint("SCM READ: %p\n", Request);
*ReplySize = 0;
return FALSE;
hPipe = (HANDLE)Context;
- DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe);
+ DPRINT("ScmNamedPipeThread(%p) - Accepting SCM commands through named pipe\n", hPipe);
for (;;)
{
}
}
- DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe);
+ DPRINT("ScmNamedPipeThread(%p) - Disconnecting named pipe connection\n", hPipe);
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
- DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe);
+ DPRINT("ScmNamedPipeThread(%p) - Done.\n", hPipe);
return ERROR_SUCCESS;
}
NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
- DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
+ DPRINT("CreateNamedPipe() failed (%lu)\n", GetLastError());
return FALSE;
}
- DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe);
+ DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%p)\n", hPipe);
bConnected = ConnectNamedPipe(hPipe,
NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", bConnected);
&dwThreadId);
if (!hThread)
{
- DPRINT("Could not create thread (%d)\n", GetLastError());
+ DPRINT("Could not create thread (%lu)\n", GetLastError());
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
DPRINT("CreateNamedPipe() - returning FALSE\n");
ScmNamedPipeListenerThread(LPVOID Context)
{
// HANDLE hPipe;
- DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context);
+ DPRINT("ScmNamedPipeListenerThread(%p) - aka SCM.\n", Context);
// hPipe = (HANDLE)Context;
for (;;)
DPRINT("\nSCM: named pipe session created.\n");
Sleep(10);
}
- DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context);
+ DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%p) - Aborted.\n\n", Context);
return ERROR_SUCCESS;
}
int nShowCmd)
{
HANDLE hScmStartEvent = NULL;
+ HANDLE hScmAutoStartCompleteEvent = NULL;
SC_RPC_LOCK Lock = NULL;
BOOL bCanDeleteNamedPipeCriticalSection = FALSE;
DWORD dwError;
DPRINT("SERVICES: Service Control Manager\n");
- /* Create start event */
- if (!ScmCreateStartEvent(&hScmStartEvent))
+ /* We are initializing ourselves */
+ ScmInitialize = TRUE;
+
+ /* Create the start event */
+ hScmStartEvent = CreateEventW(NULL, TRUE, FALSE, SCM_START_EVENT);
+ if (hScmStartEvent == NULL)
{
- DPRINT1("SERVICES: Failed to create start event\n");
+ DPRINT1("SERVICES: Failed to create the start event\n");
goto done;
}
+ DPRINT("SERVICES: Created start event with handle %p.\n", hScmStartEvent);
- DPRINT("SERVICES: created start event with handle %p.\n", hScmStartEvent);
+ /* Create the auto-start complete event */
+ hScmAutoStartCompleteEvent = CreateEventW(NULL, TRUE, FALSE, SCM_AUTOSTARTCOMPLETE_EVENT);
+ if (hScmAutoStartCompleteEvent == NULL)
+ {
+ DPRINT1("SERVICES: Failed to create the auto-start complete event\n");
+ goto done;
+ }
+ DPRINT("SERVICES: created auto-start complete event with handle %p.\n", hScmAutoStartCompleteEvent);
/* Create the shutdown event */
- hScmShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+ hScmShutdownEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (hScmShutdownEvent == NULL)
{
- DPRINT1("SERVICES: Failed to create shutdown event\n");
+ DPRINT1("SERVICES: Failed to create the shutdown event\n");
goto done;
}
/* Read the control set values */
if (!ScmGetControlSetValues())
{
- DPRINT1("SERVICES: failed to read the control set values\n");
+ DPRINT1("SERVICES: Failed to read the control set values\n");
goto done;
}
- /* Create the service database */
+ /* Create the services database */
dwError = ScmCreateServiceDatabase();
if (dwError != ERROR_SUCCESS)
{
- DPRINT1("SERVICES: failed to create SCM database (Error %lu)\n", dwError);
+ DPRINT1("SERVICES: Failed to create SCM database (Error %lu)\n", dwError);
goto done;
}
- /* Update service database */
+ /* Wait for the LSA server */
+ ScmWaitForLsa();
+
+ /* Update the services database */
ScmGetBootAndSystemDriverState();
- /* Register service process with CSRSS */
- RegisterServicesProcess(GetCurrentProcessId());
+ /* Register the Service Control Manager process with the ReactOS Subsystem */
+ if (!RegisterServicesProcess(GetCurrentProcessId()))
+ {
+ DPRINT1("SERVICES: Could not register SCM process\n");
+ goto done;
+ }
- /* Acquire the service start lock until autostart services have been started */
+ /*
+ * Acquire the user service start lock until
+ * auto-start services have been started.
+ */
dwError = ScmAcquireServiceStartLock(TRUE, &Lock);
if (dwError != ERROR_SUCCESS)
{
- DPRINT1("SERVICES: failed to acquire the service start lock (Error %lu)\n", dwError);
+ DPRINT1("SERVICES: Failed to acquire the service start lock (Error %lu)\n", dwError);
goto done;
}
/* Start the RPC server */
ScmStartRpcServer();
- DPRINT("SERVICES: Initialized.\n");
-
/* Signal start event */
SetEvent(hScmStartEvent);
+ DPRINT("SERVICES: Initialized.\n");
+
/* Register event handler (used for system shutdown) */
SetConsoleCtrlHandler(ShutdownHandlerRoutine, TRUE);
- /* Wait for the LSA server */
- ScmWaitForLsa();
+ /*
+ * Set our shutdown parameters: we want to shutdown after the maintained
+ * services (that inherit the default shutdown level of 640).
+ */
+ SetProcessShutdownParameters(480, SHUTDOWN_NORETRY);
/* Start auto-start services */
ScmAutoStartServices();
+ /* Signal auto-start complete event */
+ SetEvent(hScmAutoStartCompleteEvent);
+
/* FIXME: more to do ? */
/* Release the service start lock */
ScmReleaseServiceStartLock(&Lock);
+ /* Initialization finished */
+ ScmInitialize = FALSE;
+
DPRINT("SERVICES: Running.\n");
/* Wait until the shutdown event gets signaled */
if (hScmShutdownEvent != NULL)
CloseHandle(hScmShutdownEvent);
+ /* Close the auto-start complete event */
+ if (hScmAutoStartCompleteEvent != NULL)
+ CloseHandle(hScmAutoStartCompleteEvent);
+
/* Close the start event */
if (hScmStartEvent != NULL)
CloseHandle(hScmStartEvent);
DPRINT("SERVICES: Finished.\n");
ExitThread(0);
-
return 0;
}