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

internal/syscall/unix, os: add and use Waitid syscall wrapper on linux

Instead of open-coding the waitid syscall wrapper add it to
internal/syscall/unix. As the syscall is currently only used on Linux,
switch the implementation in os.(*Process).blockUntilWaitable to use the
128-byte unix.SiginfoChild type instead of a plain 128-byte buffer.

Also use ignoringEINTR for the waitid calls instead of open-coding it.

Change-Id: I8dc47e361faa1f5e912d5de021f119c91c9f12f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/629655
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Tobias Klauser 2024-11-19 13:38:42 +01:00 committed by Gopher Robot
parent 5fdadffe3d
commit e82308c198
3 changed files with 35 additions and 33 deletions

View File

@ -0,0 +1,23 @@
// 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"
)
const (
P_PID = 1
P_PIDFD = 3
)
func Waitid(idType int, id int, info *SiginfoChild, options int, rusage *syscall.Rusage) error {
_, _, errno := syscall.Syscall6(syscall.SYS_WAITID, uintptr(idType), uintptr(id), uintptr(unsafe.Pointer(info)), uintptr(options), uintptr(unsafe.Pointer(rusage)), 0)
if errno != 0 {
return errno
}
return nil
}

View File

@ -21,7 +21,7 @@ import (
"runtime"
"sync"
"syscall"
"unsafe"
_ "unsafe" // for linkname
)
// ensurePidfd initializes the PidFD field in sysAttr if it is not already set.
@ -78,9 +78,6 @@ func pidfdFind(pid int) (uintptr, error) {
return h, nil
}
// _P_PIDFD is used as idtype argument to waitid syscall.
const _P_PIDFD = 3
func (p *Process) pidfdWait() (*ProcessState, error) {
// When pidfd is used, there is no wait/kill race (described in CL 23967)
// because the PID recycle issue doesn't exist (IOW, pidfd, unlike PID,
@ -104,16 +101,12 @@ func (p *Process) pidfdWait() (*ProcessState, error) {
var (
info unix.SiginfoChild
rusage syscall.Rusage
e syscall.Errno
)
for {
_, _, e = syscall.Syscall6(syscall.SYS_WAITID, _P_PIDFD, handle, uintptr(unsafe.Pointer(&info)), syscall.WEXITED, uintptr(unsafe.Pointer(&rusage)), 0)
if e != syscall.EINTR {
break
}
}
if e != 0 {
return nil, NewSyscallError("waitid", e)
err := ignoringEINTR(func() error {
return unix.Waitid(unix.P_PIDFD, int(handle), &info, syscall.WEXITED, &rusage)
})
if err != nil {
return nil, NewSyscallError("waitid", err)
}
// Release the Process' handle reference, in addition to the reference
// we took above.
@ -168,12 +161,9 @@ func checkPidfd() error {
defer syscall.Close(int(fd))
// Check waitid(P_PIDFD) works.
for {
_, _, err = syscall.Syscall6(syscall.SYS_WAITID, _P_PIDFD, fd, 0, syscall.WEXITED, 0, 0)
if err != syscall.EINTR {
break
}
}
err = ignoringEINTR(func() error {
return unix.Waitid(unix.P_PIDFD, int(fd), nil, syscall.WEXITED, nil)
})
// Expect ECHILD from waitid since we're not our own parent.
if err != syscall.ECHILD {
return NewSyscallError("pidfd_wait", err)

View File

@ -10,29 +10,18 @@
package os
import (
"internal/syscall/unix"
"runtime"
"syscall"
"unsafe"
)
const _P_PID = 1
// blockUntilWaitable attempts to block until a call to p.Wait will
// succeed immediately, and reports whether it has done so.
// It does not actually call p.Wait.
func (p *Process) blockUntilWaitable() (bool, error) {
// The waitid system call expects a pointer to a siginfo_t,
// which is 128 bytes on all Linux systems.
// On darwin/amd64, it requires 104 bytes.
// We don't care about the values it returns.
var siginfo [16]uint64
psig := &siginfo[0]
var info unix.SiginfoChild
err := ignoringEINTR(func() error {
_, _, errno := syscall.Syscall6(syscall.SYS_WAITID, _P_PID, uintptr(p.Pid), uintptr(unsafe.Pointer(psig)), syscall.WEXITED|syscall.WNOWAIT, 0, 0)
if errno != 0 {
return errno
}
return nil
return unix.Waitid(unix.P_PID, p.Pid, &info, syscall.WEXITED|syscall.WNOWAIT, nil)
})
runtime.KeepAlive(p)
if err != nil {