1 | #!/bin/sh |
---|
2 | # |
---|
3 | # A slightly faster way of ending processes in a chroot. |
---|
4 | # Copyright (c) 2012 Massachusetts Institute of Technology |
---|
5 | # |
---|
6 | # This is a derivative work of, and licensed under the same terms as, |
---|
7 | # 15killprocs from the schroot (1.4.17-1) package, whose license |
---|
8 | # information appears below: |
---|
9 | # |
---|
10 | # Copyright © 2007 Kees Cook <kees@outflux.net> |
---|
11 | # Copyright © 2007-2009 Roger Leigh <rleigh@debian.org> |
---|
12 | # |
---|
13 | # schroot is free software: you can redistribute it and/or modify it |
---|
14 | # under the terms of the GNU General Public License as published by |
---|
15 | # the Free Software Foundation, either version 3 of the License, or |
---|
16 | # (at your option) any later version. |
---|
17 | # |
---|
18 | # schroot is distributed in the hope that it will be useful, but |
---|
19 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | # General Public License for more details. |
---|
22 | # |
---|
23 | # You should have received a copy of the GNU General Public License |
---|
24 | # along with this program. If not, see |
---|
25 | # <http://www.gnu.org/licenses/>. |
---|
26 | # |
---|
27 | ##################################################################### |
---|
28 | |
---|
29 | set -e |
---|
30 | |
---|
31 | . "$SETUP_DATA_DIR/common-data" |
---|
32 | . "$SETUP_DATA_DIR/common-functions" |
---|
33 | |
---|
34 | if [ -f "$CHROOT_SCRIPT_CONFIG" ]; then |
---|
35 | . "$CHROOT_SCRIPT_CONFIG" |
---|
36 | elif [ "$STATUS" = "ok" ]; then |
---|
37 | fatal "script-config file '$CHROOT_SCRIPT_CONFIG' does not exist" |
---|
38 | fi |
---|
39 | |
---|
40 | # Wrapper around kill command. Turns errors into |
---|
41 | # warnings when running in verbose mode, otherwise |
---|
42 | # it ignores them. |
---|
43 | # args: parameters for kill |
---|
44 | kill_proc() |
---|
45 | { |
---|
46 | if ! kill "$@" 2>/dev/null; then |
---|
47 | info "kill $@ failed: process already terminated?" |
---|
48 | fi |
---|
49 | } |
---|
50 | |
---|
51 | # Kill all processes that were run from within the chroot environment |
---|
52 | # $1: mount base location |
---|
53 | do_kill_all() |
---|
54 | { |
---|
55 | if [ -z "$1" ]; then |
---|
56 | fatal "No path for finding stray processes: not reaping processes in chroot" |
---|
57 | fi |
---|
58 | |
---|
59 | info "Killing processes run inside $1" |
---|
60 | # Don't use a pipe into a read loop, just use a normal for loop |
---|
61 | pids= |
---|
62 | for pid in $(ls /proc | egrep '^[[:digit:]]+$'); do |
---|
63 | # Check if process root are the same device/inode as chroot |
---|
64 | # root (for efficiency) |
---|
65 | if [ /proc/"$pid"/root -ef "$1" ]; then |
---|
66 | # Check if process and chroot root are the same (may be |
---|
67 | # different even if device/inode match). |
---|
68 | root=$(readlink /proc/"$pid"/root || true) |
---|
69 | if [ "$root" = "$1" ]; then |
---|
70 | exe=$(readlink /proc/"$pid"/exe || true) |
---|
71 | info "Killing left-over pid $pid (${exe##$1})" |
---|
72 | info " Sending SIGTERM to pid $pid" |
---|
73 | |
---|
74 | kill_proc -TERM "$pid" |
---|
75 | # Save the pid |
---|
76 | pids="$pids $pid" |
---|
77 | fi |
---|
78 | fi |
---|
79 | done |
---|
80 | # Wait one second, as most things will respond to TERM gracefully |
---|
81 | sleep 1 |
---|
82 | for pid in $pids; do |
---|
83 | count=0 |
---|
84 | max=5 |
---|
85 | while [ -d /proc/"$pid" ]; do |
---|
86 | count=$(( $count + 1 )) |
---|
87 | info " Waiting for pid $pid to shut down... ($count/$max)" |
---|
88 | sleep 1 |
---|
89 | # Wait for $max seconds for process to die before -9'ing it |
---|
90 | if [ "$count" -eq "$max" ]; then |
---|
91 | info " Sending SIGKILL to pid $pid" |
---|
92 | kill_proc -KILL "$pid" |
---|
93 | sleep 1 |
---|
94 | break |
---|
95 | fi |
---|
96 | done |
---|
97 | done |
---|
98 | } |
---|
99 | |
---|
100 | if [ $STAGE = "setup-recover" ] || [ $STAGE = "setup-stop" ]; then |
---|
101 | do_kill_all "$CHROOT_MOUNT_LOCATION" |
---|
102 | fi |
---|