syscall: only get parent PID if SysProcAttr.Pdeathsig is set

The value of the parent PID is only used to check against get value
returned by getppid(2) in case SysProcAttr.Pdeathsig is non-zero. Avoid
the useless getpid(2) system call otherwise.

Cq-Include-Trybots: luci.golang.try:gotip-freebsd-amd64
Change-Id: If89f9c7acc82016ec359c79bd861d41460f42218
Reviewed-on: https://go-review.googlesource.com/c/go/+/699175
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
Tobias Klauser 2025-08-26 14:04:14 +02:00 committed by Gopher Robot
parent 7f1864b0a8
commit ebc763f76d
2 changed files with 8 additions and 4 deletions

View file

@ -68,13 +68,15 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
pgrp _C_int
cred *Credential
ngroups, groups uintptr
upid uintptr
upid, ppid uintptr
)
rlim := origRlimitNofile.Load()
// Record parent PID so child can test if it has died.
ppid, _, _ := RawSyscall(SYS_GETPID, 0, 0, 0)
if sys.Pdeathsig != 0 {
ppid, _, _ = RawSyscall(SYS_GETPID, 0, 0, 0)
}
// guard against side effects of shuffling fds below.
// Make sure that nextfd is beyond any currently open files so

View file

@ -244,7 +244,7 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
nextfd int
i int
caps caps
fd1, flags uintptr
fd1, flags, ppid uintptr
puid, psetgroups, pgid []byte
uidmap, setgroups, gidmap []byte
clone3 *cloneArgs
@ -278,7 +278,9 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
}
// Record parent PID so child can test if it has died.
ppid, _ := rawSyscallNoError(SYS_GETPID, 0, 0, 0)
if sys.Pdeathsig != 0 {
ppid, _ = rawSyscallNoError(SYS_GETPID, 0, 0, 0)
}
// Guard against side effects of shuffling fds below.
// Make sure that nextfd is beyond any currently open files so