exec_unix.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // +build darwin dragonfly freebsd linux nacl 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. var status syscall.WaitStatus
  17. var rusage syscall.Rusage
  18. pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage)
  19. if e != nil {
  20. return nil, NewSyscallError("wait", e)
  21. }
  22. if pid1 != 0 {
  23. p.setDone()
  24. }
  25. ps = &ProcessState{
  26. pid: pid1,
  27. status: status,
  28. rusage: &rusage,
  29. }
  30. return ps, nil
  31. }
  32. var errFinished = errors.New("os: process already finished")
  33. func (p *Process) signal(sig Signal) error {
  34. if p.Pid == -1 {
  35. return errors.New("os: process already released")
  36. }
  37. if p.Pid == 0 {
  38. return errors.New("os: process not initialized")
  39. }
  40. if p.done() {
  41. return errFinished
  42. }
  43. s, ok := sig.(syscall.Signal)
  44. if !ok {
  45. return errors.New("os: unsupported signal type")
  46. }
  47. if e := syscall.Kill(p.Pid, s); e != nil {
  48. if e == syscall.ESRCH {
  49. return errFinished
  50. }
  51. return e
  52. }
  53. return nil
  54. }
  55. func (p *Process) release() error {
  56. // NOOP for unix.
  57. p.Pid = -1
  58. // no need for a finalizer anymore
  59. runtime.SetFinalizer(p, nil)
  60. return nil
  61. }
  62. func findProcess(pid int) (p *Process, err error) {
  63. // NOOP for unix.
  64. return newProcess(pid, 0), nil
  65. }
  66. func (p *ProcessState) userTime() time.Duration {
  67. return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
  68. }
  69. func (p *ProcessState) systemTime() time.Duration {
  70. return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
  71. }