| 1 |
#! /bin/sh |
| 2 |
### BEGIN INIT INFO |
| 3 |
# Provides: sendsigs |
| 4 |
# Required-Start: |
| 5 |
# Required-Stop: umountnfs |
| 6 |
# Default-Start: |
| 7 |
# Default-Stop: 0 6 |
| 8 |
# Short-Description: Kill all remaining processes. |
| 9 |
# Description: |
| 10 |
### END INIT INFO |
| 11 |
|
| 12 |
PATH=/usr/sbin:/usr/bin:/sbin:/bin |
| 13 |
|
| 14 |
. /lib/lsb/init-functions |
| 15 |
|
| 16 |
# Make it possible to see who the misbehaving processes are |
| 17 |
report_unkillable() { |
| 18 |
if [ -x /usr/bin/pstree ]; then |
| 19 |
echo "Currently running processes (pstree):" |
| 20 |
pstree |
| 21 |
elif [ -x /bin/ps ]; then |
| 22 |
echo "Currently running processes (ps):" |
| 23 |
ps -ef |
| 24 |
fi |
| 25 |
} |
| 26 |
|
| 27 |
do_stop() { |
| 28 |
OMITPIDS= |
| 29 |
|
| 30 |
for omitfile in /run/sendsigs.omit; do |
| 31 |
if [ -e $omitfile ]; then |
| 32 |
for pid in $(cat $omitfile); do |
| 33 |
OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid" |
| 34 |
done |
| 35 |
fi |
| 36 |
done |
| 37 |
|
| 38 |
# Load sendsigs.omit.d/packagename files too, to make it |
| 39 |
# possible for scripts that need to modify the list of pids at |
| 40 |
# run time without race conditions. |
| 41 |
for omitdir in /run/sendsigs.omit.d; do |
| 42 |
if [ -d "${omitdir}" ]; then |
| 43 |
for pidfile in "${omitdir}/"*; do |
| 44 |
[ -f "$pidfile" ] || continue |
| 45 |
for pid in $(cat $pidfile); do |
| 46 |
OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid" |
| 47 |
done |
| 48 |
done |
| 49 |
fi |
| 50 |
done |
| 51 |
|
| 52 |
# Upstart jobs have their own "stop on" clauses that sends |
| 53 |
# SIGTERM/SIGKILL just like this, so if they're still running, |
| 54 |
# they're supposed to be |
| 55 |
if [ -x /sbin/initctl ]; then |
| 56 |
for pid in $(initctl list | sed -n -e "/process [0-9]/s/.*process //p"); do |
| 57 |
OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid" |
| 58 |
done |
| 59 |
fi |
| 60 |
|
| 61 |
# Flush the kernel I/O buffer before we start to kill |
| 62 |
# processes, to make sure the IO of already stopped services to |
| 63 |
# not slow down the remaining processes to a point where they |
| 64 |
# are accidentily killed with SIGKILL because they did not |
| 65 |
# manage to shut down in time. |
| 66 |
sync |
| 67 |
|
| 68 |
# Kill all processes. |
| 69 |
log_action_begin_msg "Asking all remaining processes to terminate" |
| 70 |
killall5 -15 $OMITPIDS # SIGTERM |
| 71 |
log_action_end_msg 0 |
| 72 |
alldead="" |
| 73 |
for seq in 1 2 3 4 5 6 7 8 9 10; do |
| 74 |
# use SIGCONT/signal 18 to check if there are |
| 75 |
# processes left. No need to check the exit code |
| 76 |
# value, because either killall5 work and it make |
| 77 |
# sense to wait for processes to die, or it fail and |
| 78 |
# there is nothing to wait for. |
| 79 |
|
| 80 |
# did an upstart job start since we last polled initctl? check |
| 81 |
# again on each loop and add any new jobs (e.g., plymouth) to |
| 82 |
# the list. If we did miss one starting up, this beats waiting |
| 83 |
# 10 seconds before shutting down. |
| 84 |
if [ -x /sbin/initctl ]; then |
| 85 |
for pid in $(initctl list | sed -n -e "/process [0-9]/s/.*process //p"); do |
| 86 |
OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid" |
| 87 |
done |
| 88 |
fi |
| 89 |
if killall5 -18 $OMITPIDS; then |
| 90 |
: |
| 91 |
else |
| 92 |
alldead=1 |
| 93 |
break |
| 94 |
fi |
| 95 |
|
| 96 |
sleep 1 |
| 97 |
done |
| 98 |
if [ -z "$alldead" ]; then |
| 99 |
report_unkillable |
| 100 |
log_action_begin_msg "Killing all remaining processes" |
| 101 |
killall5 -9 $OMITPIDS # SIGKILL |
| 102 |
log_action_end_msg 1 |
| 103 |
else |
| 104 |
log_action_begin_msg "All processes ended within $seq seconds" |
| 105 |
log_action_end_msg 0 |
| 106 |
fi |
| 107 |
} |
| 108 |
|
| 109 |
case "$1" in |
| 110 |
start | status) |
| 111 |
# No-op |
| 112 |
;; |
| 113 |
restart | reload | force-reload) |
| 114 |
echo "Error: argument '$1' not supported" >&2 |
| 115 |
exit 3 |
| 116 |
;; |
| 117 |
stop) |
| 118 |
do_stop |
| 119 |
;; |
| 120 |
*) |
| 121 |
echo "Usage: $0 start|stop" >&2 |
| 122 |
exit 3 |
| 123 |
;; |
| 124 |
esac |
| 125 |
|
| 126 |
: |