62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
// Package systemd provides utilities for interacting with https://systemd.io/.
|
|
package systemd
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
)
|
|
|
|
// Notify sends a state message to the systemd notifier socket specified by the NOTIFY_SOCKET environment variable.
|
|
func Notify(state string) error {
|
|
socketAddr := &net.UnixAddr{
|
|
Net: "unixgram",
|
|
Name: os.Getenv("NOTIFY_SOCKET"),
|
|
}
|
|
|
|
// if the socket hasn't been set, do nothing
|
|
if socketAddr.Name == "" {
|
|
return nil
|
|
}
|
|
|
|
// connect
|
|
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to NOTIFY_SOCKET %q: %w", socketAddr.Name, err)
|
|
}
|
|
|
|
// close on return
|
|
//nolint:errcheck
|
|
defer conn.Close()
|
|
|
|
// send the message
|
|
if _, err := conn.Write([]byte(state)); err != nil {
|
|
return fmt.Errorf("failed to write to NOTIFY_SOCKET %q: %w", socketAddr.Name, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// NotifyReady sends a "READY=1" notify signal indicating that the application is fully initialised and ready to start
|
|
// operating.
|
|
func NotifyReady() error {
|
|
return Notify("READY=1")
|
|
}
|
|
|
|
// NotifyStopping sends a "STOPPING=1" notify signal indicating that the service is stopping.
|
|
// Returns an error on failure.
|
|
func NotifyStopping() error {
|
|
return Notify("STOPPING=1")
|
|
}
|
|
|
|
// NotifyReloading sends a "RELOADING=1" message to the systemd notification socket to indicate a daemon reload
|
|
// operation.
|
|
func NotifyReloading() error {
|
|
return Notify("RELOADING=1")
|
|
}
|
|
|
|
// NotifyWatchdog sends a "WATCHDOG=1" message to the systemd notify socket to indicate the service is alive.
|
|
func NotifyWatchdog() error {
|
|
return Notify("WATCHDOG=1")
|
|
}
|