#!/bin/bash
### BEGIN INIT INFO
# Provides:            killall
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start:       0 2 6
# Default-Stop:
# Short-Description:   It kills all processes.
# Description:         It kills all processes.
# X-LFS-Provided-By:   LFS
### END INIT INFO

[ -z "${1-}" ] || [ "$1" = start ] || exit 0

# Source function library.
. /etc/init.d/functions

omitpid=
omitpid_msg=
killpids()
{
	echo_msg "$1"
	killall5 "-$2" $omitpid
	local rc=$?
	local msg="sig=$2${omitpid_msg:+ (except $omitpid_msg)}"
	case "$rc" in
		2) passed "$msg"; exit 0 ;;
		0) success "$msg" ;;
		*) failure "$msg" ;;
	esac
	return $rc
}

[ -n "${IGNORE_OMITPIDS-}" ] ||
for pid in /.initrd/killall/[0-9]*; do
	[ -f "$pid" ] || break
	pid=${pid##*/}
	omitpid="$omitpid -o $pid"

	exe="$(readlink "/proc/$pid/exe" 2>/dev/null)" ||:
	omitpid_msg="${omitpid_msg:+$omitpid_msg }$pid${exe:+[$exe]}"
done

seq=5
killpids 'Asking all remaining processes to terminate:' 15 ||
for seq in 1 2 3 4 5; do
	killall5 -18 $omitpid || break
	sleep 1
done
[ "$seq" = 5 ] || exit 0

killpids 'Killing all remaining processes:' 9
