forked from mdlayher/vsock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathioctl_linux.go
More file actions
58 lines (49 loc) · 1.49 KB
/
ioctl_linux.go
File metadata and controls
58 lines (49 loc) · 1.49 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
46
47
48
49
50
51
52
53
54
55
56
57
58
package vsock
import (
"fmt"
"os"
"unsafe"
"golang.org/x/sys/unix"
)
const (
// devVsock is the location of /dev/vsock. It is exposed on both the
// hypervisor and on virtual machines.
devVsock = "/dev/vsock"
)
// A fs is an interface over the filesystem and ioctl, to enable testing.
type fs interface {
Open(name string) (*os.File, error)
Ioctl(fd uintptr, request int, argp unsafe.Pointer) error
}
// localContextID retrieves the local context ID for this system, using the
// methods from fs. The context ID is stored in cid for later use.
//
// This method uses this signature to enable easier testing without unsafe
// usage of unsafe.Pointer.
func localContextID(fs fs, cid *uint32) error {
f, err := fs.Open(devVsock)
if err != nil {
return err
}
defer f.Close()
// Retrieve the context ID of this machine from /dev/vsock.
return fs.Ioctl(f.Fd(), unix.IOCTL_VM_SOCKETS_GET_LOCAL_CID, unsafe.Pointer(cid))
}
// A sysFS is the system call implementation of fs.
type sysFS struct{}
func (sysFS) Open(name string) (*os.File, error) { return os.Open(name) }
func (sysFS) Ioctl(fd uintptr, request int, argp unsafe.Pointer) error {
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
fd,
uintptr(request),
// Note that the conversion from unsafe.Pointer to uintptr _must_
// occur in the call expression. See the package unsafe documentation
// for more details.
uintptr(argp),
)
if errno != 0 {
return os.NewSyscallError("ioctl", fmt.Errorf("%d", int(errno)))
}
return nil
}