-
Notifications
You must be signed in to change notification settings - Fork 48
/
process_group.go
159 lines (127 loc) · 2.78 KB
/
process_group.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
package main
import (
"bufio"
"flag"
"log"
"os"
"os/user"
"strconv"
"sync"
"syscall"
)
type ProcessGroup struct {
set *processSet
wg sync.WaitGroup
commandPath string
sockfile *os.File
user *user.User
}
type processSet struct {
sync.Mutex
set map[*os.Process]bool
}
func MakeProcessGroup(commandPath string, sockfile *os.File, u *user.User) *ProcessGroup {
return &ProcessGroup{
set: newProcessSet(),
commandPath: commandPath,
sockfile: sockfile,
user: u,
}
}
func (self *ProcessGroup) StartProcess() (process *os.Process, err error) {
self.wg.Add(1)
ioReader, ioWriter, err := os.Pipe()
if err != nil {
return nil, err
}
env := append(os.Environ(), "EINHORN_FDS=3")
procAttr := &os.ProcAttr{
Env: env,
Files: []*os.File{os.Stdin, ioWriter, ioWriter, self.sockfile},
Sys: &syscall.SysProcAttr{},
}
if self.user != nil {
uid, _ := strconv.Atoi(self.user.Uid)
gid, _ := strconv.Atoi(self.user.Gid)
procAttr.Sys.Credential = &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
}
}
args := append([]string{self.commandPath}, flag.Args()...)
log.Println("Starting", self.commandPath, args)
process, err = os.StartProcess(self.commandPath, args, procAttr)
if err != nil {
return
}
// Add to set
self.set.Add(process)
// Prefix stdout and stderr lines with the [pid] and send it to the log
logOutput(ioReader, process.Pid, &self.wg)
// Handle the process death
go func() {
state, err := process.Wait()
log.Println(process.Pid, state, err)
// Remove from set
self.set.Remove(process)
// Process is gone
ioReader.Close()
self.wg.Done()
}()
return
}
func (self *ProcessGroup) SignalAll(signal os.Signal, except *os.Process) {
self.set.Each(func(process *os.Process) {
if process != except {
process.Signal(signal)
}
})
}
func (self *ProcessGroup) WaitAll() {
self.wg.Wait()
}
// A thread-safe process set
func newProcessSet() *processSet {
set := new(processSet)
set.set = make(map[*os.Process]bool)
return set
}
func (self *processSet) Add(process *os.Process) {
self.Lock()
defer self.Unlock()
self.set[process] = true
}
func (self *processSet) Each(fn func(*os.Process)) {
self.Lock()
defer self.Unlock()
for process, _ := range self.set {
fn(process)
}
}
func (self *processSet) Remove(process *os.Process) {
self.Lock()
defer self.Unlock()
delete(self.set, process)
}
func (self *processSet) Len() int {
self.Lock()
defer self.Unlock()
return len(self.set)
}
func logOutput(input *os.File, pid int, wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
var (
err error
line string
reader = bufio.NewReader(input)
)
for err == nil {
line, err = reader.ReadString('\n')
if line != "" {
log.Printf("[%d] %s", pid, line)
}
}
}()
}