From 8111fcae11bbe1bcf5055abd17b4ea11a7b435bc Mon Sep 17 00:00:00 2001 From: Nate Brown Date: Mon, 24 Jul 2023 09:51:21 -0500 Subject: [PATCH 1/2] Attempt to notify systemd of service readiness on linux --- cmd/nebula/main.go | 1 + cmd/nebula/notify_linux.go | 42 +++++++++++++++++++++++++ cmd/nebula/notify_notlinux.go | 10 ++++++ dist/arch/nebula.service | 2 ++ dist/fedora/nebula.service | 2 ++ examples/service_scripts/nebula.service | 2 ++ 6 files changed, 59 insertions(+) create mode 100644 cmd/nebula/notify_linux.go create mode 100644 cmd/nebula/notify_notlinux.go diff --git a/cmd/nebula/main.go b/cmd/nebula/main.go index e9b285e7a..9461035b1 100644 --- a/cmd/nebula/main.go +++ b/cmd/nebula/main.go @@ -65,6 +65,7 @@ func main() { if !*configTest { ctrl.Start() + notifyReady(l) ctrl.ShutdownBlock() } diff --git a/cmd/nebula/notify_linux.go b/cmd/nebula/notify_linux.go new file mode 100644 index 000000000..53952b903 --- /dev/null +++ b/cmd/nebula/notify_linux.go @@ -0,0 +1,42 @@ +package main + +import ( + "net" + "os" + "time" + + "github.com/sirupsen/logrus" +) + +// SdNotifyReady tells systemd the service is ready and dependent services can now be started +// https://www.freedesktop.org/software/systemd/man/sd_notify.html +// https://www.freedesktop.org/software/systemd/man/systemd.service.html +const SdNotifyReady = "READY=1" + +func notifyReady(l *logrus.Logger) { + sockName := os.Getenv("NOTIFY_SOCKET") + if sockName == "" { + l.Debugln("NOTIFY_SOCKET systemd env var not set, not sending ready signal") + return + } + + conn, err := net.DialTimeout("unixgram", sockName, time.Second) + if err != nil { + l.WithError(err).Debugln("failed to connect to systemd notification socket") + return + } + defer conn.Close() + + err = conn.SetWriteDeadline(time.Now().Add(time.Second)) + if err != nil { + l.WithError(err).Debugln("failed to set the write deadline for the systemd notification socket") + return + } + + if _, err = conn.Write([]byte(SdNotifyReady)); err != nil { + l.WithError(err).Debugln("failed to signal the systemd notification socket") + return + } + + l.Debugln("notified systemd the service is ready") +} diff --git a/cmd/nebula/notify_notlinux.go b/cmd/nebula/notify_notlinux.go new file mode 100644 index 000000000..e7758e094 --- /dev/null +++ b/cmd/nebula/notify_notlinux.go @@ -0,0 +1,10 @@ +//go:build !linux +// +build !linux + +package main + +import "github.com/sirupsen/logrus" + +func notifyReady(_ *logrus.Logger) { + // No init service to notify +} diff --git a/dist/arch/nebula.service b/dist/arch/nebula.service index 7e5335aa8..831c71a53 100644 --- a/dist/arch/nebula.service +++ b/dist/arch/nebula.service @@ -4,6 +4,8 @@ Wants=basic.target network-online.target nss-lookup.target time-sync.target After=basic.target network.target network-online.target [Service] +Type=notify +NotifyAccess=main SyslogIdentifier=nebula ExecReload=/bin/kill -HUP $MAINPID ExecStart=/usr/bin/nebula -config /etc/nebula/config.yml diff --git a/dist/fedora/nebula.service b/dist/fedora/nebula.service index 21a99c558..0f947ead4 100644 --- a/dist/fedora/nebula.service +++ b/dist/fedora/nebula.service @@ -5,6 +5,8 @@ After=basic.target network.target network-online.target Before=sshd.service [Service] +Type=notify +NotifyAccess=main SyslogIdentifier=nebula ExecReload=/bin/kill -HUP $MAINPID ExecStart=/usr/bin/nebula -config /etc/nebula/config.yml diff --git a/examples/service_scripts/nebula.service b/examples/service_scripts/nebula.service index fd7a06710..ab5218f8d 100644 --- a/examples/service_scripts/nebula.service +++ b/examples/service_scripts/nebula.service @@ -5,6 +5,8 @@ After=basic.target network.target network-online.target Before=sshd.service [Service] +Type=notify +NotifyAccess=main SyslogIdentifier=nebula ExecReload=/bin/kill -HUP $MAINPID ExecStart=/usr/local/bin/nebula -config /etc/nebula/config.yml From ad4753f283d57f825e91a3b0123b35010dfdbe59 Mon Sep 17 00:00:00 2001 From: Nate Brown Date: Mon, 24 Jul 2023 10:24:29 -0500 Subject: [PATCH 2/2] Change to proper error logs if NOTIFY_SOCKET is present --- cmd/nebula/notify_linux.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/nebula/notify_linux.go b/cmd/nebula/notify_linux.go index 53952b903..8c3dca558 100644 --- a/cmd/nebula/notify_linux.go +++ b/cmd/nebula/notify_linux.go @@ -22,19 +22,19 @@ func notifyReady(l *logrus.Logger) { conn, err := net.DialTimeout("unixgram", sockName, time.Second) if err != nil { - l.WithError(err).Debugln("failed to connect to systemd notification socket") + l.WithError(err).Error("failed to connect to systemd notification socket") return } defer conn.Close() err = conn.SetWriteDeadline(time.Now().Add(time.Second)) if err != nil { - l.WithError(err).Debugln("failed to set the write deadline for the systemd notification socket") + l.WithError(err).Error("failed to set the write deadline for the systemd notification socket") return } if _, err = conn.Write([]byte(SdNotifyReady)); err != nil { - l.WithError(err).Debugln("failed to signal the systemd notification socket") + l.WithError(err).Error("failed to signal the systemd notification socket") return }