summaryrefslogtreecommitdiffhomepage
path: root/notify
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2022-09-02 09:23:51 -0600
committerMatthew Holt <[email protected]>2022-09-02 09:24:05 -0600
commit59286d2c7ede928cc24916abdf6c0fb9ee12b826 (patch)
tree39df5e8d9e09f3d2699d97087816920a5cd452ca /notify
parent66959d9f18768beb9531d51241978731bff8a305 (diff)
downloadcaddy-59286d2c7ede928cc24916abdf6c0fb9ee12b826.tar.gz
caddy-59286d2c7ede928cc24916abdf6c0fb9ee12b826.zip
notify: Don't send ready after error (fix #5003)
Also simplify the notify package quite a bit. Also move stop notification into better place. Add ability to send status or error.
Diffstat (limited to 'notify')
-rw-r--r--notify/notify.go30
-rw-r--r--notify/notify_linux.go71
-rw-r--r--notify/notify_other.go8
-rw-r--r--notify/notify_windows.go12
4 files changed, 49 insertions, 72 deletions
diff --git a/notify/notify.go b/notify/notify.go
deleted file mode 100644
index bca80c1f5..000000000
--- a/notify/notify.go
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2015 Matthew Holt and The Caddy Authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package notify
-
-// NotifyReadiness notifies process manager of readiness.
-func NotifyReadiness() error {
- return notifyReadiness()
-}
-
-// NotifyReloading notifies process manager of reloading.
-func NotifyReloading() error {
- return notifyReloading()
-}
-
-// NotifyStopping notifies process manager of stopping.
-func NotifyStopping() error {
- return notifyStopping()
-}
diff --git a/notify/notify_linux.go b/notify/notify_linux.go
index 1e8da7428..3457a5a6a 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")
diff --git a/notify/notify_other.go b/notify/notify_other.go
index 7fc618b7b..dbe9bdb91 100644
--- a/notify/notify_other.go
+++ b/notify/notify_other.go
@@ -16,6 +16,8 @@
package notify
-func notifyReadiness() error { return nil }
-func notifyReloading() error { return nil }
-func notifyStopping() error { return nil }
+func Ready() error { return nil }
+func Reloading() error { return nil }
+func Stopping() error { return nil }
+func Status(_ string) error { return nil }
+func Error(_ error, _ int) error { return nil }
diff --git a/notify/notify_windows.go b/notify/notify_windows.go
index a551fc7e4..5666a4c22 100644
--- a/notify/notify_windows.go
+++ b/notify/notify_windows.go
@@ -24,7 +24,7 @@ func SetGlobalStatus(status chan<- svc.Status) {
globalStatus = status
}
-func notifyReadiness() error {
+func Ready() error {
if globalStatus != nil {
globalStatus <- svc.Status{
State: svc.Running,
@@ -34,16 +34,22 @@ func notifyReadiness() error {
return nil
}
-func notifyReloading() error {
+func Reloading() error {
if globalStatus != nil {
globalStatus <- svc.Status{State: svc.StartPending}
}
return nil
}
-func notifyStopping() error {
+func Stopping() error {
if globalStatus != nil {
globalStatus <- svc.Status{State: svc.StopPending}
}
return nil
}
+
+// TODO: not implemented
+func Status(_ string) error { return nil }
+
+// TODO: not implemented
+func Error(_ error, _ int) error { return nil }