From 366eda548b2299631de08ddff209d4185ebc0d44 Mon Sep 17 00:00:00 2001 From: Chris Nauroth Date: Tue, 23 Jul 2013 22:13:04 +0000 Subject: [PATCH] HADOOP-9759. Merging change r1506325 from trunk to branch-2. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1506329 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../org/apache/hadoop/util/NativeCodeLoader.c | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index e86cf293816..1557ebe56be 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -368,6 +368,9 @@ Release 2.1.0-beta - 2013-07-02 HADOOP-9738. TestDistCh fails. (jing9 via kihwal) + HADOOP-9759. Add support for NativeCodeLoader#getLibraryName on Windows. + (Chuan Liu via cnauroth) + BREAKDOWN OF HADOOP-8562 SUBTASKS AND RELATED JIRAS HADOOP-8924. Hadoop Common creating package-info.java must not depend on diff --git a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/util/NativeCodeLoader.c b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/util/NativeCodeLoader.c index 88d9240c99f..146e1600914 100644 --- a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/util/NativeCodeLoader.c +++ b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/util/NativeCodeLoader.c @@ -38,9 +38,41 @@ JNIEXPORT jboolean JNICALL Java_org_apache_hadoop_util_NativeCodeLoader_buildSup JNIEXPORT jstring JNICALL Java_org_apache_hadoop_util_NativeCodeLoader_getLibraryName (JNIEnv *env, jclass clazz) { +#ifdef UNIX Dl_info dl_info; int ret = dladdr( Java_org_apache_hadoop_util_NativeCodeLoader_getLibraryName, &dl_info); return (*env)->NewStringUTF(env, ret==0 ? "Unavailable" : dl_info.dli_fname); +#endif + +#ifdef WINDOWS + SIZE_T ret = 0; + DWORD size = MAX_PATH; + LPWSTR filename = NULL; + HMODULE mod = NULL; + DWORD err = ERROR_SUCCESS; + + MEMORY_BASIC_INFORMATION mbi; + ret = VirtualQuery(Java_org_apache_hadoop_util_NativeCodeLoader_getLibraryName, + &mbi, sizeof(mbi)); + if (ret == 0) goto cleanup; + mod = mbi.AllocationBase; + + do { + filename = (LPWSTR) realloc(filename, size * sizeof(WCHAR)); + if (filename == NULL) goto cleanup; + GetModuleFileName(mod, filename, size); + size <<= 1; + err = GetLastError(); + } while (err == ERROR_INSUFFICIENT_BUFFER); + + if (err != ERROR_SUCCESS) goto cleanup; + + return (*env)->NewString(env, filename, (jsize) wcslen(filename)); + +cleanup: + if (filename != NULL) free(filename); + return (*env)->NewStringUTF(env, "Unavailable"); +#endif }