From ebc763f76d1151d8aa3ac1b894b08527999a4938 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 26 Aug 2025 14:04:14 +0200 Subject: [PATCH] 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 Reviewed-by: Cherry Mui Auto-Submit: Tobias Klauser Reviewed-by: Ian Lance Taylor Reviewed-by: Carlos Amedee --- src/syscall/exec_freebsd.go | 6 ++++-- src/syscall/exec_linux.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/syscall/exec_freebsd.go b/src/syscall/exec_freebsd.go index 686fd23bef..e2896d2cfe 100644 --- a/src/syscall/exec_freebsd.go +++ b/src/syscall/exec_freebsd.go @@ -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 diff --git a/src/syscall/exec_linux.go b/src/syscall/exec_linux.go index 8b06760d16..14c13e273a 100644 --- a/src/syscall/exec_linux.go +++ b/src/syscall/exec_linux.go @@ -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 -- 2.52.0