You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2019/05/02 13:51:13 UTC

[commons-daemon] branch master updated: Remaining fix for DAEMON-396

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 d081b49  Remaining fix for DAEMON-396
d081b49 is described below

commit d081b49ef10f990a7fc4b1d4052f2e686b0da3b2
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu May 2 14:50:56 2019 +0100

    Remaining fix for DAEMON-396
    
    Ensure that JAVA_HOME/bin is on the path when running in jvm mode so
    additional DLLs, such as awt.dll, can be found if required.
    Patch provide by Gerwin.
---
 src/changes/changes.xml                   |  4 +++
 src/native/windows/apps/prunsrv/prunsrv.c |  6 ++--
 src/native/windows/include/apxwin.h       |  1 +
 src/native/windows/src/javajni.c          | 60 ++++++++++++++++---------------
 4 files changed, 38 insertions(+), 33 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3a6ccb1..118fb17 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,10 @@
         Fix a bug that meant a value provided for LibraryPath replaced the value
         of the PATH environment variable rather than prepended to it.
       </action>
+      <action issue="DAEMON-396" type="fix" dev="markt" due-to="Gerwin">
+        Ensure that JAVA_HOME/bin is on the path when running in jvm mode so
+        additional DLLs, such as awt.dll, can be found if required.
+      </action>
     </release>
     <release version="1.1.0" date="2017-11-15" description="Feature and bug fix release">
       <action issue="DAEMON-368" type="add" dev="ggregory">
diff --git a/src/native/windows/apps/prunsrv/prunsrv.c b/src/native/windows/apps/prunsrv/prunsrv.c
index f2fc4d2..a295b9d 100644
--- a/src/native/windows/apps/prunsrv/prunsrv.c
+++ b/src/native/windows/apps/prunsrv/prunsrv.c
@@ -410,7 +410,7 @@ static void dumpCmdline()
     }
 }
 
-static void setInprocEnvironment()
+void apxSetInprocEnvironment()
 {
     LPWSTR p, e;
 
@@ -1204,8 +1204,6 @@ static DWORD serviceStart()
             /* Add LibraryPath to the PATH */
            apxAddToPathW(gPool, SO_LIBPATH);
         }
-        /* Set the environment using putenv, so JVM can use it */
-        setInprocEnvironment();
         /* Create the JVM global worker */
         gWorker = apxCreateJava(gPool, _jni_jvmpath);
         if (IS_INVALID_HANDLE(gWorker)) {
@@ -1249,7 +1247,7 @@ static DWORD serviceStart()
            apxAddToPathW(gPool, SO_LIBPATH);
         }
         /* Set the environment using putenv, so JVM can use it */
-        setInprocEnvironment();
+        apxSetInprocEnvironment();
         setInprocEnvironment9(SO_JVMOPTIONS9);
         /* Redirect process */
         gWorker = apxCreateProcessW(gPool,
diff --git a/src/native/windows/include/apxwin.h b/src/native/windows/include/apxwin.h
index d82436e..8a9f842 100644
--- a/src/native/windows/include/apxwin.h
+++ b/src/native/windows/include/apxwin.h
@@ -148,6 +148,7 @@ LPWSTR          apxExpandStrW(APXHANDLE hPool, LPCWSTR szString);
 void            apxStrCharReplaceA(LPSTR szString, CHAR chReplace, CHAR chReplaceWith);
 void            apxStrCharReplaceW(LPWSTR szString, WCHAR chReplace, WCHAR chReplaceWith);
 BOOL            apxAddToPathW(APXHANDLE hPool, LPCWSTR szAdd);
+void            apxSetInprocEnvironment();
 
 
 LPVOID  AplFillMemory(PVOID Destination, SIZE_T Length, BYTE Fill);
diff --git a/src/native/windows/src/javajni.c b/src/native/windows/src/javajni.c
index c932e17..256a812 100644
--- a/src/native/windows/src/javajni.c
+++ b/src/native/windows/src/javajni.c
@@ -191,12 +191,14 @@ static __inline BOOL __apxJvmDetach(LPAPXJAVAVM lpJava)
         return TRUE;
 }
 
