2017-11-20 06:03:27 +08:00
|
|
|
package sdnotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2018-08-20 19:32:46 +08:00
|
|
|
// SdNotify sends a specified string to the systemd notification socket.
|
2017-11-20 06:03:27 +08:00
|
|
|
func SdNotify(state string) error {
|
|
|
|
name := os.Getenv("NOTIFY_SOCKET")
|
|
|
|
if name == "" {
|
2018-08-20 19:32:46 +08:00
|
|
|
return ErrSdNotifyNoSocket
|
2017-11-20 06:03:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: name, Net: "unixgram"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte(state))
|
|
|
|
return err
|
|
|
|
}
|