pkg/systemd/sd_notify.go

35 lines
629 B
Go
Raw Normal View History

package systemd
import (
"errors"
"net"
"os"
)
// ErrSdNotifyNoSocket is an error returned if no socket was specified.
var ErrSdNotifyNoSocket = errors.New("No socket")
// SdNotify sends a message to the init daemon. It is common to ignore the return value.
func SdNotify(state string) error {
socketAddr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}
if socketAddr.Name == "" {
return ErrSdNotifyNoSocket
}
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
return err
}
_, err = conn.Write([]byte(state))
if err != nil {
return err
}
return nil
}