source: trunk/debathena/third/schroot/etc/setup.d/15killprocs @ 24167

Revision 24167, 2.6 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
  • Property svn:executable set to *
Line 
1#!/bin/sh
2# Copyright © 2007       Kees Cook <kees@outflux.net>
3# Copyright © 2007-2009  Roger Leigh <rleigh@debian.org>
4#
5# schroot is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# schroot is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see
17# <http://www.gnu.org/licenses/>.
18#
19#####################################################################
20
21set -e
22
23if [ -f "$CHROOT_SCRIPT_CONFIG" ]; then
24    . "$CHROOT_SCRIPT_CONFIG"
25elif [ "$2" = "ok" ]; then
26    echo "script-config file '$CHROOT_SCRIPT_CONFIG' does not exist"
27    exit 1
28fi
29
30# Kill all processes that were run from within the chroot environment
31# $1: mount base location
32do_kill_all()
33{
34    if [ -z "$1" ]; then
35        echo "No path for finding stray processes: not reaping processes in chroot"
36        exit 0
37    fi
38
39    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
40        echo "Killing processes run inside $1"
41    fi
42    ls /proc | egrep '^[[:digit:]]+$' |
43    while read pid; do
44        root=$(readlink /proc/"$pid"/root || true)
45        if [ "$root" = "$1" ]; then
46            if [ "$AUTH_VERBOSITY" = "verbose" ]; then
47                exe=$(readlink /proc/"$pid"/exe || true)
48                echo "Killing left-over pid $pid (${exe##$1})"
49                echo "  Sending SIGTERM to pid $pid"
50            fi
51
52            /bin/kill -TERM "$pid" 2>/dev/null
53
54            count=0
55            max=5
56            while [ -d /proc/"$pid" ]; do
57                count=$(( $count + 1 ))
58                if [ "$AUTH_VERBOSITY" = "verbose" ]; then
59                    echo "  Waiting for pid $pid to shut down... ($count/$max)"
60                fi
61                sleep 1
62                # Wait for $max seconds for process to die before -9'ing it
63                if [ "$count" -eq "$max" ]; then
64                    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
65                        echo "  Sending SIGKILL to pid $pid"
66                    fi
67                    /bin/kill -KILL "$pid" 2>/dev/null
68                    sleep 1
69                    break
70                fi
71            done
72        fi
73    done
74}
75
76if [ $1 = "setup-recover" ] || [ $1 = "setup-stop" ]; then
77    do_kill_all "$CHROOT_MOUNT_LOCATION"
78fi
Note: See TracBrowser for help on using the repository browser.