-
Notifications
You must be signed in to change notification settings - Fork 39
/
listener.go
144 lines (134 loc) · 4.1 KB
/
listener.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright © SAS Institute Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package activation provides utilities for inheriting listening sockets from
// systemd, einhorn, socketmaster, and crank.
package activation
import (
"errors"
"net"
"os"
"strconv"
"strings"
"syscall"
)
// GetListener checks if a daemon manager has passed a pre-activated listener
// socket. If not, then net.Listener is used to open a new one. index starts at
// 0 and increments for each additional socket being inherited.
func GetListener(index uint, family, laddr string) (listener net.Listener, err error) {
listener, err = einhornListener(index)
if listener != nil || err != nil {
return
}
listener, err = socketmasterListener(index)
if listener != nil || err != nil {
return
}
listener, err = systemdListener(index)
if listener != nil || err != nil {
return
}
if family == "unix" || family == "unixpacket" {
os.Remove(laddr)
}
return net.Listen(family, laddr)
}
// Env vars are unset as they are read to avoid passing them to child
// processes, but keep their values locally in case more than one socket is
// being inherited
var savedEnv map[string]string
func popEnv(name string) string {
if savedEnv == nil {
savedEnv = make(map[string]string, 1)
}
val := savedEnv[name]
if val != "" {
return val
}
val = os.Getenv(name)
savedEnv[name] = val
os.Unsetenv(name)
return val
}
func popEnvInt(name string) (int, error) {
str := popEnv(name)
if str == "" {
return -1, nil
}
return strconv.Atoi(str)
}
func fdListener(fd uintptr) (net.Listener, error) {
if err := syscall.SetNonblock(int(fd), true); err != nil {
return nil, err
}
file := os.NewFile(fd, "FD_"+strconv.Itoa(int(fd)))
// FileListener dupes the fd so make sure the originally inherited one gets closed
defer file.Close()
return net.FileListener(file)
}
func systemdListener(index uint) (net.Listener, error) {
// systemd's socket activation places all fds sequentially starting at 3.
// It also sets LISTEN_PID to this process' PID as a safety check. Other
// runners may not set LISTEN_PID so don't worry if it's not set.
pid, err := popEnvInt("LISTEN_PID")
if err != nil || (pid != -1 && pid != os.Getpid()) {
// This FD is not for us
return nil, err
}
nfds, err := popEnvInt("LISTEN_FDS")
if err != nil || nfds < int(index)+1 {
return nil, err
}
return fdListener(uintptr(3 + index))
}
func einhornListener(index uint) (net.Listener, error) {
// github.com/stripe/einhorn
// Verify the parent PID as a safety check. Each fd is passed in its own
// environment variable.
ppid, err := popEnvInt("EINHORN_MASTER_PID")
if err != nil || (ppid != -1 && ppid != os.Getppid()) {
// This FD is not for us
return nil, err
}
numfds, err := popEnvInt("EINHORN_FD_COUNT")
if err != nil || numfds < int(index)+1 {
return nil, err
}
name := "EINHORN_FD_" + strconv.Itoa(int(index))
fd, err := popEnvInt(name)
if err != nil {
return nil, err
} else if fd < 0 {
return nil, errors.New("Missing environment variable " + name)
}
// Make sure the old-style var is not inherited by anybody
os.Unsetenv("EINHORN_FDS")
return fdListener(uintptr(fd))
}
func socketmasterListener(index uint) (net.Listener, error) {
// github.com/zimbatm/socketmaster
// Old style of einhorn fd passing, which socketmaster emulates. Uses a
// single environment variable with a space-separated list of fds.
fdstr := popEnv("EINHORN_FDS")
if fdstr == "" {
return nil, nil
}
fds := strings.Split(fdstr, " ")
if len(fds) < int(index)+1 {
return nil, nil
}
fd, err := strconv.Atoi(fds[index])
if err != nil {
return nil, err
}
return fdListener(uintptr(fd))
}