From a8edcfe8232634bbdc56aff31556058f4359eec3 Mon Sep 17 00:00:00 2001 From: cclohmar Date: Wed, 29 Jul 2026 09:53:54 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20systemd=20path=20unit=20for=20reload=20?= =?UTF-8?q?=E2=80=94=20plugin=20touches=20trigger=20file,=20systemd=20relo?= =?UTF-8?q?ads=20as=20root?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 21 +++++++++++++++++++++ server.go | 14 ++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/install.sh b/install.sh index 6572cc7..d5f3eaa 100755 --- a/install.sh +++ b/install.sh @@ -354,8 +354,29 @@ EOF [Service] Restart=on-failure RestartSec=5 +EOF + # Add path unit: watches for reload trigger, runs systemctl reload dnsmasq as root + cat > /etc/systemd/system/dnsmasq-reload.path << 'EOF' +[Unit] +Description=Watch dnsmasq config for reload trigger + +[Path] +PathChanged=/opt/zoraxy/conf/dhcp/dnsmasq.conf.reload + +[Install] +WantedBy=multi-user.target +EOF + cat > /etc/systemd/system/dnsmasq-reload.service << 'EOF' +[Unit] +Description=Reload dnsmasq on config change + +[Service] +Type=oneshot +ExecStart=/usr/bin/systemctl reload dnsmasq EOF systemctl daemon-reload + systemctl enable dnsmasq-reload.path 2>/dev/null || true + systemctl start dnsmasq-reload.path 2>/dev/null || true systemctl enable dnsmasq 2>/dev/null || true systemctl restart dnsmasq 2>/dev/null || service dnsmasq restart 2>/dev/null || true log "dnsmasq restarted" diff --git a/server.go b/server.go index 567b452..b7cd7a4 100644 --- a/server.go +++ b/server.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "os/exec" "regexp" "strconv" "strings" @@ -24,8 +23,6 @@ var ( // Override via environment: LEASE_FILE, CONF_FILE, RELOAD_CMD, RELOAD_ARGS leasesFile = envOrDefault("LEASE_FILE", "/opt/zoraxy/conf/dhcp/dnsmasq.leases") confFile = envOrDefault("CONF_FILE", "/opt/zoraxy/conf/dhcp/dnsmasq.conf") - reloadCmd = envOrDefault("RELOAD_CMD", "/usr/bin/sudo") - reloadArgs = strings.Fields(envOrDefault("RELOAD_ARGS", "pkill -HUP dnsmasq")) ) func envOrDefault(key, fallback string) string { @@ -242,16 +239,17 @@ func unpinLease(mac string) error { // reloadDnsmasq runs the reload command (e.g. sudo systemctl reload dnsmasq). func reloadDnsmasq() error { - // Scrub expired leases before reloading so the UI stays clean. + // Scrub expired leases before reloading. if err := cleanLeaseFile(); err != nil { log.Printf("WARNING: failed to clean lease file: %v", err) - // Continue — reload is still useful even if cleaning fails. } - cmd := exec.Command(reloadCmd, reloadArgs...) - output, err := cmd.CombinedOutput() + // Touch a trigger file — systemd.path unit watches this and reloads dnsmasq. + triggerFile := confFile + ".reload" + f, err := os.Create(triggerFile) if err != nil { - return fmt.Errorf("reload failed: %w — %s", err, string(output)) + return fmt.Errorf("create trigger file: %w", err) } + f.Close() return nil }