forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
42 lines (33 loc) · 798 Bytes
/
notify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package systemd
import (
"errors"
"net"
"os"
)
const (
// magic values for systemd
// from https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description
Ready = "READY=1"
Reloading = "RELOADING=1"
Stopping = "STOPPING=1"
)
var NotifyNoSocket = errors.New("No socket")
// Notifier provides a method to send a message to systemd.
type Notifier struct{}
// Notify sends a message to the init daemon. It is common to ignore the error.
func (n *Notifier) Notify(state string) error {
addr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}
if addr.Name == "" {
return NotifyNoSocket
}
conn, err := net.DialUnix(addr.Net, nil, addr)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write([]byte(state))
return err
}