-
Notifications
You must be signed in to change notification settings - Fork 143
/
fds.go
324 lines (266 loc) · 6.49 KB
/
fds.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package tableflip
import (
"net"
"os"
"strings"
"sync"
"syscall"
"github.com/pkg/errors"
)
// Listener can be shared between processes.
type Listener interface {
net.Listener
syscall.Conn
}
// Conn can be shared between processes.
type Conn interface {
net.Conn
syscall.Conn
}
const (
listenKind = "listener"
connKind = "conn"
fdKind = "fd"
)
type fileName [3]string
func (name fileName) String() string {
return strings.Join(name[:], ":")
}
// file works around the fact that it's not possible
// to get the fd from an os.File without putting it into
// blocking mode.
type file struct {
*os.File
fd uintptr
}
func newFile(fd uintptr, name fileName) *file {
f := os.NewFile(fd, name.String())
if f == nil {
return nil
}
return &file{
f,
fd,
}
}
// Fds holds all file descriptors inherited from the
// parent process.
type Fds struct {
mu sync.Mutex
// NB: Files in these maps may be in blocking mode.
inherited map[fileName]*file
used map[fileName]*file
}
func newFds(inherited map[fileName]*file) *Fds {
if inherited == nil {
inherited = make(map[fileName]*file)
}
return &Fds{
inherited: inherited,
used: make(map[fileName]*file),
}
}
// Listen returns a listener inherited from the parent process, or creates a new one.
func (f *Fds) Listen(network, addr string) (net.Listener, error) {
f.mu.Lock()
defer f.mu.Unlock()
ln, err := f.listenerLocked(network, addr)
if err != nil {
return nil, err
}
if ln != nil {
return ln, nil
}
ln, err = net.Listen(network, addr)
if err != nil {
return nil, errors.Wrap(err, "can't create new listener")
}
if _, ok := ln.(Listener); !ok {
ln.Close()
return nil, errors.Errorf("%T doesn't implement tableflip.Listener", ln)
}
err = f.addListenerLocked(network, addr, ln.(Listener))
if err != nil {
ln.Close()
return nil, err
}
return ln, nil
}
// Listener returns an inherited listener or nil.
//
// It is safe to close the returned listener.
func (f *Fds) Listener(network, addr string) (net.Listener, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.listenerLocked(network, addr)
}
func (f *Fds) listenerLocked(network, addr string) (net.Listener, error) {
key := fileName{listenKind, network, addr}
file := f.inherited[key]
if file == nil {
return nil, nil
}
ln, err := net.FileListener(file.File)
if err != nil {
return nil, errors.Wrapf(err, "can't inherit listener %s %s", network, addr)
}
delete(f.inherited, key)
f.used[key] = file
return ln, nil
}
// AddListener adds a listener.
//
// It is safe to close ln after calling the method.
// Any existing listener with the same address is overwitten.
func (f *Fds) AddListener(network, addr string, ln Listener) error {
f.mu.Lock()
defer f.mu.Unlock()
return f.addListenerLocked(network, addr, ln)
}
type unlinkOnCloser interface {
SetUnlinkOnClose(bool)
}
func (f *Fds) addListenerLocked(network, addr string, ln Listener) error {
if ifc, ok := ln.(unlinkOnCloser); ok {
ifc.SetUnlinkOnClose(false)
}
return f.addConnLocked(listenKind, network, addr, ln)
}
// Conn returns an inherited connection or nil.
//
// It is safe to close the returned Conn.
func (f *Fds) Conn(network, addr string) (net.Conn, error) {
f.mu.Lock()
defer f.mu.Unlock()
key := fileName{connKind, network, addr}
file := f.inherited[key]
if file == nil {
return nil, nil
}
conn, err := net.FileConn(file.File)
if err != nil {
return nil, errors.Wrapf(err, "can't inherit connection %s %s", network, addr)
}
delete(f.inherited, key)
f.used[key] = file
return conn, nil
}
// AddConn adds a connection.
//
// It is safe to close conn after calling this method.
func (f *Fds) AddConn(network, addr string, conn Conn) error {
f.mu.Lock()
defer f.mu.Unlock()
return f.addConnLocked(connKind, network, addr, conn)
}
func (f *Fds) addConnLocked(kind, network, addr string, conn syscall.Conn) error {
key := fileName{kind, network, addr}
file, err := dupConn(conn, key)
if err != nil {
return errors.Wrapf(err, "can't dup listener %s %s", network, addr)
}
delete(f.inherited, key)
f.used[key] = file
return nil
}
// File returns an inherited file or nil.
//
// The descriptor may be in blocking mode.
func (f *Fds) File(name string) (*os.File, error) {
f.mu.Lock()
defer f.mu.Unlock()
key := fileName{fdKind, name}
file := f.inherited[key]
if file == nil {
return nil, nil
}
// Make a copy of the file, since we don't want to
// allow the caller to invalidate fds in f.inherited.
dup, err := dupFd(file.fd, key)
if err != nil {
return nil, err
}
delete(f.inherited, key)
f.used[key] = file
return dup.File, nil
}
// AddFile adds a file.
//
// As of Go 1.11, file will be in blocking mode
// after this call.
func (f *Fds) AddFile(name string, file *os.File) error {
key := fileName{fdKind, name}
dup, err := dupFd(file.Fd(), key)
if err != nil {
return err
}
f.mu.Lock()
defer f.mu.Unlock()
delete(f.inherited, key)
f.used[key] = dup
return nil
}
func (f *Fds) copy() map[fileName]*file {
f.mu.Lock()
defer f.mu.Unlock()
files := make(map[fileName]*file, len(f.used))
for key, file := range f.used {
files[key] = file
}
return files
}
func (f *Fds) closeInherited() {
f.mu.Lock()
defer f.mu.Unlock()
for key, file := range f.inherited {
if key[0] == listenKind && (key[1] == "unix" || key[1] == "unixpacket") {
// Remove inherited but unused Unix sockets from the file system.
// This undoes the effect of SetUnlinkOnClose(false).
_ = unlinkUnixSocket(key[2])
}
_ = file.Close()
}
f.inherited = make(map[fileName]*file)
}
func unlinkUnixSocket(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if info.Mode()&os.ModeSocket == 0 {
return nil
}
return os.Remove(path)
}
func (f *Fds) closeUsed() {
f.mu.Lock()
defer f.mu.Unlock()
for _, file := range f.used {
_ = file.Close()
}
f.used = make(map[fileName]*file)
}
func dupConn(conn syscall.Conn, name fileName) (*file, error) {
// Use SyscallConn instead of File to avoid making the original
// fd non-blocking.
raw, err := conn.SyscallConn()
if err != nil {
return nil, err
}
var dup *file
var duperr error
err = raw.Control(func(fd uintptr) {
dup, duperr = dupFd(fd, name)
})
if err != nil {
return nil, errors.Wrap(err, "can't access fd")
}
return dup, duperr
}
func dupFd(fd uintptr, name fileName) (*file, error) {
dupfd, _, errno := syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_DUPFD_CLOEXEC, 0)
if errno != 0 {
return nil, errors.Wrap(errno, "can't dup fd using fcntl")
}
return newFile(dupfd, name), nil
}