commit:     24365d81911373e036ca684f1bc47f7d4a253637
Author:     Thomas Deutschmann <whissi <AT> gentoo <DOT> org>
AuthorDate: Wed Oct  2 12:34:20 2019 +0000
Commit:     Thomas Deutschmann <whissi <AT> gentoo <DOT> org>
CommitDate: Wed Oct  2 14:43:00 2019 +0000
URL:        https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=24365d81

gen_funcs.sh: cleanup(): Try to kill all running child processes before cleanup

If genkernel was aborted, it maybe possible that child processes are still
running which maybe prevent cleanup.

With this commit, cleanup() will try to kill all running child processes.

Signed-off-by: Thomas Deutschmann <whissi <AT> gentoo.org>

 gen_funcs.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/gen_funcs.sh b/gen_funcs.sh
index cfe53b9..0e7c5ba 100755
--- a/gen_funcs.sh
+++ b/gen_funcs.sh
@@ -438,6 +438,52 @@ setup_cache_dir() {
 }
 
 cleanup() {
+       # Child processes we maybe want to kill can only appear in
+       # current session
+       local session=$(ps -o sess= ${$} 2>/dev/null | awk '{ print $1 }')
+       if [ -n "${session}" ]
+       then
+               # Time to kill any still running child process.
+               # All our childs will have GK_SHARE environment variable set.
+               local -a killed_pids
+
+               local pid_to_kill=
+               while IFS= read -r -u 3 pid_to_kill
+               do
+                       # Don't kill ourselves or we will trigger trap
+                       [ "${pid_to_kill}" = "${BASHPID}" ] && continue
+
+                       # Killing process group allows us to catch grandchilds
+                       # with clean environment, too.
+                       if kill -${pid_to_kill} &>/dev/null
+                       then
+                               killed_pids+=( ${pid_to_kill} )
+                       fi
+               done 3< <(ps e -s ${session} 2>/dev/null | grep GK_SHARE= 
2>/dev/null | awk '{ print $1 }')
+
+               if [ ${#killed_pids[@]} -gt 0 ]
+               then
+                       # Be patient -- still running process could prevent 
cleanup!
+                       sleep 3
+
+                       # Add one valid pid so that ps command won't fail
+                       killed_pids+=( ${BASHPID} )
+
+                       killed_pids=$(IFS=,; echo "${killed_pids[*]}")
+
+                       # Processes had enough time to gracefully terminate!
+                       while IFS= read -r -u 3 pid_to_kill
+                       do
+                               # Don't kill ourselves or we will trigger trap
+                               [ "${pid_to_kill}" = "${BASHPID}" ] && continue
+
+                               kill -9 -${pid_to_kill} &>/dev/null
+                       done 3< <(ps --no-headers -q ${killed_pids} 2>/dev/null 
| awk '{ print $1 }')
+               fi
+       else
+               print_warning 1 "Failed to determine session leader; Will not 
try to stop child processes"
+       fi
+
        if isTrue "${CLEANUP}"
        then
                if [ -n "${TEMP}" -a -d "${TEMP}" ]

Reply via email to