summaryrefslogtreecommitdiffhomepage
path: root/pkg/unet/unet.go
blob: c976d72303dff0a865a091ea0fdb96b5bb6ac4de (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright 2018 The gVisor Authors.
//
// 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 unet provides a minimal net package based on Unix Domain Sockets.
//
// This does no pooling, and should only be used for a limited number of
// connections in a Go process. Don't use this package for arbitrary servers.
package unet

import (
	"errors"
	"sync/atomic"
	"syscall"

	"gvisor.dev/gvisor/pkg/gate"
)

// backlog is used for the listen request.
const backlog = 16

// errClosing is returned by wait if the Socket is in the process of closing.
var errClosing = errors.New("Socket is closing")

// errMessageTruncated indicates that data was lost because the provided buffer
// was too small.
var errMessageTruncated = errors.New("message truncated")

// socketType returns the appropriate type.
func socketType(packet bool) int {
	if packet {
		return syscall.SOCK_SEQPACKET
	}
	return syscall.SOCK_STREAM
}

// socket creates a new host socket.
func socket(packet bool) (int, error) {
	// Make a new socket.
	fd, err := syscall.Socket(syscall.AF_UNIX, socketType(packet), 0)
	if err != nil {
		return 0, err
	}

	return fd, nil
}

// eventFD returns a new event FD with initial value 0.
func eventFD() (int, error) {
	f, _, e := syscall.Syscall(syscall.SYS_EVENTFD2, 0, 0, 0)
	if e != 0 {
		return -1, e
	}
	return int(f), nil
}

// Socket is a connected unix domain socket.
type Socket struct {
	// gate protects use of fd.
	gate gate.Gate

	// fd is the bound socket.
	//
	// fd must be read atomically, and only remains valid if read while
	// within gate.
	fd int32

	// efd is an event FD that is signaled when the socket is closing.
	//
	// efd is immutable and remains valid until Close/Release.
	efd int

	// race is an atomic variable used to avoid triggering the race
	// detector. See comment in SocketPair below.
	race *int32
}

// NewSocket returns a socket from an existing FD.
//
// NewSocket takes ownership of fd.
func NewSocket(fd int) (*Socket, error) {
	// fd must be non-blocking for non-blocking syscall.Accept in
	// ServerSocket.Accept.
	if err := syscall.SetNonblock(fd, true); err != nil {
		return nil, err
	}

	efd, err := eventFD()
	if err != nil {
		return nil, err
	}

	return &Socket{
		fd:  int32(fd),
		efd: efd,
	}, nil
}

// finish completes use of s.fd by evicting any waiters, closing the gate, and
// closing the event FD.
func (s *Socket) finish() error {
	// Signal any blocked or future polls.
	//
	// N.B. eventfd writes must be 8 bytes.
	if _, err := syscall.Write(s.efd, []byte{1, 0, 0, 0, 0, 0, 0, 0}); err != nil {
		return err
	}

	// Close the gate, blocking until all FD users leave.
	s.gate.Close()

	return syscall.Close(s.efd)
}

// Close closes the socket.
func (s *Socket) Close() error {
	// Set the FD in the socket to -1, to ensure that all future calls to
	// FD/Release get nothing and Close calls return immediately.
	fd := int(atomic.SwapInt32(&s.fd, -1))
	if fd < 0 {
		// Already closed or closing.
		return syscall.EBADF
	}

	// Shutdown the socket to cancel any pending accepts.
	s.shutdown(fd)

	if err := s.finish(); err != nil {
		return err
	}

	return syscall.Close(fd)
}

// Release releases ownership of the socket FD.
//
// The returned FD is non-blocking.
//
// Any concurrent or future callers of Socket methods will receive EBADF.
func (s *Socket) Release() (int, error) {
	// Set the FD in the socket to -1, to ensure that all future calls to
	// FD/Release get nothing and Close calls return immediately.
	fd := int(atomic.SwapInt32(&s.fd, -1))
	if fd < 0 {
		// Already closed or closing.
		return -1, syscall.EBADF
	}

	if err := s.finish(); err != nil {
		return -1, err
	}

	return fd, nil
}

// FD returns the FD for this Socket.
//
// The FD is non-blocking and must not be made blocking.
//
// N.B. os.File.Fd makes the FD blocking. Use of Release instead of FD is
// strongly preferred.
//
// The returned FD cannot be used safely if there may be concurrent callers to
// Close or Release.
//
// Use Release to take ownership of the FD.
func (s *Socket) FD() int {
	return int(atomic.LoadInt32(&s.fd))
}

// enterFD enters the FD gate and returns the FD value.
//
// If enterFD returns ok, s.gate.Leave must be called when done with the FD.
// Callers may only block while within the gate using s.wait.
//
// The returned FD is guaranteed to remain valid until s.gate.Leave.
func (s *Socket) enterFD() (int, bool) {
	if !s.gate.Enter() {
		return -1, false
	}

	fd := int(atomic.LoadInt32(&s.fd))
	if fd < 0 {
		s.gate.Leave()
		return -1, false
	}

	return fd, true
}

// SocketPair creates a pair of connected sockets.
func SocketPair(packet bool) (*Socket, *Socket, error) {
	// Make a new pair.
	fds, err := syscall.Socketpair(syscall.AF_UNIX, socketType(packet)|syscall.SOCK_CLOEXEC, 0)
	if err != nil {
		return nil, nil, err
	}

	// race is an atomic variable used to avoid triggering the race
	// detector. We have to fool TSAN into thinking there is a race
	// variable between our two sockets. We only use SocketPair in tests
	// anyway.
	//
	// NOTE(b/27107811): This is purely due to the fact that the raw
	// syscall does not serve as a boundary for the sanitizer.
	var race int32
	a, err := NewSocket(fds[0])
	if err != nil {
		syscall.Close(fds[0])
		syscall.Close(fds[1])
		return nil, nil, err
	}
	a.race = &race
	b, err := NewSocket(fds[1])
	if err != nil {
		a.Close()
		syscall.Close(fds[1])
		return nil, nil, err
	}
	b.race = &race
	return a, b, nil
}

// Connect connects to a server.
func Connect(addr string, packet bool) (*Socket, error) {
	fd, err := socket(packet)
	if err != nil {
		return nil, err
	}

	// Connect the socket.
	usa := &syscall.SockaddrUnix{Name: addr}
	if err := syscall.Connect(fd, usa); err != nil {
		syscall.Close(fd)
		return nil, err
	}

	return NewSocket(fd)
}

// ControlMessage wraps around a byte array and provides functions for parsing
// as a Unix Domain Socket control message.
type ControlMessage []byte

// EnableFDs enables receiving FDs via control message.
//
// This guarantees only a MINIMUM number of FDs received. You may receive MORE
// than this due to the way FDs are packed. To be specific, the number of
// receivable buffers will be rounded up to the nearest even number.
//
// This must be called prior to ReadVec if you want to receive FDs.
func (c *ControlMessage) EnableFDs(count int) {
	*c = make([]byte, syscall.CmsgSpace(count*4))
}

// ExtractFDs returns the list of FDs in the control message.
//
// Either this or CloseFDs should be used after EnableFDs.
func (c *ControlMessage) ExtractFDs() ([]int, error) {
	msgs, err := syscall.ParseSocketControlMessage(*c)
	if err != nil {
		return nil, err
	}
	var fds []int
	for _, msg := range msgs {
		thisFds, err := syscall.ParseUnixRights(&msg)
		if err != nil {
			// Different control message.
			return nil, err
		}
		for _, fd := range thisFds {
			if fd >= 0 {
				fds = append(fds, fd)
			}
		}
	}
	return fds, nil
}

// CloseFDs closes the list of FDs in the control message.
//
// Either this or ExtractFDs should be used after EnableFDs.
func (c *ControlMessage) CloseFDs() {
	fds, _ := c.ExtractFDs()
	for _, fd := range fds {
		if fd >= 0 {
			syscall.Close(fd)
		}
	}
}

// PackFDs packs the given list of FDs in the control message.
//
// This must be used prior to WriteVec.
func (c *ControlMessage) PackFDs(fds ...int) {
	*c = ControlMessage(syscall.UnixRights(fds...))
}

// UnpackFDs clears the control message.
func (c *ControlMessage) UnpackFDs() {
	*c = nil
}

// SocketWriter wraps an individual send operation.
//
// The normal entrypoint is WriteVec.
type SocketWriter struct {
	socket   *Socket
	to       []byte
	blocking bool
	race     *int32

	ControlMessage
}

// Writer returns a writer for this socket.
func (s *Socket) Writer(blocking bool) SocketWriter {
	return SocketWriter{socket: s, blocking: blocking, race: s.race}
}

// Write implements io.Writer.Write.
func (s *Socket) Write(p []byte) (int, error) {
	r := s.Writer(true)
	return r.WriteVec([][]byte{p})
}

// GetSockOpt gets the given socket option.
func (s *Socket) GetSockOpt(level int, name int, b []byte) (uint32, error) {
	fd, ok := s.enterFD()
	if !ok {
		return 0, syscall.EBADF
	}
	defer s.gate.Leave()

	return getsockopt(fd, level, name, b)
}

// SetSockOpt sets the given socket option.
func (s *Socket) SetSockOpt(level, name int, b []byte) error {
	fd, ok := s.enterFD()
	if !ok {
		return syscall.EBADF
	}
	defer s.gate.Leave()

	return setsockopt(fd, level, name, b)
}

// GetSockName returns the socket name.
func (s *Socket) GetSockName() ([]byte, error) {
	fd, ok := s.enterFD()
	if !ok {
		return nil, syscall.EBADF
	}
	defer s.gate.Leave()

	var buf []byte
	l := syscall.SizeofSockaddrAny

	for {
		// If the buffer is not large enough, allocate a new one with the hint.
		buf = make([]byte, l)
		l, err := getsockname(fd, buf)
		if err != nil {
			return nil, err
		}

		if l <= uint32(len(buf)) {
			return buf[:l], nil
		}
	}
}

// GetPeerName returns the peer name.
func (s *Socket) GetPeerName() ([]byte, error) {
	fd, ok := s.enterFD()
	if !ok {
		return nil, syscall.EBADF
	}
	defer s.gate.Leave()

	var buf []byte
	l := syscall.SizeofSockaddrAny

	for {
		// See above.
		buf = make([]byte, l)
		l, err := getpeername(fd, buf)
		if err != nil {
			return nil, err
		}

		if l <= uint32(len(buf)) {
			return buf[:l], nil
		}
	}
}

// GetPeerCred returns the peer's unix credentials.
func (s *Socket) GetPeerCred() (*syscall.Ucred, error) {
	fd, ok := s.enterFD()
	if !ok {
		return nil, syscall.EBADF
	}
	defer s.gate.Leave()

	return syscall.GetsockoptUcred(fd, syscall.SOL_SOCKET, syscall.SO_PEERCRED)
}

// SocketReader wraps an individual receive operation.
//
// This may be used for doing vectorized reads and/or sending additional
// control messages (e.g. FDs). The normal entrypoint is ReadVec.
//
// One of ExtractFDs or DisposeFDs must be called if EnableFDs is used.
type SocketReader struct {
	socket   *Socket
	source   []byte
	blocking bool
	race     *int32

	ControlMessage
}

// Reader returns a reader for this socket.
func (s *Socket) Reader(blocking bool) SocketReader {
	return SocketReader{socket: s, blocking: blocking, race: s.race}
}

// Read implements io.Reader.Read.
func (s *Socket) Read(p []byte) (int, error) {
	r := s.Reader(true)
	return r.ReadVec([][]byte{p})
}

func (s *Socket) shutdown(fd int) error {
	// Shutdown the socket to cancel any pending accepts.
	return syscall.Shutdown(fd, syscall.SHUT_RDWR)
}

// Shutdown closes the socket for read and write.
func (s *Socket) Shutdown() error {
	fd, ok := s.enterFD()
	if !ok {
		return syscall.EBADF
	}
	defer s.gate.Leave()

	return s.shutdown(fd)
}

// ServerSocket is a bound unix domain socket.
type ServerSocket struct {
	socket *Socket
}

// NewServerSocket returns a socket from an existing FD.
func NewServerSocket(fd int) (*ServerSocket, error) {
	s, err := NewSocket(fd)
	if err != nil {
		return nil, err
	}
	return &ServerSocket{socket: s}, nil
}

// Bind creates and binds a new socket.
func Bind(addr string, packet bool) (*ServerSocket, error) {
	fd, err := socket(packet)
	if err != nil {
		return nil, err
	}

	// Do the bind.
	usa := &syscall.SockaddrUnix{Name: addr}
	if err := syscall.Bind(fd, usa); err != nil {
		syscall.Close(fd)
		return nil, err
	}

	return NewServerSocket(fd)
}

// BindAndListen creates, binds and listens on a new socket.
func BindAndListen(addr string, packet bool) (*ServerSocket, error) {
	s, err := Bind(addr, packet)
	if err != nil {
		return nil, err
	}

	// Start listening.
	if err := s.Listen(); err != nil {
		s.Close()
		return nil, err
	}

	return s, nil
}

// Listen starts listening on the socket.
func (s *ServerSocket) Listen() error {
	fd, ok := s.socket.enterFD()
	if !ok {
		return syscall.EBADF
	}
	defer s.socket.gate.Leave()

	return syscall.Listen(fd, backlog)
}

// Accept accepts a new connection.
//
// This is always blocking.
//
// Preconditions:
// * ServerSocket is listening (Listen called).
func (s *ServerSocket) Accept() (*Socket, error) {
	fd, ok := s.socket.enterFD()
	if !ok {
		return nil, syscall.EBADF
	}
	defer s.socket.gate.Leave()

	for {
		nfd, _, err := syscall.Accept(fd)
		switch err {
		case nil:
			return NewSocket(nfd)
		case syscall.EAGAIN:
			err = s.socket.wait(false)
			if err == errClosing {
				err = syscall.EBADF
			}
		}
		if err != nil {
			return nil, err
		}
	}
}

// Close closes the server socket.
//
// This must only be called once.
func (s *ServerSocket) Close() error {
	return s.socket.Close()
}

// FD returns the socket's file descriptor.
//
// See Socket.FD.
func (s *ServerSocket) FD() int {
	return s.socket.FD()
}

// Release releases ownership of the socket's file descriptor.
//
// See Socket.Release.
func (s *ServerSocket) Release() (int, error) {
	return s.socket.Release()
}