[SERVICES] Implement ScmGenerateServiceTag and call it on service creation
authorPierre Schweitzer <pierre@reactos.org>
Sun, 30 Dec 2018 13:09:54 +0000 (14:09 +0100)
committerPierre Schweitzer <pierre@reactos.org>
Sun, 30 Dec 2018 13:27:04 +0000 (14:27 +0100)
This allows assigning an unique ServiceTag to each Win32 service

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

index be6290b..26172a7 100644 (file)
@@ -31,6 +31,7 @@ LIST_ENTRY ServiceListHead;
 static RTL_RESOURCE DatabaseLock;
 static DWORD ResumeCount = 1;
 static DWORD NoInteractiveServices = 0;
+static DWORD ServiceTag = 0;
 
 /* The critical section synchronizes service control requests */
 static CRITICAL_SECTION ControlServiceCriticalSection;
@@ -634,6 +635,29 @@ ScmGetServiceEntryByResumeCount(DWORD dwResumeCount)
 }
 
 
+DWORD
+ScmGenerateServiceTag(PSERVICE lpServiceRecord)
+{
+    /* Check for an overflow */
+    if (ServiceTag == -1)
+    {
+        return ERROR_INVALID_DATA;
+    }
+
+    /* This is only valid for Win32 services */
+    if (!(lpServiceRecord->Status.dwServiceType & SERVICE_WIN32))
+    {
+        return ERROR_INVALID_PARAMETER;
+    }
+
+    /* Increment the tag counter and set it */
+    ServiceTag = ServiceTag % 0xFFFFFFFF + 1;
+    lpServiceRecord->dwTag = ServiceTag;
+
+    return ERROR_SUCCESS;
+}
+
+
 DWORD
 ScmCreateNewServiceRecord(LPCWSTR lpServiceName,
                           PSERVICE *lpServiceRecord,
@@ -847,6 +871,8 @@ CreateServiceListEntry(LPCWSTR lpServiceName,
 
     if (ScmIsDeleteFlagSet(hServiceKey))
         lpService->bDeleted = TRUE;
+    else
+        ScmGenerateServiceTag(lpService);
 
     if (lpService->Status.dwServiceType & SERVICE_WIN32)
     {
index b11db5a..83d83dd 100644 (file)
@@ -2636,6 +2636,10 @@ RCreateServiceW(
         goto done;
 
     lpService->dwRefCount = 1;
+
+    /* Get the service tag (if Win32) */
+    ScmGenerateServiceTag(lpService);
+
     DPRINT("CreateService - lpService->dwRefCount %u\n", lpService->dwRefCount);
 
 done:
index 4e6f1f1..9297c7c 100644 (file)
@@ -200,6 +200,8 @@ VOID ScmDeleteNamedPipeCriticalSection(VOID);
 DWORD ScmGetServiceNameFromTag(PTAG_INFO_NAME_FROM_TAG_IN_PARAMS InParams,
                                PTAG_INFO_NAME_FROM_TAG_OUT_PARAMS *OutParams);
 
+DWORD ScmGenerateServiceTag(PSERVICE lpServiceRecord);
+
 /* driver.c */
 
 DWORD ScmStartDriver(PSERVICE lpService);