2
0
mirror of git://github.com/lxc/lxc synced 2025-08-31 21:29:27 +00:00

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 <christian@iwakd.de>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Christian Seiler
2013-03-30 15:45:38 +01:00
committed by Stéphane Graber
parent f3ca99fd5f
commit 818fd9c752

View File

@@ -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"