exec_posix.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. //go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
  5. package os
  6. import (
  7. "internal/itoa"
  8. "internal/syscall/execenv"
  9. "runtime"
  10. "syscall"
  11. )
  12. // The only signal values guaranteed to be present in the os package on all
  13. // systems are os.Interrupt (send the process an interrupt) and os.Kill (force
  14. // the process to exit). On Windows, sending os.Interrupt to a process with
  15. // os.Process.Signal is not implemented; it will return an error instead of
  16. // sending a signal.
  17. var (
  18. Interrupt Signal = syscall.SIGINT
  19. Kill Signal = syscall.SIGKILL
  20. )
  21. func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
  22. // If there is no SysProcAttr (ie. no Chroot or changed
  23. // UID/GID), double-check existence of the directory we want
  24. // to chdir into. We can make the error clearer this way.
  25. if attr != nil && attr.Sys == nil && attr.Dir != "" {
  26. if _, err := Stat(attr.Dir); err != nil {
  27. pe := err.(*PathError)
  28. pe.Op = "chdir"
  29. return nil, pe
  30. }
  31. }
  32. sysattr := &syscall.ProcAttr{
  33. Dir: attr.Dir,
  34. Env: attr.Env,
  35. Sys: attr.Sys,
  36. }
  37. if sysattr.Env == nil {
  38. sysattr.Env, err = execenv.Default(sysattr.Sys)
  39. if err != nil {
  40. return nil, err
  41. }
  42. }
  43. sysattr.Files = make([]uintptr, 0, len(attr.Files))
  44. for _, f := range attr.Files {
  45. sysattr.Files = append(sysattr.Files, f.Fd())
  46. }
  47. pid, h, e := syscall.StartProcess(name, argv, sysattr)
  48. // Make sure we don't run the finalizers of attr.Files.
  49. runtime.KeepAlive(attr)
  50. if e != nil {
  51. return nil, &PathError{Op: "fork/exec", Path: name, Err: e}
  52. }
  53. return newProcess(pid, h), nil
  54. }
  55. func (p *Process) kill() error {
  56. return p.Signal(Kill)
  57. }
  58. // ProcessState stores information about a process, as reported by Wait.
  59. type ProcessState struct {
  60. pid int // The process's id.
  61. status syscall.WaitStatus // System-dependent status info.
  62. rusage *syscall.Rusage
  63. }
  64. // Pid returns the process id of the exited process.
  65. func (p *ProcessState) Pid() int {
  66. return p.pid
  67. }
  68. func (p *ProcessState) exited() bool {
  69. return p.status.Exited()
  70. }
  71. func (p *ProcessState) success() bool {
  72. return p.status.ExitStatus() == 0
  73. }
  74. func (p *ProcessState) sys() any {
  75. return p.status
  76. }
  77. func (p *ProcessState) sysUsage() any {
  78. return p.rusage
  79. }
  80. func (p *ProcessState) String() string {
  81. if p == nil {
  82. return "<nil>"
  83. }
  84. status := p.Sys().(syscall.WaitStatus)
  85. res := ""
  86. switch {
  87. case status.Exited():
  88. code := status.ExitStatus()
  89. if runtime.GOOS == "windows" && uint(code) >= 1<<16 { // windows uses large hex numbers
  90. res = "exit status " + uitox(uint(code))
  91. } else { // unix systems use small decimal integers
  92. res = "exit status " + itoa.Itoa(code) // unix
  93. }
  94. case status.Signaled():
  95. res = "signal: " + status.Signal().String()
  96. case status.Stopped():
  97. res = "stop signal: " + status.StopSignal().String()
  98. if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
  99. res += " (trap " + itoa.Itoa(status.TrapCause()) + ")"
  100. }
  101. case status.Continued():
  102. res = "continued"
  103. }
  104. if status.CoreDump() {
  105. res += " (core dumped)"
  106. }
  107. return res
  108. }
  109. // ExitCode returns the exit code of the exited process, or -1
  110. // if the process hasn't exited or was terminated by a signal.
  111. func (p *ProcessState) ExitCode() int {
  112. // return -1 if the process hasn't started.
  113. if p == nil {
  114. return -1
  115. }
  116. return p.status.ExitStatus()
  117. }