Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
vc: modify ioctl function to handle shim test
Browse files Browse the repository at this point in the history
The kata shim tests make use of an ioctl function, so instead
of having a custom one within that file, use the ioctl
function in utils/utils_linux

Fixes #1419

Signed-off-by: Gabi Beyer <Gabrielle.n.beyer@intel.com>
  • Loading branch information
Gabi Beyer authored and Gabi Beyer committed Apr 10, 2019
1 parent 185b494 commit c611cab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
13 changes: 3 additions & 10 deletions virtcontainers/kata_shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"unsafe"

. "github.com/kata-containers/runtime/virtcontainers/pkg/mock"
"github.com/kata-containers/runtime/virtcontainers/utils"
)

const (
Expand Down Expand Up @@ -281,26 +282,18 @@ func TestKataShimStartWithConsoleNonExistingFailure(t *testing.T) {
testKataShimStart(t, sandbox, params, true)
}

func ioctl(fd uintptr, flag, data uintptr) error {
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 {
return err
}

return nil
}

// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
func unlockpt(f *os.File) error {
var u int32

return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
return utils.Ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
}

// ptsname retrieves the name of the first available pts for the given master.
func ptsname(f *os.File) (string, error) {
var n int32

if err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
if err := utils.Ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
return "", err
}

Expand Down
18 changes: 8 additions & 10 deletions virtcontainers/utils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ import (
// VHOST_VSOCK_SET_GUEST_CID = _IOW(VHOST_VIRTIO, 0x60, __u64)
const ioctlVhostVsockSetGuestCid = 0x4008AF60

var ioctlFunc = ioctl
var ioctlFunc = Ioctl

// maxUInt represents the maximum valid value for the context ID.
// The upper 32 bits of the CID are reserved and zeroed.
// See http://stefanha.github.io/virtio/
var maxUInt uint64 = 1<<32 - 1

func ioctl(fd uintptr, request int, arg1 uint64) error {
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
fd,
uintptr(request),
uintptr(unsafe.Pointer(&arg1)),
); errno != 0 {
func Ioctl(fd uintptr, request, data uintptr) error {
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, fd, request, data); errno != 0 {
//uintptr(request)
//uintptr(unsafe.Pointer(&arg1)),
//); errno != 0 {
return os.NewSyscallError("ioctl", fmt.Errorf("%d", int(errno)))
}

Expand Down Expand Up @@ -75,14 +73,14 @@ func FindContextID() (*os.File, uint64, error) {

// Looking for the first available context ID.
for cid := contextID; cid <= maxUInt; cid++ {
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, cid); err == nil {
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, uintptr(unsafe.Pointer(&cid))); err == nil {
return vsockFd, cid, nil
}
}

// Last chance to get a free context ID.
for cid := contextID - 1; cid >= firstContextID; cid-- {
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, cid); err == nil {
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, uintptr(unsafe.Pointer(&cid))); err == nil {
return vsockFd, cid, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/utils/utils_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestFindContextID(t *testing.T) {
assert := assert.New(t)

ioctlFunc = func(fd uintptr, request int, arg1 uint64) error {
ioctlFunc = func(fd uintptr, request, arg1 uintptr) error {
return errors.New("ioctl")
}

Expand Down

0 comments on commit c611cab

Please sign in to comment.