From e97a3d15c933962687e212a9c17de0942075fc63 Mon Sep 17 00:00:00 2001 From: Leonard van der Stel Date: Sun, 28 Oct 2018 22:45:18 -0400 Subject: [PATCH] Fix FD leak with Runtime.exec(). --- classpath/java-lang.cpp | 10 ++++++++-- classpath/java/lang/Runtime.java | 15 +++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 75f89088c..975f1354f 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -495,10 +495,13 @@ extern "C" JNIEXPORT jint JNICALL } extern "C" JNIEXPORT void JNICALL - Java_java_lang_Runtime_kill(JNIEnv* e UNUSED, jclass, jlong pid) + Java_java_lang_Runtime_kill(JNIEnv* e UNUSED, jclass, jlong pid, jint in, jint out, jint err) { #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) TerminateProcess(reinterpret_cast(pid), 1); + CloseHandle(reinterpret_cast(in)); + CloseHandle(reinterpret_cast(out)); + CloseHandle(reinterpret_cast(err)); #else throwNew(e, "java/io/Exception", strdup("Not supported on WinRT/WinPhone8")); #endif @@ -798,9 +801,12 @@ extern "C" JNIEXPORT jint JNICALL } extern "C" JNIEXPORT void JNICALL - Java_java_lang_Runtime_kill(JNIEnv*, jclass, jlong pid) + Java_java_lang_Runtime_kill(JNIEnv*, jclass, jlong pid, jint in, jint out, jint err) { kill((pid_t)pid, SIGTERM); + close(in); + close(out); + close(err); } Locale getLocale() diff --git a/classpath/java/lang/Runtime.java b/classpath/java/lang/Runtime.java index e73d74b35..1073f3a1c 100644 --- a/classpath/java/lang/Runtime.java +++ b/classpath/java/lang/Runtime.java @@ -120,7 +120,7 @@ private static native void exec(String[] command, long[] process) private static native int waitFor(long pid, long tid); - private static native void kill(long pid); + private static native void kill(long pid, int in, int out, int err); public native void gc(); @@ -148,10 +148,17 @@ public MyProcess(long pid, long tid, int in, int out, int err) { this.err = err; } + @Override + protected void finalize() throws Throwable { + destroy(); + super.finalize(); + } + public void destroy() { - if (pid != 0) { - kill(pid); - } + if (pid != 0) { + kill(pid, in, out, err); + pid = 0; + } } public InputStream getInputStream() {