[MSIEXEC] Sync with Wine Staging 2.16. CORE-13762
[reactos.git] / base / system / services / services.c
index ca7401e..58a6256 100644 (file)
@@ -8,14 +8,12 @@
  *
  */
 
-/* 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>
 
@@ -23,9 +21,12 @@ int WINAPI RegisterServicesProcess(DWORD ServicesProcessId);
 
 /* GLOBALS ******************************************************************/
 
-#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;
 
@@ -49,7 +50,8 @@ PrintString(LPCSTR fmt, ...)
 
 
 VOID
-ScmLogError(DWORD dwEventId,
+ScmLogEvent(DWORD dwEventId,
+            WORD wType,
             WORD wStrings,
             LPCWSTR *lpStrings)
 {
@@ -59,316 +61,239 @@ ScmLogError(DWORD dwEventId,
                                 L"Service Control Manager");
     if (hLog == NULL)
     {
-        DPRINT1("ScmLogEvent: RegisterEventSourceW failed %d\n", GetLastError());
+        DPRINT1("ScmLogEvent: RegisterEventSourceW failed %lu\n", GetLastError());
         return;
     }
 
     if (!ReportEventW(hLog,
-                      EVENTLOG_ERROR_TYPE,
+                      wType,
                       0,
                       dwEventId,
-                      NULL, // Sid,
+                      NULL,
                       wStrings,
                       0,
                       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");
 }
 
 
-BOOL
-ScmNamedPipeHandleRequest(PVOID Request,
-                          DWORD RequestSize,
-                          PVOID Reply,
-                          LPDWORD ReplySize)
-{
-    DbgPrint("SCM READ: %s\n", Request);
-
-    *ReplySize = 0;
-    return FALSE;
-}
-
-
-DWORD WINAPI
-ScmNamedPipeThread(LPVOID Context)
+BOOL WINAPI
+ShutdownHandlerRoutine(DWORD dwCtrlType)
 {
-    CHAR chRequest[PIPE_BUFSIZE];
-    CHAR chReply[PIPE_BUFSIZE];
-    DWORD cbReplyBytes;
-    DWORD cbBytesRead;
-    DWORD cbWritten;
-    BOOL bSuccess;
-    HANDLE hPipe;
-
-    hPipe = (HANDLE)Context;
-
-    DPRINT("ScmNamedPipeThread(%x) - Accepting SCM commands through named pipe\n", hPipe);
+    DPRINT1("ShutdownHandlerRoutine() called\n");
 
-    for (;;)
+    if (dwCtrlType & (CTRL_SHUTDOWN_EVENT | CTRL_LOGOFF_EVENT))
     {
-        bSuccess = ReadFile(hPipe,
-                            &chRequest,
-                            PIPE_BUFSIZE,
-                            &cbBytesRead,
-                            NULL);
-        if (!bSuccess || cbBytesRead == 0)
-        {
-            break;
-        }
+        DPRINT1("Shutdown event received!\n");
+        ScmShutdown = TRUE;
 
-        if (ScmNamedPipeHandleRequest(&chRequest, cbBytesRead, &chReply, &cbReplyBytes))
-        {
-            bSuccess = WriteFile(hPipe,
-                                 &chReply,
-                                 cbReplyBytes,
-                                 &cbWritten,
-                                 NULL);
-            if (!bSuccess || cbReplyBytes != cbWritten)
-            {
-                break;
-            }
-        }
-    }
+        ScmAutoShutdownServices();
+        ScmShutdownServiceDatabase();
 
-    DPRINT("ScmNamedPipeThread(%x) - Disconnecting named pipe connection\n", hPipe);
+        /* Set the shutdown event */
+        SetEvent(hScmShutdownEvent);
+    }
 
-    FlushFileBuffers(hPipe);
-    DisconnectNamedPipe(hPipe);
-    CloseHandle(hPipe);
+    return TRUE;
+}
 
-    DPRINT("ScmNamedPipeThread(%x) - Done.\n", hPipe);
 
-    return ERROR_SUCCESS;
-}
+/*** HACK CORE-12541: Special service accounts initialization HACK ************/
 
+#include <ndk/setypes.h>
+#include <sddl.h>
+#include <userenv.h>
+#include <strsafe.h>
 
+/* Inspired from userenv.dll's CreateUserProfileExW and LoadUserProfileW APIs */
+static
 BOOL
-ScmCreateNamedPipe(VOID)
+ScmLogAccountHack(IN LPCWSTR pszAccountName,
+                  IN LPCWSTR pszSid,
+                  OUT PHKEY phProfile)
 {
-    DWORD dwThreadId;
-    BOOL bConnected;
-    HANDLE hThread;
-    HANDLE hPipe;
-
-    DPRINT("ScmCreateNamedPipe() - CreateNamedPipe(\"\\\\.\\pipe\\Ntsvcs\")\n");
-
-    hPipe = CreateNamedPipeW(L"\\\\.\\pipe\\Ntsvcs",
-              PIPE_ACCESS_DUPLEX,
-              PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
-              PIPE_UNLIMITED_INSTANCES,
-              PIPE_BUFSIZE,
-              PIPE_BUFSIZE,
-              PIPE_TIMEOUT,
-              NULL);
-    if (hPipe == INVALID_HANDLE_VALUE)
+    BOOL Success = FALSE;
+    LONG Error;
+    NTSTATUS Status;
+    BOOLEAN WasPriv1Set = FALSE, WasPriv2Set = FALSE;
+    PSID pSid;
+    DWORD dwLength;
+    WCHAR szUserHivePath[MAX_PATH];
+
+    DPRINT1("ScmLogAccountsHack(%S, %S)\n", pszAccountName, pszSid);
+    if (!pszAccountName || !pszSid || !phProfile)
+        return ERROR_INVALID_PARAMETER;
+
+    /* Convert the SID string into a SID. NOTE: No RTL equivalent. */
+    if (!ConvertStringSidToSidW(pszSid, &pSid))
     {
-        DPRINT("CreateNamedPipe() failed (%d)\n", GetLastError());
+        DPRINT1("ConvertStringSidToSidW() failed (error %lu)\n", GetLastError());
         return FALSE;
     }
 
-    DPRINT("CreateNamedPipe() - calling ConnectNamedPipe(%x)\n", hPipe);
-    bConnected = ConnectNamedPipe(hPipe,
-                                  NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
-    DPRINT("CreateNamedPipe() - ConnectNamedPipe() returned %d\n", bConnected);
+    /* Determine a suitable profile path */
+    dwLength = ARRAYSIZE(szUserHivePath);
+    if (!GetProfilesDirectoryW(szUserHivePath, &dwLength))
+    {
+        DPRINT1("GetProfilesDirectoryW() failed (error %lu)\n", GetLastError());
+        goto Quit;
+    }
 
-    if (bConnected)
+    /* Create user hive name */
+    StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\");
+    StringCbCatW(szUserHivePath, sizeof(szUserHivePath), pszAccountName);
+    StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\ntuser.dat");
+    DPRINT("szUserHivePath: %S\n", szUserHivePath);
+
+    /* Magic #1: Create the special user profile if needed */
+    if (GetFileAttributesW(szUserHivePath) == INVALID_FILE_ATTRIBUTES)
     {
-        DPRINT("Pipe connected\n");
-        hThread = CreateThread(NULL,
-                               0,
-                               ScmNamedPipeThread,
-                               (LPVOID)hPipe,
-                               0,
-                               &dwThreadId);
-        if (!hThread)
+        if (!CreateUserProfileW(pSid, pszAccountName))
         {
-            DPRINT("Could not create thread (%d)\n", GetLastError());
-            DisconnectNamedPipe(hPipe);
-            CloseHandle(hPipe);
-            DPRINT("CreateNamedPipe() - returning FALSE\n");
-            return FALSE;
+            DPRINT1("CreateUserProfileW() failed (error %lu)\n", GetLastError());
+            goto Quit;
         }
+    }
+
+    /*
+     * Now Da Magiks #2: Manually mount the user profile registry hive
+     * aka. manually do what LoadUserProfile does!! But we don't require
+     * a security token!
+     */
 
-        CloseHandle(hThread);
+    /* Acquire restore privilege */
+    Status = RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, TRUE, FALSE, &WasPriv1Set);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE) failed (Error 0x%08lx)\n", Status);
+        goto Quit;
     }
