-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
64 lines (55 loc) · 1.31 KB
/
server.go
File metadata and controls
64 lines (55 loc) · 1.31 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"syscall"
)
func write(path string, data []byte) error {
file, err := os.OpenFile(path, os.O_WRONLY, 0600)
if err != nil {
return err
}
_, err = file.Write(data)
file.Close()
return err
}
func main() {
log.SetFlags(0)
notifySocket := os.Getenv("NOTIFY_SOCKET")
if !filepath.IsAbs(notifySocket) {
log.Fatalf("NOTIFY_SOCKET must be an absolute path, otherwise systemd-notify will throw Protocol error")
}
ppid := os.Getppid()
outPath := fmt.Sprintf("/tmp/%d-systemd-notify.fifo", ppid)
err := syscall.Mkfifo(outPath, 0666)
if err != nil {
log.Fatalf("Failed to create pipe: %v", err)
}
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
})
if err != nil {
log.Fatalf("Failed to create Unix domain socket: %v", err)
}
defer conn.Close()
readyPath := fmt.Sprintf("/tmp/%d-systemd-notify-server.fifo", ppid)
if err := write(readyPath, []byte{}); err != nil {
log.Fatalf("Ready path is not writable: %v", err)
}
buf := make([]byte, 65536)
for {
n, _, err := conn.ReadFromUnix(buf)
if err != nil {
log.Fatalf("Error reading from socket: %v", err)
continue
}
if err := write(outPath, buf[:n]); err != nil {
log.Fatalf("Error writing to pipe: %v", err)
continue
}
}
}