[SERVICES] Fix the initial service status
authorEric Kohl <eric.kohl@reactos.org>
Mon, 12 Feb 2018 13:58:02 +0000 (14:58 +0100)
committerEric Kohl <eric.kohl@reactos.org>
Mon, 12 Feb 2018 13:58:02 +0000 (14:58 +0100)
- The initial dwWin32ExitCode for a disabled service is ERROR_SERVICE_DISABLED and ERROR_SRVICE_NEVER_STARTED for non-disabled services.
- The initial dwWaitHint is 2000 (2 seconds) for (user-mode) services and 0 for drivers.
- Move all driver-related code to driver.c.

base/system/services/database.c
base/system/services/driver.c
base/system/services/rpcserver.c
base/system/services/services.h

index 19c4e05..b450fe4 100644 (file)
@@ -559,7 +559,9 @@ ScmGetServiceEntryByResumeCount(DWORD dwResumeCount)
 
 DWORD
 ScmCreateNewServiceRecord(LPCWSTR lpServiceName,
-                          PSERVICE* lpServiceRecord)
+                          PSERVICE *lpServiceRecord,
+                          DWORD dwServiceType,
+                          DWORD dwStartType)
 {
     PSERVICE lpService = NULL;
 
@@ -579,6 +581,9 @@ ScmCreateNewServiceRecord(LPCWSTR lpServiceName,
     lpService->lpServiceName = lpService->szServiceName;
     lpService->lpDisplayName = lpService->lpServiceName;
 
+    /* Set the start type */
+    lpService->dwStartType = dwStartType;
+
     /* Set the resume count */
     lpService->dwResumeCount = ResumeCount++;
 
@@ -587,12 +592,15 @@ ScmCreateNewServiceRecord(LPCWSTR lpServiceName,
                    &lpService->ServiceListEntry);
 
     /* Initialize the service status */
+    lpService->Status.dwServiceType = dwServiceType;
     lpService->Status.dwCurrentState = SERVICE_STOPPED;
     lpService->Status.dwControlsAccepted = 0;
-    lpService->Status.dwWin32ExitCode = ERROR_SERVICE_NEVER_STARTED;
+    lpService->Status.dwWin32ExitCode =
+        (dwStartType == SERVICE_DISABLED) ? ERROR_SERVICE_DISABLED : ERROR_SERVICE_NEVER_STARTED;
     lpService->Status.dwServiceSpecificExitCode = 0;
     lpService->Status.dwCheckPoint = 0;
-    lpService->Status.dwWaitHint = 2000; /* 2 seconds */
+    lpService->Status.dwWaitHint =
+        (dwServiceType & SERVICE_DRIVER) ? 0 : 2000; /* 2 seconds */
 
     return ERROR_SUCCESS;
 }
@@ -719,12 +727,12 @@ CreateServiceListEntry(LPCWSTR lpServiceName,
     DPRINT("Display name: %S\n", lpDisplayName);
 
     dwError = ScmCreateNewServiceRecord(lpServiceName,
-                                        &lpService);
+                                        &lpService,
+                                        dwServiceType,
+                                        dwStartType);
     if (dwError != ERROR_SUCCESS)
         goto done;
 
-    lpService->Status.dwServiceType = dwServiceType;
-    lpService->dwStartType = dwStartType;
     lpService->dwErrorControl = dwErrorControl;
     lpService->dwTag = dwTagId;
 
@@ -1801,13 +1809,8 @@ ScmLoadService(PSERVICE Service,
 
     if (Service->Status.dwServiceType & SERVICE_DRIVER)
     {
-        /* Load driver */
-        dwError = ScmLoadDriver(Service);
-        if (dwError == ERROR_SUCCESS)
-        {
-            Service->Status.dwCurrentState = SERVICE_RUNNING;
-            Service->Status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
-        }
+        /* Start the driver */
+        dwError = ScmStartDriver(Service);
     }
     else // if (Service->Status.dwServiceType & (SERVICE_WIN32 | SERVICE_INTERACTIVE_PROCESS))
     {
index de8bfc1..819183e 100644 (file)
@@ -19,6 +19,7 @@
 
 /* FUNCTIONS ****************************************************************/
 
+static
 DWORD
 ScmLoadDriver(PSERVICE lpService)
 {
@@ -70,7 +71,7 @@ done:
     return RtlNtStatusToDosError(Status);
 }
 
-
+static
 DWORD
 ScmUnloadDriver(PSERVICE lpService)
 {
@@ -123,6 +124,7 @@ done:
 }
 
 
+static
 DWORD
 ScmGetDriverStatus(PSERVICE lpService,
                    LPSERVICE_STATUS lpServiceStatus)
@@ -285,6 +287,27 @@ ScmGetDriverStatus(PSERVICE lpService,
 }
 
 
+DWORD
+ScmStartDriver(PSERVICE pService)
+{
+    DWORD dwError;
+
+    DPRINT("ScmStartDriver(%p)\n", pService);
+
+    dwError = ScmLoadDriver(pService);
+    if (dwError == ERROR_SUCCESS)
+    {
+        pService->Status.dwCurrentState = SERVICE_RUNNING;
+        pService->Status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+        pService->Status.dwWin32ExitCode = ERROR_SUCCESS;
+    }
+
+    DPRINT("ScmStartDriver returns %lu\n", dwError);
+
+    return dwError;
+}
+
+
 DWORD
 ScmControlDriver(PSERVICE lpService,
                  DWORD dwControl,
@@ -328,7 +351,7 @@ ScmControlDriver(PSERVICE lpService,
             dwError = ERROR_INVALID_SERVICE_CONTROL;
     }
 
-done:;
+done:
     DPRINT("ScmControlDriver() done (Erorr: %lu)\n", dwError);
 
     return dwError;
index f1ce146..a4718aa 100644 (file)
@@ -2279,13 +2279,13 @@ RCreateServiceW(
 
     /* Allocate a new service entry */
     dwError = ScmCreateNewServiceRecord(lpServiceName,
-                                        &lpService);
+                                        &lpService,
+                                        dwServiceType,
+                                        dwStartType);
     if (dwError != ERROR_SUCCESS)
         goto done;
 
     /* Fill the new service entry */
-    lpService->Status.dwServiceType = dwServiceType;
-    lpService->dwStartType = dwStartType;
     lpService->dwErrorControl = dwErrorControl;
 
     /* Fill the display name */
index 1d20f12..0f03849 100644 (file)
@@ -168,7 +168,9 @@ PSERVICE ScmGetServiceEntryByName(LPCWSTR lpServiceName);
 PSERVICE ScmGetServiceEntryByDisplayName(LPCWSTR lpDisplayName);
 PSERVICE ScmGetServiceEntryByResumeCount(DWORD dwResumeCount);
 DWORD ScmCreateNewServiceRecord(LPCWSTR lpServiceName,
-                                PSERVICE *lpServiceRecord);
+                                PSERVICE *lpServiceRecord,
+                                DWORD dwServiceType,
+                                DWORD dwStartType);
 VOID ScmDeleteServiceRecord(PSERVICE lpService);
 DWORD ScmMarkServiceForDelete(PSERVICE pService);
 
@@ -185,8 +187,7 @@ VOID ScmDeleteNamedPipeCriticalSection(VOID);
 
 /* driver.c */
 
-DWORD ScmLoadDriver(PSERVICE lpService);
-DWORD ScmUnloadDriver(PSERVICE lpService);
+DWORD ScmStartDriver(PSERVICE lpService);
 DWORD ScmControlDriver(PSERVICE lpService,
                        DWORD dwControl,
                        LPSERVICE_STATUS lpServiceStatus);