-
Notifications
You must be signed in to change notification settings - Fork 8
/
connwrap.go
382 lines (347 loc) · 9.03 KB
/
connwrap.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright (c) 2020 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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.
//go:build !windows
// +build !windows
package grpcfd
import (
"context"
"fmt"
"net"
"os"
"runtime"
"syscall"
"github.com/edwarnicke/serialize"
"github.com/pkg/errors"
"google.golang.org/grpc/peer"
)
const (
maxFDCount = 64
)
// SyscallConn - having the SyscallConn method to access syscall.RawConn
type SyscallConn interface {
SyscallConn() (syscall.RawConn, error)
}
// FDSender - capable of Sending a file
type FDSender interface {
SendFD(fd uintptr) <-chan error
SendFile(file SyscallConn) <-chan error
SendFilename(filename string) <-chan error
}
// FDRecver - capable of Recving an fd by (dev,ino)
type FDRecver interface {
RecvFD(dev, inode uint64) <-chan uintptr
RecvFile(dev, ino uint64) <-chan *os.File
RecvFileByURL(urlStr string) (<-chan *os.File, error)
RecvFDByURL(urlStr string) (<-chan uintptr, error)
}
// FDTransceiver - combination of FDSender and FDRecver
type FDTransceiver interface {
FDSender
FDRecver
}
type inodeKey struct {
dev uint64
ino uint64
}
type connWrap struct {
net.Conn
sendFDs []int
errChs []chan error
sendExecutor serialize.Executor
closed bool
recvFDChans map[inodeKey][]chan uintptr
recvedFDs map[inodeKey]uintptr
recvExecutor serialize.Executor
}
func wrapConn(conn net.Conn) net.Conn {
if _, ok := conn.(*connWrap); ok {
return conn
}
_, ok := conn.(interface {
WriteMsgUnix(b, oob []byte, addr *net.UnixAddr) (n, oobn int, err error)
})
if !ok {
return conn
}
conn = &connWrap{
Conn: conn,
recvFDChans: make(map[inodeKey][]chan uintptr),
recvedFDs: make(map[inodeKey]uintptr),
}
runtime.SetFinalizer(conn, func(conn *connWrap) {
_ = conn.close()
})
return conn
}
func (w *connWrap) Close() error {
runtime.SetFinalizer(w, nil)
return w.close()
}
func (w *connWrap) close() error {
err := w.Conn.Close()
w.recvExecutor.AsyncExec(func() {
for k, fd := range w.recvedFDs {
_ = syscall.Close(int(fd))
delete(w.recvedFDs, k)
}
for k, recvChs := range w.recvFDChans {
for _, recvCh := range recvChs {
close(recvCh)
}
delete(w.recvFDChans, k)
}
w.sendExecutor.AsyncExec(func() {
for k, fd := range w.sendFDs {
w.errChs[k] <- errors.Errorf("unable to send fd %d because connection closed", fd)
close(w.errChs[k])
_ = syscall.Close(fd)
}
w.sendFDs = nil
w.errChs = nil
})
w.closed = true
})
return err
}
func (w *connWrap) Write(b []byte) (int, error) {
var n int
var err error
<-w.sendExecutor.AsyncExec(func() {
var sendFDs []int
var errChs []chan error
if len(w.sendFDs) > 0 {
limit := len(w.sendFDs)
if maxFDCount < limit {
limit = maxFDCount
}
sendFDs = w.sendFDs[:limit]
w.sendFDs = w.sendFDs[limit:]
errChs = w.errChs[:limit]
w.errChs = w.errChs[limit:]
}
for i, fd := range sendFDs {
rights := syscall.UnixRights(fd)
// TODO handle when n != 1 and handle oobn not as expected
// maybe with a for {n == 0} ?
// maybe if oobn == 0 we simply prepend the remainder of the fds to w.sendFDs and call it good?
_, _, err = w.Conn.(interface {
WriteMsgUnix(b, oob []byte, addr *net.UnixAddr) (n, oobn int, err error)
}).WriteMsgUnix([]byte{b[i]}, rights, nil)
if err != nil {
errChs[i] <- err
}
close(errChs[i])
_ = syscall.Close(fd)
}
n, err = w.Conn.Write(b[len(sendFDs):])
if err == nil {
n += len(sendFDs)
}
})
return n, err
}
func (w *connWrap) SendFD(fd uintptr) <-chan error {
errCh := make(chan error, 1)
// Dup the fd because we have no way of knowing what the caller will do with it between
// now and when we can send it
fd, _, err := syscall.Syscall(syscall.SYS_FCNTL, fd, uintptr(syscall.F_DUPFD), 0)
if err != 0 {
errCh <- errors.WithStack(err)
close(errCh)
return errCh
}
w.sendExecutor.AsyncExec(func() {
if w.closed {
close(errCh)
return
}
w.sendFDs = append(w.sendFDs, int(fd))
w.errChs = append(w.errChs, errCh)
})
return errCh
}
func (w *connWrap) SendFile(file SyscallConn) <-chan error {
errCh := make(chan error, 1)
raw, err := file.SyscallConn()
if err != nil {
errCh <- errors.Wrapf(err, "unable to retrieve syscall.RawConn for src %+v", file)
close(errCh)
return errCh
}
err = raw.Control(func(fd uintptr) {
var stat syscall.Stat_t
statErr := syscall.Fstat(int(fd), &stat)
if statErr != nil {
errCh <- statErr
close(errCh)
return
}
go func(errChIn <-chan error, errChOut chan<- error) {
for err := range errChIn {
errChOut <- err
}
close(errChOut)
}(w.SendFD(fd), errCh)
})
if err != nil {
errCh <- err
close(errCh)
}
return errCh
}
func (w *connWrap) RemoteAddr() net.Addr {
return w
}
func (w *connWrap) Network() string {
return w.Conn.RemoteAddr().Network()
}
func (w *connWrap) String() string {
return w.Conn.RemoteAddr().String()
}
func (w *connWrap) RecvFD(dev, ino uint64) <-chan uintptr {
fdCh := make(chan uintptr, 1)
w.recvExecutor.AsyncExec(func() {
if w.closed {
close(fdCh)
return
}
key := inodeKey{
dev: dev,
ino: ino,
}
// If we have the fd for this (dev,ino) already
if fd, ok := w.recvedFDs[key]; ok {
// Copy it
var errno syscall.Errno
fd, _, errno = syscall.Syscall(syscall.SYS_FCNTL, fd, uintptr(syscall.F_DUPFD), 0)
if errno != 0 {
// TODO - this is terrible error handling
close(fdCh)
return
}
// Send it to the requestor
fdCh <- fd
// Close the channel
close(fdCh)
return
}
// Otherwise queue the requestor up to receive the fd if we ever get it
w.recvFDChans[key] = append(w.recvFDChans[key], fdCh)
})
return fdCh
}
func (w *connWrap) RecvFDByURL(urlStr string) (<-chan uintptr, error) {
dev, ino, err := URLStringToDevIno(urlStr)
if err != nil {
return nil, err
}
return w.RecvFD(dev, ino), nil
}
func (w *connWrap) RecvFile(dev, ino uint64) <-chan *os.File {
fileCh := make(chan *os.File, 1)
go func(fdCh <-chan uintptr, fileCh chan<- *os.File) {
for fd := range fdCh {
if runtime.GOOS == "linux" {
fileCh <- os.NewFile(fd, fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), fd))
continue
}
fileCh <- os.NewFile(fd, "")
}
close(fileCh)
}(w.RecvFD(dev, ino), fileCh)
return fileCh
}
func (w *connWrap) RecvFileByURL(urlStr string) (<-chan *os.File, error) {
dev, ino, err := URLStringToDevIno(urlStr)
if err != nil {
return nil, err
}
return w.RecvFile(dev, ino), nil
}
func (w *connWrap) Read(b []byte) (n int, err error) {
oob := make([]byte, syscall.CmsgSpace(4*maxFDCount))
n, oobn, _, _, err := w.Conn.(interface {
ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *net.UnixAddr, err error)
}).ReadMsgUnix(b, oob)
// Go async for updating info
w.recvExecutor.AsyncExec(func() {
// We got oob info
if oobn != 0 {
msgs, parseCtlErr := syscall.ParseSocketControlMessage(oob[:oobn])
if parseCtlErr != nil {
return
}
for i := range msgs {
fds, parseRightsErr := syscall.ParseUnixRights(&msgs[i])
if parseRightsErr != nil {
return
}
for _, fd := range fds {
var stat syscall.Stat_t
// Get the (dev,ino) for the fd
fstatErr := syscall.Fstat(fd, &stat)
if fstatErr != nil {
continue
}
key := inodeKey{
dev: uint64(stat.Dev),
ino: stat.Ino,
}
// If we have one already... close the new one
if _, ok := w.recvedFDs[key]; ok {
_ = syscall.Close(fd)
continue
}
// If its new store it in our map of recvedFDs
w.recvedFDs[key] = uintptr(fd)
// Iterate through any waiting receivers
for _, fdCh := range w.recvFDChans[key] {
// Copy the fd. Always copy the fd. Who knows what the recipient might choose to do with it.
fd, _, errno := syscall.Syscall(syscall.SYS_FCNTL, w.recvedFDs[key], uintptr(syscall.F_DUPFD), 0)
if errno != 0 { // TODO - this is terrible error handling
close(fdCh)
continue
}
fdCh <- fd
close(fdCh)
}
delete(w.recvFDChans, key)
}
}
}
})
if err != nil {
return 0, err
}
return n, err
}
// FromPeer - return grpcfd.FDTransceiver from peer.Peer
//
// ok is true of successful, false otherwise
func FromPeer(p *peer.Peer) (transceiver FDTransceiver, ok bool) {
transceiver, ok = p.Addr.(FDTransceiver)
return transceiver, ok
}
// FromContext - return grpcfd.FDTransceiver from context.Context
//
// ok is true of successful, false otherwise
func FromContext(ctx context.Context) (transceiver FDTransceiver, ok bool) {
p, ok := peer.FromContext(ctx)
if !ok {
return nil, false
}
return FromPeer(p)
}