aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec.go
AgeCommit message (Collapse)Author
24 hoursos/exec: doc typoWeixie Cui
Change-Id: I623eee6738a8ebb1db2bc5693a9973c58878260c GitHub-Last-Rev: d7d918d4ede9d7a374a0a51f569c6890d74c8f91 GitHub-Pull-Request: golang/go#78609 Reviewed-on: https://go-review.googlesource.com/c/go/+/764364 Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Sean Liao <sean@liao.dev>
36 hoursos/exec: use argv() to avoid panic inside of Cmd.String()Neal Patel
A surprisingly non-zero amount of direct uses Cmd make this panic possible. Change-Id: If86cabfb0f7c0250e2a5aa3fcaba367de5d10ca4 Reviewed-on: https://go-review.googlesource.com/c/go/+/765680 Reviewed-by: Ian Lance Taylor <iant@golang.org> Auto-Submit: Neal Patel <nealpatel@google.com> TryBot-Bypass: Nicholas Husin <nsh@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Nicholas Husin <nsh@golang.org> Reviewed-by: Keith Randall <khr@google.com>
2026-03-17os/exec: document that Cmd.Wait must not be called concurrentlyBasavaraj PB
Clarify in the Wait documentation that it must not be called concurrently from multiple goroutines. Also note that a custom Cmd.Cancel function should not call Wait because Cancel may be invoked by watchCtx in a separate goroutine. Fixes #78046 Change-Id: I5c0ebc41bd3c39c78f3b37539c59cdfedfd90e72 Reviewed-on: https://go-review.googlesource.com/c/go/+/753602 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com>
2026-01-27os/exec: document blocking Stdin/Stdout/StderrIan Lance Taylor
WaitDelay only handles writes to Stdin and reads from Stdout/Stderr. If Stdin is set to a blocking Reader, or Stdout/Stderr are set to a blocking Writer, Wait can hang indefinitely. I don't see any way to fix this with the current API, as there is no general way that the os/exec package can interrupt the blocking Read or Write. This CL documents the limitation and points people toward the workaround of using StdinPipe/StdoutPipe/StderrPipe and arranging for their own way to interrupt the blocking Read or Write. Fixes #77227 Change-Id: I3150ae7af89dccf8d859b41eb43eaf0bbbb55fee Reviewed-on: https://go-review.googlesource.com/c/go/+/739422 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
2026-01-06os/exec: avoid atomic.Bool for Cmd.startCalledAlan Donovan
An atomic.Bool isn't necessary here since, unless otherwise specified, the methods of an object are not concurrency-safe w.r.t. each other. Using an atomic causes the copylocks vet check to warn about copying of Cmd, which is not wrong, because one shouldn't be copying opaque complex structs from other packages, but it is a nuisance in the absence of any safe way to copy a Cmd. If and when we add a Clone method to Cmd (see #77075) then it would be appropriate to revert this change so that we get the benefit of the static check (though ideally we would make a more explicit tool-readable declaration of the "do not copy" attribute than merely happening to use an atomic.Bool). For #77075 Change-Id: I982d4e86623ca165a3e76bbf648fd44041d5f6bb Reviewed-on: https://go-review.googlesource.com/c/go/+/734200 Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-12-22os/exec: second call to Cmd.Start is always an errorAlan Donovan
Previously it would return an error only if the first call resulted in process creation, contra the intent of the comment at exec.Cmd: // A Cmd cannot be reused after calling its [Cmd.Start], [Cmd.Run], // [Cmd.Output], or [Cmd.CombinedOutput] methods. Also, clear the Cmd.goroutines slice in case of failure to start a process, so that the closures can be GC'd and their pipe fds finalized and closed. Fixes #76746 Change-Id: Ic63a4dced0aa52c2d4be7d44f6dcfc84ee22282c Reviewed-on: https://go-review.googlesource.com/c/go/+/728642 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
2025-11-12os/exec: include Cmd.Start in the list of methods that run CmdSean Liao
Fixes #76265 Change-Id: I451271c5662dd3bcdeec07b55761b15f64c00dcd Reviewed-on: https://go-review.googlesource.com/c/go/+/719860 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Keith Randall <khr@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com>
2025-07-29os/exec: fix incorrect expansion of "", "." and ".." in LookPathOlivier Mengué
Fix incorrect expansion of "" and "." when $PATH contains an executable file or, on Windows, a parent directory of a %PATH% element contains an file with the same name as the %PATH% element but with one of the %PATHEXT% extension (ex: C:\utils\bin is in PATH, and C:\utils\bin.exe exists). Fix incorrect expansion of ".." when $PATH contains an element which is an the concatenation of the path to an executable file (or on Windows a path that can be expanded to an executable by appending a %PATHEXT% extension), a path separator and a name. "", "." and ".." are now rejected early with ErrNotFound. Fixes CVE-2025-47906 Fixes #74466 Change-Id: Ie50cc0a660fce8fbdc952a7f2e05c36062dcb50e Reviewed-on: https://go-review.googlesource.com/c/go/+/685755 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2025-06-12all: replace a few user-visible mentions of golang.org and godoc.orgAlberto Donizetti
This change replaces a few user-visible mentions of golang.org and godoc.org with go.dev and pkg.go.dev, respectively. Non-user-visible mentions (e.g. in test scripts) were left untouched. Change-Id: I5d828edcd618b6c55243d0dfcadc6fa1ce9422ce Reviewed-on: https://go-review.googlesource.com/c/go/+/681255 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2024-11-27os/exec: edit comment to remove invalid linkAdam Bender
Update comment to remove link formatting that doesn't turn into a link, because the target field is not a top-level member of the package. Re-word comment slightly. Change-Id: I43ebd8fb105b772a4362c0c763e6464321a92747 Reviewed-on: https://go-review.googlesource.com/c/go/+/631856 Reviewed-by: Veronica Silina <veronicasilina@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-08-29os/exec: document interaction of Dir, PWD, os.Getwd and CAlan Donovan
Fixes #68000 Change-Id: Ie70a8ecc9573b2a4cf57119bda57e0af5e16c42f Reviewed-on: https://go-review.googlesource.com/c/go/+/609395 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-07-07os/exec: only use cachedLookExtensions if Cmd.Path is unmodifiedDmitri Shuralyov
Caching the invocation of lookExtensions on an absolute path in Command and reusing the cached result in Start is only viable if Cmd.Path isn't set to a different value after Command returns. For #66586. Fixes #68314. Change-Id: I57007850aca2011b11344180c00faded737617b5 Reviewed-on: https://go-review.googlesource.com/c/go/+/596875 Reviewed-by: qiu laidongfeng2 <2645477756@qq.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2024-06-07os/exec: on Windows look for extensions in Run if not already doneqiulaidongfeng
CL 512155 fixed #36768, but introduced #62596. CL 527820 fixed #62596, but meant that the code failed to look up file extensions on Windows for a relative path. This CL fixes that problem by recording whether it has already looked up file extensions. This does mean that if Path is set manually then we do not update it with file extensions, as doing that would be racy. Fixes #66586 Change-Id: I9a0305d1e466c5e07bfbe442566ea12f5255a96e GitHub-Last-Rev: dc3169f2350f61acac5ef7842b7514013abacbe1 GitHub-Pull-Request: golang/go#67035 Reviewed-on: https://go-review.googlesource.com/c/go/+/581695 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-03-26all: fix a large number of commentscui fliter
Partial typo corrections, following https://go.dev/wiki/Spelling Change-Id: I2357906ff2ea04305c6357418e4e9556e20375d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/573776 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: shuang cui <imcusg@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@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>
2023-09-13os/exec: avoid calling LookPath in cmd.Start for resolved pathsBryan C. Mills
This reapplies CL 512155, which was previously reverted in CL 527337. The race that prompted the revert should be fixed by CL 527820, which will be submitted before this one. For #36768. Updates #62596. Change-Id: I3c3cd92470254072901b6ef91c0ac52c8071e0a2 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-race,gotip-windows-amd64-race,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/528038 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com>
2023-09-13os/exec: avoid writing to Cmd.Path in Cmd.Start on WindowsBryan C. Mills
Fixes #62596. Change-Id: I9003318ac1c4e3036f32383e62e9ba08c383d5c2 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-race,gotip-windows-amd64-race,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/527820 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-09-12Revert "os/exec: avoid calling LookPath in cmd.Start for resolved paths"Ian Lance Taylor
This reverts CL 512155. Reason for revert: CL 512155 introduced a race in that it caused cmd.Start to set cmd.Path. Previously it was fine if code looked at cmd.Path in one goroutine while calling cmd.Start in a different goroutine. A test case for this race is in CL 527495. Change-Id: Ic18fdadf6763727f8ea748280d5f0e601b9bf374 Reviewed-on: https://go-review.googlesource.com/c/go/+/527337 Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-08-01os/exec: Use the built-in function min instead of minIntapocelipes
The built-in function `min` has been implemented and can now be used to replace some manually written `minType` helper functions. Change-Id: Ie8ffc7881c8652ece752751214f1242bf76a6e7e GitHub-Last-Rev: 5db344f13142c78f437571e3a1cdc0b02c0589cb GitHub-Pull-Request: golang/go#60866 Reviewed-on: https://go-review.googlesource.com/c/go/+/504315 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: qiulaidongfeng <2645477756@qq.com>
2023-07-26os/exec: avoid calling LookPath in cmd.Start for resolved pathsqiulaidongfeng
Follow up on CL 511458, see https://go-review.googlesource.com/c/go/+/511458/2..4/src/cmd/go/main.go#b270 . For #36768. Change-Id: Icc2a4dbb1219b1d69dd10a900478957b0e975847 Change-Id: Icc2a4dbb1219b1d69dd10a900478957b0e975847 GitHub-Last-Rev: bac7e66496806d505270c5b90d53672d80a1ca29 GitHub-Pull-Request: golang/go#61517 Reviewed-on: https://go-review.googlesource.com/c/go/+/512155 Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
2023-04-18cmd/go: add check for unknown godebug settingRuss Cox
A //go:debug line mentioning an unknown or retired setting should be diagnosed as making the program invalid. Do that. We agreed on this in the proposal but I forgot to implement it. Change-Id: Ie69072a1682d4eeb6866c02adbbb426f608567c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/476280 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-14internal/godebug: define more efficient APIRuss Cox
We have been expanding our use of GODEBUG for compatibility, and the current implementation forces a tradeoff between freshness and efficiency. It parses the environment variable in full each time it is called, which is expensive. But if clients cache the result, they won't respond to run-time GODEBUG changes, as happened with x509sha1 (#56436). This CL changes the GODEBUG API to provide efficient, up-to-date results. Instead of a single Get function, New returns a *godebug.Setting that itself has a Get method. Clients can save the result of New, which is no more expensive than errors.New, in a global variable, and then call that variable's Get method to get the value. Get costs only two atomic loads in the case where the variable hasn't changed since the last call. Unfortunately, these changes do require importing sync from godebug, which will mean that sync itself will never be able to use a GODEBUG setting. That doesn't seem like such a hardship. If it was really necessary, the runtime could pass a setting to package sync itself at startup, with the caveat that that setting, like the ones used by runtime itself, would not respond to run-time GODEBUG changes. Change-Id: I99a3acfa24fb2a692610af26a5d14bbc62c966ac Reviewed-on: https://go-review.googlesource.com/c/go/+/449504 Run-TryBot: Russ Cox <rsc@golang.org> Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-04os/exec: allow NUL in environment variables on Plan 9Matthew Dempsky
Plan 9 uses NUL as os.PathListSeparator, so it's almost always going to appear in the environment variable list. Exempt GOOS=plan9 from the check for NUL in environment variables. For #56284. Fixes #56544. Change-Id: I23df233cdf20c0a9a606fd9253e15a9b5482575a Reviewed-on: https://go-review.googlesource.com/c/go/+/447715 Reviewed-by: David du Colombier <0intro@gmail.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2022-11-01syscall, os/exec: reject environment variables containing NULsDamien Neil
Check for and reject environment variables containing NULs. The conventions for passing environment variables to subprocesses cause most or all systems to interpret a NUL as a separator. The syscall package rejects environment variables containing a NUL on most systems, but erroniously did not do so on Windows. This causes an environment variable such as "FOO=a\x00BAR=b" to be interpreted as "FOO=a", "BAR=b". Check for and reject NULs in environment variables passed to syscall.StartProcess on Windows. Add a redundant check to os/exec as extra insurance. Fixes #56284 Fixes CVE-2022-41716 Change-Id: I2950e2b0cb14ebd26e5629be1521858f66a7d4ae Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1609434 Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Tatiana Bradley <tatianabradley@google.com> Reviewed-by: Roland Shoemaker <bracewell@google.com> TryBot-Result: Security TryBots <security-trybots@go-security-trybots.iam.gserviceaccount.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/446916 Reviewed-by: Tatiana Bradley <tatiana@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com>
2022-10-25os/exec: add the Cancel and WaitDelay fieldsBryan C. Mills
Fixes #50436. Change-Id: I9dff8caa317a04b7b2b605f810b8f12ef8ca485d Reviewed-on: https://go-review.googlesource.com/c/go/+/401835 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-10-11os/exec: remove protection against a duplicate Close on StdinPipeBryan C. Mills
As of CL 438347, multiple concurrents calls to Close should be safe. This removes some indirection and may also make some programs that use type-assertions marginally more efficient. For example, if a program calls (*exec.Cmd).StdinPipe to obtain a pipe and then sets that as the Stdout of another command, that program will now allow the second command to inherit the file descriptor directly instead of copying everything through a goroutine. This will also cause calls to Close after the first to return an error wrapping os.ErrClosed instead of nil. However, it seems unlikely that programs will depend on that error behavior: if a program is calling Write in a loop followed by Close, then if a racing Close occurs it is likely that the Write would have already reported an error. (The only programs likely to notice a change are those that call Close — without Write! — after a call to Wait.) Updates #56043. Updates #9307. Updates #6270. Change-Id: Iec734b23acefcc7e7ad0c8bc720085bc45988efb Reviewed-on: https://go-review.googlesource.com/c/go/+/439195 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-07os/exec: document ProcessState available after a call to Wait or Runhopehook
Wait or Run will populate its ProcessState when the command completes. Fixes #56002. Change-Id: I21547431f5d2d3e0fc0734fd1705421a0ac4209c Reviewed-on: https://go-review.googlesource.com/c/go/+/437996 Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-10-04os/exec: add a GODEBUG setting to diagnose leaked processesBryan C. Mills
Updates #52580. For #50436. Change-Id: I669f13863f1f85d576c3c94500b118e6989000eb Reviewed-on: https://go-review.googlesource.com/c/go/+/436655 Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
2022-10-04Revert "os/exec: make StdoutPipe and StderrPipe safe to Close concurrently"Bryan Mills
This reverts CL 437176. Reason for revert: broke programs that plumb StdoutPipe from one command to Stdin on another and then call Wait on the former. os/exec itself uses a type-assertion to *os.File to determine whether to copy stdin using a goroutine or just pass a file descriptor. An early Wait using a *os.File is benign (because closing the pipe doesn't close the child's inherited file descriptor), but an early Wait using a non-*os.File is not. Updates #50436. Change-Id: I4a2993e290982834f91696d890dfe77364c0cc50 Reviewed-on: https://go-review.googlesource.com/c/go/+/438695 Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-10-01os/exec: make StdoutPipe and StderrPipe safe to Close concurrentlyBryan C. Mills
For #50436, I want to be able to close the pipes returned by StdoutPipe and StderrPipe after the Context has been canceled and the WaitDelay has subsequently expired. However, the fact that the exec.onceCloser wrapper for StdinPipe (added in CL 13329043) was retained in CL 65490 suggests to me that (*os.File).Close is still not safe to call concurrently. This may cause type assertions of these ReadClosers to *os.File that once succeeded to no longer do so. However, the StdoutPipe and StderrPipe methods return interfaces, not concrete *os.Files, so callers already should not have been relying on that implementation detail — and as far as I can tell the closeOnce wrapper does not mask any (*os.File) methods, so assertions to any interface type that previously succeeded will continue to do so. This change is logically part of CL 401835, but since it may expose fragile type-assertions in callers I want to keep it separate for clearer bisection of any new test failures. For #50436. Change-Id: I58de1d48fb6fd788502f13657d8d4484516271cf Reviewed-on: https://go-review.googlesource.com/c/go/+/437176 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-01os/exec: recombine goroutinePipes and userPipesBryan C. Mills
This change undoes CL 401894, because on further consideration it turns out not to be needed. This also makes (*Cmd).closeDescriptors a free function, since it does not actually use the receiver in any way and is not needed to satisfy any interfaces. For #50436. Change-Id: I7915265b0e6398ed5a34fae0c12873ab08a14194 Reviewed-on: https://go-review.googlesource.com/c/go/+/437175 Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
2022-09-29os/exec: refactor goroutine error reportingBryan C. Mills
Use a separate channel to report the final error of the copying goroutines, receiving a value only when all of the copying goroutines have completed. In a followup change (CL 401835), that will allow waiters to select on goroutine completion alongside other events (such as Context cancellation). Also mildly refactor the watchCtx helper method so that its structure better matches what will be needed to implement WaitDelay. For #50436. Change-Id: I54b3997fb6931d204814d8382f0a388a67b520f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/435995 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
2022-09-29os/exec: do not close pipes on a double-Start errorBryan C. Mills
This fixes a bug introduced in CL 401834 in which calling Start twice with pipes attached to a command would spuriously close those pipes when returning the error from the second Start call. For #50436. Change-Id: I3563cc99c0a0987752190fef95da3e9927a76fda Reviewed-on: https://go-review.googlesource.com/c/go/+/436095 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com>
2022-09-26os/exec: split parent I/O pipes by whether they are pumped by user code or ↵Bryan C. Mills
internal goroutines The pipes pumped by goroutines can be closed as soon as their respective goroutines are done. The pipes pumped by user code, however, are documented to be closed in Wait. When we add the WaitDelay field, it isn't obvious that we should terminate the user-pumped pipes when the WaitDelay expires, since Wait itself isn't going to wait for those user-controlled goroutines to complete. (It's a bit more complicated than that because the documentation currently states that Wait must not be called while the pipes are being read — but it isn't obvious to me that that advice is entirely correct.) For #50436. Change-Id: I97909c91d2097fb75138a360747168c08609696d Reviewed-on: https://go-review.googlesource.com/c/go/+/401894 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
2022-09-26os/exec: clean up pipe-closing logicBryan C. Mills
Change the childFiles field to a local variable, since it was populated during Start and (as far as I can determine) has no purpose after Start returns. Rename closeAfterStart and closeAfterWait to childIOFiles and parentIOPipes respectively. That makes their contents clearer, and also helps to clarify what should happen on error (when, for example, Wait shouldn't be called at all). Use a deferred call instead of individual calls to close child (and, if necessary, pipe) FDs after Start. That helps to clarify the invariants around when they are closed, and also makes the function a bit more robust for future refactoring. Also nil out the slices containing the file closers so that they can be collected earlier. This CL is intended as a pure refactor in preparation for #50436. Change-Id: I05d13fa91d539b95b84b2ba923c1733f9a6203e5 Reviewed-on: https://go-review.googlesource.com/c/go/+/401834 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
2022-07-28os/exec: add GODEBUG setting to opt out of ErrDot changesRuss Cox
The changes are likely to break users, and we need to make it easy to unbreak without code changes. For #43724. Fixes #53962. Change-Id: I105c5d6c801d354467e0cefd268189c18846858e Reviewed-on: https://go-review.googlesource.com/c/go/+/419794 Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-07-06os/exec: clarify that Wait must be calledIan Lance Taylor
Fixes #52580 Change-Id: Ib2dd8a793b9c6fcb083abb3f7c346f6279adefc9 Reviewed-on: https://go-review.googlesource.com/c/go/+/414056 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2022-05-10os/exec: return clear error for missing cmd.PathRuss Cox
Following up on CL 403694, there is a bit of confusion about when Path is and isn't set, along with now the exported Err field. Catch the case where Path and Err (and lookPathErr) are all unset and give a helpful error. Fixes #52574 Followup after #43724. Change-Id: I03205172aef3801c3194f5098bdb93290c02b1b6 Reviewed-on: https://go-review.googlesource.com/c/go/+/403759 Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org>
2022-05-06os/exec: refactor goroutine communication in WaitBryan C. Mills
This provides clearer synchronization invariants: if it occurs at all, the call to c.Process.Kill always occurs before Wait returns. It also allows any unexpected errors from the goroutine to be propagated back to Wait. For #50436. Change-Id: I7ddadc73e6e67399596e35393f5845646f6111ab Reviewed-on: https://go-review.googlesource.com/c/go/+/401896 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-05-03os/exec: in Command, update cmd.Path even if LookPath returns an errorBryan C. Mills
Fixes #52666. Updates #43724. Updates #43947. Change-Id: I72cb585036b7e93cd7adbff318b400586ea97bd5 Reviewed-on: https://go-review.googlesource.com/c/go/+/403694 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
2022-05-02os/exec: return error when PATH lookup would use current directoryRuss Cox
CL 381374 was reverted because x/sys/execabs broke. This CL reapplies CL 381374, but adding a lookPathErr error field back, for execabs to manipulate with reflect. That field will just be a bit of scar tissue in this package forever, to keep old code working with new toolchains. CL 403256 fixes x/sys/execabs's test to be ready for the change. Older versions of x/sys/execabs will keep working (that is, will keep rejecting what they should reject), but they will return a slightly different error from LookPath without that CL, and the test fails because of the different error text. For #43724. This reverts commit f2b674756b3b684118e4245627d4ed8c07e518e7. Change-Id: Iee55f8cd9939e1bd31e5cbdada50681cdc505117 Reviewed-on: https://go-review.googlesource.com/c/go/+/403274 Auto-Submit: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-04-29Revert "os/exec: return error when PATH lookup would use current directory"Bryan Mills
This reverts CL 381374. Reason for revert: broke tests for x/sys/execabs. Updates #43724. Updates #43947. Change-Id: I9eb3adb5728dead66dbd20f6afe1e7a77e2a26f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/403058 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
2022-04-29os/exec: return error when PATH lookup would use current directoryRuss Cox
Following discussion on #43724, change os/exec to take the approach of golang.org/x/sys/execabs, refusing to respect path entries mentioning relative paths by default. Code that insists on being able to find executables in relative directories in the path will need to add a couple lines to override the error. See the updated package docs in exec.go for more details. Fixes #43724. Fixes #43947. Change-Id: I73c1214f322b60b4167a23e956e933d50470fe13 Reviewed-on: https://go-review.googlesource.com/c/go/+/381374 Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com>
2022-04-26os/exec: make skipStdinCopyError a function instead of a variableBryan C. Mills
This makes clearer that skipStdinCopyError is always defined and never overridden in tests. Secondarily, it may also help reduce init-time work and allow the linker and/or inliner to better optimize this package. (Noticed while prototyping #50436.) Change-Id: I4f3c1bc146384a98136a4039f82165ed106c14b8 Reviewed-on: https://go-review.googlesource.com/c/go/+/401897 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-04-21os/exec: set PWD implicitly if Dir is non-empty and Env is nilBryan C. Mills
Fixes #50599. Change-Id: I4e5dbb3972cdf21ede049567bfb98f2c992c5849 Reviewed-on: https://go-review.googlesource.com/c/go/+/401340 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-04-21os/exec: preserve original order of entries in dedupEnvBryan C. Mills
Once #50599 is implemented, the entries will be observable via the Environ method. I find it confusing for later entries in the list to jump arbitrarily far forward based on entries for the same key that no longer exist. This also fixes the deduplication logic for the degenerate Windows keys observed in #49886, which were previously deduplicated as empty keys. (It does not do anything about the even-more-degenerate keys observed in #52436.) For #50599. Change-Id: Ia7cd2200ec34ccc4b9d18631cb513194dc420c25 Reviewed-on: https://go-review.googlesource.com/c/go/+/401339 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@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-06all: use bytes.Cut, strings.CutRuss Cox
Many uses of Index/IndexByte/IndexRune/Split/SplitN can be written more clearly using the new Cut functions. Do that. Also rewrite to other functions if that's clearer. For #46336. Change-Id: I68d024716ace41a57a8bf74455c62279bde0f448 Reviewed-on: https://go-review.googlesource.com/c/go/+/351711 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-25os/exec: use environment variables for user token when presentLiam 'Auzzie' Haworth
Builds upon the changes from #32000 which supported sourcing environment variables for a new process from the environment of a Windows user token when supplied. But due to the logic of os/exec, the Env field of a process was always non-nil when it reached that change. This change moves the logic up to os/exec, specifically when os.ProcAttr is being built for the os.StartProcess call, this ensures that if a user token has been supplied and no Env slice has been provided on the command it will be sourced from the user's environment. If no token is provided, or the program is compiled for any other platform than Windows, the default environment will be sourced from syscall.Environ(). Fixes #35314 Change-Id: I4c1722e90b91945eb6980d5c5928183269b50487 GitHub-Last-Rev: 32216b7291418f9285147a93ed6d0ba028f94ef2 GitHub-Pull-Request: golang/go#37402 Reviewed-on: https://go-review.googlesource.com/c/go/+/220587 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-12-15os/exec: ignore hungup error while copying stdin on Plan 9Fazlul Shahriar
Fixes #35753 Change-Id: I38674c59c601785eb25b778dc25efdb92231dd9b Reviewed-on: https://go-review.googlesource.com/c/go/+/208223 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>