1
0
mirror of https://github.com/golang/go synced 2024-11-22 10:54:46 -07:00

os/signal: avoid calling ioctl via syscall.Syscall on BSDs

Provide appropriate implementations of internal/syscall/unix.Tcsetpgrp
and use this for runSessionLeader in os/signal/signal_cgo_test.go.
This avoids calling syscall.Syscall with SYS_IOCTL on BSDs.

Updates #59667
Updates #63900

Change-Id: Ifa4696bba9f1eb68e81e7103f030bc254adaf0af
Reviewed-on: https://go-review.googlesource.com/c/go/+/540020
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Joel Sing <joel@sing.id.au>
This commit is contained in:
Joel Sing 2023-11-07 00:39:27 +11:00
parent f94d82b2c0
commit fba54f6345
3 changed files with 48 additions and 7 deletions

View File

@ -0,0 +1,22 @@
// Copyright 2024 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 darwin || dragonfly || freebsd || netbsd || openbsd
package unix
import (
"syscall"
"unsafe"
)
//go:linkname ioctlPtr syscall.ioctlPtr
func ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error)
// Note that pgid should really be pid_t, however _C_int (aka int32) is
// generally equivalent.
func Tcsetpgrp(fd int, pgid int32) (err error) {
return ioctlPtr(fd, syscall.TIOCSPGRP, unsafe.Pointer(&pgid))
}

View File

@ -0,0 +1,21 @@
// Copyright 2024 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.
package unix
import (
"syscall"
"unsafe"
)
// Note that pgid should really be pid_t, however _C_int (aka int32) is
// generally equivalent.
func Tcsetpgrp(fd int, pgid int32) (err error) {
_, _, errno := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCSPGRP), uintptr(unsafe.Pointer(&pgid)), 0, 0, 0)
if errno != 0 {
return errno
}
return nil
}

View File

@ -14,6 +14,7 @@ import (
"context"
"encoding/binary"
"fmt"
"internal/syscall/unix"
"internal/testenv"
"internal/testpty"
"os"
@ -23,7 +24,6 @@ import (
"syscall"
"testing"
"time"
"unsafe"
)
const (
@ -304,9 +304,8 @@ func runSessionLeader(t *testing.T, pause time.Duration) {
// Take TTY.
pgrp := int32(syscall.Getpgrp()) // assume that pid_t is int32
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, ptyFD, syscall.TIOCSPGRP, uintptr(unsafe.Pointer(&pgrp)))
if errno != 0 {
return fmt.Errorf("error setting tty process group: %w", errno)
if err := unix.Tcsetpgrp(ptyFD, pgrp); err != nil {
return fmt.Errorf("error setting tty process group: %w", err)
}
// Give the kernel time to potentially wake readers and have
@ -315,9 +314,8 @@ func runSessionLeader(t *testing.T, pause time.Duration) {
// Give TTY back.
pid := int32(cmd.Process.Pid) // assume that pid_t is int32
_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, ptyFD, syscall.TIOCSPGRP, uintptr(unsafe.Pointer(&pid)))
if errno != 0 {
return fmt.Errorf("error setting tty process group back: %w", errno)
if err := unix.Tcsetpgrp(ptyFD, pid); err != nil {
return fmt.Errorf("error setting tty process group back: %w", err)
}
// Report that we are done and SIGCONT can be sent. Note that