Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FD leak with Runtime.exec(). #567

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions classpath/java-lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HANDLE>(pid), 1);
CloseHandle(reinterpret_cast<HANDLE>(in));
CloseHandle(reinterpret_cast<HANDLE>(out));
CloseHandle(reinterpret_cast<HANDLE>(err));
#else
throwNew(e, "java/io/Exception", strdup("Not supported on WinRT/WinPhone8"));
#endif
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 11 additions & 4 deletions classpath/java/lang/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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() {
Expand Down