PageRenderTime 316ms CodeModel.GetById 311ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/init.d/sendsigs

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