2013-12-23 23:07:01 +00:00
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// ErrSdNotifyNoSocket is an error returned if no socket was specified.
|
|
|
|
var ErrSdNotifyNoSocket = errors.New("No socket")
|
2013-12-23 23:07:01 +00:00
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// SdNotify sends a message to the init daemon. It is common to ignore the return value.
|
2013-12-23 23:07:01 +00:00
|
|
|
func SdNotify(state string) error {
|
|
|
|
socketAddr := &net.UnixAddr{
|
|
|
|
Name: os.Getenv("NOTIFY_SOCKET"),
|
|
|
|
Net: "unixgram",
|
|
|
|
}
|
|
|
|
|
|
|
|
if socketAddr.Name == "" {
|
2015-07-25 08:35:07 +00:00
|
|
|
return ErrSdNotifyNoSocket
|
2013-12-23 23:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|