-static BOOL __apxLoadJvmDll(LPCWSTR szJvmDllPath)
+static BOOL __apxLoadJvmDll(APXHANDLE hPool, LPCWSTR szJvmDllPath)
 {
     UINT errMode;
     WCHAR  jreAltPath[SIZ_PATHLEN];
     LPWSTR dllJvmPath = (LPWSTR)szJvmDllPath;
     DYNLOAD_FPTR_DECLARE(SetDllDirectoryW);
+    DWORD  i, l = 0;
+    WCHAR  jreBinPath[SIZ_PATHLEN];
 
     if (!IS_INVALID_HANDLE(_st_sys_jvmDllHandle))
         return TRUE;    /* jvm.dll is already loaded */
@@ -233,6 +235,21 @@ static BOOL __apxLoadJvmDll(LPCWSTR szJvmDllPath)
     /* Suppress the not found system popup message */
     errMode = SetErrorMode(SEM_FAILCRITICALERRORS);
 
+    lstrlcpyW(jreBinPath, SIZ_PATHLEN, dllJvmPath);
+
+    for (i = lstrlenW(jreBinPath); i > 0, l < 2; i--) {
+        if (jreBinPath[i] == L'\\' || jreBinPath[i] == L'/') {
+            jreBinPath[i] = L'\0';
+            l++;
+        }
+    }
+
+    /* Add java bin path to the PATH to fix loading of awt.dll */
+    apxAddToPathW(hPool, jreBinPath);
+
+    /* Set the environment using putenv, so JVM can use it */
+    apxSetInprocEnvironment();
+
     apxLogWrite(APXLOG_MARK_DEBUG "loading jvm '%S'", dllJvmPath);
     _st_sys_jvmDllHandle = LoadLibraryExW(dllJvmPath, NULL, 0);
     if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle) &&
@@ -240,26 +257,19 @@ static BOOL __apxLoadJvmDll(LPCWSTR szJvmDllPath)
         /* There is a file but cannot be loaded.
          * Try to load the MSVCRTxx.dll before JVM.dll
          */
-        WCHAR  jreBinPath[SIZ_PATHLEN];
         WCHAR  crtBinPath[SIZ_PATHLEN];
-        DWORD  i, l = 0;
 
         lstrlcpyW(jreBinPath, SIZ_PATHLEN, dllJvmPath);
-        for (i = lstrlenW(jreBinPath); i > 0, l < 2; i--) {
-            if (jreBinPath[i] == L'\\' || jreBinPath[i] == L'/') {
-                jreBinPath[i] = L'\0';
-                lstrlcpyW(crtBinPath, SIZ_PATHLEN, jreBinPath);
-                lstrlcatW(crtBinPath, SIZ_PATHLEN, MSVCRT71_DLLNAME);
-                if (GetFileAttributesW(crtBinPath) != INVALID_FILE_ATTRIBUTES) {
-                    if (LoadLibraryW(crtBinPath)) {
-                        /* Found MSVCRTxx.dll
-                         */
-                        apxLogWrite(APXLOG_MARK_DEBUG "preloaded '%S'",
-                                    crtBinPath);
-                        break;
-                    }
+        if(l == 2) {
+            lstrlcpyW(crtBinPath, SIZ_PATHLEN, jreBinPath);
+            lstrlcatW(crtBinPath, SIZ_PATHLEN, MSVCRT71_DLLNAME);
+            if (GetFileAttributesW(crtBinPath) != INVALID_FILE_ATTRIBUTES) {
+                if (LoadLibraryW(crtBinPath)) {
+                    /* Found MSVCRTxx.dll
+                     */
+                    apxLogWrite(APXLOG_MARK_DEBUG "preloaded '%S'",
+                                crtBinPath);
                 }
-                l++;
             }
         }
     }
@@ -269,19 +279,11 @@ static BOOL __apxLoadJvmDll(LPCWSTR szJvmDllPath)
                                               LOAD_WITH_ALTERED_SEARCH_PATH);
 
     if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle)) {
-        WCHAR  jreBinPath[SIZ_PATHLEN];
-        DWORD  i, l = 0;
-
-        lstrlcpyW(jreBinPath, SIZ_PATHLEN, dllJvmPath);
         DYNLOAD_FPTR_ADDRESS(SetDllDirectoryW, KERNEL32);
-        for (i = lstrlenW(jreBinPath); i > 0, l < 2; i--) {
-            if (jreBinPath[i] == L'\\' || jreBinPath[i] == L'/') {
-                jreBinPath[i] = L'\0';
-                DYNLOAD_CALL(SetDllDirectoryW)(jreBinPath);
-                apxLogWrite(APXLOG_MARK_DEBUG "Setting DLL search path to '%S'",
+        if(l == 2) {
+            DYNLOAD_CALL(SetDllDirectoryW)(jreBinPath);
+            apxLogWrite(APXLOG_MARK_DEBUG "Setting DLL search path to '%S'",
                             jreBinPath);
-                l++;
-            }
         }
         _st_sys_jvmDllHandle = LoadLibraryExW(dllJvmPath, NULL, 0);
         if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle))
@@ -358,7 +360,7 @@ apxCreateJava(APXHANDLE hPool, LPCWSTR szJvmDllPath)
     JavaVM       *lpJvm = NULL;
     struct       APX_JDK1_1InitArgs jArgs1_1;
 
-    if (!__apxLoadJvmDll(szJvmDllPath))
+    if (!__apxLoadJvmDll(hPool, szJvmDllPath))
         return NULL;