This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
process.go
303 lines (246 loc) · 5.43 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) 2012 VMware, Inc.
package gonit
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"
)
var byteOrder = binary.LittleEndian
const DEFAULT_ENV_PATH = "PATH=/bin:/usr/bin:/sbin:/usr/sbin"
// Redirect child process fd (stdout | stderr) to a file
func (p *Process) Redirect(fd *io.Writer, where string) error {
if where == "" {
return nil
}
// exec package takes care of closing after fork+exec
file, err := os.OpenFile(where, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
*fd = file
return nil
}
// exec.Cmd wrapper
func (p *Process) Command(program string) (*exec.Cmd, error) {
var credential *syscall.Credential
if p.Uid != "" || p.Gid != "" {
credential = &syscall.Credential{}
err := p.lookupCredentials(credential)
if err != nil {
return nil, err
}
}
argv := strings.Fields(program)
path, err := exec.LookPath(argv[0])
if err != nil {
return nil, err
}
// XXX moar defaults
defaultEnv := []string{
DEFAULT_ENV_PATH,
}
env := make([]string, len(defaultEnv)+len(p.Env))
copy(env, defaultEnv)
copy(env[len(defaultEnv):], p.Env)
cmd := &exec.Cmd{
Path: path,
Args: argv,
Env: env,
Dir: p.Dir,
SysProcAttr: &syscall.SysProcAttr{
Setsid: true,
Credential: credential,
},
}
return cmd, nil
}
// Fork+Exec program with std{out,err} redirected and
// new session so program becomes the session and process group leader.
func (p *Process) Spawn(program string) (*exec.Cmd, error) {
cmd, err := p.Command(program)
if err != nil {
return nil, err
}
err = p.Redirect(&cmd.Stderr, p.Stderr)
if err != nil {
return nil, err
}
err = p.Redirect(&cmd.Stdout, p.Stdout)
if err != nil {
return nil, err
}
err = cmd.Start()
return cmd, err
}
// Start a process:
// If Detached == true; process must detached itself and
// manage its own Pidfile.
// Otherwise; we will detach the process and manage its Pidfile.
func (p *Process) StartProcess() (int, error) {
cmd, err := p.Spawn(p.Start)
if err != nil {
Log.Errorf("Error starting process '%v': %v", p.Name, err.Error())
return 0, err
}
pid := cmd.Process.Pid
if p.Detached {
err = cmd.Wait()
} else {
err = p.SavePid(pid)
go cmd.Wait()
}
return pid, err
}
// Stop a process:
// Spawn Stop program if configured,
// otherwise send SIGTERM.
func (p *Process) StopProcess() error {
if p.Stop == "" {
pid, err := p.Pid()
if err != nil {
return err
}
return syscall.Kill(pid, syscall.SIGTERM)
}
cmd, err := p.Spawn(p.Stop)
if err != nil {
return err
}
return cmd.Wait()
}
// Restart a process:
// Spawn Restart program if configured,
// otherwise call StopProcess() + StartProcess()
func (p *Process) RestartProcess() error {
if p.Restart == "" {
err := p.StopProcess()
if err != nil {
return err
}
_, err = p.StartProcess()
return err
}
cmd, err := p.Spawn(p.Restart)
if err != nil {
return err
}
return cmd.Wait()
}
// Helper method to check if process is running via Pidfile
func (p *Process) IsRunning() bool {
pid, err := p.Pid()
if err != nil {
return false
}
err = syscall.Kill(pid, 0)
if err != nil {
return false
}
return true
}
// Read pid from a file
func ReadPidFile(path string) (int, error) {
pid, err := ioutil.ReadFile(path)
if err == nil {
pidString := strings.TrimSpace(string(pid))
return strconv.Atoi(pidString)
}
return 0, err
}
// Read pid from Pidfile
func (p *Process) Pid() (int, error) {
return ReadPidFile(p.Pidfile)
}
// Write pid to a file
func WritePidFile(pid int, path string) error {
pidString := []byte(strconv.Itoa(pid))
err := ioutil.WriteFile(path, pidString, 0644)
return err
}
// Write pid to Pidfile
func (p *Process) SavePid(pid int) error {
Log.Debugf("Saving %q pid to file=%s", p.Name, p.Pidfile)
return WritePidFile(pid, p.Pidfile)
}
// If User is configured, lookup and set Uid
func (p *Process) lookupUid(credential *syscall.Credential) error {
if p.Uid == "" {
return nil
}
id, err := user.Lookup(p.Uid)
if err != nil {
return err
}
uid, _ := strconv.Atoi(id.Uid)
gid, _ := strconv.Atoi(id.Gid)
credential.Uid = uint32(uid)
if p.Gid == "" {
credential.Gid = uint32(gid)
}
return nil
}
// If Group is configured, lookup and set Gid
func (p *Process) lookupGid(credential *syscall.Credential) error {
if p.Gid == "" {
return nil
}
gid, err := LookupGroupId(p.Gid)
if err != nil {
return err
}
credential.Gid = uint32(gid)
if p.Uid == "" {
credential.Uid = uint32(os.Getuid())
}
return nil
}
func (p *Process) lookupCredentials(credential *syscall.Credential) error {
if err := p.lookupUid(credential); err != nil {
return err
}
if err := p.lookupGid(credential); err != nil {
return err
}
return nil
}
// until we have user.LookupGroupId: http://codereview.appspot.com/4589049
func LookupGroupId(group string) (int, error) {
const (
GR_NAME = iota
_
GR_ID
)
file, err := os.Open("/etc/group")
if err != nil {
return -1, err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return 0, err
}
line = bytes.TrimSpace(line)
if len(line) == 0 || line[0] == '#' {
continue
}
fields := strings.Split(string(line), ":")
if len(fields) == 4 && fields[GR_NAME] == group {
return strconv.Atoi(fields[GR_ID])
}
}
return -1, errors.New("group: unknown group " + group)
}