JDK-4873419 : (process) Process.exitValue() returns random value if child killed (lnx)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2003-06-03
  • Updated: 2003-07-18
  • Resolved: 2003-07-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Relates :  
Description
If you use Runtime.exec() on Linux to create a process which is subsequently
killed by a signal, the exit value returned by
Process.waitFor() and
Process.exitValue() is unexpected.

Consider this code:

------------------------------------------------------------------------
public class KilledExitValue {

    public static void main (String[] args) throws Exception {
	Process proc = Runtime.getRuntime().exec
	    (new String[] { "sh", "-c", "kill -9 $$" });
	System.out.println(proc.waitFor());
	System.out.println(proc.exitValue());
    }
}
----------------------------------------------------------------------
On Solaris, this prints:
9
9

On Linux, it prints a random value greater than 128.
Java 1.5 prints
248
248

while 1.4.2 prints
129
129

The Solaris behavior does not allow one to distinguish between a process
that did "exit (9)" and one that was killed by signal 9.

The intent on Linux is that in case of a signal, the exit value is
128 + signo, but the implementation is buggy.

The correct and expected behavior is to always return 128+signo,
which allows one to distinguish between normal and abnormal termination,
and is exactly the behavior of Unix shells:

$ sh -c 'kill -9 $$'; echo $?
Killed
137
###@###.### 2003-06-03

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b12
14-06-2004

PUBLIC COMMENTS -
10-06-2004

SUGGESTED FIX The Linux code includes this line (from /UNIXProcess_md.c.linux) status = 0x80 + WTERMSIG(status); but that is clearly incorrect, because the status variable has never been initialized. The intent was clearly to do this: status = 0x80 + WTERMSIG(info); ###@###.### 2003-06-03
03-06-2003

WORK AROUND If one wants to distinguish between processes that have exited and ones that have been killed, one needs to write a wrapper that transforms signal kills to regular return values, for example in the range 64-127. ###@###.### 2003-06-03
03-06-2003

EVALUATION This will be fixed as part of the work for 4790606, by fixing the bug in the Linux code and using that on Solaris as well. ###@###.### 2003-06-03
03-06-2003