Permalink
Browse files

Add option to show the exit code if timeout kills

The --confess or -c option now makes timeout wait for the child to die
when it is terminating a process, and show its return code instead of
zero.
  • Loading branch information...
pshved committed Sep 15, 2012
1 parent 534e065 commit 62c77b764184382de533bfaf1f82edb1939a3103
Showing with 35 additions and 8 deletions.
  1. +12 −0 README
  2. +23 −8 timeout
View
12 README
@@ -70,9 +70,21 @@ Advanced options:
* `--no-info-on-success` - disable printing usage statistics if the
controlled process has been successfully terminated.
+* `--confess`, `-c` - when killing the controlled process, return its exit
+ code or signal+128. This also makes timeout to wait until the controlled
+ process is terminated. Without this option, the script returns zero.
+
More options may be read in the script itself. More documentation will be
added in the future releases!
+Exit code of the script is the exit code of the controlled process. If the
+controlled process was killed by a signal, the exit code is 128+N, where N is
+the number of the signal. This simulates Bash exit code policy. If the
+controlled process was terminated by the timeout script itself the script
+returns zero because having the timeout terminate the child is expected
+behavior. If you want the child's return code in such a situation (which may
+be nonzero if the child handles SIGTERM), use `--confess` option.
+
EXAMPLES
========
View
31 timeout
@@ -69,6 +69,7 @@ GetOptions(
'debug!'=>\$debug,
'just-kill'=>\$just_kill,
'info-on-success!'=>\$info_on_success,
+ 'confess|c!'=>\$confess,
) or usage;
@ARGV or usage;
@@ -169,13 +170,7 @@ while ($status eq 'wait'){
if ($arrived == $blackbox_pid){
# Child process terminated.
# "Simulate" shell behavior, when signal code is returned as exit code. See http://www.gnu.org/software/bash/manual/html_node/Exit-Status.html for more info.
- if (($child_retv > 0) && (($child_retv >> 8) == 0)){
- # The 8th bit indicates if the core was dumped. If it was not, we are to add 128 anyway, so just set the bit.
- $box_status = $child_retv | 128;
- }else{
- # This is also executed when there was no error, and the result is zero.
- $box_status = $child_retv >> 8;
- }
+ $box_status = child_status_to_exit_code($child_retv);
$status = 'exit'
}elsif ($arrived == -1){
# Something happened!
@@ -190,7 +185,14 @@ while ($status eq 'wait'){
# have some sleep for output to be flushed
usleep($uflush_time);
print_uinfo($reason,$uinfo);
- exit 0;
+ # If we killed the child process, we may need to return its error code.
+ if ($confess) {
+ if (waitpid($blackbox_pid,0) != -1){
+ exit(child_status_to_exit_code($?));
+ }
+ }else{
+ exit 0;
+ }
}
}
}
@@ -480,3 +482,16 @@ sub limits_exceeded
}
return undef;
}
+
+# Convert child exit status to exit code. Follow Bash way.
+sub child_status_to_exit_code
+{
+ my ($child_retv) = @_;
+ if (($child_retv > 0) && (($child_retv >> 8) == 0)){
+ # The 8th bit indicates if the core was dumped. If it was not, we are to add 128 anyway, so just set the bit.
+ return $child_retv | 128;
+ }else{
+ # This is also executed when there was no error, and the result is zero.
+ return $child_retv >> 8;
+ }
+}

0 comments on commit 62c77b7

Please sign in to comment.