J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | #include <windows.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "jni.h" |
| 30 | #include "jni_util.h" |
| 31 | |
| 32 | #include "sun_tools_attach_WindowsAttachProvider.h" |
| 33 | |
| 34 | /* |
| 35 | * Class: sun_tools_attach_WindowsAttachProvider |
| 36 | * Method: tempPath |
| 37 | * Signature: ()Ljava/lang/String; |
| 38 | */ |
| 39 | JNIEXPORT jstring JNICALL |
| 40 | Java_sun_tools_attach_WindowsAttachProvider_tempPath(JNIEnv *env, jclass cls) |
| 41 | { |
| 42 | char buf[256]; |
| 43 | DWORD bufLen, actualLen; |
| 44 | jstring result = NULL; |
| 45 | |
| 46 | bufLen = sizeof(buf) / sizeof(char); |
| 47 | actualLen = GetTempPath(bufLen, buf); |
| 48 | if (actualLen > 0) { |
| 49 | char* bufP = buf; |
| 50 | if (actualLen > bufLen) { |
| 51 | actualLen += sizeof(char); |
| 52 | bufP = (char*)malloc(actualLen * sizeof(char)); |
| 53 | actualLen = GetTempPath(actualLen, bufP); |
| 54 | } |
| 55 | if (actualLen > 0) { |
| 56 | result = JNU_NewStringPlatform(env, bufP); |
| 57 | } |
| 58 | if (bufP != buf) { |
| 59 | free((void*)bufP); |
| 60 | } |
| 61 | } |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Class: sun_tools_attach_WindowsAttachProvider |
| 67 | * Method: volumeFlags |
| 68 | * Signature: ()J |
| 69 | */ |
| 70 | JNIEXPORT jlong JNICALL |
| 71 | Java_sun_tools_attach_WindowsAttachProvider_volumeFlags(JNIEnv *env, jclass cls, jstring str) |
| 72 | { |
| 73 | jboolean isCopy; |
| 74 | const char* volume; |
| 75 | DWORD result = 0; |
| 76 | |
| 77 | volume = JNU_GetStringPlatformChars(env, str, &isCopy); |
| 78 | if (volume != NULL) { |
| 79 | DWORD componentLen, flags; |
| 80 | BOOL res = GetVolumeInformation(volume, |
| 81 | NULL, |
| 82 | 0, |
| 83 | NULL, |
| 84 | &componentLen, |
| 85 | &flags, |
| 86 | NULL, |
| 87 | 0); |
| 88 | if (res != 0) { |
| 89 | result = flags; |
| 90 | } |
| 91 | if (isCopy) { |
| 92 | JNU_ReleaseStringPlatformChars(env, str, volume); |
| 93 | } |
| 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * Process status helper library functions |
| 101 | */ |
| 102 | static BOOL (WINAPI *_EnumProcesses) (DWORD *, DWORD, DWORD *); |
| 103 | static BOOL (WINAPI *_EnumProcessModules)(HANDLE, HMODULE *, DWORD, LPDWORD); |
| 104 | static DWORD (WINAPI *_GetModuleBaseName) (HANDLE, HMODULE, LPTSTR, DWORD); |
| 105 | |
| 106 | |
| 107 | /* |
| 108 | * Class: sun_tools_attach_WindowsAttachProvider |
| 109 | * Method: initializeProcessStatusHelper |
| 110 | * Signature: ()V |
| 111 | */ |
| 112 | JNIEXPORT void JNICALL |
| 113 | Java_sun_tools_attach_WindowsAttachProvider_initializeProcessStatusHelper(JNIEnv *env, jclass cls) |
| 114 | { |
| 115 | HINSTANCE psapi = LoadLibrary("PSAPI.DLL") ; |
| 116 | if (psapi != NULL) { |
| 117 | _EnumProcesses = (BOOL(WINAPI *)(DWORD *, DWORD, DWORD *)) |
| 118 | GetProcAddress(psapi, "EnumProcesses") ; |
| 119 | _EnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) |
| 120 | GetProcAddress(psapi, "EnumProcessModules"); |
| 121 | _GetModuleBaseName = (DWORD(WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD)) |
| 122 | GetProcAddress(psapi, "GetModuleBaseNameA"); |
| 123 | } |
| 124 | |
| 125 | if ((_EnumProcesses == NULL) || |
| 126 | (_EnumProcessModules == NULL) || |
| 127 | (_GetModuleBaseName == NULL)) |
| 128 | { |
| 129 | JNU_ThrowInternalError(env, "Unable to initialize process status helper library"); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | * Class: sun_tools_attach_WindowsAttachProvider |
| 136 | * Method: enumProcesses |
| 137 | * Signature: ([JI)I |
| 138 | */ |
| 139 | JNIEXPORT jint JNICALL |
| 140 | Java_sun_tools_attach_WindowsAttachProvider_enumProcesses(JNIEnv *env, jclass cls, |
| 141 | jintArray arr, jint max) |
| 142 | { |
| 143 | DWORD size, bytesReturned; |
| 144 | DWORD* ptr; |
| 145 | jint result = 0; |
| 146 | |
| 147 | size = max * sizeof(DWORD); |
| 148 | ptr = (DWORD*)malloc(size); |
| 149 | if (ptr != NULL) { |
| 150 | BOOL res = (*_EnumProcesses)(ptr, size, &bytesReturned); |
| 151 | if (res != 0) { |
| 152 | result = (jint)(bytesReturned / sizeof(DWORD)); |
| 153 | (*env)->SetIntArrayRegion(env, arr, 0, (jsize)result, (jint*)ptr); |
| 154 | } |
| 155 | free((void*)ptr); |
| 156 | } |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Class: sun_tools_attach_WindowsAttachProvider |
| 162 | * Method: isLibraryLoadedByProcess |
| 163 | * Signature: (I[Ljava/lang/String;)Z |
| 164 | */ |
| 165 | JNIEXPORT jboolean JNICALL |
| 166 | Java_sun_tools_attach_WindowsAttachProvider_isLibraryLoadedByProcess(JNIEnv *env, jclass cls, |
| 167 | jstring str, jint processId) |
| 168 | { |
| 169 | HANDLE hProcess; |
| 170 | jboolean isCopy; |
| 171 | const char* lib; |
| 172 | DWORD size, bytesReturned; |
| 173 | HMODULE* ptr; |
| 174 | jboolean result = JNI_FALSE; |
| 175 | |
| 176 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | |
| 177 | PROCESS_VM_READ, |
| 178 | FALSE, (DWORD)processId); |
| 179 | if (hProcess == NULL) { |
| 180 | return JNI_FALSE; |
| 181 | } |
| 182 | lib = JNU_GetStringPlatformChars(env, str, &isCopy); |
| 183 | if (lib == NULL) { |
| 184 | CloseHandle(hProcess); |
| 185 | return JNI_FALSE; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Enumerate the modules that the process has opened and see if we have a |
| 190 | * match. |
| 191 | */ |
| 192 | size = 1024 * sizeof(HMODULE); |
| 193 | ptr = (HMODULE*)malloc(size); |
| 194 | if (ptr != NULL) { |
| 195 | BOOL res = (*_EnumProcessModules)(hProcess, ptr, size, &bytesReturned); |
| 196 | if (res != 0) { |
| 197 | int count = bytesReturned / sizeof(HMODULE); |
| 198 | int i = 0; |
| 199 | while (i < count) { |
| 200 | char base[256]; |
| 201 | BOOL res = (*_GetModuleBaseName)(hProcess, ptr[i], base, sizeof(base)); |
| 202 | if (res != 0) { |
| 203 | if (strcmp(base, lib) == 0) { |
| 204 | result = JNI_TRUE; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | i++; |
| 209 | } |
| 210 | } |
| 211 | free((void*)ptr); |
| 212 | } |
| 213 | if (isCopy) { |
| 214 | JNU_ReleaseStringPlatformChars(env, str, lib); |
| 215 | } |
| 216 | CloseHandle(hProcess); |
| 217 | |
| 218 | return result; |
| 219 | } |