summaryrefslogtreecommitdiff
path: root/notify/notify_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'notify/notify_linux.go')
-rw-r--r--notify/notify_linux.go71
1 files changed, 35 insertions, 36 deletions
diff --git a/notify/notify_linux.go b/notify/notify_linux.go
index 1e8da74..3457a5a 100644
--- a/notify/notify_linux.go
+++ b/notify/notify_linux.go
@@ -17,7 +17,7 @@
package notify
import (
- "io"
+ "fmt"
"net"
"os"
"strings"
@@ -26,9 +26,13 @@ import (
// The documentation about this IPC protocol is available here:
// https://www.freedesktop.org/software/systemd/man/sd_notify.html
-func sdNotify(path, payload string) error {
+func sdNotify(payload string) error {
+ if socketPath == "" {
+ return nil
+ }
+
socketAddr := &net.UnixAddr{
- Name: path,
+ Name: socketPath,
Net: "unixgram",
}
@@ -38,45 +42,40 @@ func sdNotify(path, payload string) error {
}
defer conn.Close()
- if _, err := io.Copy(conn, strings.NewReader(payload)); err != nil {
- return err
- }
- return nil
+ _, err = conn.Write([]byte(payload))
+ return err
}
-// notifyReadiness notifies systemd that caddy has finished its
+// Ready notifies systemd that caddy has finished its
// initialization routines.
-func notifyReadiness() error {
- val, ok := os.LookupEnv("NOTIFY_SOCKET")
- if !ok || val == "" {
- return nil
- }
- if err := sdNotify(val, "READY=1"); err != nil {
- return err
- }
- return nil
+func Ready() error {
+ return sdNotify("READY=1")
}
-// notifyReloading notifies systemd that caddy is reloading its config.
-func notifyReloading() error {
- val, ok := os.LookupEnv("NOTIFY_SOCKET")
- if !ok || val == "" {
- return nil
- }
- if err := sdNotify(val, "RELOADING=1"); err != nil {
- return err
- }
- return nil
+// Reloading notifies systemd that caddy is reloading its config.
+func Reloading() error {
+ return sdNotify("RELOADING=1")
}
-// notifyStopping notifies systemd that caddy is stopping.
-func notifyStopping() error {
- val, ok := os.LookupEnv("NOTIFY_SOCKET")
- if !ok || val == "" {
- return nil
- }
- if err := sdNotify(val, "STOPPING=1"); err != nil {
- return err
+// Stopping notifies systemd that caddy is stopping.
+func Stopping() error {
+ return sdNotify("STOPPING=1")
+}
+
+// Status sends systemd an updated status message.
+func Status(msg string) error {
+ return sdNotify("STATUS=" + msg)
+}
+
+// Error is like Status, but sends systemd an error message
+// instead, with an optional errno-style error number.
+func Error(err error, errno int) error {
+ collapsedErr := strings.ReplaceAll(err.Error(), "\n", " ")
+ msg := fmt.Sprintf("STATUS=%s", collapsedErr)
+ if errno > 0 {
+ msg += fmt.Sprintf("\nERRNO=%d", errno)
}
- return nil
+ return sdNotify(msg)
}
+
+var socketPath, _ = os.LookupEnv("NOTIFY_SOCKET")