#!/sbin/openrc-run
# shellcheck shell=sh disable=SC2034 # OpenRC consumes these service variables.
# OpenRC init script for the Sermo service supervisor.
#
# Sermo runs as root (OpenRC's default for a service with no command_user): it
# inspects and signals other users' processes, controls the service manager and
# opens raw ICMP sockets. The operation engine only ever signals processes that
# exactly match a service's kill_only_if selector (resolved exe + real UID).

name="sermod"
description="Sermo service supervisor"

command="/usr/bin/sermod"
command_args="run --config /etc/sermo/sermo.yml"
command_background="yes"
pidfile="/run/sermod.pid"
retry="${SERMOD_RETRY:-TERM/30/KILL/5}"

# Capture daemon output to a log file.
output_log="/var/log/sermod.log"
error_log="/var/log/sermod.log"

depend() {
	need net
	after firewall
}

start_pre() {
	# Owner-only (root) directories, matching the systemd tmpfiles.d config.
	checkpath --directory --mode 0700 /run/sermo
	# Persistent state database (monitoring state, restored across reboots).
	checkpath --directory --mode 0700 /var/lib/sermo
}

reload() {
	ebegin "Reloading ${RC_SVCNAME} configuration"
	# Match both the init-managed pidfile and the exact daemon executable before
	# sending HUP, so a stale pidfile cannot target an unrelated process.
	start-stop-daemon --signal HUP --pidfile "${pidfile}" --exec "${command}"
	eend $?
}
