325N/A/*
325N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A#include <windows.h>
325N/A#include <stdlib.h>
325N/A#include <string.h>
325N/A#include <Psapi.h>
325N/A
325N/A#include "jni.h"
325N/A#include "jni_util.h"
325N/A
325N/A#include "sun_tools_attach_WindowsAttachProvider.h"
325N/A
325N/A/*
325N/A * Class: sun_tools_attach_WindowsAttachProvider
325N/A * Method: tempPath
325N/A * Signature: ()Ljava/lang/String;
325N/A */
325N/AJNIEXPORT jstring JNICALL
325N/AJava_sun_tools_attach_WindowsAttachProvider_tempPath(JNIEnv *env, jclass cls)
325N/A{
325N/A char buf[256];
325N/A DWORD bufLen, actualLen;
325N/A jstring result = NULL;
325N/A
325N/A bufLen = sizeof(buf) / sizeof(char);
325N/A actualLen = GetTempPath(bufLen, buf);
325N/A if (actualLen > 0) {
325N/A char* bufP = buf;
325N/A if (actualLen > bufLen) {
325N/A actualLen += sizeof(char);
325N/A bufP = (char*)malloc(actualLen * sizeof(char));
325N/A actualLen = GetTempPath(actualLen, bufP);
325N/A }
325N/A if (actualLen > 0) {
325N/A result = JNU_NewStringPlatform(env, bufP);
325N/A }
325N/A if (bufP != buf) {
325N/A free((void*)bufP);
325N/A }
325N/A }
325N/A return result;
325N/A}
325N/A
325N/A/*
325N/A * Class: sun_tools_attach_WindowsAttachProvider
325N/A * Method: volumeFlags
325N/A * Signature: ()J
325N/A */
325N/AJNIEXPORT jlong JNICALL
325N/AJava_sun_tools_attach_WindowsAttachProvider_volumeFlags(JNIEnv *env, jclass cls, jstring str)
325N/A{
325N/A jboolean isCopy;
325N/A const char* volume;
325N/A DWORD result = 0;
325N/A
325N/A volume = JNU_GetStringPlatformChars(env, str, &isCopy);
325N/A if (volume != NULL) {
325N/A DWORD componentLen, flags;
325N/A BOOL res = GetVolumeInformation(volume,
325N/A NULL,
325N/A 0,
325N/A NULL,
325N/A &componentLen,
325N/A &flags,
325N/A NULL,
325N/A 0);
325N/A if (res != 0) {
325N/A result = flags;
325N/A }
325N/A if (isCopy) {
325N/A JNU_ReleaseStringPlatformChars(env, str, volume);
325N/A }
325N/A }
325N/A return result;
325N/A}
325N/A
325N/A
325N/A/*
325N/A * Class: sun_tools_attach_WindowsAttachProvider
325N/A * Method: enumProcesses
325N/A * Signature: ([JI)I
325N/A */
325N/AJNIEXPORT jint JNICALL
325N/AJava_sun_tools_attach_WindowsAttachProvider_enumProcesses(JNIEnv *env, jclass cls,
325N/A jintArray arr, jint max)
325N/A{
325N/A DWORD size, bytesReturned;
325N/A DWORD* ptr;
325N/A jint result = 0;
325N/A
325N/A size = max * sizeof(DWORD);
325N/A ptr = (DWORD*)malloc(size);
325N/A if (ptr != NULL) {
325N/A BOOL res = EnumProcesses(ptr, size, &bytesReturned);
325N/A if (res != 0) {
325N/A result = (jint)(bytesReturned / sizeof(DWORD));
325N/A (*env)->SetIntArrayRegion(env, arr, 0, (jsize)result, (jint*)ptr);
325N/A }
325N/A free((void*)ptr);
325N/A }
325N/A return result;
325N/A}
325N/A
325N/A/*
325N/A * Class: sun_tools_attach_WindowsAttachProvider
325N/A * Method: isLibraryLoadedByProcess
325N/A * Signature: (I[Ljava/lang/String;)Z
325N/A */
325N/AJNIEXPORT jboolean JNICALL
325N/AJava_sun_tools_attach_WindowsAttachProvider_isLibraryLoadedByProcess(JNIEnv *env, jclass cls,
325N/A jstring str, jint processId)
325N/A{
325N/A HANDLE hProcess;
325N/A jboolean isCopy;
325N/A const char* lib;
325N/A DWORD size, bytesReturned;
325N/A HMODULE* ptr;
325N/A jboolean result = JNI_FALSE;
325N/A
325N/A hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
325N/A PROCESS_VM_READ,
325N/A FALSE, (DWORD)processId);
325N/A if (hProcess == NULL) {
325N/A return JNI_FALSE;
325N/A }
325N/A lib = JNU_GetStringPlatformChars(env, str, &isCopy);
325N/A if (lib == NULL) {
325N/A CloseHandle(hProcess);
325N/A return JNI_FALSE;
325N/A }
325N/A
325N/A /*
325N/A * Enumerate the modules that the process has opened and see if we have a
325N/A * match.
325N/A */
325N/A size = 1024 * sizeof(HMODULE);
325N/A ptr = (HMODULE*)malloc(size);
325N/A if (ptr != NULL) {
325N/A BOOL res = EnumProcessModules(hProcess, ptr, size, &bytesReturned);
325N/A if (res != 0) {
325N/A int count = bytesReturned / sizeof(HMODULE);
325N/A int i = 0;
325N/A while (i < count) {
325N/A char base[256];
325N/A BOOL res = GetModuleBaseName(hProcess, ptr[i], base, sizeof(base));
325N/A if (res != 0) {
325N/A if (strcmp(base, lib) == 0) {
325N/A result = JNI_TRUE;
325N/A break;
325N/A }
325N/A }
325N/A i++;
325N/A }
325N/A }
325N/A free((void*)ptr);
325N/A }
325N/A if (isCopy) {
325N/A JNU_ReleaseStringPlatformChars(env, str, lib);
325N/A }
325N/A CloseHandle(hProcess);
325N/A
325N/A return result;
325N/A}
325N/A