-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathremote_fds.go
184 lines (145 loc) · 4.18 KB
/
remote_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
package client
import (
"context"
"encoding/binary"
"errors"
"fmt"
"net"
"syscall"
"github.com/containers/conmon-rs/internal/proto"
)
var (
errTooManyFileDescriptors = errors.New("too many file descriptors")
errResponseTooShort = errors.New("response too short")
errResponseIDDoesNotMatch = errors.New("response id does not match")
errNumberOfFDsDoesNotMatch = errors.New("number of fds does not match")
errInvalidResponseLength = errors.New("invalid response length")
)
type serverError string
func (s serverError) Error() string {
return "server error: " + string(s)
}
const (
uint64Bytes = 8
maxFDs = 253
msgBufferSize = uint64Bytes + maxFDs*uint64Bytes + 1 // one additional byte used to detect packet truncation
numFDsBits = 32
)
// RemoteFD represents a file descriptor on the server, identified by a slot number.
type RemoteFD uint64
func (r RemoteFD) String() string {
return fmt.Sprintf("RemoteFD(%d)", r)
}
// RemoteFDs can be used to send file descriptors to the server.
type RemoteFDs struct {
conn *net.UnixConn
reqID uint32
}
// NewRemoteFDs connects to the fd socket at `path`.
func NewRemoteFDs(path string) (*RemoteFDs, error) {
conn, err := DialLongSocket("unixpacket", path)
if err != nil {
return nil, fmt.Errorf("dial long socket: %w", err)
}
return &RemoteFDs{
conn: conn,
}, nil
}
// Send file descriptors to the server.
func (r *RemoteFDs) Send(fds ...int) ([]RemoteFD, error) {
if len(fds) == 0 {
return nil, nil
}
if len(fds) > maxFDs {
return nil, errTooManyFileDescriptors
}
r.reqID++
reqID := r.reqID
b := binary.LittleEndian.AppendUint64(nil, uint64(reqID)<<numFDsBits|uint64(len(fds)))
oob := syscall.UnixRights(fds...)
_, _, err := r.conn.WriteMsgUnix(b, oob, nil)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
buf := make([]byte, msgBufferSize)
n, err := r.conn.Read(buf)
if err != nil {
return nil, fmt.Errorf("receviree reaponse: %w", err)
}
buf = buf[:n]
if len(buf) < uint64Bytes {
return nil, errResponseTooShort
}
resIDAndNumFDs := binary.LittleEndian.Uint64(buf[:8])
buf = buf[8:]
if resID := uint32(resIDAndNumFDs >> numFDsBits); resID != reqID {
return nil, fmt.Errorf("%w: %d (expected %d)", errResponseIDDoesNotMatch, resID, reqID)
}
numFDs := int(resIDAndNumFDs & (1<<numFDsBits - 1))
if int64(numFDs) == 1<<numFDsBits-1 {
return nil, serverError(buf)
}
if numFDs != len(fds) {
return nil, fmt.Errorf("%w: %d (expected %d)", errNumberOfFDsDoesNotMatch, numFDs, len(fds))
}
if len(buf) != numFDs*uint64Bytes {
return nil, errInvalidResponseLength
}
slots := make([]RemoteFD, 0, numFDs)
for i := range numFDs {
slots = append(slots, RemoteFD(binary.LittleEndian.Uint64(buf[i*uint64Bytes:])))
}
return slots, nil
}
// Close the connection and unused remote file descriptors.
func (r *RemoteFDs) Close() error {
if err := r.conn.Close(); err != nil {
return fmt.Errorf("close fd socket: %w", err)
}
return nil
}
// RemoteFDs can be used start and connect to the remote fd socket.
func (c *ConmonClient) RemoteFDs(ctx context.Context) (*RemoteFDs, error) {
ctx, span := c.startSpan(ctx, "AttachContainer")
if span != nil {
defer span.End()
}
conn, err := c.newRPCConn()
if err != nil {
return nil, fmt.Errorf("create RPC connection: %w", err)
}
defer func() {
if err := conn.Close(); err != nil {
c.logger.Errorf("Unable to close connection: %v", err)
}
}()
client := proto.Conmon(conn.Bootstrap(ctx))
future, free := client.StartFdSocket(ctx, func(p proto.Conmon_startFdSocket_Params) error {
req, err := p.NewRequest()
if err != nil {
return fmt.Errorf("create request: %w", err)
}
if err := c.setMetadata(ctx, req); err != nil {
return fmt.Errorf("set metadata: %w", err)
}
return nil
})
defer free()
result, err := future.Struct()
if err != nil {
return nil, fmt.Errorf("create result: %w", err)
}
res, err := result.Response()
if err != nil {
return nil, fmt.Errorf("get response: %w", err)
}
path, err := res.Path()
if err != nil {
return nil, fmt.Errorf("get path: %w", err)
}
r, err := NewRemoteFDs(path)
if err != nil {
return nil, fmt.Errorf("connect to remote fd socket: %w", err)
}
return r, nil
}