-2

How to get internal implementation of private static native void terminateProcess(long handle) i.e. being called by destroyForcibly() method?

As per the below reference

Where to find source code for java.lang native methods?

destroyForcibly() method is not available in this source. Please help.

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
  • native code is usually tight to platform. So what platform are you talking about? – rkosegi Mar 03 '21 at 10:30
  • You can see the Windows implementation of the native methods called by ProcessHandleImpl here: https://github.com/openjdk/jdk/blob/05a764f4ffb8030d6b768f2d362c388e5aabd92d/src/java.base/windows/native/libjava/ProcessHandleImpl_win.c – tgdavies Mar 03 '21 at 10:35
  • have you tried the repository link that is given in the answer of the link you posted? browsing a bit you should find http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/windows/native/java/lang/ProcessImpl_md.c (the github link commented above probably is more actual) –  Mar 03 '21 at 10:35

1 Answers1

2

You can implement it in some programming language with can crate native machine (CPU) code. For example C, C++, Fortran, Pascal, D, Go, or assembly. Java compiler - javac produces a universal byte code i.e. an intermediate representation. When java - virtual machine i.e. an interpreter - convert it to the native CPU code first. So that gives you an ability to run some generic program on different CPU architectures or operating systems, without any modification. This paradigm called - write once, run everywhere. However sometimes your program need some functionality implemented in another programming language, or there is some performance critical functionality which should be boost. In this case java virtual machine have an ability to run some external native function. This function must satisfy specific binary interface. I.e. calling convention and naming convention. This function should be put into platform specific shared library i.e. DLL on windows, so on Linux and most of another unix-like systems except Mac OS X - where it is dynalib. If you really need to use JNI please follow the manual JNI

In any case - in my understanding you are looking for implementation of Process.destroyForcibly It is platform depended an you can found it in OpenJDK public repository libjava For example Unix version looks like following:

/*
 * Class:     java_lang_ProcessHandleImpl
 * Method:    destroy0
 * Signature: (JJZ)Z
 */
JNIEXPORT jboolean JNICALL
Java_java_lang_ProcessHandleImpl_destroy0(JNIEnv *env,
                                          jobject obj,
                                          jlong jpid,
                                          jlong startTime,
                                          jboolean force) {
    pid_t pid = (pid_t) jpid;
    int sig = (force == JNI_TRUE) ? SIGKILL : SIGTERM;
    jlong start = Java_java_lang_ProcessHandleImpl_isAlive0(env, obj, jpid);

    if (start == startTime || start == 0 || startTime == 0) {
        return (kill(pid, sig) < 0) ? JNI_FALSE : JNI_TRUE;
    } else {
        return JNI_FALSE;
    }
}

You can found it at ProcessHandleImpl_unix

Windows version looks like:

/*
 * Destroy the process.
 *
 * Class:     java_lang_ProcessHandleImpl
 * Method:    destroy0
 * Signature: (Z)V
 */
JNIEXPORT jboolean JNICALL
Java_java_lang_ProcessHandleImpl_destroy0(JNIEnv *env,
                                          jclass clazz,
                                          jlong jpid,
                                          jlong startTime,
                                          jboolean force) {
    DWORD pid = (DWORD)jpid;
    jboolean ret = JNI_FALSE;
    HANDLE handle = OpenProcess(PROCESS_TERMINATE | THREAD_QUERY_INFORMATION
                                | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
    if (handle != NULL) {
        jlong start = getStartTime(handle);
        if (start == startTime || startTime == 0) {
            ret = TerminateProcess(handle, 1) ? JNI_TRUE : JNI_FALSE;
        }
        CloseHandle(handle);         // Ignore return code
    }
    return ret;
}

You can found it at ProcessHandleImpl_win.c

As you can see both implementations - are simple wrapper on top of the kill and TerminateProcess system calls.

Victor Gubin
  • 2,782
  • 10
  • 24