Source file
src/os/exec_unix.go
1
2
3
4
5
6
7 package os
8
9 import (
10 "errors"
11 "runtime"
12 "syscall"
13 "time"
14 )
15
16 func (p *Process) wait() (ps *ProcessState, err error) {
17 if p.Pid == -1 {
18 return nil, syscall.EINVAL
19 }
20
21
22 ready, err := p.blockUntilWaitable()
23 if err != nil {
24 return nil, err
25 }
26 if ready {
27
28
29 p.setDone()
30
31
32 p.sigMu.Lock()
33 p.sigMu.Unlock()
34 }
35
36 var (
37 status syscall.WaitStatus
38 rusage syscall.Rusage
39 pid1 int
40 e error
41 )
42 for {
43 pid1, e = syscall.Wait4(p.Pid, &status, 0, &rusage)
44 if e != syscall.EINTR {
45 break
46 }
47 }
48 if e != nil {
49 return nil, NewSyscallError("wait", e)
50 }
51 if pid1 != 0 {
52 p.setDone()
53 }
54 ps = &ProcessState{
55 pid: pid1,
56 status: status,
57 rusage: &rusage,
58 }
59 return ps, nil
60 }
61
62 func (p *Process) signal(sig Signal) error {
63 if p.Pid == -1 {
64 return errors.New("os: process already released")
65 }
66 if p.Pid == 0 {
67 return errors.New("os: process not initialized")
68 }
69 p.sigMu.RLock()
70 defer p.sigMu.RUnlock()
71 if p.done() {
72 return ErrProcessDone
73 }
74 s, ok := sig.(syscall.Signal)
75 if !ok {
76 return errors.New("os: unsupported signal type")
77 }
78 if e := syscall.Kill(p.Pid, s); e != nil {
79 if e == syscall.ESRCH {
80 return ErrProcessDone
81 }
82 return e
83 }
84 return nil
85 }
86
87 func (p *Process) release() error {
88
89 p.Pid = -1
90
91 runtime.SetFinalizer(p, nil)
92 return nil
93 }
94
95 func findProcess(pid int) (p *Process, err error) {
96
97 return newProcess(pid, 0), nil
98 }
99
100 func (p *ProcessState) userTime() time.Duration {
101 return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
102 }
103
104 func (p *ProcessState) systemTime() time.Duration {
105 return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
106 }
107
View as plain text