-    else
+
+    /* Acquire backup privilege */
+    Status = RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE, TRUE, FALSE, &WasPriv2Set);
+    if (!NT_SUCCESS(Status))
     {
-        DPRINT("Pipe not connected\n");
-        CloseHandle(hPipe);
-        DPRINT("CreateNamedPipe() - returning FALSE\n");
-        return FALSE;
+        DPRINT1("RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE) failed (Error 0x%08lx)\n", Status);
+        RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasPriv1Set, FALSE, &WasPriv1Set);
+        goto Quit;
     }
-    DPRINT("CreateNamedPipe() - returning TRUE\n");
-    return TRUE;
-}
 
+    /* Load user registry hive */
+    Error = RegLoadKeyW(HKEY_USERS, pszSid, szUserHivePath);
 
-DWORD WINAPI
-ScmNamedPipeListenerThread(LPVOID Context)
-{
-//    HANDLE hPipe;
-    DPRINT("ScmNamedPipeListenerThread(%x) - aka SCM.\n", Context);
+    /* Remove restore and backup privileges */
+    RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE, WasPriv2Set, FALSE, &WasPriv2Set);
+    RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasPriv1Set, FALSE, &WasPriv1Set);
 
-//    hPipe = (HANDLE)Context;
-    for (;;)
+    /* HACK: Do not fail if the profile has already been loaded! */
+    if (Error == ERROR_SHARING_VIOLATION)
+        Error = ERROR_SUCCESS;
+
+    if (Error != ERROR_SUCCESS)
     {
-        DPRINT("SCM: Waiting for new connection on named pipe...\n");
-        /* Create named pipe */
-        if (!ScmCreateNamedPipe())
-        {
-            DPRINT1("\nSCM: Failed to create named pipe\n");
-            break;
-            //ExitThread(0);
-        }
-        DPRINT("\nSCM: named pipe session created.\n");
-        Sleep(10);
+        DPRINT1("RegLoadKeyW() failed (Error %ld)\n", Error);
+        goto Quit;
     }
