|
|
@@ -1,4 +1,5 @@
|
|
|
// Copyright 2014 Docker, Inc.
|
|
|
+// Copyright 2015-2018 CoreOS, Inc.
|
|
|
//
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
@@ -13,7 +14,11 @@
|
|
|
// limitations under the License.
|
|
|
//
|
|
|
|
|
|
-// Code forked from Docker project
|
|
|
+// Package daemon provides a Go implementation of the sd_notify protocol.
|
|
|
+// It can be used to inform systemd of service start-up completion, watchdog
|
|
|
+// events, and other status changes.
|
|
|
+//
|
|
|
+// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description
|
|
|
package daemon
|
|
|
|
|
|
import (
|
|
|
@@ -21,6 +26,25 @@ import (
|
|
|
"os"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ // SdNotifyReady tells the service manager that service startup is finished
|
|
|
+ // or the service finished loading its configuration.
|
|
|
+ SdNotifyReady = "READY=1"
|
|
|
+
|
|
|
+ // SdNotifyStopping tells the service manager that the service is beginning
|
|
|
+ // its shutdown.
|
|
|
+ SdNotifyStopping = "STOPPING=1"
|
|
|
+
|
|
|
+ // SdNotifyReloading tells the service manager that this service is
|
|
|
+ // reloading its configuration. Note that you must call SdNotifyReady when
|
|
|
+ // it completed reloading.
|
|
|
+ SdNotifyReloading = "RELOADING=1"
|
|
|
+
|
|
|
+ // SdNotifyWatchdog tells the service manager to update the watchdog
|
|
|
+ // timestamp for the service.
|
|
|
+ SdNotifyWatchdog = "WATCHDOG=1"
|
|
|
+)
|
|
|
+
|
|
|
// SdNotify sends a message to the init daemon. It is common to ignore the error.
|
|
|
// If `unsetEnvironment` is true, the environment variable `NOTIFY_SOCKET`
|
|
|
// will be unconditionally unset.
|
|
|
@@ -29,7 +53,7 @@ import (
|
|
|
// (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
|
|
|
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
|
|
|
// (true, nil) - notification supported, data has been sent
|
|
|
-func SdNotify(unsetEnvironment bool, state string) (sent bool, err error) {
|
|
|
+func SdNotify(unsetEnvironment bool, state string) (bool, error) {
|
|
|
socketAddr := &net.UnixAddr{
|
|
|
Name: os.Getenv("NOTIFY_SOCKET"),
|
|
|
Net: "unixgram",
|
|
|
@@ -41,10 +65,9 @@ func SdNotify(unsetEnvironment bool, state string) (sent bool, err error) {
|
|
|
}
|
|
|
|
|
|
if unsetEnvironment {
|
|
|
- err = os.Unsetenv("NOTIFY_SOCKET")
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- return false, err
|
|
|
+ if err := os.Unsetenv("NOTIFY_SOCKET"); err != nil {
|
|
|
+ return false, err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
|
|
@@ -54,9 +77,7 @@ func SdNotify(unsetEnvironment bool, state string) (sent bool, err error) {
|
|
|
}
|
|
|
defer conn.Close()
|
|
|
|
|
|
- _, err = conn.Write([]byte(state))
|
|
|
- // Error sending the message
|
|
|
- if err != nil {
|
|
|
+ if _, err = conn.Write([]byte(state)); err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
return true, nil
|