This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-daemon.git


The following commit(s) were added to refs/heads/master by this push:
     new 57520a2  Fix DAEMON-458
57520a2 is described below

commit 57520a2ea880dcf46dcb8b8ad0e6240056c12f85
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Apr 20 19:30:21 2023 +0100

    Fix DAEMON-458
    
    All service dependencies to be managed via procrun or be 'sc config ...'
---
 src/changes/changes.xml                   |  4 ++++
 src/native/windows/apps/prunsrv/prunsrv.c |  2 +-
 src/native/windows/src/service.c          | 37 ++++++++++++++++++++++---------
 src/site/xdoc/procrun.xml                 | 11 +++++++--
 4 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ac41c29..9d68669 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,10 @@
         create any missing parent directories, as well as the specified
         directory, when the service starts.
       </action>
+      <action issue="DAEMON-458" type="fix" dev="markt">
+        Procrun. Allow Windows service dependencies to be managed by Procrun or
+        by 'sc config ...'.
+      </action>
       <!-- UPDATES -->
       <action type="update" dev="ggregory" due-to="Dependabot">
         Bump commons-parent from 54 to 55 #71.
diff --git a/src/native/windows/apps/prunsrv/prunsrv.c 
b/src/native/windows/apps/prunsrv/prunsrv.c
index 17bdc3c..7789011 100644
--- a/src/native/windows/apps/prunsrv/prunsrv.c
+++ b/src/native/windows/apps/prunsrv/prunsrv.c
@@ -113,8 +113,8 @@ static APXCMDLINEOPT _options[] = {
 /* 4  */    { L"ServicePassword",   L"ServicePassword", NULL,           
APXCMDOPT_STR | APXCMDOPT_SRV, NULL, 0},
 /* 5  */    { L"Startup",           L"Startup",         NULL,           
APXCMDOPT_STR | APXCMDOPT_SRV, NULL, 0},
 /* 6  */    { L"Type",              L"Type",            NULL,           
APXCMDOPT_STR | APXCMDOPT_SRV, NULL, 0},
+/* 7  */    { L"DependsOn",         L"DependOnService", NULL,           
APXCMDOPT_MSZ | APXCMDOPT_SRV, NULL, 0},
 
-/* 7  */    { L"DependsOn",         L"DependsOn",       NULL,           
APXCMDOPT_MSZ | APXCMDOPT_REG, NULL, 0},
 /* 8  */    { L"Environment",       L"Environment",     NULL,           
APXCMDOPT_MSZ | APXCMDOPT_REG, NULL, 0},
 /* 9  */    { L"User",              L"User",            NULL,           
APXCMDOPT_STR | APXCMDOPT_REG, NULL, 0},
 /* 10 */    { L"Password",          L"Password",        NULL,           
APXCMDOPT_BIN | APXCMDOPT_REG, NULL, 0},
diff --git a/src/native/windows/src/service.c b/src/native/windows/src/service.c
index a6fbbde..d3db7d9 100644
--- a/src/native/windows/src/service.c
+++ b/src/native/windows/src/service.c
@@ -306,12 +306,6 @@ apxServiceSetOptions(APXHANDLE hService,
         return FALSE;
     }
 
-    if (lpDependencies) {
-        /* Add the mandatory dependencies to the explicitly configured 
dependencies */
-        lpDependencies = apxMultiSzCombine(NULL, lpDependencies,
-                                           L"Tcpip\0Afd\0", NULL);
-    }
-
     if (!ChangeServiceConfig(lpService->hService, dwServiceType,
                                    dwStartType, dwErrorControl,
                                    NULL, NULL, NULL,
@@ -692,11 +686,34 @@ apxServiceInstall(APXHANDLE hService, LPCWSTR 
szServiceName,
     lpService->stServiceEntry.lpConfig = NULL;
     AplZeroMemory(&lpService->stServiceEntry, sizeof(APXSERVENTRY));
 
-    if (lpDependencies)
-        lpDependencies = apxMultiSzCombine(NULL, lpDependencies,
-                                           L"Tcpip\0Afd\0", NULL);
-    else
+    if (lpDependencies) {
+        /* Only add Tcpip and Afd if not already present. */
+        BOOL needTcpip = TRUE;
+        BOOL needAfd = TRUE;
+        LPCWSTR p = lpDependencies;
+        while (*p && (needTcpip || needAfd)) {
+            if (lstrcmpiW(p, L"Tcpip") == 0) {
+                needTcpip = FALSE;
+            }
+            if (lstrcmpiW(p, L"Afd") == 0) {
+                needAfd = FALSE;
+            }
+            while (*p) {
+                p++;
+            }
+            p++;
+        }
+        if (needTcpip) {
+            lpDependencies = apxMultiSzCombine(NULL, lpDependencies,
+                                               L"Tcpip\0", NULL);
+        }
+        if (needAfd) {
+            lpDependencies = apxMultiSzCombine(NULL, lpDependencies,
+                                               L"Afd\0", NULL);
+        }
+    } else {
         lpDependencies = L"Tcpip\0Afd\0";
+    }
 
     if ((dwServiceType & SERVICE_INTERACTIVE_PROCESS) == 
SERVICE_INTERACTIVE_PROCESS) {
         // Caller is responsible for checking the user is set appropriately
diff --git a/src/site/xdoc/procrun.xml b/src/site/xdoc/procrun.xml
index b259e70..abaa294 100644
--- a/src/site/xdoc/procrun.xml
+++ b/src/site/xdoc/procrun.xml
@@ -331,8 +331,15 @@ will add the new value(s) to any existing value(s).
     <tr>
     <td>++DependsOn</td>
     <td></td>
-    <td>List of services that this service depends on. Dependent services
-        are separated using either <b>#</b> or <b>;</b> characters</td>
+    <td>List of services that this service depends on. Services are separated
+        using either <b>#</b> or <b>;</b> characters</td>
+        <br />
+        <b>Note:</b> At installation, additional dependencies will be created
+        on the services <i>Tcpip</i> and <i>Afd</i> if not explicitly
+        specified. These additional dependencies will not be created
+        automatically when updating the service configuration with
+        <i>--DependsOn</i> and must be explicitly defined if required in that
+        case.
     </tr>
     <tr>
     <td>++Environment</td>

Reply via email to