Skip to content

Commit

Permalink
Merge pull request #917 from stgraber/main
Browse files Browse the repository at this point in the history
Improve ioctl handling
  • Loading branch information
hallyn authored Jun 4, 2024
2 parents 65f4b5a + 870014c commit 83d4b91
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
49 changes: 12 additions & 37 deletions cmd/incusd/devices.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
package main

/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <linux/hidraw.h>
#include "../../shared/cgo/memory_utils.h"
#ifndef HIDIOCGRAWINFO
#define HIDIOCGRAWINFO _IOR('H', 0x03, struct hidraw_devinfo)
struct hidraw_devinfo {
__u32 bustype;
__s16 vendor;
__s16 product;
};
#endif
static int get_hidraw_devinfo(int fd, struct hidraw_devinfo *info)
{
int ret;
ret = ioctl(fd, HIDIOCGRAWINFO, info);
if (ret)
return -1;
return 0;
}
*/
import "C"

import (
"fmt"
"os"
Expand All @@ -41,16 +9,17 @@ import (
"sort"
"strconv"
"strings"
"unsafe"

"golang.org/x/sys/unix"

"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/server/cgroup"
"github.com/lxc/incus/v6/internal/server/device"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/resources"
"github.com/lxc/incus/v6/internal/server/state"
_ "github.com/lxc/incus/v6/shared/cgo" // Used by cgo
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/util"
)
Expand Down Expand Up @@ -709,10 +678,16 @@ func devicesRegister(instances []instance.Instance) {
}

func getHidrawDevInfo(fd int) (string, string, error) {
info := C.struct_hidraw_devinfo{}
ret, err := C.get_hidraw_devinfo(C.int(fd), &info)
if ret != 0 {
return "", "", err
type hidInfo struct {
busType uint32
vendor int16
product int16
}

var info hidInfo
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), linux.IoctlHIDIOCGrawInfo, uintptr(unsafe.Pointer(&info)))
if errno != 0 {
return "", "", fmt.Errorf("Failed setting received UUID: %w", unix.Errno(errno))
}

return fmt.Sprintf("%04x", info.vendor), fmt.Sprintf("%04x", info.product), nil
Expand Down
12 changes: 12 additions & 0 deletions internal/linux/ioctls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package linux

/*
#include <linux/btrfs.h>
#include <linux/hidraw.h>
#include <linux/vhost.h>
*/
import "C"

const IoctlBtrfsSetReceivedSubvol = C.BTRFS_IOC_SET_RECEIVED_SUBVOL

Check failure on line 10 in internal/linux/ioctls.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported const IoctlBtrfsSetReceivedSubvol should have comment or be unexported (revive)
const IoctlHIDIOCGrawInfo = C.HIDIOCGRAWINFO

Check failure on line 11 in internal/linux/ioctls.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported const IoctlHIDIOCGrawInfo should have comment or be unexported (revive)
const IoctlVhostVsockSetGuestCid = C.VHOST_VSOCK_SET_GUEST_CID

Check failure on line 12 in internal/linux/ioctls.go

View workflow job for this annotation

GitHub Actions / Code

exported: exported const IoctlVhostVsockSetGuestCid should have comment or be unexported (revive)
6 changes: 3 additions & 3 deletions internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7973,10 +7973,10 @@ func (d *qemu) acquireVsockID(vsockID uint32) (*os.File, error) {
revert.Add(func() { _ = vsockF.Close() })

// The vsock Context ID cannot be supplied as type uint32.
vsockIDInt := int(vsockID)
vsockIDInt := uint64(vsockID)

// 0x4008AF60 = VHOST_VSOCK_SET_GUEST_CID = _IOW(VHOST_VIRTIO, 0x60, __u64)
_, _, errno := unix.Syscall(unix.SYS_IOCTL, vsockF.Fd(), 0x4008AF60, uintptr(unsafe.Pointer(&vsockIDInt)))
// Call the ioctl to set the context ID.
_, _, errno := unix.Syscall(unix.SYS_IOCTL, vsockF.Fd(), linux.IoctlVhostVsockSetGuestCid, uintptr(unsafe.Pointer(&vsockIDInt)))
if errno != 0 {
if !errors.Is(errno, unix.EADDRINUSE) {
return nil, fmt.Errorf("Failed ioctl syscall to vhost socket: %q", errno.Error())
Expand Down
4 changes: 2 additions & 2 deletions internal/server/storage/drivers/driver_btrfs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"golang.org/x/sys/unix"
"gopkg.in/yaml.v2"

"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/revert"
"github.com/lxc/incus/v6/internal/server/backup"
"github.com/lxc/incus/v6/shared/api"
Expand Down Expand Up @@ -62,8 +63,7 @@ func setReceivedUUID(path string, UUID string) error {

copy(args.uuid[:], binUUID)

// 0xC0C09425 = _IOWR(BTRFS_IOCTL_MAGIC, 37, struct btrfs_ioctl_received_subvol_args)
_, _, errno := unix.Syscall(unix.SYS_IOCTL, f.Fd(), 0xC0C09425, uintptr(unsafe.Pointer(&args)))
_, _, errno := unix.Syscall(unix.SYS_IOCTL, f.Fd(), linux.IoctlBtrfsSetReceivedSubvol, uintptr(unsafe.Pointer(&args)))
if errno != 0 {
return fmt.Errorf("Failed setting received UUID: %w", unix.Errno(errno))
}
Expand Down

0 comments on commit 83d4b91

Please sign in to comment.