Whether I use this:
process = Runtime.getRuntime().exec("logcat -d time");
process = new ProcessBuilder() .command("logcat", "-d", "time") .redirectErrorStream(true) .start();
I get the same results: it often hangs within the exec() or start() call, no matter what I tried to do! The thread running this cannot even be interrupted with Thread.interrupt()! The child process is definitely started and if killed the above commands return.
These calls may fail on first attempt, so THERE IS NO WAY TO READ THEIR OUTPUT! I can also use a simple "su -c kill xxx" command line, same result!
EDIT: Started debugging the java_lang_ProcessManager.cpp file in an NDK project with some debugging logs! So here is what I found so far, after the fork() the parent does this:
int result; int count = read(statusIn, &result, sizeof(int)); <- hangs there close(statusIn);
Though the child process is not supposed to block on it: That's what the child does (if started at all!):
// Make statusOut automatically close if execvp() succeeds. fcntl(statusOut, F_SETFD, FD_CLOEXEC); <- make the parent will not block // Close remaining unwanted open fds. closeNonStandardFds(statusOut, androidSystemPropertiesFd); <- hangs here sometimes ... execvp(commands, commands); // If we got here, execvp() failed or the working dir was invalid. execFailed: int error = errno; write(statusOut, &error, sizeof(int)); close(statusOut); exit(error);
The child can fail for 2 reproducible reasons: 1- child code is not running, but the parent believes it is! 2- child blocks on closeNonStandardFds(statusOut, androidSystemPropertiesFd);
In either case the read(statusIn...) in the parent ends in deadlock! and a child process is left dead (and cannot be accessed, pid unknown, no Process object)!