-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Process.handle field to store pidfd, and make use of it. Only use pidfd functionality if all the needed syscalls are available. 1. Add/use pidfdWorks, which checks that all needed pidfd-related functionality works. 2. os.StartProcess: obtain the pidfd from the kernel, if possible, using the functionality added by CL 520266. Note we could not modify syscall.StartProcess to return pidfd directly because it is a public API and its callers do not expect it, so we have to use ensurePidfd and getPidfd. 3. (*Process).Kill: use pidfdSendSignal, if available and the pidfd is known. Otherwise, fall back to the old implementation. 4. (*Process).Wait: use pidfdWait, if available, otherwise fall back to using waitid/wait4. This is more complicated than expected due to struct siginfo_t idiosyncrasy. NOTE pidfdSendSignal and pidfdWait are used without a race workaround (blockUntilWaitable and sigMu, added by CL 23967) because with pidfd, PID recycle issue doesn't exist (IOW, pidfd, unlike PID, is guaranteed to refer to one particular process) and thus the race doesn't exist either. Rework of CL 528438 (reverted in CL 566477 because of #65857). For #62654. Updates #13987. Change-Id: If5ef8920bd8619dc428b6282ffe4fb8c258ca224 Reviewed-on: https://go-review.googlesource.com/c/go/+/570036 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Cherry Mui <cherryyz@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
- Loading branch information
Showing
20 changed files
with
393 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 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" | ||
) | ||
|
||
const is64bit = ^uint(0) >> 63 // 0 for 32-bit hosts, 1 for 64-bit ones. | ||
|
||
// SiginfoChild is a struct filled in by Linux waitid syscall. | ||
// In C, siginfo_t contains a union with multiple members; | ||
// this struct corresponds to one used when Signo is SIGCHLD. | ||
// | ||
// NOTE fields are exported to be used by TestSiginfoChildLayout. | ||
type SiginfoChild struct { | ||
Signo int32 | ||
siErrnoCode // Two int32 fields, swapped on MIPS. | ||
_ [is64bit]int32 // Extra padding for 64-bit hosts only. | ||
|
||
// End of common part. Beginning of signal-specific part. | ||
|
||
Pid int32 | ||
Uid uint32 | ||
Status int32 | ||
|
||
// Pad to 128 bytes. | ||
_ [128 - (6+is64bit)*4]byte | ||
} | ||
|
||
const ( | ||
// Possible values for SiginfoChild.Code field. | ||
_CLD_EXITED int32 = 1 | ||
_CLD_KILLED = 2 | ||
_CLD_DUMPED = 3 | ||
_CLD_TRAPPED = 4 | ||
_CLD_STOPPED = 5 | ||
_CLD_CONTINUED = 6 | ||
|
||
// These are the same as in syscall/syscall_linux.go. | ||
core = 0x80 | ||
stopped = 0x7f | ||
continued = 0xffff | ||
) | ||
|
||
// WaitStatus converts SiginfoChild, as filled in by the waitid syscall, | ||
// to syscall.WaitStatus. | ||
func (s *SiginfoChild) WaitStatus() (ws syscall.WaitStatus) { | ||
switch s.Code { | ||
case _CLD_EXITED: | ||
ws = syscall.WaitStatus(s.Status << 8) | ||
case _CLD_DUMPED: | ||
ws = syscall.WaitStatus(s.Status) | core | ||
case _CLD_KILLED: | ||
ws = syscall.WaitStatus(s.Status) | ||
case _CLD_TRAPPED, _CLD_STOPPED: | ||
ws = syscall.WaitStatus(s.Status<<8) | stopped | ||
case _CLD_CONTINUED: | ||
ws = continued | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2023 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 linux && (mips || mipsle || mips64 || mips64le) | ||
|
||
package unix | ||
|
||
type siErrnoCode struct { | ||
Code int32 | ||
Errno int32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2023 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 linux && !(mips || mipsle || mips64 || mips64le) | ||
|
||
package unix | ||
|
||
type siErrnoCode struct { | ||
Errno int32 | ||
Code int32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 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_test | ||
|
||
import ( | ||
"internal/goarch" | ||
"internal/syscall/unix" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
// TestSiginfoChildLayout validates SiginfoChild layout. Modelled after | ||
// static assertions in linux kernel's arch/*/kernel/signal*.c. | ||
func TestSiginfoChildLayout(t *testing.T) { | ||
var si unix.SiginfoChild | ||
|
||
const host64bit = goarch.PtrSize == 8 | ||
|
||
if v := unsafe.Sizeof(si); v != 128 { | ||
t.Fatalf("sizeof: got %d, want 128", v) | ||
} | ||
|
||
ofSigno := 0 | ||
ofErrno := 4 | ||
ofCode := 8 | ||
if strings.HasPrefix(runtime.GOARCH, "mips") { | ||
// These two fields are swapped on MIPS platforms. | ||
ofErrno, ofCode = ofCode, ofErrno | ||
} | ||
ofPid := 12 | ||
if host64bit { | ||
ofPid = 16 | ||
} | ||
ofUid := ofPid + 4 | ||
ofStatus := ofPid + 8 | ||
|
||
offsets := []struct { | ||
name string | ||
got uintptr | ||
want int | ||
}{ | ||
{"Signo", unsafe.Offsetof(si.Signo), ofSigno}, | ||
{"Errno", unsafe.Offsetof(si.Errno), ofErrno}, | ||
{"Code", unsafe.Offsetof(si.Code), ofCode}, | ||
{"Pid", unsafe.Offsetof(si.Pid), ofPid}, | ||
{"Uid", unsafe.Offsetof(si.Uid), ofUid}, | ||
{"Status", unsafe.Offsetof(si.Status), ofStatus}, | ||
} | ||
|
||
for _, tc := range offsets { | ||
if int(tc.got) != tc.want { | ||
t.Errorf("offsetof %s: got %d, want %d", tc.name, tc.got, tc.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.