#!/bin/bash

CHILDREN=()

# Maybe better create a process group or a cgroup
_term(){
	for (( i = ${#CHILDREN[@]}-1; i >= 0 ; i-- ))
	do
		# kill process group (process and its children)
		kill -- "${CHILDREN[$i]}"
	done
}
trap '_term; exit 0' SIGTERM

# Example of script /etc/kdm-greeter.d/foo:
# exec onboard
# If onboard does not have to be killed when this script receives SIGTERM,
# then dedetach the process: "onboard &"
while read -r line
do
	"$line" &
	CHILDREN+=($!)
done < <(find /etc/kdm-greeter.d -maxdepth 1 -type f -executable | sort -h)

# We have started everything, wait for SIGTERM from kdm_greet
# sleep is an external program, wait is a bash built-in function
sleep infinity &
sleep_pid=$!
CHILDREN+=("$sleep_pid")
wait "$sleep_pid"
