aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec.go
AgeCommit message (Collapse)Author
2025-09-15os: add (*Process).WithHandleKir Kolyshkin
Implement (*Process).WithHandle, add tests for all platforms. Fixes #70352 Change-Id: I7a8012fb4e1e1b4ce1e75a59403ff6e77504fc56 Reviewed-on: https://go-review.googlesource.com/c/go/+/699615 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Auto-Submit: Kirill Kolyshkin <kolyshkin@gmail.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2025-09-15os: add and use errProcessReleasedKir Kolyshkin
This error is already used in three places, so let's define it. Change-Id: I73565d94aebcf3d5a278201d96839d82db85a2d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/702436 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Sean Liao <sean@liao.dev> Reviewed-by: Michael Pratt <mpratt@google.com>
2025-02-07os: improve comments for process support, minor code cleanupIan Lance Taylor
Change-Id: I97ecbc6fc0c73c6d8469144f86a7ad8c2655a658 Reviewed-on: https://go-review.googlesource.com/c/go/+/638581 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-02-07os: simplify Process.ReleaseIan Lance Taylor
Consolidate release/deactivation into a single doRelease method. It needs to check GOOS for backward compatibility, but it's simpler to keep all the logic in one place. Change-Id: I242eb084d44d2682f862a8fbf55c410fb8c53358 Reviewed-on: https://go-review.googlesource.com/c/go/+/638580 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-02-07os: use AddCleanup, not SetFinalizer, for ProcessIan Lance Taylor
There is no reason to use a cleanup/finalizer for a Process that doesn't use a handle, because Release doesn't change anything visible about the process. For #70907 Change-Id: I3b92809175523ceee2e07d601cc2a8e8b86321e0 Reviewed-on: https://go-review.googlesource.com/c/go/+/638579 Reviewed-by: Carlos Amedee <carlos@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2025-02-06os: simplify process statusIan Lance Taylor
Since it no longer holds a reference count, just use values. For #70907 Change-Id: I19a42583988d4f8a9133b1c837356ca0179d688c Reviewed-on: https://go-review.googlesource.com/c/go/+/638578 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
2025-02-06os: don't store reference count in Process.stateIan Lance Taylor
We only need a reference count in processHandle. For #70907 Fixes #71564 Change-Id: I209ded869203dea10f12b070190774fb5f1d3d71 Reviewed-on: https://go-review.googlesource.com/c/go/+/638577 Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2025-02-04os: remove Process.mode fieldIan Lance Taylor
It's now redundant with checking whether the handle field is nil. For #70907 Change-Id: I877f2a7c63d15ab5f8e3d2c9aa24776c2e3e2056 Reviewed-on: https://go-review.googlesource.com/c/go/+/638576 Reviewed-by: Robert Griesemer <gri@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: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com>
2025-02-04os: separate Process.handle into a separate memory allocationIan Lance Taylor
This is a step toward using AddCleanup rather than SetFinalizer to close process handles. For #70907 Change-Id: I7fb37461dd67b27135eab46fbdae94f0058ace85 Reviewed-on: https://go-review.googlesource.com/c/go/+/638575 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com>
2024-06-10os: overhaul handling of PID vs pidfd within ProcessMichael Pratt
There are several issues with pidfd handling today: * The zero value of a Process makes the handle field appear valid, so methods attempt to use it as a pidfd rather than falling back to the PID as they should (#67634). * If a process doesn't exist, FindProcess returns a Process with Pid == -2, which is not a compatible change (#67640). * pidfd close is racy as-is. A Release call or successful Wait will clear the handle field and close the pidfd. However, a concurrent call may have already loaded the handle field and could then proceed to use the closed FD (which could have been reopened as a different pidfd, targeting a different process) (#67641). This CL performs multiple structural changes to the internals of Process. First and foremost, each method is refactored to clearly select either pidfd or raw pid mode. Previously, raw pid mode was structured as a fallback when pidfd mode is unavailable. This works fine, but it does not make it clear that a given Process object either always uses pidfd or always uses raw pid. Since each mode needs to handle different race conditions, it helps to make it clear that we can't switch between modes within a single Process object. Second, pidfd close safety is handled by reference counting uses of the FD. The last user of the FD will close the FD. For example, this means that with concurrent Release and Signal, the Signal call may be the one to close the FD. This is the bulk of this CL, though I find the end result makes the overall implementation easier to reason about. Third, the PID path handles a similar race condtion between Wait and Kill: Wait frees the PID value in the kernel, which could be reallocated causing Kill to target the wrong process. This is handled with a done flag and a mutex. The done flag now shares the same state field used for the handle. Similarly, the Windows implementation reuses all of the handle reference counting that Linux uses. This means the implementations more consistent, and make Windows safe against the same handle reuse problems. (Though I am unsure if Windows ever reuses handles). Wait has a slight behavior change on Windows: previously Wait after Release or an earlier Wait would hang indefinitely (WaitForSingleObject on syscall.InvalidHandle waits indefinitely). Now it returns the same errors as Linux (EINVAL and ErrProcessDone, respectively). Similarly, Release on Windows no longer returns close errors, as it may not actually be the place where the close occurs. Fixes #67634. Fixes #67640. Fixes #67641. Updates #67642. Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Change-Id: I2ad998f7b67d32031e6f870e8533dbd55d3c3d10 Reviewed-on: https://go-review.googlesource.com/c/go/+/588675 Reviewed-by: Austin Clements <austin@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-05-17os: make use of pidfd on linuxKir Kolyshkin
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>
2024-02-26os: add available godoc linkcui fliter
Change-Id: I430c9a7c4936d7a8c8c787aa63de9a796d20fdf3 Reviewed-on: https://go-review.googlesource.com/c/go/+/539597 Reviewed-by: Carlos Amedee <carlos@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: shuang cui <imcusg@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-02-23Revert "os: make FindProcess use pidfd on Linux"Michael Pratt
This reverts CL 542699. Reason for revert: Some applications assume FindProcess does not return errors. For #62654. Fixes #65866. Change-Id: Ic185a6253c8e508b08150b618c39a9905f6cdd60 Reviewed-on: https://go-review.googlesource.com/c/go/+/566476 Reviewed-by: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Pratt <mpratt@google.com>
2024-02-21os: make FindProcess use pidfd on LinuxKir Kolyshkin
Amend FindProcess to use pidfdFind, and make it return ErrProcessDone if pidfdFind is used and the process is not found. Since this is a change in API, introduce GODEBUG osfinderr=0 setting to disable the feature. Change-Id: I724c6f622f0c99f21a70b864cf7cf2b8836869ee Reviewed-on: https://go-review.googlesource.com/c/go/+/542699 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-02-21os: use atomic.Uintptr for Process.handleKir Kolyshkin
Suggested-by: Michael Knyszek <mknyszek@google.com> Change-Id: I116731b6c3738aae8ff1d3be227f8f51fa3320c7 Reviewed-on: https://go-review.googlesource.com/c/go/+/544795 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com> Reviewed-by: qiulaidongfeng <2645477756@qq.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-06-17os/exec: document a method to check if a process is aliveMoritz Poldrack
Fixes #34396 Change-Id: I35c4e3447f84e349adf7edba92ccb19b324bfe14 GitHub-Last-Rev: 4f06764109ddd9bdfbe4841fc1bebebe026eeb29 GitHub-Pull-Request: golang/go#60763 Reviewed-on: https://go-review.googlesource.com/c/go/+/502815 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2022-09-15os: convert Process.isdone to atomic typecuiweixie
Change-Id: Ia3213d22678be0d56bf4f34dfe458441f7f5da97 Reviewed-on: https://go-review.googlesource.com/c/go/+/426077 Run-TryBot: Michael Pratt <mpratt@google.com> Run-TryBot: Jenny Rakoczy <jenny@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Jenny Rakoczy <jenny@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Jenny Rakoczy <jenny@golang.org>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-10-13os: explain ProccessState.Exited() SIGKILL interactionForest Johnson
When a process is forcefully killed (for example, with SIGKILL on unix), its ProccessState.Exited() will return false. Change-Id: I8cebc8d28f2ba1b687c145f6d941647fc3a10665 GitHub-Last-Rev: 414ae1980fe61dd530c46cc2c7f4de3a5bc83ed5 GitHub-Pull-Request: golang/go#48871 Reviewed-on: https://go-review.googlesource.com/c/go/+/354698 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Alexander Rakoczy <alex@golang.org> Run-TryBot: Alexander Rakoczy <alex@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2021-05-21os: document that StartProcess puts files into blocking modeIan Lance Taylor
Fixes #43894 Change-Id: I2add7b8a4f6ae69a5ef1c48703fde21a4b74307c Reviewed-on: https://go-review.googlesource.com/c/go/+/321852 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2020-11-03os: export ErrProcessDone variable in windows and plan9Alwin Doss
Exposes ErrProcessDone variable in windows and plan9 also returns this error code instead of errors.New("os: process already finished") Fixes #42311 Change-Id: Ie807b6526e7b6c27636e6bffe5ff0c904b319be4 GitHub-Last-Rev: 2153e0d7020d8ee9e94087d02977ea049b7fd6a0 GitHub-Pull-Request: golang/go#42313 Reviewed-on: https://go-review.googlesource.com/c/go/+/266997 Reviewed-by: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dmitri Shuralyov <dmitshur@golang.org>
2018-03-05os/exec: document Process.Kill behaviourYury Smolsky
It is not clear from documentation what the Process.Kill does. And it leads to reccuring confusion about Cmd.Start/Wait methods. Fixes #24220 Change-Id: I66609d21d2954e195d13648014681530eed8ea6c Reviewed-on: https://go-review.googlesource.com/98715 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-01-26os: document inheritance of thread state over execAustin Clements
Fixes #23570. Change-Id: I462ada2960d710c2c94dc22a59d292703d83f612 Reviewed-on: https://go-review.googlesource.com/90255 Reviewed-by: Russ Cox <rsc@golang.org>
2018-01-09os: fix grammar nitRuss Cox
There should not be a comma before "and" in the original text, because what follows is not a complete sentence. Rewrite. Change-Id: Ie99f204cc87e911fb46149e2eb65e132fa1eb63a Reviewed-on: https://go-review.googlesource.com/87020 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-12-30os: document that StartProcess's argv starts with the binary nameBrad Fitzpatrick
Fixes #23277 Change-Id: Idbe09913c95dc951b9b195eb7ff1e75d2bb4d63d Reviewed-on: https://go-review.googlesource.com/85675 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-12-11cmd/go: invalidate cached test results if env vars or files changeRuss Cox
When we write a cached test result, we now also write a log of the environment variables and files inspected by the test run, along with a hash of their content. Before reusing a cached test result, we recompute the hash of the content specified by the log, and only use the result if that content has not changed. This makes test caching behave correctly for tests that consult environment variables or stat or read files or directories. Fixes #22593. Change-Id: I8608798e73c90e0c1911a38bf7e03e1232d784dc Reviewed-on: https://go-review.googlesource.com/81895 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-08-16os: consolidate filesHiroshi Ioka
Code movement only. If someone finds function 'foo' in "foo_linux.go", they will expect that the Window version of 'foo' exists in "foo_windows.go". Current code doesn't follow this manner. For example, 'sameFile' exists in "file_unix.go", "stat_plan9.go" and "types_windows.go". The CL address that problem by following rules: * readdir family => dir.go, dir_$GOOS.go * stat family => stat.go, stat_$GOOS.go * path-functions => path_$GOOS.go * sameFile => types.go, types_$GOOS.go * process-functions => exec.go, exec_$GOOS.go * hostname => sys.go, sys_$GOOS.go Change-Id: Ic3c64663ce0b2a364d7a414351cd3c772e70187b Reviewed-on: https://go-review.googlesource.com/27035 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-06-10os: on GNU/Linux use waitid to avoid wait/kill raceIan Lance Taylor
On systems that support the POSIX.1-2008 waitid function, we can use it to block until a wait will succeed. This avoids a possible race condition: if a program calls p.Kill/p.Signal and p.Wait from two different goroutines, then it is possible for the wait to complete just before the signal is sent. In that case, it is possible that the system will start a new process using the same PID between the wait and the signal, causing the signal to be sent to the wrong process. The Process.isdone field attempts to avoid that race, but there is a small gap of time between when wait returns and isdone is set when the race can occur. This CL avoids that race by using waitid to wait until the process has exited without actually collecting the PID. Then it sets isdone, then waits for any active signals to complete, and only then collects the PID. No test because any plausible test would require starting enough processes to recycle all the process IDs. Update #13987. Update #16028. Change-Id: Id2939431991d3b355dfb22f08793585fc0568ce8 Reviewed-on: https://go-review.googlesource.com/23967 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-02all: single space after period.Brad Fitzpatrick
The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-06-11os: fix a race between Process.signal() and wait() on WindowsPatrick Mezard
Process.handle was accessed without synchronization while wait() and signal() could be called concurrently. A first solution was to add a Mutex in Process but it was probably too invasive given Process.handle is only used on Windows. This version uses atomic operations to read the handle value. There is still a race between isDone() and the value of the handle, but it only leads to slightly incorrect error codes. The caller may get a: errors.New("os: process already finished") instead of: syscall.EINVAL which sounds harmless. Fixes #9382 Change-Id: Iefcc687a1166d5961c8f27154647b9b15a0f748a Reviewed-on: https://go-review.googlesource.com/9904 Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2014-09-08build: move package sources from src/pkg to srcRuss Cox
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.