#!/bin/sh # Called by rollcall-dnsmasq.path when rollcall rewrites dnsmasq's files. # # Host files (leases, names) only need a HUP. When the generated configuration # changed, dnsmasq is restarted. If it does not come back (a conflicting # hand-written file in /etc/dnsmasq.d, for instance - `dnsmasq --test` misses # some of those), the last configuration that worked is put back and dnsmasq # is started with it, so DHCP stays up. The rejected content is kept next to it # and not tried again until it changes. set -u D=/var/lib/rollcall/dnsmasq CONF=$D/conf.d/rollcall.conf GOOD=$D/rollcall.conf.accepted BAD=$D/rollcall.conf.rejected STAMP=/run/rollcall-dnsmasq.sum sum() { cat "$1" 2>/dev/null | sha256sum | cut -d' ' -f1; } restore() { # put the last working configuration back (or none) if [ -s "$GOOD" ]; then cp "$GOOD" "$CONF.tmp" && mv -f "$CONF.tmp" "$CONF"; else rm -f "$CONF"; fi sum "$CONF" > "$STAMP" } new=$(sum "$CONF") if [ "$new" = "$(cat "$STAMP" 2>/dev/null)" ]; then systemctl kill -s HUP dnsmasq.service exit 0 fi if [ -e "$CONF" ] && [ "$new" = "$(sum "$BAD")" ]; then restore echo "generated dnsmasq configuration was rejected before and has not changed; keeping the last working one" >&2 exit 1 fi if out=$(dnsmasq --test 2>&1) && systemctl restart dnsmasq.service; then echo "$new" > "$STAMP" if [ -e "$CONF" ]; then cp "$CONF" "$GOOD"; else rm -f "$GOOD"; fi rm -f "$BAD" echo "dnsmasq restarted: generated configuration changed" exit 0 fi why=$(journalctl -u dnsmasq.service -n 5 --no-pager -o cat 2>/dev/null | grep -v '^$' | tail -3 | tr '\n' ' ') [ -e "$CONF" ] && cp "$CONF" "$BAD" restore if systemctl restart dnsmasq.service; then echo "generated dnsmasq configuration rejected ($out $why); dnsmasq runs with the last working one, the rejected file is $BAD" >&2 exit 1 fi # not even that starts: something outside rollcall's file conflicts. Run dnsmasq # without rollcall's file so whatever else is configured keeps working. rm -f "$CONF" sum "$CONF" > "$STAMP" systemctl restart dnsmasq.service || true echo "dnsmasq does not start with rollcall's configuration ($why); started without it - check /etc/dnsmasq.conf and /etc/dnsmasq.d for conflicting options" >&2 exit 1