Skip to content

Commit

Permalink
unix: match ioctl req argument type to libc type
Browse files Browse the repository at this point in the history
On Solaris, AIX, and zOS, the req argument of ioctl() is a signed int,
not an unsigned long like on other platforms, which means many constants
are negative, causing friction when passing them to a uint argument.
Correct the signature of these functions to pass the req argument as
signed, just like libc.

Fixes golang/go#59030.

Change-Id: Ia14e92a150f4b5fb9488c5032ca296cb786e9811
Reviewed-on: https://go-review.googlesource.com/c/sys/+/476515
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Nahum Shalman <nahamu@gmail.com>
  • Loading branch information
zx2c4 committed Mar 17, 2023
1 parent d0781cc commit 00d8004
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 37 deletions.
70 changes: 70 additions & 0 deletions unix/ioctl_signed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || solaris
// +build aix solaris

package unix

import (
"unsafe"
)

// ioctl itself should not be exposed directly, but additional get/set
// functions for specific types are permissible.

// IoctlSetInt performs an ioctl operation which sets an integer value
// on fd, using the specified request number.
func IoctlSetInt(fd int, req int, value int) error {
return ioctl(fd, req, uintptr(value))
}

// IoctlSetPointerInt performs an ioctl operation which sets an
// integer value on fd, using the specified request number. The ioctl
// argument is called with a pointer to the integer value, rather than
// passing the integer value directly.
func IoctlSetPointerInt(fd int, req int, value int) error {
v := int32(value)
return ioctlPtr(fd, req, unsafe.Pointer(&v))
}

// IoctlSetWinsize performs an ioctl on fd with a *Winsize argument.
//
// To change fd's window size, the req argument should be TIOCSWINSZ.
func IoctlSetWinsize(fd int, req int, value *Winsize) error {
// TODO: if we get the chance, remove the req parameter and
// hardcode TIOCSWINSZ.
return ioctlPtr(fd, req, unsafe.Pointer(value))
}

// IoctlSetTermios performs an ioctl on fd with a *Termios.
//
// The req value will usually be TCSETA or TIOCSETA.
func IoctlSetTermios(fd int, req int, value *Termios) error {
// TODO: if we get the chance, remove the req parameter.
return ioctlPtr(fd, req, unsafe.Pointer(value))
}

// IoctlGetInt performs an ioctl operation which gets an integer value
// from fd, using the specified request number.
//
// A few ioctl requests use the return value as an output parameter;
// for those, IoctlRetInt should be used instead of this function.
func IoctlGetInt(fd int, req int) (int, error) {
var value int
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return value, err
}

func IoctlGetWinsize(fd int, req int) (*Winsize, error) {
var value Winsize
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return &value, err
}

func IoctlGetTermios(fd int, req int) (*Termios, error) {
var value Termios
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return &value, err
}
4 changes: 2 additions & 2 deletions unix/ioctl.go → unix/ioctl_unsigned.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd hurd linux netbsd openbsd solaris
//go:build darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd hurd linux netbsd openbsd

package unix