-    DPRINT("\n\nWARNING: ScmNamedPipeListenerThread(%x) - Aborted.\n\n", Context);
-    return ERROR_SUCCESS;
-}
 
-
-BOOL
-StartScmNamedPipeThreadListener(VOID)
-{
-    DWORD dwThreadId;
-    HANDLE hThread;
-
-    hThread = CreateThread(NULL,
-                           0,
-                           ScmNamedPipeListenerThread,
-                           NULL, /*(LPVOID)hPipe,*/
-                           0,
-                           &dwThreadId);
-    if (!hThread)
+    /* Open future HKEY_CURRENT_USER */
+    Error = RegOpenKeyExW(HKEY_USERS,
+                          pszSid,
+                          0,
+                          MAXIMUM_ALLOWED,
+                          phProfile);
+    if (Error != ERROR_SUCCESS)
     {
-        DPRINT1("SERVICES: Could not create thread (Status %lx)\n", GetLastError());
-        return FALSE;
+        DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error);
+        goto Quit;
     }
 
-    CloseHandle(hThread);
+    Success = TRUE;
 
-    return TRUE;
-}
+Quit:
+    LocalFree(pSid);
 
+    DPRINT1("ScmLogAccountsHack(%S) returned %s\n",
+            pszAccountName, Success ? "success" : "failure");
 
-VOID FASTCALL
-AcquireLoadDriverPrivilege(VOID)
-{
-    HANDLE hToken;
-    TOKEN_PRIVILEGES tkp;
+    return Success;
+}
 
