#!/usr/bin/execlineb -P
# watchdog in foreground (-F), matching debian/sv/watchdog.
# Pause unless run_watchdog=1 in /etc/default/watchdog (runit exits 161).

envfile -I /etc/default/watchdog
importas -uD "0" run_watchdog run_watchdog
importas -sCuD "" watchdog_options watchdog_options
importas -uD "none" watchdog_module watchdog_module
envfile -I /etc/s6-rc/config/watchdog.conf
importas -sCuD "-F" Args Args

ifelse { test "${run_watchdog}" = 1 } {
	foreground {
		ifelse { test "${watchdog_module}" != none } {
			/sbin/modprobe ${watchdog_module}
		}
		exit 0
	}
	# Only one feeder may own /dev/watchdog.
	foreground {
		ifelse { test -x /usr/sbin/wd_keepalive } {
			start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/wd_keepalive.pid --exec /usr/sbin/wd_keepalive
		}
		exit 0
	}
	foreground { rm -f /run/sendsigs.omit.d/wd_keepalive.pid }
	fdmove -c 2 1
	exec /usr/sbin/watchdog ${Args} ${watchdog_options}
}
s6-pause
