[SERVICES]
[reactos.git] / reactos / base / system / services / services.c
index 003ab4c..e5a0041 100644 (file)
@@ -1,24 +1,10 @@
 /*
- * service control manager
- *
- * ReactOS Operating System
- *
- * --------------------------------------------------------------------
- *
- * This software is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.LIB. If not, write
- * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
- * MA 02139, USA.
+ * PROJECT:     ReactOS Service Control Manager
+ * LICENSE:     GPL - See COPYING in the top level directory
+ * FILE:        base/system/services/services.c
+ * PURPOSE:     Main SCM controller
+ * COPYRIGHT:   Copyright 2001-2005 Eric Kohl
+ *              Copyright 2007 Ged Murphy <gedmurphy@reactos.org>
  *
  */
 
@@ -48,7 +34,7 @@ BOOL ScmShutdown = FALSE;
 VOID
 PrintString(LPCSTR fmt, ...)
 {
-#ifdef DBG
+#if DBG
     CHAR buffer[512];
     va_list ap;
 
@@ -69,14 +55,14 @@ ScmCreateStartEvent(PHANDLE StartEvent)
     hEvent = CreateEvent(NULL,
                          TRUE,
                          FALSE,
-                         TEXT("SvcctrlStartEvent_A3725DX"));
+                         TEXT("SvcctrlStartEvent_A3752DX"));
     if (hEvent == NULL)
     {
         if (GetLastError() == ERROR_ALREADY_EXISTS)
         {
             hEvent = OpenEvent(EVENT_ALL_ACCESS,
                                FALSE,
-                               TEXT("SvcctrlStartEvent_A3725DX"));
+                               TEXT("SvcctrlStartEvent_A3752DX"));
             if (hEvent == NULL)
             {
                 return FALSE;
@@ -94,6 +80,42 @@ ScmCreateStartEvent(PHANDLE StartEvent)
 }
 
 
+static VOID
+ScmWaitForLsass(VOID)
+{
+    HANDLE hEvent;
+    DWORD dwError;
+
+    hEvent = CreateEventW(NULL,
+                          TRUE,
+                          FALSE,
+                          L"LSA_RPC_SERVER_ACTIVE");
+    if (hEvent == NULL)
+    {
+        dwError = GetLastError();
+        DPRINT("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;
+            }
+        }
+    }
+
+    DPRINT("Wait for the LSA server!\n");
+    WaitForSingleObject(hEvent, INFINITE);
+    DPRINT("LSA server running!\n");
+
+    CloseHandle(hEvent);
+}
+
+
 BOOL
 ScmNamedPipeHandleRequest(PVOID Request,
                           DWORD RequestSize,
@@ -293,23 +315,24 @@ ShutdownHandlerRoutine(DWORD dwCtrlType)
 {
     DPRINT1("ShutdownHandlerRoutine() called\n");
 
-    if (dwCtrlType == CTRL_SHUTDOWN_EVENT)
+    if (dwCtrlType & (CTRL_SHUTDOWN_EVENT | CTRL_LOGOFF_EVENT))
     {
         DPRINT1("Shutdown event received!\n");
         ScmShutdown = TRUE;
 
-        /* FIXME: Shut all services down */
+        ScmAutoShutdownServices();
+        ScmShutdownServiceDatabase();
     }
 
     return TRUE;
 }
 
 
-int STDCALL
-WinMain(HINSTANCE hInstance,
-        HINSTANCE hPrevInstance,
-        LPSTR lpCmdLine,
-        int nShowCmd)
+int WINAPI
+wWinMain(HINSTANCE hInstance,
+         HINSTANCE hPrevInstance,
+         LPWSTR lpCmdLine,
+         int nShowCmd)
 {
     HANDLE hScmStartEvent;
     HANDLE hEvent;
@@ -317,9 +340,6 @@ WinMain(HINSTANCE hInstance,
 
     DPRINT("SERVICES: Service Control Manager\n");
 
-    /* Acquire privileges to load drivers */
-    AcquireLoadDriverPrivilege();
-
     /* Create start event */
     if (!ScmCreateStartEvent(&hScmStartEvent))
     {
@@ -359,6 +379,14 @@ WinMain(HINSTANCE hInstance,
     /* Register event handler (used for system shutdown) */
     SetConsoleCtrlHandler(ShutdownHandlerRoutine, TRUE);
 
+    /* Wait for the LSA server */
+    ScmWaitForLsass();
+
+    /* Acquire privileges to load drivers */
+    AcquireLoadDriverPrivilege();
+
+    ScmInitNamedPipeCriticalSection();
+
     /* Start auto-start services */
     ScmAutoStartServices();
 
@@ -378,6 +406,10 @@ WinMain(HINSTANCE hInstance,
     }
 #endif
 
+    ScmDeleteNamedPipeCriticalSection();
+
+    CloseHandle(hScmStartEvent);
+
     DPRINT("SERVICES: Finished.\n");
 
     ExitThread(0);