exec_unix.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  5. package os
  6. import (
  7. "errors"
  8. "runtime"
  9. "syscall"
  10. "time"
  11. )
  12. func (p *Process) wait() (ps *ProcessState, err error) {
  13. if p.Pid == -1 {
  14. return nil, syscall.EINVAL
  15. }
  16. // If we can block until Wait4 will succeed immediately, do so.
  17. ready, err := p.blockUntilWaitable()
  18. if err != nil {
  19. return nil, err
  20. }
  21. if ready {
  22. // Mark the process done now, before the call to Wait4,
  23. // so that Process.signal will not send a signal.
  24. p.setDone()
  25. // Acquire a write lock on sigMu to wait for any
  26. // active call to the signal method to complete.
  27. p.sigMu.Lock()
  28. p.sigMu.Unlock()
  29. }
  30. var (
  31. status syscall.WaitStatus
  32. rusage syscall.Rusage
  33. pid1 int
  34. e error
  35. )
  36. for {
  37. pid1, e = syscall.Wait4(p.Pid, &status, 0, &rusage)
  38. if e != syscall.EINTR {
  39. break
  40. }
  41. }
  42. if e != nil {
  43. return nil, NewSyscallError("wait", e)
  44. }
  45. if pid1 != 0 {
  46. p.setDone()
  47. }
  48. ps = &ProcessState{
  49. pid: pid1,
  50. status: status,
  51. rusage: &rusage,
  52. }
  53. return ps, nil
  54. }
  55. func (p *Process) signal(sig Signal) error {
  56. if p.Pid == -1 {
  57. return errors.New("os: process already released")
  58. }
  59. if p.Pid == 0 {
  60. return errors.New("os: process not initialized")
  61. }
  62. p.sigMu.RLock()
  63. defer p.sigMu.RUnlock()
  64. if p.done() {
  65. return ErrProcessDone
  66. }
  67. s, ok := sig.(syscall.Signal)
  68. if !ok {
  69. return errors.New("os: unsupported signal type")
  70. }
  71. if e := syscall.Kill(p.Pid, s); e != nil {
  72. if e == syscall.ESRCH {
  73. return ErrProcessDone
  74. }
  75. return e
  76. }
  77. return nil
  78. }
  79. func (p *Process) release() error {
  80. // NOOP for unix.
  81. p.Pid = -1
  82. // no need for a finalizer anymore
  83. runtime.SetFinalizer(p, nil)
  84. return nil
  85. }
  86. func findProcess(pid int) (p *Process, err error) {
  87. // NOOP for unix.
  88. return newProcess(pid, 0), nil
  89. }
  90. func (p *ProcessState) userTime() time.Duration {
  91. return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
  92. }
  93. func (p *ProcessState) systemTime() time.Duration {
  94. return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
  95. }