-    /* Get a token for this process */
-    if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
-    {
-        /* Get the LUID for the debug privilege */
-        LookupPrivilegeValue(NULL, SE_LOAD_DRIVER_NAME, &tkp.Privileges[0].Luid);
+static struct
+{
+    LPCWSTR pszAccountName;
+    LPCWSTR pszSid;
+    HKEY    hProfile;
+} AccountHandles[] = {
+//  {L"LocalSystem"   , L"S-1-5-18", NULL},
+    {L"LocalService"  , L"S-1-5-19", NULL}, // L"NT AUTHORITY\\LocalService"
+    {L"NetworkService", L"S-1-5-20", NULL}, // L"NT AUTHORITY\\NetworkService"
+};
+
+static VOID
+ScmCleanupServiceAccountsHack(VOID)
+{
+    UINT i;
 
-        /* One privilege to set */
-        tkp.PrivilegeCount = 1;
-        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+    DPRINT1("ScmCleanupServiceAccountsHack()\n");
 
-        /* Get the debug privilege for this process */
-        AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
+    for (i = 0; i < ARRAYSIZE(AccountHandles); ++i)
+    {
+        if (AccountHandles[i].hProfile)
+        {
+            RegCloseKey(AccountHandles[i].hProfile);
+            AccountHandles[i].hProfile = NULL;
+        }
     }
 }
 
-
-BOOL WINAPI
-ShutdownHandlerRoutine(DWORD dwCtrlType)
+static BOOL
+ScmApplyServiceAccountsHack(VOID)
 {
-    DPRINT1("ShutdownHandlerRoutine() called\n");
+    UINT i;
 
-    if (dwCtrlType & (CTRL_SHUTDOWN_EVENT | CTRL_LOGOFF_EVENT))
-    {
-        DPRINT1("Shutdown event received!\n");
-        ScmShutdown = TRUE;
+    DPRINT1("ScmApplyServiceAccountsHack()\n");
 
-        ScmAutoShutdownServices();
-        ScmShutdownServiceDatabase();
-
-        /* Set the shutdwon event */
-        SetEvent(hScmShutdownEvent);
+    for (i = 0; i < ARRAYSIZE(AccountHandles); ++i)
+    {
+        if (!ScmLogAccountHack( AccountHandles[i].pszAccountName,
+                                AccountHandles[i].pszSid,
+                               &AccountHandles[i].hProfile))
+        {
+            ScmCleanupServiceAccountsHack();
+            return FALSE;
+        }
     }
 
     return TRUE;
 }
 
+/*************************** END OF HACK CORE-12541 ***************************/
+
 
 int WINAPI
 wWinMain(HINSTANCE hInstance,
@@ -377,103 +302,151 @@ wWinMain(HINSTANCE hInstance,
          int nShowCmd)
 {
     HANDLE hScmStartEvent = NULL;
+    HANDLE hScmAutoStartCompleteEvent = NULL;
     SC_RPC_LOCK Lock = NULL;
-    BOOL bDeleteCriticalSection = FALSE;
+    BOOL bCanDeleteNamedPipeCriticalSection = FALSE;
     DWORD dwError;
 
     DPRINT("SERVICES: Service Control Manager\n");
 
-    /* Create start event */
-    if (!ScmCreateStartEvent(&hScmStartEvent))
+    /* Make us critical */
+    RtlSetProcessIsCritical(TRUE, NULL, TRUE);
+
+    /* 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;
     }
 
+    /* Initialize our communication named pipe's critical section */
+    ScmInitNamedPipeCriticalSection();
+    bCanDeleteNamedPipeCriticalSection = TRUE;
+
 //    ScmInitThreadManager();
 
+    ScmInitializeSecurity();
+
     /* FIXME: more initialization */
 
     /* 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();
 
-    /* Start the RPC server */
-    ScmStartRpcServer();
+    /* Register the Service Control Manager process with the ReactOS Subsystem */
+    if (!RegisterServicesProcess(GetCurrentProcessId()))
+    {
+        DPRINT1("SERVICES: Could not register SCM process\n");
+        goto done;
+    }
 
-    /* Register service process with CSRSS */
-    RegisterServicesProcess(GetCurrentProcessId());
+    /*
+     * 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);
+        goto done;
+    }
 
-    DPRINT("SERVICES: Initialized.\n");
+    /* Start the RPC server */
+    ScmStartRpcServer();
 
     /* 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();
-
-    /* Acquire privileges to load drivers */
-    AcquireLoadDriverPrivilege();
+    /*
+     * Set our shutdown parameters: we want to shutdown after the maintained
+     * services (that inherit the default shutdown level of 640).
+     */
+    SetProcessShutdownParameters(480, SHUTDOWN_NORETRY);
 
-    ScmInitNamedPipeCriticalSection();
-    bDeleteCriticalSection = TRUE;
-
-    /* Acquire the service start lock until autostart 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);
-        goto done;
-    }
+    /*** HACK CORE-12541: Apply service accounts HACK ***/
+    ScmApplyServiceAccountsHack();
 
     /* 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 */
     WaitForSingleObject(hScmShutdownEvent, INFINITE);
 
+    /*** HACK CORE-12541: Cleanup service accounts HACK ***/
+    ScmCleanupServiceAccountsHack();
+
 done:
-    if (bDeleteCriticalSection == TRUE)
+    ScmShutdownSecurity();
+
+    /* Delete our communication named pipe's critical section */
+    if (bCanDeleteNamedPipeCriticalSection != FALSE)
         ScmDeleteNamedPipeCriticalSection();
 
     /* Close the shutdown event */
     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);
@@ -481,7 +454,6 @@ done:
     DPRINT("SERVICES: Finished.\n");
 
     ExitThread(0);
-
     return 0;
 }