Expand Down
12 changes: 6 additions & 6 deletions unix/ioctl_zos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (

// IoctlSetInt performs an ioctl operation which sets an integer value
// on fd, using the specified request number.
func IoctlSetInt(fd int, req uint, value int) error {
func IoctlSetInt(fd int, req int, value int) error {
return ioctl(fd, req, uintptr(value))
}

// IoctlSetWinsize performs an ioctl on fd with a *Winsize argument.
//
// To change fd's window size, the req argument should be TIOCSWINSZ.
func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
func IoctlSetWinsize(fd int, req int, value *Winsize) error {
// TODO: if we get the chance, remove the req parameter and
// hardcode TIOCSWINSZ.
return ioctlPtr(fd, req, unsafe.Pointer(value))
Expand All @@ -33,7 +33,7 @@ func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
// IoctlSetTermios performs an ioctl on fd with a *Termios.
//
// The req value is expected to be TCSETS, TCSETSW, or TCSETSF
func IoctlSetTermios(fd int, req uint, value *Termios) error {
func IoctlSetTermios(fd int, req int, value *Termios) error {
if (req != TCSETS) && (req != TCSETSW) && (req != TCSETSF) {
return ENOSYS
}
Expand All @@ -47,13 +47,13 @@ func IoctlSetTermios(fd int, req uint, value *Termios) error {
//
// A few ioctl requests use the return value as an output parameter;
// for those, IoctlRetInt should be used instead of this function.
func IoctlGetInt(fd int, req uint) (int, error) {
func IoctlGetInt(fd int, req int) (int, error) {
var value int
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return value, err
}

func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
func IoctlGetWinsize(fd int, req int) (*Winsize, error) {
var value Winsize
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return &value, err
Expand All @@ -62,7 +62,7 @@ func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
// IoctlGetTermios performs an ioctl on fd with a *Termios.
//
// The req value is expected to be TCGETS
func IoctlGetTermios(fd int, req uint) (*Termios, error) {
func IoctlGetTermios(fd int, req int) (*Termios, error) {
var value Termios
if req != TCGETS {
return &value, ENOSYS
Expand Down
4 changes: 2 additions & 2 deletions unix/syscall_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ func (w WaitStatus) CoreDump() bool { return w&0x80 == 0x80 }

func (w WaitStatus) TrapCause() int { return -1 }

//sys ioctl(fd int, req uint, arg uintptr) (err error)
//sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = ioctl
//sys ioctl(fd int, req int, arg uintptr) (err error)
//sys ioctlPtr(fd int, req int, arg unsafe.Pointer) (err error) = ioctl

// fcntl must never be called with cmd=F_DUP2FD because it doesn't work on AIX
// There is no way to create a custom fcntl and to keep //sys fcntl easily,
Expand Down
20 changes: 10 additions & 10 deletions unix/syscall_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,24 +545,24 @@ func Minor(dev uint64) uint32 {
* Expose the ioctl function
*/

//sys ioctlRet(fd int, req uint, arg uintptr) (ret int, err error) = libc.ioctl
//sys ioctlPtrRet(fd int, req uint, arg unsafe.Pointer) (ret int, err error) = libc.ioctl
//sys ioctlRet(fd int, req int, arg uintptr) (ret int, err error) = libc.ioctl
//sys ioctlPtrRet(fd int, req int, arg unsafe.Pointer) (ret int, err error) = libc.ioctl

func ioctl(fd int, req uint, arg uintptr) (err error) {
func ioctl(fd int, req int, arg uintptr) (err error) {
_, err = ioctlRet(fd, req, arg)
return err
}

func ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) {
func ioctlPtr(fd int, req int, arg unsafe.Pointer) (err error) {
_, err = ioctlPtrRet(fd, req, arg)
return err
}

func IoctlSetTermio(fd int, req uint, value *Termio) error {
func IoctlSetTermio(fd int, req int, value *Termio) error {
return ioctlPtr(fd, req, unsafe.Pointer(value))
}

func IoctlGetTermio(fd int, req uint) (*Termio, error) {
func IoctlGetTermio(fd int, req int) (*Termio, error) {
var value Termio
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return &value, err
Expand Down Expand Up @@ -1079,11 +1079,11 @@ func Getmsg(fd int, cl []byte, data []byte) (retCl []byte, retData []byte, flags
return retCl, retData, flags, nil
}

func IoctlSetIntRetInt(fd int, req uint, arg int) (int, error) {
func IoctlSetIntRetInt(fd int, req int, arg int) (int, error) {
return ioctlRet(fd, req, uintptr(arg))
}

func IoctlSetString(fd int, req uint, val string) error {
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]))
Expand Down Expand Up @@ -1119,7 +1119,7 @@ func (l *Lifreq) GetLifruUint() uint {
return *(*uint)(unsafe.Pointer(&l.Lifru[0]))
}

func IoctlLifreq(fd int, req uint, l *Lifreq) error {
func IoctlLifreq(fd int, req int, l *Lifreq) error {
return ioctlPtr(fd, req, unsafe.Pointer(l))
}

Expand All @@ -1130,6 +1130,6 @@ func (s *Strioctl) SetInt(i int) {
s.Dp = (*int8)(unsafe.Pointer(&i))
}

func IoctlSetStrioctlRetInt(fd int, req uint, s *Strioctl) (int, error) {
func IoctlSetStrioctlRetInt(fd int, req int, s *Strioctl) (int, error) {
return ioctlPtrRet(fd, req, unsafe.Pointer(s))
}
6 changes: 1 addition & 5 deletions unix/syscall_solaris_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,13 @@ func TestLifreqGetMTU(t *testing.T) {
if err != nil {
t.Fatalf("could not open udp socket: %v", err)
}
// SIOCGLIFMTU is negative which confuses the compiler if used inline:
// Using "unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l)" results in
// "constant -1065850502 overflows uint"
reqnum := int(unix.SIOCGLIFMTU)
var l unix.Lifreq
for link, mtu := range tc {
err = l.SetName(link)
if err != nil {
t.Fatalf("Lifreq.SetName(%q) failed: %v", link, err)
}
if err = unix.IoctlLifreq(ip_fd, uint(reqnum), &l); err != nil {
if err = unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l); err != nil {
t.Fatalf("unable to SIOCGLIFMTU: %v", err)
}
m := l.GetLifruUint()
Expand Down
4 changes: 2 additions & 2 deletions unix/syscall_zos_s390x.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func (cmsg *Cmsghdr) SetLen(length int) {
//sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = SYS___SENDMSG_A
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) = SYS_MMAP
//sys munmap(addr uintptr, length uintptr) (err error) = SYS_MUNMAP
//sys ioctl(fd int, req uint, arg uintptr) (err error) = SYS_IOCTL
//sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL
//sys ioctl(fd int, req int, arg uintptr) (err error) = SYS_IOCTL
//sys ioctlPtr(fd int, req int, arg unsafe.Pointer) (err error) = SYS_IOCTL

//sys Access(path string, mode uint32) (err error) = SYS___ACCESS_A
//sys Chdir(path string) (err error) = SYS___CHDIR_A
Expand Down
4 changes: 2 additions & 2 deletions unix/zsyscall_aix_ppc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions unix/zsyscall_aix_ppc64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions unix/zsyscall_solaris_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions unix/zsyscall_zos_s390x.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00d8004

Please sign in to comment.