forked from mdlayher/vsock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfd_linux.go
More file actions
45 lines (38 loc) · 1.43 KB
/
fd_linux.go
File metadata and controls
45 lines (38 loc) · 1.43 KB
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
package vsock
import (
"os"
"golang.org/x/sys/unix"
)
// A fd is an interface for a file descriptor, used to perform system
// calls or swap them out for tests.
type fd interface {
Accept4(flags int) (fd, unix.Sockaddr, error)
Bind(sa unix.Sockaddr) error
Close() error
Connect(sa unix.Sockaddr) error
Getsockname() (unix.Sockaddr, error)
Listen(n int) error
NewFile(name string) *os.File
SetNonblock(nonblocking bool) error
}
var _ fd = &sysFD{}
// sysFD is the system call implementation of fd.
type sysFD struct {
fd int
}
func (fd *sysFD) Accept4(flags int) (fd, unix.Sockaddr, error) {
// Returns a regular file descriptor, must be wrapped in another
// sysFD for it to work properly.
nfd, sa, err := unix.Accept4(fd.fd, flags)
if err != nil {
return nil, nil, err
}
return &sysFD{fd: nfd}, sa, nil
}
func (fd *sysFD) Bind(sa unix.Sockaddr) error { return unix.Bind(fd.fd, sa) }
func (fd *sysFD) Close() error { return unix.Close(fd.fd) }
func (fd *sysFD) Connect(sa unix.Sockaddr) error { return unix.Connect(fd.fd, sa) }
func (fd *sysFD) Getsockname() (unix.Sockaddr, error) { return unix.Getsockname(fd.fd) }
func (fd *sysFD) Listen(n int) error { return unix.Listen(fd.fd, n) }
func (fd *sysFD) NewFile(name string) *os.File { return os.NewFile(uintptr(fd.fd), name) }
func (fd *sysFD) SetNonblock(nonblocking bool) error { return unix.SetNonblock(fd.fd, nonblocking) }