-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotify_linux.go
More file actions
38 lines (32 loc) · 848 Bytes
/
notify_linux.go
File metadata and controls
38 lines (32 loc) · 848 Bytes
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
package sdnotify
import (
"fmt"
"net"
"os"
"golang.org/x/sys/unix"
)
// SdNotify sends a specified string to the systemd notification socket.
func SdNotify(state string) error {
name := os.Getenv("NOTIFY_SOCKET")
if name == "" {
return ErrSdNotifyNoSocket
}
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
}
// Reloading sends RELOADING=1\nMONOTONIC_USEC=<monotonic_time> to the
// systemd notify socket.
func Reloading() error {
var ts unix.Timespec
if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts); err != nil {
return fmt.Errorf("getting monotonic time: %w", err)
}
nsecs := ts.Sec*1e09 + ts.Nsec
return SdNotify(fmt.Sprintf("RELOADING=1\nMONOTONIC_USEC=%d",
nsecs/1000))
}