Files
kata-containers/vendor/github.com/mdlayher/vsock/ioctl_linux.go
Jose Carlos Venegas Munoz dad59fe092 vendor: Move virtcontainers to project's root directory
Lets have a global vendor base on virtcontainers.

Signed-off-by: Julio Montes <julio.montes@intel.com>
Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
Signed-off-by: Jose Carlos Venegas Munoz <jose.carlos.venegas.munoz@intel.com>
2018-03-14 11:42:02 -06:00

59 lines
1.5 KiB
Go

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
}