Skip to content

Commit fba54f6

Browse files
committed
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 <[email protected]> Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Joel Sing <[email protected]>
1 parent f94d82b commit fba54f6

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
6+
7+
package unix
8+
9+
import (
10+
"syscall"
11+
"unsafe"
12+
)
13+
14+
//go:linkname ioctlPtr syscall.ioctlPtr
15+
func ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error)
16+
17+
// Note that pgid should really be pid_t, however _C_int (aka int32) is
18+
// generally equivalent.
19+
20+
func Tcsetpgrp(fd int, pgid int32) (err error) {
21+
return ioctlPtr(fd, syscall.TIOCSPGRP, unsafe.Pointer(&pgid))
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
import (
8+
"syscall"
9+
"unsafe"
10+
)
11+
12+
// Note that pgid should really be pid_t, however _C_int (aka int32) is
13+
// generally equivalent.
14+
15+
func Tcsetpgrp(fd int, pgid int32) (err error) {
16+
_, _, errno := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCSPGRP), uintptr(unsafe.Pointer(&pgid)), 0, 0, 0)
17+
if errno != 0 {
18+
return errno
19+
}
20+
return nil
21+
}

src/os/signal/signal_cgo_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"context"
1515
"encoding/binary"
1616
"fmt"
17+
"internal/syscall/unix"
1718
"internal/testenv"
1819
"internal/testpty"
1920
"os"
@@ -23,7 +24,6 @@ import (
2324
"syscall"
2425
"testing"
2526
"time"
26-
"unsafe"
2727
)
2828

2929
const (
@@ -304,9 +304,8 @@ func runSessionLeader(t *testing.T, pause time.Duration) {
304304

305305
// Take TTY.
306306
pgrp := int32(syscall.Getpgrp()) // assume that pid_t is int32
307-
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, ptyFD, syscall.TIOCSPGRP, uintptr(unsafe.Pointer(&pgrp)))
308-
if errno != 0 {
309-
return fmt.Errorf("error setting tty process group: %w", errno)
307+
if err := unix.Tcsetpgrp(ptyFD, pgrp); err != nil {
308+
return fmt.Errorf("error setting tty process group: %w", err)
310309
}
311310

312311
// Give the kernel time to potentially wake readers and have
@@ -315,9 +314,8 @@ func runSessionLeader(t *testing.T, pause time.Duration) {
315314

316315
// Give TTY back.
317316
pid := int32(cmd.Process.Pid) // assume that pid_t is int32
318-
_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, ptyFD, syscall.TIOCSPGRP, uintptr(unsafe.Pointer(&pid)))
319-
if errno != 0 {
320-
return fmt.Errorf("error setting tty process group back: %w", errno)
317+
if err := unix.Tcsetpgrp(ptyFD, pid); err != nil {
318+
return fmt.Errorf("error setting tty process group back: %w", err)
321319
}
322320

323321
// Report that we are done and SIGCONT can be sent. Note that

0 commit comments

Comments
 (0)