1 |
commit: 24365d81911373e036ca684f1bc47f7d4a253637 |
2 |
Author: Thomas Deutschmann <whissi <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Oct 2 12:34:20 2019 +0000 |
4 |
Commit: Thomas Deutschmann <whissi <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Oct 2 14:43:00 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=24365d81 |
7 |
|
8 |
gen_funcs.sh: cleanup(): Try to kill all running child processes before cleanup |
9 |
|
10 |
If genkernel was aborted, it maybe possible that child processes are still |
11 |
running which maybe prevent cleanup. |
12 |
|
13 |
With this commit, cleanup() will try to kill all running child processes. |
14 |
|
15 |
Signed-off-by: Thomas Deutschmann <whissi <AT> gentoo.org> |
16 |
|
17 |
gen_funcs.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ |
18 |
1 file changed, 46 insertions(+) |
19 |
|
20 |
diff --git a/gen_funcs.sh b/gen_funcs.sh |
21 |
index cfe53b9..0e7c5ba 100755 |
22 |
--- a/gen_funcs.sh |
23 |
+++ b/gen_funcs.sh |
24 |
@@ -438,6 +438,52 @@ setup_cache_dir() { |
25 |
} |
26 |
|
27 |
cleanup() { |
28 |
+ # Child processes we maybe want to kill can only appear in |
29 |
+ # current session |
30 |
+ local session=$(ps -o sess= ${$} 2>/dev/null | awk '{ print $1 }') |
31 |
+ if [ -n "${session}" ] |
32 |
+ then |
33 |
+ # Time to kill any still running child process. |
34 |
+ # All our childs will have GK_SHARE environment variable set. |
35 |
+ local -a killed_pids |
36 |
+ |
37 |
+ local pid_to_kill= |
38 |
+ while IFS= read -r -u 3 pid_to_kill |
39 |
+ do |
40 |
+ # Don't kill ourselves or we will trigger trap |
41 |
+ [ "${pid_to_kill}" = "${BASHPID}" ] && continue |
42 |
+ |
43 |
+ # Killing process group allows us to catch grandchilds |
44 |
+ # with clean environment, too. |
45 |
+ if kill -${pid_to_kill} &>/dev/null |
46 |
+ then |
47 |
+ killed_pids+=( ${pid_to_kill} ) |
48 |
+ fi |
49 |
+ done 3< <(ps e -s ${session} 2>/dev/null | grep GK_SHARE= 2>/dev/null | awk '{ print $1 }') |
50 |
+ |
51 |
+ if [ ${#killed_pids[@]} -gt 0 ] |
52 |
+ then |
53 |
+ # Be patient -- still running process could prevent cleanup! |
54 |
+ sleep 3 |
55 |
+ |
56 |
+ # Add one valid pid so that ps command won't fail |
57 |
+ killed_pids+=( ${BASHPID} ) |
58 |
+ |
59 |
+ killed_pids=$(IFS=,; echo "${killed_pids[*]}") |
60 |
+ |
61 |
+ # Processes had enough time to gracefully terminate! |
62 |
+ while IFS= read -r -u 3 pid_to_kill |
63 |
+ do |
64 |
+ # Don't kill ourselves or we will trigger trap |
65 |
+ [ "${pid_to_kill}" = "${BASHPID}" ] && continue |
66 |
+ |
67 |
+ kill -9 -${pid_to_kill} &>/dev/null |
68 |
+ done 3< <(ps --no-headers -q ${killed_pids} 2>/dev/null | awk '{ print $1 }') |
69 |
+ fi |
70 |
+ else |
71 |
+ print_warning 1 "Failed to determine session leader; Will not try to stop child processes" |
72 |
+ fi |
73 |
+ |
74 |
if isTrue "${CLEANUP}" |
75 |
then |
76 |
if [ -n "${TEMP}" -a -d "${TEMP}" ] |