Home > Article > Backend Development > How to Resolve \'Invalid Argument\' Errors When Implementing `ioctl()` in Go?
IOCTL Implementation in Go
Question:
In translating C code involving ioctl() to Go, an issue arises when converting the following C code:
#define MAJOR_NUM 100 #define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *) static int mbox_property(int file_desc, void *buf){ int ret_val = ioctl(file_desc, IOCTL_MBOX_PROPERTY, buf); return ret_val; }
The corresponding Go code:
func mBoxProperty(f *os.File, buf [256]int64) { err := Ioctl(f.Fd(), IOWR(100, 0, 8), uintptr(unsafe.Pointer(&buf[0]))) if err != nil { log.Fatalln("mBoxProperty() : ", err) } } func Ioctl(fd, op, arg uintptr) error { _, _, ep := syscall.Syscall(syscall.SYS_IOCTL, fd, op, arg) if ep != 0 { return syscall.Errno(ep) } return nil } func IOWR(t, nr, size uintptr) uintptr { return IOC(IocRead|IocWrite, t, nr, size) } func IOC(dir, t, nr, size uintptr) uintptr { return (dir << IocDirshift) | (t << IocTypeshift) | (nr << IocNrshift) | (size << IocSizeshift) }
causes an "invalid argument" error. What is the potential issue and how can it be resolved?
Answer:
To rectify the issue, consider the following:
The above is the detailed content of How to Resolve \'Invalid Argument\' Errors When Implementing `ioctl()` in Go?. For more information, please follow other related articles on the PHP Chinese website!