-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsystemd.go
62 lines (53 loc) · 1.31 KB
/
systemd.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"errors"
"net"
"os"
"strconv"
"syscall"
)
// fnctl syscall wrapper
func fcntl(fd int, cmd int, arg int) (int, int) {
r0, _, e1 := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg))
return int(r0), int(e1)
}
// ListenSystemdFds - Listen to FDs provided by systemd
func ListenSystemdFds() ([]net.Listener, error) {
const listenFdsStart = 3
pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
if err != nil || pid != os.Getpid() {
if err == nil {
return nil, err
} else if pid != os.Getpid() {
return nil, errors.New("systemd pid mismatch")
}
}
nfds, err := strconv.Atoi(os.Getenv("LISTEN_FDS"))
if err != nil || nfds == 0 {
if err == nil {
return nil, err
} else if nfds == 0 {
return nil, errors.New("nfds is zero (could not listen to any provided fds)")
}
}
listeners := []net.Listener(nil)
for fd := listenFdsStart; fd < listenFdsStart+nfds; fd++ {
flags, errno := fcntl(fd, syscall.F_GETFD, 0)
if errno != 0 {
if errno != 0 {
return nil, syscall.Errno(errno)
}
}
if flags&syscall.FD_CLOEXEC != 0 {
continue
}
syscall.CloseOnExec(fd)
file := os.NewFile(uintptr(fd), "")
listener, err := net.FileListener(file)
if err != nil {
return nil, err
}
listeners = append(listeners, listener)
}
return listeners, nil
}