From 0058bc063e1fb4faf1a490c99f6430ef80f44fba Mon Sep 17 00:00:00 2001 From: Markus Linnala Date: Thu, 3 Oct 2019 15:39:09 +0300 Subject: [PATCH] feature: service_linux: Support systemd watchdog --- dnscrypt-proxy/privilege_linux.go | 4 +++- dnscrypt-proxy/privilege_others.go | 4 +++- dnscrypt-proxy/proxy.go | 4 +++- dnscrypt-proxy/service_android.go | 3 ++- dnscrypt-proxy/service_linux.go | 27 +++++++++++++++++++++++++-- dnscrypt-proxy/service_others.go | 3 ++- dnscrypt-proxy/service_windows.go | 4 +++- 7 files changed, 41 insertions(+), 8 deletions(-) diff --git a/dnscrypt-proxy/privilege_linux.go b/dnscrypt-proxy/privilege_linux.go index 85d19dbe..5e73037e 100644 --- a/dnscrypt-proxy/privilege_linux.go +++ b/dnscrypt-proxy/privilege_linux.go @@ -47,7 +47,9 @@ func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) { dlog.Fatal(err) } - ServiceManagerReadyNotify() + if err := ServiceManagerReadyNotify(); err != nil { + dlog.Fatal(err) + } args = append(args, "-child") diff --git a/dnscrypt-proxy/privilege_others.go b/dnscrypt-proxy/privilege_others.go index 64a9bca0..895d299f 100644 --- a/dnscrypt-proxy/privilege_others.go +++ b/dnscrypt-proxy/privilege_others.go @@ -48,7 +48,9 @@ func (proxy *Proxy) dropPrivilege(userStr string, fds []*os.File) { dlog.Fatal(err) } - ServiceManagerReadyNotify() + if err := ServiceManagerReadyNotify(); err != nil { + dlog.Fatal(err) + } args = append(args, "-child") diff --git a/dnscrypt-proxy/proxy.go b/dnscrypt-proxy/proxy.go index cd25f59f..53ae94fc 100644 --- a/dnscrypt-proxy/proxy.go +++ b/dnscrypt-proxy/proxy.go @@ -166,7 +166,9 @@ func (proxy *Proxy) StartProxy() { if liveServers > 0 { dlog.Noticef("dnscrypt-proxy is ready - live servers: %d", liveServers) if !proxy.child { - ServiceManagerReadyNotify() + if err := ServiceManagerReadyNotify(); err != nil { + dlog.Fatal(err) + } } } else if err != nil { dlog.Error(err) diff --git a/dnscrypt-proxy/service_android.go b/dnscrypt-proxy/service_android.go index 7efa55fa..34d6b799 100644 --- a/dnscrypt-proxy/service_android.go +++ b/dnscrypt-proxy/service_android.go @@ -6,5 +6,6 @@ func ServiceManagerStartNotify() error { return nil } -func ServiceManagerReadyNotify() { +func ServiceManagerReadyNotify() error { + return nil } diff --git a/dnscrypt-proxy/service_linux.go b/dnscrypt-proxy/service_linux.go index 25ec024c..09a7d181 100644 --- a/dnscrypt-proxy/service_linux.go +++ b/dnscrypt-proxy/service_linux.go @@ -2,13 +2,36 @@ package main -import "github.com/coreos/go-systemd/daemon" +import ( + "github.com/coreos/go-systemd/daemon" + clocksmith "github.com/jedisct1/go-clocksmith" +) func ServiceManagerStartNotify() error { daemon.SdNotify(false, "STATUS=Starting") return nil } -func ServiceManagerReadyNotify() { +func ServiceManagerReadyNotify() error { daemon.SdNotify(false, "READY=1") + return systemDWatchdog() +} + +func systemDWatchdog() error { + watchdogFailureDelay, err := daemon.SdWatchdogEnabled(false) + if err != nil || watchdogFailureDelay == 0 { + return err + } + refreshInterval := watchdogFailureDelay / 3 + go func() { + for { + // TODO There could be other checks this just + // checks program is not totally stuck and can + // run this goroutine + daemon.SdNotify(false, "WATCHDOG=1") + clocksmith.Sleep(refreshInterval) + } + + }() + return nil } diff --git a/dnscrypt-proxy/service_others.go b/dnscrypt-proxy/service_others.go index 50a01289..3396c4b9 100644 --- a/dnscrypt-proxy/service_others.go +++ b/dnscrypt-proxy/service_others.go @@ -6,5 +6,6 @@ func ServiceManagerStartNotify() error { return nil } -func ServiceManagerReadyNotify() { +func ServiceManagerReadyNotify() error { + return nil } diff --git a/dnscrypt-proxy/service_windows.go b/dnscrypt-proxy/service_windows.go index 3be2a170..0a53d8aa 100644 --- a/dnscrypt-proxy/service_windows.go +++ b/dnscrypt-proxy/service_windows.go @@ -11,4 +11,6 @@ func ServiceManagerStartNotify() error { return nil } -func ServiceManagerReadyNotify() {} +func ServiceManagerReadyNotify() error { + return nil +}