From: Christian Seiler Date: Sat, 30 Mar 2013 14:45:38 +0000 (+0100) Subject: lxc-shutdown: Make all processes exit before timeout if shutdown works X-Git-Tag: lxc-0.9.0~1^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=818fd9c7529fa22b16f214b4feedaa311497053f;p=thirdparty%2Flxc.git lxc-shutdown: Make all processes exit before timeout if shutdown works The following rationale is for using the -t option: Currently, lxc-shutdown uses a subprocess for the timeout handling, where a 'sleep $TIMEOUT' is executed, which will kill the main process after the timeout has occurred, thus causing the main process to stop the container hard with lxc-stop. On the other hand, if the timeout is not reached, the main process kills the subprocess. The trouble now is that if you kill a shell that is running in the background, the kill will only take effect as soon as the program currently running in the shell exits. This in turn means that the subprocess will never terminate before reaching the timeout. In an interactive shell, this does not matter, since people will just not notice the process and lxc-shutdown returns immediately. In a non-interactive enironment, however, there may be circumstances that cause the calling program to wait until even that subprocess is terminated, which means that shutdown will always take as long as the timeout, even if the container shuts down quite a bit earlier. This change makes sure that also all subprocesses of the background process are killed from the main process. This will immediately terminate the background process, thus ensuring the desired behaviour. Signed-off-by: Christian Seiler Acked-by: Serge E. Hallyn --- diff --git a/src/lxc/lxc-shutdown.in b/src/lxc/lxc-shutdown.in index d82cebf32..ee07f7595 100644 --- a/src/lxc/lxc-shutdown.in +++ b/src/lxc/lxc-shutdown.in @@ -131,7 +131,7 @@ fi if [ $timeout != "-1" ]; then trap dolxcstop EXIT - alarm $$ $timeout & + alarm $$ $timeout 2>/dev/null & alarmpid=$! fi @@ -141,7 +141,9 @@ done if [ $timeout != "-1" ]; then trap - EXIT - kill $alarmpid + # include subprocesses; otherwise, we may have to wait until sleep completes + # if called from a non-interactive context + kill $alarmpid $(ps --no-headers --ppid $alarmpid -o pid) 2>/dev/null || : fi echo "Container $lxc_name has shut down"