I'm trying to convert, or at least understand this ioctl
call:
#define NVME_URING_CMD_IO _IOWR('N', 0x80, struct nvme_uring_cmd)
Is there an equivalent of _IOWR
in golang? maybe with the unix
package?
Ok, I managed to convert it, thanks to help in comments:
const (
IocNrBits = 8
IocTypeBits = 8
IocSizeBits = 14
IocNrShift = 0
IocRead uintptr = 2
IocWrite uintptr = 2
)
const (
IocTypeShift = IocNrShift + IocNrBits
IocSizeShift = IocTypeShift + IocTypeBits
IocDirshift = IocSizeShift + IocSizeBits
)
func IOC(dir, t, nr, size uintptr) uintptr {
return (dir << IocDirshift) |
(t << IocTypeShift) |
(nr << IocNrShift) |
(size << IocSizeShift)
}
func IOWR(t, nr, size uintptr) uintptr {
return IOC(IocRead|IocWrite, t, nr, size)
}
func NVME_URING_CMD_IO() uintptr {
return IOWR('N', 0x80, 32)
}
grep -r -E '#define\s+_IOWR' /usr/include/
on your system and then read theioctl.h
file it finds? I mean, the question as stated makes little sense as you've shown a definition of a C macro which ostensibly refers to another C macro (which may refer to other—all the way down to the elephants and the turtle). So you first have to come up with the "unwrapped" bit of C code for your question to have sense. Note that the definition you have shown does not look like anioctl
call.#include <sys/ioctl.h>
and then contains the definition you've cited, and then declares a variable of typeNVME_URING_CMD_IO
. Then ask the preprocessor to expand all the macros—forgcc
that will begcc -E yourprogramfile.c
. You will see what kind of thing all this gets unwrapped in the end._IOWR
and similar macros compile to a magic constant. You can't always get this constant directly from Go as the constant embeds the result ofsizeof
some C data type and not all C data types can be expressed in Go (unions are problematic in particular).