#!/usr/sbin/openrc-run

description="Kill all remaining processes"

depend()
{
	# OpenRC: "after X" => start after X, stop *before* X.
	# Wanted stop order: umountnfs -> sendsigs -> umountfs -> umountroot.
	after umountfs
}

stop()
{
	local OMITPIDS omitfile omitdir pidfile pid seq alldead

	report_unkillable() {
		if [ -x /usr/bin/pstree ] ; then
			echo "Currently running processes (pstree):"
			pstree
		elif [ -x /bin/ps ] ; then
			echo "Currently running processes (ps):"
			ps -ef
		fi
	}

	OMITPIDS=

	for omitfile in /run/sendsigs.omit; do
		if [ -e $omitfile ]; then
			for pid in $(cat $omitfile); do
				OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
			done
		fi
	done

	for omitdir in /run/sendsigs.omit.d; do
		if [ -d "${omitdir}" ]; then
			for pidfile in "${omitdir}/"*; do
				[ -f "$pidfile" ] || continue
				for pid in $(cat $pidfile); do
					OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
				done
			done
		fi
	done

	if [ -x /sbin/initctl ]; then
		for pid in $(initctl list | sed -n -e "/process [0-9]/s/.*process //p"); do
			OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
		done
	fi

	sync

	ebegin "Asking all remaining processes to terminate"
	killall5 -15 $OMITPIDS
	eend 0
	alldead=""
	for seq in 1 2 3 4 5 6 7 8 9 10; do
		if [ -x /sbin/initctl ]; then
		    for pid in $(initctl list | sed -n -e "/process [0-9]/s/.*process //p"); do
			OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
		    done
		fi
		if killall5 -18 $OMITPIDS ; then
		    :
		else
		    alldead=1
		    break
		fi

		sleep 1
	done
	if [ -z "$alldead" ] ; then
	    report_unkillable
	    ebegin "Killing all remaining processes"
	    killall5 -9 $OMITPIDS
	    eend 1
	else
	    einfo "All processes ended within $seq seconds"
	fi
}

start()
{
	:
}
