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
23
24
25
26 if ps, e := p.pidfdWait(); e != syscall.ENOSYS {
27 return ps, NewSyscallError("waitid", e)
28 }
29
30
31 ready, err := p.blockUntilWaitable()
32 if err != nil {
33 return nil, err
34 }
35 if ready {
36
37
38 p.setDone()
39
40
41 p.sigMu.Lock()
42 p.sigMu.Unlock()
43 }
44
45 var (
46 status syscall.WaitStatus
47 rusage syscall.Rusage
48 pid1 int
49 e error
50 )
51 for {
52 pid1, e = syscall.Wait4(p.Pid, &status, 0, &rusage)
53 if e != syscall.EINTR {
54 break
55 }
56 }
57 if e != nil {
58 return nil, NewSyscallError("wait", e)
59 }
60 p.setDone()
61 ps = &ProcessState{
62 pid: pid1,
63 status: status,
64 rusage: &rusage,
65 }
66 return ps, nil
67 }
68
69 func (p *Process) signal(sig Signal) error {
70 if p.Pid == -1 {
71 return errors.New("os: process already released")
72 }
73 if p.Pid == 0 {
74 return errors.New("os: process not initialized")
75 }
76 s, ok := sig.(syscall.Signal)
77 if !ok {
78 return errors.New("os: unsupported signal type")
79 }
80
81 if err := p.pidfdSendSignal(s); err != syscall.ENOSYS {
82 return err
83 }
84 p.sigMu.RLock()
85 defer p.sigMu.RUnlock()
86 if p.done() {
87 return ErrProcessDone
88 }
89 return convertESRCH(syscall.Kill(p.Pid, s))
90 }
91
92 func convertESRCH(err error) error {
93 if err == syscall.ESRCH {
94 return ErrProcessDone
95 }
96 return err
97 }
98
99 func (p *Process) release() error {
100 p.pidfdRelease()
101 p.Pid = -1
102
103 runtime.SetFinalizer(p, nil)
104 return nil
105 }
106
107 func findProcess(pid int) (p *Process, err error) {
108
109 return newProcess(pid, unsetHandle), nil
110 }
111
112 func (p *ProcessState) userTime() time.Duration {
113 return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
114 }
115
116 func (p *ProcessState) systemTime() time.Duration {
117 return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
118 }
119
View as plain text