Path: blob/master/src/jdk.attach/windows/native/libattach/AttachProviderImpl.c
40932 views
/*1* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include <windows.h>25#include <stdlib.h>26#include <string.h>27#include <Psapi.h>2829#include "jni.h"30#include "jni_util.h"3132#include "sun_tools_attach_AttachProviderImpl.h"3334/*35* Class: sun_tools_attach_AttachProviderImpl36* Method: tempPath37* Signature: ()Ljava/lang/String;38*/39JNIEXPORT jstring JNICALL40Java_sun_tools_attach_AttachProviderImpl_tempPath(JNIEnv *env, jclass cls)41{42char buf[256];43DWORD bufLen, actualLen;44jstring result = NULL;4546bufLen = sizeof(buf) / sizeof(char);47actualLen = GetTempPath(bufLen, buf);48if (actualLen > 0) {49char* bufP = buf;50if (actualLen > bufLen) {51actualLen += sizeof(char);52bufP = (char*)malloc(actualLen * sizeof(char));53actualLen = GetTempPath(actualLen, bufP);54}55if (actualLen > 0) {56result = JNU_NewStringPlatform(env, bufP);57}58if (bufP != buf) {59free((void*)bufP);60}61}62return result;63}6465/*66* Class: sun_tools_attach_AttachProviderImpl67* Method: volumeFlags68* Signature: ()J69*/70JNIEXPORT jlong JNICALL71Java_sun_tools_attach_AttachProviderImpl_volumeFlags(JNIEnv *env, jclass cls, jstring str)72{73jboolean isCopy;74const char* volume;75DWORD result = 0;7677volume = JNU_GetStringPlatformChars(env, str, &isCopy);78if (volume != NULL) {79DWORD componentLen, flags;80BOOL res = GetVolumeInformation(volume,81NULL,820,83NULL,84&componentLen,85&flags,86NULL,870);88if (res != 0) {89result = flags;90}91if (isCopy) {92JNU_ReleaseStringPlatformChars(env, str, volume);93}94}95return result;96}979899/*100* Class: sun_tools_attach_AttachProviderImpl101* Method: enumProcesses102* Signature: ([JI)I103*/104JNIEXPORT jint JNICALL105Java_sun_tools_attach_AttachProviderImpl_enumProcesses(JNIEnv *env, jclass cls,106jintArray arr, jint max)107{108DWORD size, bytesReturned;109DWORD* ptr;110jint result = 0;111112size = max * sizeof(DWORD);113ptr = (DWORD*)malloc(size);114if (ptr != NULL) {115BOOL res = EnumProcesses(ptr, size, &bytesReturned);116if (res != 0) {117result = (jint)(bytesReturned / sizeof(DWORD));118(*env)->SetIntArrayRegion(env, arr, 0, (jsize)result, (jint*)ptr);119}120free((void*)ptr);121}122return result;123}124125/*126* Class: sun_tools_attach_AttachProviderImpl127* Method: isLibraryLoadedByProcess128* Signature: (I[Ljava/lang/String;)Z129*/130JNIEXPORT jboolean JNICALL131Java_sun_tools_attach_AttachProviderImpl_isLibraryLoadedByProcess(JNIEnv *env, jclass cls,132jstring str, jint processId)133{134HANDLE hProcess;135jboolean isCopy;136const char* lib;137DWORD size, bytesReturned;138HMODULE* ptr;139jboolean result = JNI_FALSE;140141hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |142PROCESS_VM_READ,143FALSE, (DWORD)processId);144if (hProcess == NULL) {145return JNI_FALSE;146}147lib = JNU_GetStringPlatformChars(env, str, &isCopy);148if (lib == NULL) {149CloseHandle(hProcess);150return JNI_FALSE;151}152153/*154* Enumerate the modules that the process has opened and see if we have a155* match.156*/157size = 1024 * sizeof(HMODULE);158ptr = (HMODULE*)malloc(size);159if (ptr != NULL) {160BOOL res = EnumProcessModules(hProcess, ptr, size, &bytesReturned);161if (res != 0) {162int count = bytesReturned / sizeof(HMODULE);163int i = 0;164while (i < count) {165char base[256];166BOOL res = GetModuleBaseName(hProcess, ptr[i], base, sizeof(base));167if (res != 0) {168if (strcmp(base, lib) == 0) {169result = JNI_TRUE;170break;171}172}173i++;174}175}176free((void*)ptr);177}178if (isCopy) {179JNU_ReleaseStringPlatformChars(env, str, lib);180}181CloseHandle(hProcess);182183return result;184}185186187