@omar Not sure what you mean by "unskippable infinite time-out"? #FreeBSD's #shutdown will send SIGKILL to daemons that don't die eventually. Making it reliable just means to a) make sure this won't happen by handling #SIGTERM as quick as possible (here I close all client connections and do the necessary cleanup like remove the pidfile) and b) recover gracefully from when it happened nevertheless (e.g. detect whether a #pidfile is stale, possible to do reliably by proper locking, and then remove it on startup).

BTW, the daemon currently weighs in at roughly 30k loc in #C, including bundled dependencies and build tooling. Very little of that is spent on "best practices daemon behavior" (and it could be reduced further by e.g. using the BSD's daemon(3) function, but that would sacrifice portability).

When writing a #daemon that follows best practices (handling of #detaching with a locked #pidfile, and #SIGHUP for #configuration #reload), an extremely simple "init script" will do (reliably!) for #FreeBSD's mewburn-rc. 😎

#C #coding #swad

Too Good To #014

In today’s installment:

- #dmesg from before the machine crashed
- #Kill process ID from #pidfile
- Let #systemd retry a task
- Set #MTU on #OpenVPN connections in #Networkmanager

https://binblog.de/2025/04/14/too-good-to-014/

#Blogpost

Too Good To #014 | #!/bin/blog

@charadon

Oh clever! Here's my implementation:

if [[ -e $pidfile ]]
then
if (( $(wc -l < $pidfile) < 2 )) && grep -qE "^[0-9]+$" $pidfile
then
#pidfile format ok
pid=$(cat $pidfile)
if ps axo pid |grep -q "^ *$pid"
then
die "$progname already running on pid $pid"
else
#Pid not currently running
warn "Stale $pidfile found, deleting"
rm $pidfile || die "couldn't delete $pidfile"
...