-
Notifications
You must be signed in to change notification settings - Fork 4
/
process.go
177 lines (148 loc) · 3.77 KB
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//+build !windows,!plan9
// Package process manages the lifecyle of processes and process groups
package process
import (
"errors"
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
// TODO(nightlyone) remove me, when everything is implemented
var errUnimplemented = errors.New("process: function not implemented")
// Group is a process group we manage.
type Group struct {
pid int
waitc <-chan error
}
// Background runs a command which can fork other commands (e.g. a shell script) building a tree of processes.
// This tree of processes are managed in a their own process group.
// NOTE: It overwrites cmd.SysProcAttr
func Background(cmd *exec.Cmd) (*Group, error) {
if cmd.ProcessState != nil {
return nil, fmt.Errorf("process: command already executed: %q", cmd.Path)
}
if cmd.Process != nil {
return nil, fmt.Errorf("process: command already executing: %q", cmd.Path)
}
startc := make(chan startResult)
waitc := make(chan error, 1)
// NOTE: Cannot setsid and and setpgid in one child. Would need double fork or exec,
// which makes things very hard.
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
} else {
return nil, errUnimplemented
}
// Try to start process
go startProcess(cmd, startc, waitc)
res := <-startc
if res.err != nil {
return nil, res.err
}
// Now running in the background
return &Group{
pid: res.pid,
waitc: waitc,
}, nil
}
// ErrNotLeader is returned when we request actions for a process group, but are not their process group leader
var ErrNotLeader = errors.New("process is not process group leader")
// Signal sends POSIX signal sig to this process group
func (g *Group) Signal(sig os.Signal) error {
if g == nil || g.waitc == nil {
return syscall.ESRCH
}
if leader, err := g.isLeader(); err != nil {
return err
} else if !leader {
return ErrNotLeader
}
// This just creates a process object from a Pid in Unix
// instead of actually searching it.
grp, _ := os.FindProcess(-g.pid)
return grp.Signal(sig)
}
// Terminate first tries to gracefully terminate the process, waits patience time, then does final termination and waits for it to exit.
func (g *Group) Terminate(patience time.Duration) error {
var terminated bool
// did we exit in the meantime?
select {
case errWait, opened := <-g.waitc:
if opened {
<-g.waitc
g.waitc = nil
return errWait
}
default:
}
// try to be soft
if err := g.Signal(syscall.SIGTERM); err != nil {
return err
}
// wait at most patience time for exit
select {
case _, opened := <-g.waitc:
if opened {
<-g.waitc
g.waitc = nil
terminated = true
}
case <-time.After(patience):
}
// exited gracefully
if terminated {
return nil
}
// do it the hard way
if err := g.Signal(syscall.SIGKILL); err != nil {
return err
}
// But we need to wait on the result now
<-g.waitc
<-g.waitc
g.waitc = nil
return nil
}
// isLeader determines, whether g is still the leader of the process group
func (g *Group) isLeader() (ok bool, err error) {
if g == nil {
return false, syscall.ESRCH
}
pgid, err := syscall.Getpgid(g.pid)
if err != nil {
return false, err
}
// Pids 0 and 1 will have special meaning, so don't return them.
if pgid < 2 {
return false, nil
}
// the process is not the leader?
if pgid != g.pid {
return false, nil
}
return true, nil
}
type startResult struct {
pid int
err error
}
// startProcess tries to a new process. Start result in startc, exit result in waitc.
func startProcess(cmd *exec.Cmd, startc chan<- startResult, waitc chan<- error) {
var res startResult
// Startup new process
if err := cmd.Start(); err != nil {
res.err = err
startc <- res
return
}
res.pid = cmd.Process.Pid
startc <- res
close(startc)
// No wait until we finish or get killed
waitc <- cmd.Wait()
close(waitc)
}