-
Notifications
You must be signed in to change notification settings - Fork 21
/
daemon.go
364 lines (340 loc) · 9.03 KB
/
daemon.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package framework
import (
"encoding/json"
"errors"
"fmt"
"github.com/sevlyar/go-daemon"
"log"
"os"
"path/filepath"
"syscall"
)
type DaemonizedService interface {
Start() (output string, err error)
Stop() (output string, err error)
Snapshot() (output string, err error)
}
type asyncOperateResult struct {
Error string `json:"error,omitempty"`
Output string `json:"output,omitempty"`
}
type ConfigGenerator func(workingPath string) (err error)
type ServiceGenerator func(workingPath string) (service DaemonizedService, err error)
var (
pipFileName string
daemonizedService DaemonizedService
)
func ProcessDaemon(executeName string, configGenerator ConfigGenerator, serviceGenerator ServiceGenerator) {
const (
ValidArguesCount = 2
StartCommand = "start"
StopCommand = "stop"
StatusCommand = "status"
HaltCommand = "halt"
SnapshotCommand = "snap"
LogPathName = "log"
)
if len(os.Args) != ValidArguesCount {
printUsage(executeName)
return
}
workingPath, err := getWorkingPath()
if err != nil {
fmt.Printf("get working path fail: %s\n", err.Error())
return
}
var command = os.Args[1]
var pidFileName = filepath.Join(workingPath, fmt.Sprintf("%s.pid", executeName))
if "" == pipFileName {
pipFileName = filepath.Join(workingPath, fmt.Sprintf("%s.pip", executeName))
}
context := &daemon.Context{
PidFileName: pidFileName,
PidFilePerm: daemon.FILE_PERM,
}
daemon.AddCommand(daemon.StringFlag(&command, StopCommand), syscall.SIGTERM, onStopDaemon)
daemon.AddCommand(daemon.StringFlag(&command, SnapshotCommand), syscall.SIGUSR1, onDaemonSnapshot)
daemon.AddCommand(daemon.StringFlag(&command, HaltCommand), syscall.SIGKILL, nil)
switch command {
case StopCommand:
if process, err := context.Search(); err != nil || nil == process {
fmt.Printf("%s is already stopped\n", executeName)
return
} else if !isRunning(process) {
fmt.Printf("%s is already stopped (process %d not running)\n", executeName, process.Pid)
} else {
defer os.Remove(pipFileName)
if err = createPipe(pipFileName); err != nil {
fmt.Printf("open pipe fail: %s\n", err.Error())
return
}
go daemon.SendCommands(process)
msg, err := readPipe(pipFileName)
if err != nil {
fmt.Printf("stop %s fail: %s\n", executeName, err.Error())
} else if "" != msg {
fmt.Println(msg)
fmt.Printf("stop %s success\n", executeName)
} else {
fmt.Printf("stop %s success\n", executeName)
}
}
return
case SnapshotCommand:
var process *os.Process
if process, err = context.Search(); err != nil || nil == process {
fmt.Printf("%s is already stopped\n", executeName)
return
} else if !isRunning(process) {
fmt.Printf("%s is already stopped (process %d not running)\n", executeName, process.Pid)
} else {
defer os.Remove(pipFileName)
if err = createPipe(pipFileName); err != nil {
fmt.Printf("open pipe fail: %s\n", err.Error())
return
}
go daemon.SendCommands(process)
var msg string
msg, err = readPipe(pipFileName)
if err != nil {
fmt.Printf("capture snapshot of %s fail: %s\n", executeName, err.Error())
} else if "" != msg {
fmt.Println(msg)
fmt.Printf("capture snapshot of %s success\n", executeName)
} else {
fmt.Printf("capture snapshot of %s success\n", executeName)
}
}
return
case HaltCommand:
if process, err := context.Search(); err != nil || nil == process {
fmt.Printf("%s is already stopped\n", executeName)
} else if !isRunning(process) {
fmt.Printf("%s is already stopped (process %d not running)\n", executeName, process.Pid)
} else {
daemon.SendCommands(process)
}
return
case StatusCommand:
process, err := context.Search()
if err != nil || nil == process {
fmt.Printf("%s is stopped\n", executeName)
} else if !isRunning(process) {
fmt.Printf("%s is stopped (pid %d)\n", executeName, process.Pid)
} else {
fmt.Printf("%s is running, current pid %d\n", executeName, process.Pid)
}
return
case StartCommand:
if err := configGenerator(workingPath); err != nil {
fmt.Printf("generate config fail: %s\n", err.Error())
return
}
if process, err := context.Search(); err == nil && nil != process {
if isRunning(process) {
fmt.Printf("%s is already running\n", executeName)
return
}
}
if _, err = context.Reborn(); err != nil {
fmt.Printf("create daemon fail:%s\n", err.Error())
return
}
//parent or child
if !daemon.WasReborn() {
//parent
msg, err := readMessageFromPipe(pipFileName)
if err != nil {
fmt.Printf("start %s fail: %s\n", executeName, err.Error())
} else {
fmt.Println(msg)
fmt.Printf("%s started\n", executeName)
}
return
} else {
//child
defer os.Remove(pidFileName)
var logPath = filepath.Join(workingPath, LogPathName)
if err = redirectLog(executeName, logPath); err != nil {
notifyErrorToPipe(pipFileName, err.Error())
log.Printf("redirect log fail: %s", err.Error())
return
}
daemonizedService, err = serviceGenerator(workingPath)
if err != nil {
log.Printf("generate service fail: %s", err.Error())
notifyErrorToPipe(pipFileName, err.Error())
return
}
msg, err := daemonizedService.Start()
if err != nil {
log.Printf("start service fail: %s", err.Error())
notifyErrorToPipe(pipFileName, err.Error())
} else {
notifyMessageToPipe(pipFileName, msg)
daemon.ServeSignals()
}
return
}
default:
printUsage(executeName)
}
}
func onStopDaemon(sig os.Signal) error {
if nil == daemonizedService {
log.Println("invalid daemon service")
return daemon.ErrStop
}
if "" == pipFileName {
log.Println("invalid pipe file")
return daemon.ErrStop
}
msg, err := daemonizedService.Stop()
if err != nil {
log.Printf("stop service fail: %s", err.Error())
notifyErrorToPipe(pipFileName, err.Error())
} else {
notifyMessageToPipe(pipFileName, msg)
}
return daemon.ErrStop
}
func onDaemonSnapshot(sig os.Signal) error {
if nil == daemonizedService {
log.Println("invalid daemon service")
return daemon.ErrStop
}
if "" == pipFileName {
log.Println("invalid pipe file")
return daemon.ErrStop
}
msg, err := daemonizedService.Snapshot()
if err != nil {
log.Printf("invoke snapshot fail: %s", err.Error())
notifyErrorToPipe(pipFileName, err.Error())
} else {
notifyMessageToPipe(pipFileName, msg)
}
return nil
}
func readMessageFromPipe(pipeName string) (message string, err error) {
defer os.Remove(pipeName)
if err = createPipe(pipeName); err != nil {
return
}
message, err = readPipe(pipeName)
return
}
func createPipe(pipeName string) (err error) {
const (
PipeFilePerm = 0600
)
if _, err = os.Stat(pipeName); !os.IsNotExist(err) {
os.Remove(pipeName)
}
if err = syscall.Mkfifo(pipeName, PipeFilePerm); err != nil {
return
}
return
}
func readPipe(pipeName string) (message string, err error) {
const (
PipeFilePerm = 0600
)
var pipe *os.File
pipe, err = os.OpenFile(pipeName, os.O_RDONLY, PipeFilePerm)
if err != nil {
return
}
defer pipe.Close()
var data = make([]byte, 1<<10)
var n int
n, err = pipe.Read(data)
if err != nil {
return
}
var result asyncOperateResult
if err = json.Unmarshal(data[:n], &result); err != nil {
return "", fmt.Errorf("unmarshal fail: %s, data %s", err.Error(), data[:n])
}
if result.Error != "" {
err = errors.New(result.Error)
} else {
message = result.Output
}
return
}
func notifyMessageToPipe(pipeName, message string) (err error) {
const (
PipeFilePerm = 0600
)
pip, err := os.OpenFile(pipeName, os.O_RDWR, PipeFilePerm)
if err != nil {
return
}
defer pip.Close()
var result = asyncOperateResult{Output: message}
data, err := json.MarshalIndent(result, "", " ")
if err != nil {
return
}
_, err = pip.Write(data)
return
}
func notifyErrorToPipe(pipeName, message string) (err error) {
const (
PipeFilePerm = 0600
)
pip, err := os.OpenFile(pipeName, os.O_RDWR, PipeFilePerm)
if err != nil {
return
}
defer pip.Close()
var result = asyncOperateResult{Error: message}
data, err := json.MarshalIndent(result, "", " ")
if err != nil {
return
}
_, err = pip.Write(data)
return
}
func getWorkingPath() (path string, err error) {
executable, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Abs(filepath.Dir(executable))
}
func isRunning(process *os.Process) bool {
if nil == process {
return false
}
if err := process.Signal(syscall.Signal(0)); err == nil {
return true
} else {
return false
}
}
func redirectLog(executeName, logPathName string) (err error) {
const (
DefaultLogPathPerm = 0740
)
if _, err = os.Stat(logPathName); os.IsNotExist(err) {
//create path
err = os.Mkdir(logPathName, DefaultLogPathPerm)
if err != nil {
return err
}
fmt.Printf("log path %s created\n", logPathName)
}
var filename = fmt.Sprintf("%s/%s.log", logPathName, executeName)
output, err := os.Create(filename)
if err != nil {
return err
}
fmt.Printf("log redirected to '%s'\n", filename)
log.SetOutput(output)
return nil
}
func printUsage(executeName string) {
fmt.Printf("Usage: %s [start|stop|status|halt|snap]\n", executeName)
}