Skip to content

Commit 5d876e3

Browse files
committed
os: use wait6 to avoid wait/kill race on freebsd
This change is a followup to https://go-review.googlesource.com/23967 for FreeBSD. Updates #13987. Updates #16028. Change-Id: I0f0737372fce6df89d090fe9847305749b79eb4c Reviewed-on: https://go-review.googlesource.com/24021 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ccd9a55 commit 5d876e3

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/os/wait_unimp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build dragonfly freebsd nacl netbsd openbsd solaris
5+
// +build dragonfly nacl netbsd openbsd solaris
66

77
package os
88

src/os/wait_wait6.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 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+
// +build freebsd
6+
7+
package os
8+
9+
import (
10+
"runtime"
11+
"syscall"
12+
)
13+
14+
const _P_PID = 0
15+
16+
// blockUntilWaitable attempts to block until a call to p.Wait will
17+
// succeed immediately, and returns whether it has done so.
18+
// It does not actually call p.Wait.
19+
func (p *Process) blockUntilWaitable() (bool, error) {
20+
var errno syscall.Errno
21+
switch runtime.GOARCH {
22+
case "386", "arm":
23+
// The arguments on 32-bit FreeBSD look like the
24+
// following:
25+
// - freebsd32_wait6_args{ idtype, id1, id2, status, options, wrusage, info } or
26+
// - freebsd32_wait6_args{ idtype, pad, id1, id2, status, options, wrusage, info } when PAD64_REQUIRED=1 on MIPS or PowerPC
27+
_, _, errno = syscall.Syscall9(syscall.SYS_WAIT6, _P_PID, 0, uintptr(p.Pid), 0, syscall.WEXITED|syscall.WNOWAIT, 0, 0, 0, 0)
28+
default:
29+
_, _, errno = syscall.Syscall6(syscall.SYS_WAIT6, _P_PID, uintptr(p.Pid), 0, syscall.WEXITED|syscall.WNOWAIT, 0, 0)
30+
}
31+
if errno != 0 {
32+
// The wait6 system call is supported only on FreeBSD
33+
// 9.3 and above, so it may return an ENOSYS error.
34+
// Also the system call may return an ECHILD error
35+
// when the child process has not finished the
36+
// transformation using execve system call.
37+
// In both cases, we just leave the care of child
38+
// process to the following wait4 system call in
39+
// Process.wait.
40+
if errno == syscall.ENOSYS || errno == syscall.ECHILD {
41+
return false, nil
42+
}
43+
return false, NewSyscallError("wait6", errno)
44+
}
45+
return true, nil
46+
}

0 commit comments

Comments
 (0)