github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/os/exec/exec.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package exec runs external commands. It wraps os.StartProcess to make it 6 // easier to remap stdin and stdout, connect I/O with pipes, and do other 7 // adjustments. 8 // 9 // Unlike the "system" library call from C and other languages, the 10 // os/exec package intentionally does not invoke the system shell and 11 // does not expand any glob patterns or handle other expansions, 12 // pipelines, or redirections typically done by shells. The package 13 // behaves more like C's "exec" family of functions. To expand glob 14 // patterns, either call the shell directly, taking care to escape any 15 // dangerous input, or use the path/filepath package's Glob function. 16 // To expand environment variables, use package os's ExpandEnv. 17 // 18 // Note that the examples in this package assume a Unix system. 19 // They may not run on Windows, and they do not run in the Go Playground 20 // used by golang.org and godoc.org. 21 // 22 // # Executables in the current directory 23 // 24 // The functions Command and LookPath look for a program 25 // in the directories listed in the current path, following the 26 // conventions of the host operating system. 27 // Operating systems have for decades included the current 28 // directory in this search, sometimes implicitly and sometimes 29 // configured explicitly that way by default. 30 // Modern practice is that including the current directory 31 // is usually unexpected and often leads to security problems. 32 // 33 // To avoid those security problems, as of Go 1.19, this package will not resolve a program 34 // using an implicit or explicit path entry relative to the current directory. 35 // That is, if you run exec.LookPath("go"), it will not successfully return 36 // ./go on Unix nor .\go.exe on Windows, no matter how the path is configured. 37 // Instead, if the usual path algorithms would result in that answer, 38 // these functions return an error err satisfying errors.Is(err, ErrDot). 39 // 40 // For example, consider these two program snippets: 41 // 42 // path, err := exec.LookPath("prog") 43 // if err != nil { 44 // log.Fatal(err) 45 // } 46 // use(path) 47 // 48 // and 49 // 50 // cmd := exec.Command("prog") 51 // if err := cmd.Run(); err != nil { 52 // log.Fatal(err) 53 // } 54 // 55 // These will not find and run ./prog or .\prog.exe, 56 // no matter how the current path is configured. 57 // 58 // Code that always wants to run a program from the current directory 59 // can be rewritten to say "./prog" instead of "prog". 60 // 61 // Code that insists on including results from relative path entries 62 // can instead override the error using an errors.Is check: 63 // 64 // path, err := exec.LookPath("prog") 65 // if errors.Is(err, exec.ErrDot) { 66 // err = nil 67 // } 68 // if err != nil { 69 // log.Fatal(err) 70 // } 71 // use(path) 72 // 73 // and 74 // 75 // cmd := exec.Command("prog") 76 // if errors.Is(cmd.Err, exec.ErrDot) { 77 // cmd.Err = nil 78 // } 79 // if err := cmd.Run(); err != nil { 80 // log.Fatal(err) 81 // } 82 // 83 // Setting the environment variable GODEBUG=execerrdot=0 84 // disables generation of ErrDot entirely, temporarily restoring the pre-Go 1.19 85 // behavior for programs that are unable to apply more targeted fixes. 86 // A future version of Go may remove support for this variable. 87 // 88 // Before adding such overrides, make sure you understand the 89 // security implications of doing so. 90 // See https://go.dev/blog/path-security for more information. 91 package exec 92 93 import ( 94 "bytes" 95 "context" 96 "errors" 97 "internal/godebug" 98 "internal/syscall/execenv" 99 "io" 100 "os" 101 "path/filepath" 102 "runtime" 103 "strconv" 104 "strings" 105 "syscall" 106 ) 107 108 // Error is returned by LookPath when it fails to classify a file as an 109 // executable. 110 type Error struct { 111 // Name is the file name for which the error occurred. 112 Name string 113 // Err is the underlying error. 114 Err error 115 } 116 117 func (e *Error) Error() string { 118 return "exec: " + strconv.Quote(e.Name) + ": " + e.Err.Error() 119 } 120 121 func (e *Error) Unwrap() error { return e.Err } 122 123 // wrappedError wraps an error without relying on fmt.Errorf. 124 type wrappedError struct { 125 prefix string 126 err error 127 } 128 129 func (w wrappedError) Error() string { 130 return w.prefix + ": " + w.err.Error() 131 } 132 133 func (w wrappedError) Unwrap() error { 134 return w.err 135 } 136 137 // Cmd represents an external command being prepared or run. 138 // 139 // A Cmd cannot be reused after calling its Run, Output or CombinedOutput 140 // methods. 141 type Cmd struct { 142 // Path is the path of the command to run. 143 // 144 // This is the only field that must be set to a non-zero 145 // value. If Path is relative, it is evaluated relative 146 // to Dir. 147 Path string 148 149 // Args holds command line arguments, including the command as Args[0]. 150 // If the Args field is empty or nil, Run uses {Path}. 151 // 152 // In typical use, both Path and Args are set by calling Command. 153 Args []string 154 155 // Env specifies the environment of the process. 156 // Each entry is of the form "key=value". 157 // If Env is nil, the new process uses the current process's 158 // environment. 159 // If Env contains duplicate environment keys, only the last 160 // value in the slice for each duplicate key is used. 161 // As a special case on Windows, SYSTEMROOT is always added if 162 // missing and not explicitly set to the empty string. 163 Env []string 164 165 // Dir specifies the working directory of the command. 166 // If Dir is the empty string, Run runs the command in the 167 // calling process's current directory. 168 Dir string 169 170 // Stdin specifies the process's standard input. 171 // 172 // If Stdin is nil, the process reads from the null device (os.DevNull). 173 // 174 // If Stdin is an *os.File, the process's standard input is connected 175 // directly to that file. 176 // 177 // Otherwise, during the execution of the command a separate 178 // goroutine reads from Stdin and delivers that data to the command 179 // over a pipe. In this case, Wait does not complete until the goroutine 180 // stops copying, either because it has reached the end of Stdin 181 // (EOF or a read error) or because writing to the pipe returned an error. 182 Stdin io.Reader 183 184 // Stdout and Stderr specify the process's standard output and error. 185 // 186 // If either is nil, Run connects the corresponding file descriptor 187 // to the null device (os.DevNull). 188 // 189 // If either is an *os.File, the corresponding output from the process 190 // is connected directly to that file. 191 // 192 // Otherwise, during the execution of the command a separate goroutine 193 // reads from the process over a pipe and delivers that data to the 194 // corresponding Writer. In this case, Wait does not complete until the 195 // goroutine reaches EOF or encounters an error. 196 // 197 // If Stdout and Stderr are the same writer, and have a type that can 198 // be compared with ==, at most one goroutine at a time will call Write. 199 Stdout io.Writer 200 Stderr io.Writer 201 202 // ExtraFiles specifies additional open files to be inherited by the 203 // new process. It does not include standard input, standard output, or 204 // standard error. If non-nil, entry i becomes file descriptor 3+i. 205 // 206 // ExtraFiles is not supported on Windows. 207 ExtraFiles []*os.File 208 209 // SysProcAttr holds optional, operating system-specific attributes. 210 // Run passes it to os.StartProcess as the os.ProcAttr's Sys field. 211 SysProcAttr *syscall.SysProcAttr 212 213 // Process is the underlying process, once started. 214 Process *os.Process 215 216 // ProcessState contains information about an exited process. 217 // If the process was started successfully, Wait or Run will 218 // populate its ProcessState when the command completes. 219 ProcessState *os.ProcessState 220 221 ctx context.Context // nil means none 222 Err error // LookPath error, if any. 223 224 // childIOFiles holds closers for any of the child process's 225 // stdin, stdout, and/or stderr files that were opened by the Cmd itself 226 // (not supplied by the caller). These should be closed as soon as they 227 // are inherited by the child process. 228 childIOFiles []io.Closer 229 230 // parentIOPipes holds closers for the parent's end of any pipes 231 // connected to the child's stdin, stdout, and/or stderr streams 232 // that were opened by the Cmd itself (not supplied by the caller). 233 // These should be closed after Wait sees the command exit. 234 parentIOPipes []io.Closer 235 236 // goroutine holds a set of closures to execute to copy data 237 // to and/or from the command's I/O pipes. 238 goroutine []func() error 239 240 // If goroutineErr is non-nil, it receives the first error from a copying 241 // goroutine once all such goroutines have completed. 242 // goroutineErr is set to nil once its error has been received. 243 goroutineErr <-chan error 244 245 ctxErr <-chan error // if non nil, receives the error from watchCtx exactly once 246 247 // The stack saved when the Command was created, if GODEBUG contains 248 // execwait=2. Used for debugging leaks. 249 createdByStack []byte 250 251 // For a security release long ago, we created x/sys/execabs, 252 // which manipulated the unexported lookPathErr error field 253 // in this struct. For Go 1.19 we exported the field as Err error, 254 // above, but we have to keep lookPathErr around for use by 255 // old programs building against new toolchains. 256 // The String and Start methods look for an error in lookPathErr 257 // in preference to Err, to preserve the errors that execabs sets. 258 // 259 // In general we don't guarantee misuse of reflect like this, 260 // but the misuse of reflect was by us, the best of various bad 261 // options to fix the security problem, and people depend on 262 // those old copies of execabs continuing to work. 263 // The result is that we have to leave this variable around for the 264 // rest of time, a compatibility scar. 265 // 266 // See https://go.dev/blog/path-security 267 // and https://go.dev/issue/43724 for more context. 268 lookPathErr error 269 } 270 271 // Command returns the Cmd struct to execute the named program with 272 // the given arguments. 273 // 274 // It sets only the Path and Args in the returned structure. 275 // 276 // If name contains no path separators, Command uses LookPath to 277 // resolve name to a complete path if possible. Otherwise it uses name 278 // directly as Path. 279 // 280 // The returned Cmd's Args field is constructed from the command name 281 // followed by the elements of arg, so arg should not include the 282 // command name itself. For example, Command("echo", "hello"). 283 // Args[0] is always name, not the possibly resolved Path. 284 // 285 // On Windows, processes receive the whole command line as a single string 286 // and do their own parsing. Command combines and quotes Args into a command 287 // line string with an algorithm compatible with applications using 288 // CommandLineToArgvW (which is the most common way). Notable exceptions are 289 // msiexec.exe and cmd.exe (and thus, all batch files), which have a different 290 // unquoting algorithm. In these or other similar cases, you can do the 291 // quoting yourself and provide the full command line in SysProcAttr.CmdLine, 292 // leaving Args empty. 293 func Command(name string, arg ...string) *Cmd { 294 cmd := &Cmd{ 295 Path: name, 296 Args: append([]string{name}, arg...), 297 } 298 299 if execwait := godebug.Get("execwait"); execwait != "" { 300 if execwait == "2" { 301 // Obtain the caller stack. (This is equivalent to runtime/debug.Stack, 302 // copied to avoid importing the whole package.) 303 stack := make([]byte, 1024) 304 for { 305 n := runtime.Stack(stack, false) 306 if n < len(stack) { 307 stack = stack[:n] 308 break 309 } 310 stack = make([]byte, 2*len(stack)) 311 } 312 313 if i := bytes.Index(stack, []byte("\nos/exec.Command(")); i >= 0 { 314 stack = stack[i+1:] 315 } 316 cmd.createdByStack = stack 317 } 318 319 runtime.SetFinalizer(cmd, func(c *Cmd) { 320 if c.Process != nil && c.ProcessState == nil { 321 debugHint := "" 322 if c.createdByStack == nil { 323 debugHint = " (set GODEBUG=execwait=2 to capture stacks for debugging)" 324 } else { 325 os.Stderr.WriteString("GODEBUG=execwait=2 detected a leaked exec.Cmd created by:\n") 326 os.Stderr.Write(c.createdByStack) 327 os.Stderr.WriteString("\n") 328 debugHint = "" 329 } 330 panic("exec: Cmd started a Process but leaked without a call to Wait" + debugHint) 331 } 332 }) 333 } 334 335 if filepath.Base(name) == name { 336 lp, err := LookPath(name) 337 if lp != "" { 338 // Update cmd.Path even if err is non-nil. 339 // If err is ErrDot (especially on Windows), lp may include a resolved 340 // extension (like .exe or .bat) that should be preserved. 341 cmd.Path = lp 342 } 343 if err != nil { 344 cmd.Err = err 345 } 346 } 347 return cmd 348 } 349 350 // CommandContext is like Command but includes a context. 351 // 352 // The provided context is used to kill the process (by calling 353 // os.Process.Kill) if the context becomes done before the command 354 // completes on its own. 355 func CommandContext(ctx context.Context, name string, arg ...string) *Cmd { 356 if ctx == nil { 357 panic("nil Context") 358 } 359 cmd := Command(name, arg...) 360 cmd.ctx = ctx 361 return cmd 362 } 363 364 // String returns a human-readable description of c. 365 // It is intended only for debugging. 366 // In particular, it is not suitable for use as input to a shell. 367 // The output of String may vary across Go releases. 368 func (c *Cmd) String() string { 369 if c.Err != nil || c.lookPathErr != nil { 370 // failed to resolve path; report the original requested path (plus args) 371 return strings.Join(c.Args, " ") 372 } 373 // report the exact executable path (plus args) 374 b := new(strings.Builder) 375 b.WriteString(c.Path) 376 for _, a := range c.Args[1:] { 377 b.WriteByte(' ') 378 b.WriteString(a) 379 } 380 return b.String() 381 } 382 383 // interfaceEqual protects against panics from doing equality tests on 384 // two interfaces with non-comparable underlying types. 385 func interfaceEqual(a, b any) bool { 386 defer func() { 387 recover() 388 }() 389 return a == b 390 } 391 392 func (c *Cmd) argv() []string { 393 if len(c.Args) > 0 { 394 return c.Args 395 } 396 return []string{c.Path} 397 } 398 399 func (c *Cmd) childStdin() (*os.File, error) { 400 if c.Stdin == nil { 401 f, err := os.Open(os.DevNull) 402 if err != nil { 403 return nil, err 404 } 405 c.childIOFiles = append(c.childIOFiles, f) 406 return f, nil 407 } 408 409 if f, ok := c.Stdin.(*os.File); ok { 410 return f, nil 411 } 412 413 pr, pw, err := os.Pipe() 414 if err != nil { 415 return nil, err 416 } 417 418 c.childIOFiles = append(c.childIOFiles, pr) 419 c.parentIOPipes = append(c.parentIOPipes, pw) 420 c.goroutine = append(c.goroutine, func() error { 421 _, err := io.Copy(pw, c.Stdin) 422 if skipStdinCopyError(err) { 423 err = nil 424 } 425 if err1 := pw.Close(); err == nil { 426 err = err1 427 } 428 return err 429 }) 430 return pr, nil 431 } 432 433 func (c *Cmd) childStdout() (*os.File, error) { 434 return c.writerDescriptor(c.Stdout) 435 } 436 437 func (c *Cmd) childStderr(childStdout *os.File) (*os.File, error) { 438 if c.Stderr != nil && interfaceEqual(c.Stderr, c.Stdout) { 439 return childStdout, nil 440 } 441 return c.writerDescriptor(c.Stderr) 442 } 443 444 // writerDescriptor returns an os.File to which the child process 445 // can write to send data to w. 446 // 447 // If w is nil, writerDescriptor returns a File that writes to os.DevNull. 448 func (c *Cmd) writerDescriptor(w io.Writer) (*os.File, error) { 449 if w == nil { 450 f, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) 451 if err != nil { 452 return nil, err 453 } 454 c.childIOFiles = append(c.childIOFiles, f) 455 return f, nil 456 } 457 458 if f, ok := w.(*os.File); ok { 459 return f, nil 460 } 461 462 pr, pw, err := os.Pipe() 463 if err != nil { 464 return nil, err 465 } 466 467 c.childIOFiles = append(c.childIOFiles, pw) 468 c.parentIOPipes = append(c.parentIOPipes, pr) 469 c.goroutine = append(c.goroutine, func() error { 470 _, err := io.Copy(w, pr) 471 pr.Close() // in case io.Copy stopped due to write error 472 return err 473 }) 474 return pw, nil 475 } 476 477 func closeDescriptors(closers []io.Closer) { 478 for _, fd := range closers { 479 fd.Close() 480 } 481 } 482 483 // Run starts the specified command and waits for it to complete. 484 // 485 // The returned error is nil if the command runs, has no problems 486 // copying stdin, stdout, and stderr, and exits with a zero exit 487 // status. 488 // 489 // If the command starts but does not complete successfully, the error is of 490 // type *ExitError. Other error types may be returned for other situations. 491 // 492 // If the calling goroutine has locked the operating system thread 493 // with runtime.LockOSThread and modified any inheritable OS-level 494 // thread state (for example, Linux or Plan 9 name spaces), the new 495 // process will inherit the caller's thread state. 496 func (c *Cmd) Run() error { 497 if err := c.Start(); err != nil { 498 return err 499 } 500 return c.Wait() 501 } 502 503 // lookExtensions finds windows executable by its dir and path. 504 // It uses LookPath to try appropriate extensions. 505 // lookExtensions does not search PATH, instead it converts `prog` into `.\prog`. 506 func lookExtensions(path, dir string) (string, error) { 507 if filepath.Base(path) == path { 508 path = "." + string(filepath.Separator) + path 509 } 510 if dir == "" { 511 return LookPath(path) 512 } 513 if filepath.VolumeName(path) != "" { 514 return LookPath(path) 515 } 516 if len(path) > 1 && os.IsPathSeparator(path[0]) { 517 return LookPath(path) 518 } 519 dirandpath := filepath.Join(dir, path) 520 // We assume that LookPath will only add file extension. 521 lp, err := LookPath(dirandpath) 522 if err != nil { 523 return "", err 524 } 525 ext := strings.TrimPrefix(lp, dirandpath) 526 return path + ext, nil 527 } 528 529 // Start starts the specified command but does not wait for it to complete. 530 // 531 // If Start returns successfully, the c.Process field will be set. 532 // 533 // After a successful call to Start the Wait method must be called in 534 // order to release associated system resources. 535 func (c *Cmd) Start() error { 536 // Check for doubled Start calls before we defer failure cleanup. If the prior 537 // call to Start succeeded, we don't want to spuriously close its pipes. 538 if c.Process != nil { 539 return errors.New("exec: already started") 540 } 541 542 started := false 543 defer func() { 544 closeDescriptors(c.childIOFiles) 545 c.childIOFiles = nil 546 547 if !started { 548 closeDescriptors(c.parentIOPipes) 549 c.parentIOPipes = nil 550 } 551 }() 552 553 if c.Path == "" && c.Err == nil && c.lookPathErr == nil { 554 c.Err = errors.New("exec: no command") 555 } 556 if c.Err != nil || c.lookPathErr != nil { 557 if c.lookPathErr != nil { 558 return c.lookPathErr 559 } 560 return c.Err 561 } 562 if runtime.GOOS == "windows" { 563 lp, err := lookExtensions(c.Path, c.Dir) 564 if err != nil { 565 return err 566 } 567 c.Path = lp 568 } 569 if c.ctx != nil { 570 select { 571 case <-c.ctx.Done(): 572 return c.ctx.Err() 573 default: 574 } 575 } 576 577 childFiles := make([]*os.File, 0, 3+len(c.ExtraFiles)) 578 stdin, err := c.childStdin() 579 if err != nil { 580 return err 581 } 582 childFiles = append(childFiles, stdin) 583 stdout, err := c.childStdout() 584 if err != nil { 585 return err 586 } 587 childFiles = append(childFiles, stdout) 588 stderr, err := c.childStderr(stdout) 589 if err != nil { 590 return err 591 } 592 childFiles = append(childFiles, stderr) 593 childFiles = append(childFiles, c.ExtraFiles...) 594 595 env, err := c.environ() 596 if err != nil { 597 return err 598 } 599 600 c.Process, err = os.StartProcess(c.Path, c.argv(), &os.ProcAttr{ 601 Dir: c.Dir, 602 Files: childFiles, 603 Env: env, 604 Sys: c.SysProcAttr, 605 }) 606 if err != nil { 607 return err 608 } 609 started = true 610 611 // Don't allocate the goroutineErr channel unless there are goroutines to start. 612 if len(c.goroutine) > 0 { 613 goroutineErr := make(chan error, 1) 614 c.goroutineErr = goroutineErr 615 616 type goroutineStatus struct { 617 running int 618 firstErr error 619 } 620 statusc := make(chan goroutineStatus, 1) 621 statusc <- goroutineStatus{running: len(c.goroutine)} 622 for _, fn := range c.goroutine { 623 go func(fn func() error) { 624 err := fn() 625 626 status := <-statusc 627 if status.firstErr == nil { 628 status.firstErr = err 629 } 630 status.running-- 631 if status.running == 0 { 632 goroutineErr <- status.firstErr 633 } else { 634 statusc <- status 635 } 636 }(fn) 637 } 638 c.goroutine = nil // Allow the goroutines' closures to be GC'd when they complete. 639 } 640 641 if c.ctx != nil && c.ctx.Done() != nil { 642 errc := make(chan error) 643 c.ctxErr = errc 644 go c.watchCtx(errc) 645 } 646 647 return nil 648 } 649 650 // watchCtx watches c.ctx until it is able to send a result to errc. 651 // 652 // If c.ctx is done before a result can be sent, watchCtx terminates c.Process. 653 func (c *Cmd) watchCtx(errc chan<- error) { 654 select { 655 case errc <- nil: 656 return 657 case <-c.ctx.Done(): 658 } 659 660 var err error 661 if killErr := c.Process.Kill(); killErr == nil { 662 // We appear to have killed the process. c.Process.Wait should return a 663 // non-nil error to c.Wait unless the Kill signal races with a successful 664 // exit, and if that does happen we shouldn't report a spurious error, 665 // so don't set err to anything here. 666 } else if !errors.Is(killErr, os.ErrProcessDone) { 667 err = wrappedError{ 668 prefix: "exec: error sending signal to Cmd", 669 err: killErr, 670 } 671 } 672 errc <- err 673 } 674 675 // An ExitError reports an unsuccessful exit by a command. 676 type ExitError struct { 677 *os.ProcessState 678 679 // Stderr holds a subset of the standard error output from the 680 // Cmd.Output method if standard error was not otherwise being 681 // collected. 682 // 683 // If the error output is long, Stderr may contain only a prefix 684 // and suffix of the output, with the middle replaced with 685 // text about the number of omitted bytes. 686 // 687 // Stderr is provided for debugging, for inclusion in error messages. 688 // Users with other needs should redirect Cmd.Stderr as needed. 689 Stderr []byte 690 } 691 692 func (e *ExitError) Error() string { 693 return e.ProcessState.String() 694 } 695 696 // Wait waits for the command to exit and waits for any copying to 697 // stdin or copying from stdout or stderr to complete. 698 // 699 // The command must have been started by Start. 700 // 701 // The returned error is nil if the command runs, has no problems 702 // copying stdin, stdout, and stderr, and exits with a zero exit 703 // status. 704 // 705 // If the command fails to run or doesn't complete successfully, the 706 // error is of type *ExitError. Other error types may be 707 // returned for I/O problems. 708 // 709 // If any of c.Stdin, c.Stdout or c.Stderr are not an *os.File, Wait also waits 710 // for the respective I/O loop copying to or from the process to complete. 711 // 712 // Wait releases any resources associated with the Cmd. 713 func (c *Cmd) Wait() error { 714 if c.Process == nil { 715 return errors.New("exec: not started") 716 } 717 if c.ProcessState != nil { 718 return errors.New("exec: Wait was already called") 719 } 720 721 state, err := c.Process.Wait() 722 if err == nil && !state.Success() { 723 err = &ExitError{ProcessState: state} 724 } 725 c.ProcessState = state 726 727 if c.ctxErr != nil { 728 interruptErr := <-c.ctxErr 729 // If c.Process.Wait returned an error, prefer that. 730 // Otherwise, report any error from the interrupt goroutine. 731 if err == nil { 732 err = interruptErr 733 } 734 } 735 736 // Wait for the pipe-copying goroutines to complete. 737 if c.goroutineErr != nil { 738 // Report an error from the copying goroutines only if the program otherwise 739 // exited normally on its own. Otherwise, the copying error may be due to the 740 // abnormal termination. 741 copyErr := <-c.goroutineErr 742 if err == nil { 743 err = copyErr 744 } 745 } 746 closeDescriptors(c.parentIOPipes) 747 c.parentIOPipes = nil 748 749 return err 750 } 751 752 // Output runs the command and returns its standard output. 753 // Any returned error will usually be of type *ExitError. 754 // If c.Stderr was nil, Output populates ExitError.Stderr. 755 func (c *Cmd) Output() ([]byte, error) { 756 if c.Stdout != nil { 757 return nil, errors.New("exec: Stdout already set") 758 } 759 var stdout bytes.Buffer 760 c.Stdout = &stdout 761 762 captureErr := c.Stderr == nil 763 if captureErr { 764 c.Stderr = &prefixSuffixSaver{N: 32 << 10} 765 } 766 767 err := c.Run() 768 if err != nil && captureErr { 769 if ee, ok := err.(*ExitError); ok { 770 ee.Stderr = c.Stderr.(*prefixSuffixSaver).Bytes() 771 } 772 } 773 return stdout.Bytes(), err 774 } 775 776 // CombinedOutput runs the command and returns its combined standard 777 // output and standard error. 778 func (c *Cmd) CombinedOutput() ([]byte, error) { 779 if c.Stdout != nil { 780 return nil, errors.New("exec: Stdout already set") 781 } 782 if c.Stderr != nil { 783 return nil, errors.New("exec: Stderr already set") 784 } 785 var b bytes.Buffer 786 c.Stdout = &b 787 c.Stderr = &b 788 err := c.Run() 789 return b.Bytes(), err 790 } 791 792 // StdinPipe returns a pipe that will be connected to the command's 793 // standard input when the command starts. 794 // The pipe will be closed automatically after Wait sees the command exit. 795 // A caller need only call Close to force the pipe to close sooner. 796 // For example, if the command being run will not exit until standard input 797 // is closed, the caller must close the pipe. 798 func (c *Cmd) StdinPipe() (io.WriteCloser, error) { 799 if c.Stdin != nil { 800 return nil, errors.New("exec: Stdin already set") 801 } 802 if c.Process != nil { 803 return nil, errors.New("exec: StdinPipe after process started") 804 } 805 pr, pw, err := os.Pipe() 806 if err != nil { 807 return nil, err 808 } 809 c.Stdin = pr 810 c.childIOFiles = append(c.childIOFiles, pr) 811 c.parentIOPipes = append(c.parentIOPipes, pw) 812 return pw, nil 813 } 814 815 // StdoutPipe returns a pipe that will be connected to the command's 816 // standard output when the command starts. 817 // 818 // Wait will close the pipe after seeing the command exit, so most callers 819 // need not close the pipe themselves. It is thus incorrect to call Wait 820 // before all reads from the pipe have completed. 821 // For the same reason, it is incorrect to call Run when using StdoutPipe. 822 // See the example for idiomatic usage. 823 func (c *Cmd) StdoutPipe() (io.ReadCloser, error) { 824 if c.Stdout != nil { 825 return nil, errors.New("exec: Stdout already set") 826 } 827 if c.Process != nil { 828 return nil, errors.New("exec: StdoutPipe after process started") 829 } 830 pr, pw, err := os.Pipe() 831 if err != nil { 832 return nil, err 833 } 834 c.Stdout = pw 835 c.childIOFiles = append(c.childIOFiles, pw) 836 c.parentIOPipes = append(c.parentIOPipes, pr) 837 return pr, nil 838 } 839 840 // StderrPipe returns a pipe that will be connected to the command's 841 // standard error when the command starts. 842 // 843 // Wait will close the pipe after seeing the command exit, so most callers 844 // need not close the pipe themselves. It is thus incorrect to call Wait 845 // before all reads from the pipe have completed. 846 // For the same reason, it is incorrect to use Run when using StderrPipe. 847 // See the StdoutPipe example for idiomatic usage. 848 func (c *Cmd) StderrPipe() (io.ReadCloser, error) { 849 if c.Stderr != nil { 850 return nil, errors.New("exec: Stderr already set") 851 } 852 if c.Process != nil { 853 return nil, errors.New("exec: StderrPipe after process started") 854 } 855 pr, pw, err := os.Pipe() 856 if err != nil { 857 return nil, err 858 } 859 c.Stderr = pw 860 c.childIOFiles = append(c.childIOFiles, pw) 861 c.parentIOPipes = append(c.parentIOPipes, pr) 862 return pr, nil 863 } 864 865 // prefixSuffixSaver is an io.Writer which retains the first N bytes 866 // and the last N bytes written to it. The Bytes() methods reconstructs 867 // it with a pretty error message. 868 type prefixSuffixSaver struct { 869 N int // max size of prefix or suffix 870 prefix []byte 871 suffix []byte // ring buffer once len(suffix) == N 872 suffixOff int // offset to write into suffix 873 skipped int64 874 875 // TODO(bradfitz): we could keep one large []byte and use part of it for 876 // the prefix, reserve space for the '... Omitting N bytes ...' message, 877 // then the ring buffer suffix, and just rearrange the ring buffer 878 // suffix when Bytes() is called, but it doesn't seem worth it for 879 // now just for error messages. It's only ~64KB anyway. 880 } 881 882 func (w *prefixSuffixSaver) Write(p []byte) (n int, err error) { 883 lenp := len(p) 884 p = w.fill(&w.prefix, p) 885 886 // Only keep the last w.N bytes of suffix data. 887 if overage := len(p) - w.N; overage > 0 { 888 p = p[overage:] 889 w.skipped += int64(overage) 890 } 891 p = w.fill(&w.suffix, p) 892 893 // w.suffix is full now if p is non-empty. Overwrite it in a circle. 894 for len(p) > 0 { // 0, 1, or 2 iterations. 895 n := copy(w.suffix[w.suffixOff:], p) 896 p = p[n:] 897 w.skipped += int64(n) 898 w.suffixOff += n 899 if w.suffixOff == w.N { 900 w.suffixOff = 0 901 } 902 } 903 return lenp, nil 904 } 905 906 // fill appends up to len(p) bytes of p to *dst, such that *dst does not 907 // grow larger than w.N. It returns the un-appended suffix of p. 908 func (w *prefixSuffixSaver) fill(dst *[]byte, p []byte) (pRemain []byte) { 909 if remain := w.N - len(*dst); remain > 0 { 910 add := minInt(len(p), remain) 911 *dst = append(*dst, p[:add]...) 912 p = p[add:] 913 } 914 return p 915 } 916 917 func (w *prefixSuffixSaver) Bytes() []byte { 918 if w.suffix == nil { 919 return w.prefix 920 } 921 if w.skipped == 0 { 922 return append(w.prefix, w.suffix...) 923 } 924 var buf bytes.Buffer 925 buf.Grow(len(w.prefix) + len(w.suffix) + 50) 926 buf.Write(w.prefix) 927 buf.WriteString("\n... omitting ") 928 buf.WriteString(strconv.FormatInt(w.skipped, 10)) 929 buf.WriteString(" bytes ...\n") 930 buf.Write(w.suffix[w.suffixOff:]) 931 buf.Write(w.suffix[:w.suffixOff]) 932 return buf.Bytes() 933 } 934 935 func minInt(a, b int) int { 936 if a < b { 937 return a 938 } 939 return b 940 } 941 942 // environ returns a best-effort copy of the environment in which the command 943 // would be run as it is currently configured. If an error occurs in computing 944 // the environment, it is returned alongside the best-effort copy. 945 func (c *Cmd) environ() ([]string, error) { 946 var err error 947 948 env := c.Env 949 if env == nil { 950 env, err = execenv.Default(c.SysProcAttr) 951 if err != nil { 952 env = os.Environ() 953 // Note that the non-nil err is preserved despite env being overridden. 954 } 955 956 if c.Dir != "" { 957 switch runtime.GOOS { 958 case "windows", "plan9": 959 // Windows and Plan 9 do not use the PWD variable, so we don't need to 960 // keep it accurate. 961 default: 962 // On POSIX platforms, PWD represents βan absolute pathname of the 963 // current working directory.β Since we are changing the working 964 // directory for the command, we should also update PWD to reflect that. 965 // 966 // Unfortunately, we didn't always do that, so (as proposed in 967 // https://go.dev/issue/50599) to avoid unintended collateral damage we 968 // only implicitly update PWD when Env is nil. That way, we're much 969 // less likely to override an intentional change to the variable. 970 if pwd, absErr := filepath.Abs(c.Dir); absErr == nil { 971 env = append(env, "PWD="+pwd) 972 } else if err == nil { 973 err = absErr 974 } 975 } 976 } 977 } 978 979 return addCriticalEnv(dedupEnv(env)), err 980 } 981 982 // Environ returns a copy of the environment in which the command would be run 983 // as it is currently configured. 984 func (c *Cmd) Environ() []string { 985 // Intentionally ignore errors: environ returns a best-effort environment no matter what. 986 env, _ := c.environ() 987 return env 988 } 989 990 // dedupEnv returns a copy of env with any duplicates removed, in favor of 991 // later values. 992 // Items not of the normal environment "key=value" form are preserved unchanged. 993 func dedupEnv(env []string) []string { 994 return dedupEnvCase(runtime.GOOS == "windows", env) 995 } 996 997 // dedupEnvCase is dedupEnv with a case option for testing. 998 // If caseInsensitive is true, the case of keys is ignored. 999 func dedupEnvCase(caseInsensitive bool, env []string) []string { 1000 // Construct the output in reverse order, to preserve the 1001 // last occurrence of each key. 1002 out := make([]string, 0, len(env)) 1003 saw := make(map[string]bool, len(env)) 1004 for n := len(env); n > 0; n-- { 1005 kv := env[n-1] 1006 1007 i := strings.Index(kv, "=") 1008 if i == 0 { 1009 // We observe in practice keys with a single leading "=" on Windows. 1010 // TODO(#49886): Should we consume only the first leading "=" as part 1011 // of the key, or parse through arbitrarily many of them until a non-"="? 1012 i = strings.Index(kv[1:], "=") + 1 1013 } 1014 if i < 0 { 1015 if kv != "" { 1016 // The entry is not of the form "key=value" (as it is required to be). 1017 // Leave it as-is for now. 1018 // TODO(#52436): should we strip or reject these bogus entries? 1019 out = append(out, kv) 1020 } 1021 continue 1022 } 1023 k := kv[:i] 1024 if caseInsensitive { 1025 k = strings.ToLower(k) 1026 } 1027 if saw[k] { 1028 continue 1029 } 1030 1031 saw[k] = true 1032 out = append(out, kv) 1033 } 1034 1035 // Now reverse the slice to restore the original order. 1036 for i := 0; i < len(out)/2; i++ { 1037 j := len(out) - i - 1 1038 out[i], out[j] = out[j], out[i] 1039 } 1040 1041 return out 1042 } 1043 1044 // addCriticalEnv adds any critical environment variables that are required 1045 // (or at least almost always required) on the operating system. 1046 // Currently this is only used for Windows. 1047 func addCriticalEnv(env []string) []string { 1048 if runtime.GOOS != "windows" { 1049 return env 1050 } 1051 for _, kv := range env { 1052 k, _, ok := strings.Cut(kv, "=") 1053 if !ok { 1054 continue 1055 } 1056 if strings.EqualFold(k, "SYSTEMROOT") { 1057 // We already have it. 1058 return env 1059 } 1060 } 1061 return append(env, "SYSTEMROOT="+os.Getenv("SYSTEMROOT")) 1062 } 1063 1064 // ErrDot indicates that a path lookup resolved to an executable 1065 // in the current directory due to β.β being in the path, either 1066 // implicitly or explicitly. See the package documentation for details. 1067 // 1068 // Note that functions in this package do not return ErrDot directly. 1069 // Code should use errors.Is(err, ErrDot), not err == ErrDot, 1070 // to test whether a returned error err is due to this condition. 1071 var ErrDot = errors.New("cannot run executable found relative to current directory")