#!/bin/sh # # FreeBSD rc.d script for the Alarm Sender headless receiver. # # # install -d first: a stock FreeBSD has NO /usr/local/bin until some # # package creates it, and the install below otherwise fails outright. # install -d -m 0755 /usr/local/bin /usr/local/etc/rc.d /usr/local/etc/alarmsender # install -m 0755 alarmsenderd /usr/local/bin/alarmsenderd # install -m 0755 alarmsenderd.rc /usr/local/etc/rc.d/alarmsenderd # install -m 0644 agent.conf.example /usr/local/etc/alarmsender/agent.conf # $EDITOR /usr/local/etc/alarmsender/agent.conf # set the hook # alarmsenderd test-hook # prove it rings # alarmsenderd pair XXXX-XXXX # code from the console # service alarmsenderd onecheckconf # would it start? # sysrc alarmsenderd_enable=YES # service alarmsenderd start # # Works on FreeBSD and on the appliances built from it — TrueNAS CORE, # OPNsense, pfSense — where /usr/local/etc/rc.d is the place for a service that # is not part of the base system. # # ## Why this is not a translation of the systemd unit # # Two things do not carry over, and both changed the design. # # **systemd's RestartPreventExitStatus=2 has no rc.d equivalent.** The agent # exits 2 for the things a restart cannot fix: not paired, a revoked token, a # typo in the configuration. On Linux the unit refuses to restart that and # leaves the one log line that explains it. daemon(8) can restart a child # (-r) but cannot be told to respect an exit code, so a supervised agent that # cannot start would be relaunched for ever and the explanation would scroll # away. # # The answer is to refuse earlier: start_precmd runs `alarmsenderd check`, # which exits 2 for exactly those conditions, so `service alarmsenderd start` # fails loudly with the reason instead of launching something that dies. A # token revoked while the agent is already running is the one case left, and # it is covered by making the restart delay long enough that the log stays # readable. # # **The agent does not need supervision to survive a network outage.** It # reconnects on its own, for ever, with backoff — a dropped connection never # ends the process. So -r is here for a crash or an OOM kill, not for the # ordinary case, and alarmsenderd_restart=NO is a reasonable choice for an # operator who would rather a dead agent stay dead and be noticed. # # ## Paths # # /usr/local/etc and /var/db, not /etc and /var/lib. The binary resolves both # on its own (see internal/config/paths_bsd.go) — /var/lib does not exist on # FreeBSD at all, and creating it would leave the device token somewhere no # FreeBSD administrator, backup or jail template would think to look. # PROVIDE: alarmsenderd # REQUIRE: LOGIN DAEMON NETWORKING # KEYWORD: shutdown # ------------------------------------------------------------------------- # rc.conf(5) settings, with their defaults: # # alarmsenderd_enable (bool): NO — set with: sysrc alarmsenderd_enable=YES # alarmsenderd_config (path): /usr/local/etc/alarmsender/agent.conf # alarmsenderd_statedir (path): empty; the binary's own default (/var/db/alarmsender) # alarmsenderd_user (user): root. See the note below before changing it. # alarmsenderd_restart (bool): YES — relaunch on a crash # alarmsenderd_restart_delay: 30 (seconds) # alarmsenderd_flags (str): extra flags, e.g. -d for debug logging # # root is the default deliberately, and for the same reason as the systemd # unit: the useful handlers on an appliance are privileged. ipmitool, a GPIO # write, a relay, wall. Dropping privileges is the right thing to do when the # handler does not need them, and the wrong thing to discover at three in the # morning when it silently cannot run. If you do change it, run # `alarmsenderd test-hook` as that user first, and pair again — the state # directory belongs to whoever created it. # ------------------------------------------------------------------------- . /etc/rc.subr name="alarmsenderd" desc="Alarm Sender headless receiver" rcvar=alarmsenderd_enable load_rc_config $name : ${alarmsenderd_enable:="NO"} : ${alarmsenderd_config:="/usr/local/etc/alarmsender/agent.conf"} : ${alarmsenderd_statedir:=""} : ${alarmsenderd_user:="root"} : ${alarmsenderd_restart:="YES"} : ${alarmsenderd_restart_delay:="30"} : ${alarmsenderd_flags:=""} alarmsenderd_bin="/usr/local/bin/alarmsenderd" # pidfile is what `service stop` and `service status` act on, and procname has # to name the executable of whatever pid is IN that file. Those are different # processes depending on supervision, and getting the pair wrong is how a # running service reports itself stopped: # # supervised pidfile holds daemon(8)'s pid -> procname /usr/sbin/daemon # unsupervised pidfile holds the agent's pid -> procname the agent # # This was wrong in the first version: procname was the agent in both cases, # which reads sensibly. rc.subr then compared the supervisor's pid against the # agent's executable, found no match, and reported "alarmsenderd is not # running" while the agent was connected and ringing alarms -- `service # restart` and `service stop` failed the same way. Only a real rc.subr showed # it. The stubbed check in tools/check-rc-script.sh had asserted the wrong # thing, because it encoded the same assumption, so it agreed with the bug. child_pidfile="/var/run/${name}.pid" supervisor_pidfile="/var/run/${name}-supervisor.pid" # The agent's own arguments. --config is passed explicitly rather than left to # discovery so that rc.conf is the single answer to "which file is this service # using", and so `service alarmsenderd status` and a hand-run # `alarmsenderd check` cannot disagree. # agent_base is everything before the subcommand, so `run`, `check` and # `test-hook` are all invoked with identical settings. Kept as its own # variable rather than stripped back off agent_args with a suffix expansion: # that worked, and it was the sort of line a reader has to stop and decode. agent_base="--config ${alarmsenderd_config}" if [ -n "$alarmsenderd_statedir" ]; then agent_base="${agent_base} --state ${alarmsenderd_statedir}" fi if [ -n "$alarmsenderd_flags" ]; then agent_base="${agent_base} ${alarmsenderd_flags}" fi agent_args="${agent_base} run" # -S sends the agent's stdout and stderr to syslog, which is where a FreeBSD # operator looks; -T tags every line so `grep alarmsenderd /var/log/messages` # finds them. Without this the output goes to /dev/null and a daemon whose only # interface is its log becomes silent. # # -f closes daemon(8)'s OWN standard descriptors. Without it daemon inherits # whatever invoked `service alarmsenderd start` and holds it open for the life # of the service, so `out=$(service alarmsenderd start)` never returns -- the # command substitution waits for every writer of the pipe to close. Anything # that captures the output of a start hangs for ever: a provisioning run, a CI # step, a shell pipeline. Measured on FreeBSD 14.5, where it hung this # project's own test harness; with -f the same call returns in under a second. # The child's output still reaches syslog, because -S captures it through a # pipe before daemon touches its own descriptors. command="/usr/sbin/daemon" command_args="-f -S -T ${name} -p ${child_pidfile}" if checkyesno alarmsenderd_restart; then command_args="${command_args} -r -R ${alarmsenderd_restart_delay} -P ${supervisor_pidfile}" # Stop the supervisor, not the child: kill the child of a running # daemon -r and it is relaunched a moment later, which looks exactly # like a service that refuses to stop. pidfile="$supervisor_pidfile" procname="$command" else pidfile="$child_pidfile" procname="$alarmsenderd_bin" fi command_args="${command_args} ${alarmsenderd_bin} ${agent_args}" start_precmd="alarmsenderd_precmd" extra_commands="checkconf testhook" checkconf_cmd="alarmsenderd_checkconf" testhook_cmd="alarmsenderd_testhook" alarmsenderd_precmd() { if [ ! -x "$alarmsenderd_bin" ]; then err 1 "$alarmsenderd_bin is missing or not executable" fi if [ ! -f "$alarmsenderd_config" ]; then err 1 "no configuration at $alarmsenderd_config Copy the example and set a hook: install -d -m 0755 /usr/local/etc/alarmsender install -m 0644 agent.conf.example $alarmsenderd_config" fi # The whole reason this function exists. `check` exits 2 when the agent # could not start — unpaired, revoked, unparseable config — and prints # why. Refusing here turns "the service starts and then is gone" into one # clear failure, which matters more on FreeBSD than on Linux because # nothing below can decline to restart a hopeless child. # # It is local and offline on purpose: a box rebooting during an internet # outage must still bring its own alarm receiver up. if ! ${alarmsenderd_bin} ${agent_base} check; then err 1 "alarmsenderd check failed — fix the above, then start again" fi } # NOTE the `one` prefix before the service is enabled: # `service alarmsenderd onecheckconf`. rc.subr gates every command, extra ones # included, behind $rcvar -- and on a disabled service the un-prefixed form # prints "Cannot 'checkconf' ... use 'onecheckconf' instead" and then **exits # 0**, which is a check reporting success without having run. Measured on # FreeBSD 14.5, 2026-11-05. alarmsenderd_checkconf() { ${alarmsenderd_bin} ${agent_base} check } # `service alarmsenderd testhook` — raise a synthetic alarm through the real # handler, as the user the service runs as, with no pairing and no network. # This is the command to reach for when somebody says an alarm did not arrive. alarmsenderd_testhook() { if [ "$(id -u)" = 0 ] && [ "$alarmsenderd_user" != root ]; then su -m "$alarmsenderd_user" -c \ "${alarmsenderd_bin} ${agent_base} test-hook" else ${alarmsenderd_bin} ${agent_base} test-hook fi } run_rc_command "$1"