Skip to content

Commit ac0b2f9

Browse files
committed
syscall: export Tc{get,set}pgrp for testing
Provide appropriate implementations of Tc{get,set}pgrp and export these for use in the TestForeground* tests in exec_unix_test.go. This avoids calling ioctl via syscall.Syscall on BSDs. Fixes #59667 Updates #63900 Change-Id: Ice4dcedae1f0931c026bddf33043d3864a52d44e Reviewed-on: https://go-review.googlesource.com/c/go/+/572155 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Joel Sing <[email protected]>
1 parent 4f0408a commit ac0b2f9

7 files changed

+77
-50
lines changed

src/syscall/exec_aix_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ func Getpgrp() (pgrp int) {
3434
return
3535
}
3636

37-
var IoctlPtr = ioctlPtr
37+
func Tcgetpgrp(fd int) (pgid int32, err error) {
38+
if errno := ioctlPtr(uintptr(fd), TIOCGPGRP, unsafe.Pointer(&pgid)); errno != 0 {
39+
return -1, errno
40+
}
41+
return pgid, nil
42+
}
43+
44+
func Tcsetpgrp(fd int, pgid int32) (err error) {
45+
return ioctlPtr(uintptr(fd), TIOCSPGRP, unsafe.Pointer(&pgid))
46+
}

src/syscall/exec_solaris_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ func Getpgrp() (pgrp int) {
3434
return
3535
}
3636

37-
var IoctlPtr = ioctlPtr
37+
func Tcgetpgrp(fd int) (pgid int32, err error) {
38+
if errno := ioctlPtr(uintptr(fd), TIOCGPGRP, unsafe.Pointer(&pgid)); errno != 0 {
39+
return -1, errno
40+
}
41+
return pgid, nil
42+
}
43+
44+
func Tcsetpgrp(fd int, pgid int32) (err error) {
45+
return ioctlPtr(uintptr(fd), TIOCSPGRP, unsafe.Pointer(&pgid))
46+
}

src/syscall/exec_unix_test.go

+12-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"syscall"
2020
"testing"
2121
"time"
22-
"unsafe"
2322
)
2423

2524
type command struct {
@@ -176,15 +175,12 @@ func TestForeground(t *testing.T) {
176175
}
177176
defer tty.Close()
178177

179-
// This should really be pid_t, however _C_int (aka int32) is generally
180-
// equivalent.
181-
fpgrp := int32(0)
178+
ttyFD := int(tty.Fd())
182179

183-
errno := syscall.IoctlPtr(tty.Fd(), syscall.TIOCGPGRP, unsafe.Pointer(&fpgrp))
184-
if errno != 0 {
185-
t.Fatalf("TIOCGPGRP failed with error code: %s", errno)
180+
fpgrp, err := syscall.Tcgetpgrp(ttyFD)
181+
if err != nil {
182+
t.Fatalf("Tcgetpgrp failed: %v", err)
186183
}
187-
188184
if fpgrp == 0 {
189185
t.Fatalf("Foreground process group is zero")
190186
}
@@ -194,7 +190,7 @@ func TestForeground(t *testing.T) {
194190
cmd := create(t)
195191

196192
cmd.proc.SysProcAttr = &syscall.SysProcAttr{
197-
Ctty: int(tty.Fd()),
193+
Ctty: ttyFD,
198194
Foreground: true,
199195
}
200196
cmd.Start()
@@ -217,7 +213,7 @@ func TestForeground(t *testing.T) {
217213

218214
// This call fails on darwin/arm64. The failure doesn't matter, though.
219215
// This is just best effort.
220-
syscall.IoctlPtr(tty.Fd(), syscall.TIOCSPGRP, unsafe.Pointer(&fpgrp))
216+
syscall.Tcsetpgrp(ttyFD, fpgrp)
221217
}
222218

223219
func TestForegroundSignal(t *testing.T) {
@@ -227,22 +223,19 @@ func TestForegroundSignal(t *testing.T) {
227223
}
228224
defer tty.Close()
229225

230-
// This should really be pid_t, however _C_int (aka int32) is generally
231-
// equivalent.
232-
fpgrp := int32(0)
226+
ttyFD := int(tty.Fd())
233227

234-
errno := syscall.IoctlPtr(tty.Fd(), syscall.TIOCGPGRP, unsafe.Pointer(&fpgrp))
235-
if errno != 0 {
236-
t.Fatalf("TIOCGPGRP failed with error code: %s", errno)
228+
fpgrp, err := syscall.Tcgetpgrp(ttyFD)
229+
if err != nil {
230+
t.Fatalf("Tcgetpgrp failed: %v", err)
237231
}
238-
239232
if fpgrp == 0 {
240233
t.Fatalf("Foreground process group is zero")
241234
}
242235

243236
defer func() {
244237
signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)
245-
syscall.IoctlPtr(tty.Fd(), syscall.TIOCSPGRP, unsafe.Pointer(&fpgrp))
238+
syscall.Tcsetpgrp(ttyFD, fpgrp)
246239
signal.Reset()
247240
}()
248241

@@ -256,7 +249,7 @@ func TestForegroundSignal(t *testing.T) {
256249

257250
go func() {
258251
cmd.proc.SysProcAttr = &syscall.SysProcAttr{
259-
Ctty: int(tty.Fd()),
252+
Ctty: ttyFD,
260253
Foreground: true,
261254
}
262255
cmd.Start()

src/syscall/export_bsd_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 syscall
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
// pgid should really be pid_t, however _C_int (aka int32) is generally
14+
// equivalent.
15+
16+
func Tcgetpgrp(fd int) (pgid int32, err error) {
17+
if err := ioctlPtr(fd, TIOCGPGRP, unsafe.Pointer(&pgid)); err != nil {
18+
return -1, err
19+
}
20+
return pgid, nil
21+
}
22+
23+
func Tcsetpgrp(fd int, pgid int32) (err error) {
24+
return ioctlPtr(fd, TIOCSPGRP, unsafe.Pointer(&pgid))
25+
}

src/syscall/export_darwin_test.go

-15
This file was deleted.

src/syscall/export_linux_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
package syscall
66

7+
import (
8+
"unsafe"
9+
)
10+
711
var (
812
RawSyscallNoError = rawSyscallNoError
913
ForceClone3 = &forceClone3
@@ -12,3 +16,19 @@ var (
1216
const (
1317
Sys_GETEUID = sys_GETEUID
1418
)
19+
20+
func Tcgetpgrp(fd int) (pgid int32, err error) {
21+
_, _, errno := Syscall6(SYS_IOCTL, uintptr(fd), uintptr(TIOCGPGRP), uintptr(unsafe.Pointer(&pgid)), 0, 0, 0)
22+
if errno != 0 {
23+
return -1, errno
24+
}
25+
return pgid, nil
26+
}
27+
28+
func Tcsetpgrp(fd int, pgid int32) (err error) {
29+
_, _, errno := Syscall6(SYS_IOCTL, uintptr(fd), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgid)), 0, 0, 0)
30+
if errno != 0 {
31+
return errno
32+
}
33+
return nil
34+
}

src/syscall/export_unix_test.go

-14
This file was deleted.

0 commit comments

Comments
 (0)