Hello Jake,
Apologies about formatting, here is correctly formatted code:
// GetGroupFD gets File descriptor for a specified by PCI address device
func GetGroupFD(group int, pciDevice string) (int, error) {
buffer := make([]byte, len(pciDevice)+1)
for i, c := range pciDevice {
buffer[i] = uint8(c)
}
ioctlID := VFIO_GROUP_GET_DEVICE_FD()
fmt.Printf("VFIO_GROUP_GET_DEVICE_FD() returned: %04x\n",
ioctlID)
buffer[len(pciDevice)] = 0x0
fmt.Printf("pciDevice: %s\n", string(buffer))
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer(&ioctlID)),
uintptr(unsafe.Pointer(&buffer[0])),
)
if errno != 0 {
return 0, fmt.Errorf("fail to get file
descriptor for %d with errno: %+v", group, errno)
}
return int(device), nil
}
I have also addressed your suggestions without any success. It is latest Centos
7.5.
Thank you
Serguei
From: <[email protected]> on behalf of "[email protected]"
<[email protected]>
Date: Thursday, August 23, 2018 at 12:27 PM
To: golang-nuts <[email protected]>
Subject: Re: [go-nuts] Re: Ioctl Syscall with go and c
First off, I don't know how you are posting your code samples, but they are
unreadable in my Firefox and Chrome on windows.
Second, I would point out that the system you use for converting a "string" to
the buffer will fail if there are any non-ascii characters.
Third, OT, but I wonder why pciDevice is a string pointer, instead of a string?
Personally, I don't see anything decisively incorrect, but there is a lot of
context that is missing. If you wanted to indicate your OS, and provide a
complete working sample, I'm sure I, or someone else could help more.
One thing that jumps out at me is the use of
uintptr(unsafe.Pointer(&ioctlId)), in your initial example. It is hidden by the
function VFIO_GROUP_GET_DEVICE_FD(), which is not provided, in your larger
example. Are you sure you want the pointer to that value, and not the actual
value? Maybe uintptr(ioctlId)) instead. Seems more standard for IOCTL to me.
And, of course the changes you made to create an appropriate string buffer are
necessary.
On Thursday, August 23, 2018 at 11:47:18 AM UTC-4, [email protected] wrote:
Hi Max,
Thanks for the suggestion, unfortunately it did not help, see below:
func GetGroupFD(group int, pciDevice *string) (int, error) {
fmt.Printf("VFIO_GROUP_GET_DEVICE_FD() returned: %04x\n",
VFIO_GROUP_GET_DEVICE_FD())
buffer := make([]byte, len(*pciDevice)+1)
for i, c := range *pciDevice {
buffer[i] = uint8(c)
}
buffer[len(*pciDevice)] = 0x0
fmt.Printf("pciDevice: %s\n", string(buffer))
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(VFIO_GROUP_GET_DEVICE_FD()),
uintptr(unsafe.Pointer(&buffer[0])),
)
if errno != 0 {
return 0, fmt.Errorf("fail to get file descriptor for %d with errno:
%+v", group, errno)
}
return int(device), nil
}
Any other suggestions?
Thank you
Serguei
From: <[email protected]<javascript:>> on behalf of Max
<[email protected]<javascript:>>
Date: Thursday, August 23, 2018 at 10:46 AM
To: golang-nuts <[email protected]<javascript:>>
Subject: Re: [go-nuts] Re: Ioctl Syscall with go and c
Hi Serguei,
a Go string or *string do not correspond to a C char *
You must pass the address of the first byte of a sufficiently large buffer:
func GetGroupFD(group int, pciDevice *string) (int, error) {
const N = 256
var buffer [N]byte
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer(&ioctlId)),
&buffer[0],
)
/* if ioctl() is successful, find '\0' in buffer[] and copy the relevant
portion in *pciDevice */
}
On Thursday, August 23, 2018 at 2:55:29 PM UTC+2, [email protected] wrote:
I changed code a little bit to be able to examine variables:
func GetGroupFD(group int, pciDevice *string) (int, error) {
ioctlId := 0x3b6a
var buffer uintptr
buffer = uintptr(unsafe.Pointer(pciDevice))
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected]<javascript:>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
[email protected]<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.