From d25a7aaff8c2b056b2059fd7065afe1d4132e082 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 14 Jan 2026 11:54:12 +0100 Subject: [PATCH] unix: add IoctlSetString on all platforms Currently, only solaris provides IoctlSetString. However, it might be useful on other platforms too, e.g. for SIOCBRADDBR on linux. Change-Id: I19ed47a3e4ed0180ba6777bc193e32bfb61c0506 Reviewed-on: https://go-review.googlesource.com/c/sys/+/720200 Reviewed-by: Michael Pratt Reviewed-by: Ian Lance Taylor Reviewed-by: Florian Lehner LUCI-TryBot-Result: Go LUCI Auto-Submit: Tobias Klauser Reviewed-by: Cherry Mui --- unix/ioctl_signed.go | 11 ++++++++--- unix/ioctl_unsigned.go | 11 ++++++++--- unix/syscall_solaris.go | 8 -------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/unix/ioctl_signed.go b/unix/ioctl_signed.go index 5b0759bd..be0f3fba 100644 --- a/unix/ioctl_signed.go +++ b/unix/ioctl_signed.go @@ -6,9 +6,7 @@ package unix -import ( - "unsafe" -) +import "unsafe" // ioctl itself should not be exposed directly, but additional get/set // functions for specific types are permissible. @@ -28,6 +26,13 @@ func IoctlSetPointerInt(fd int, req int, value int) error { return ioctlPtr(fd, req, unsafe.Pointer(&v)) } +// IoctlSetString performs an ioctl operation which sets a string value +// on fd, using the specified request number. +func IoctlSetString(fd int, req int, value string) error { + bs := append([]byte(value), 0) + return ioctlPtr(fd, req, unsafe.Pointer(&bs[0])) +} + // IoctlSetWinsize performs an ioctl on fd with a *Winsize argument. // // To change fd's window size, the req argument should be TIOCSWINSZ. diff --git a/unix/ioctl_unsigned.go b/unix/ioctl_unsigned.go index 20f470b9..f0c28213 100644 --- a/unix/ioctl_unsigned.go +++ b/unix/ioctl_unsigned.go @@ -6,9 +6,7 @@ package unix -import ( - "unsafe" -) +import "unsafe" // ioctl itself should not be exposed directly, but additional get/set // functions for specific types are permissible. @@ -28,6 +26,13 @@ func IoctlSetPointerInt(fd int, req uint, value int) error { return ioctlPtr(fd, req, unsafe.Pointer(&v)) } +// IoctlSetString performs an ioctl operation which sets a string value +// on fd, using the specified request number. +func IoctlSetString(fd int, req uint, value string) error { + bs := append([]byte(value), 0) + return ioctlPtr(fd, req, unsafe.Pointer(&bs[0])) +} + // IoctlSetWinsize performs an ioctl on fd with a *Winsize argument. // // To change fd's window size, the req argument should be TIOCSWINSZ. diff --git a/unix/syscall_solaris.go b/unix/syscall_solaris.go index 18a3d9bd..a6a2ea0c 100644 --- a/unix/syscall_solaris.go +++ b/unix/syscall_solaris.go @@ -1052,14 +1052,6 @@ func IoctlSetIntRetInt(fd int, req int, arg int) (int, error) { return ioctlRet(fd, req, uintptr(arg)) } -func IoctlSetString(fd int, req int, val string) error { - bs := make([]byte, len(val)+1) - copy(bs[:len(bs)-1], val) - err := ioctlPtr(fd, req, unsafe.Pointer(&bs[0])) - runtime.KeepAlive(&bs[0]) - return err -} - // Lifreq Helpers func (l *Lifreq) SetName(name string) error {