-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
service_linux.go
394 lines (351 loc) · 8.8 KB
/
service_linux.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2020-2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a GPL-3.0
// license that can be found in the LICENSE file.
package service
import (
"bytes"
"fmt"
"log"
"log/syslog"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"text/template"
"changkun.de/x/midgard/internal/osext"
)
const (
initSystemV = initFlavor(iota)
initUpstart
initSystemd
)
// the default flavor is initSystemV. we lookup the command line of
// process 1 to detect systemd or upstart
func getFlavor() (initFlavor, error) {
initCmd, err := os.ReadFile("/proc/1/cmdline")
if err != nil {
log.Println("cannot locate /proc/1/cmdline, use /proc/cmdline")
// Try a different file:
if initCmd, err = os.ReadFile("/proc/cmdline"); err != nil {
return initSystemV, err
}
}
// Trim any nul bytes from the result, which are present with some
// kernels but not others
init := string(bytes.TrimRight(initCmd, "\x00"))
if strings.Contains(init, "init [") {
return initSystemV, nil
}
if strings.Contains(init, "systemd") {
return initSystemd, nil
}
if strings.Contains(init, "init") {
// not so fast! you may think this is upstart, but it may be
// a symlink to systemd... yeah, debian does that... ( x )
var target string
if len(init) > 9 && init[0:10] == "/sbin/init" {
target, err = filepath.EvalSymlinks("/sbin/init")
} else {
target, err = filepath.EvalSymlinks(init)
}
if err == nil && strings.Contains(target, "systemd") {
return initSystemd, nil
}
return initUpstart, nil
}
// failed to detect init system, falling back to sysvinit
return initSystemV, nil
}
func newService(c *config) (Service, error) {
var err error
flavor, err := getFlavor()
if err != nil {
return nil, err
}
s := &linuxService{
flavor: flavor,
name: c.Name,
displayName: c.DisplayName,
description: c.Description,
args: c.Args,
}
s.logger, err = syslog.New(syslog.LOG_INFO, s.name)
if err != nil {
return nil, err
}
return s, nil
}
type linuxService struct {
flavor initFlavor
name, displayName, description string
args []string
logger *syslog.Writer
}
type initFlavor uint8
func (f initFlavor) String() string {
switch f {
case initSystemV:
return "sysvinit"
case initUpstart:
return "upstart"
case initSystemd:
return "systemd"
default:
return "unknown"
}
}
func (f initFlavor) ConfigPath(name string) string {
switch f {
case initSystemd:
return "/etc/systemd/system/" + name + ".service"
case initSystemV:
return "/etc/init.d/" + name
case initUpstart:
return "/etc/init/" + name + ".conf"
default:
return ""
}
}
func (f initFlavor) GetTemplate() *template.Template {
var templ string
switch f {
case initSystemd:
templ = systemdScript
case initSystemV:
templ = systemVScript
case initUpstart:
templ = upstartScript
}
return template.Must(template.New(f.String() + "Script").Parse(templ))
}
func (s *linuxService) Install() error {
confPath := s.flavor.ConfigPath(s.name)
_, err := os.Stat(confPath)
if err == nil {
return fmt.Errorf("service already exists: %s", confPath)
}
log.Println("creating: ", confPath)
f, err := os.Create(confPath)
if err != nil {
return err
}
defer f.Close()
path, err := osext.Executable()
if err != nil {
return fmt.Errorf("%s executable does not exists, err: %w", s.name, err)
}
var to = &struct {
Display string
Description string
Path string
Args string
}{
s.displayName,
s.description,
path,
strings.Join(s.args, " "),
}
err = s.flavor.GetTemplate().Execute(f, to)
if err != nil {
return err
}
if s.flavor == initSystemV {
if err = os.Chmod(confPath, 0755); err != nil {
return err
}
for _, i := range [...]string{"2", "3", "4", "5"} {
if err = os.Symlink(confPath, "/etc/rc"+i+".d/S50"+s.name); err != nil {
continue
}
}
for _, i := range [...]string{"0", "1", "6"} {
if err = os.Symlink(confPath, "/etc/rc"+i+".d/K02"+s.name); err != nil {
continue
}
}
}
if s.flavor == initSystemd {
err = exec.Command("systemctl", "enable", s.name+".service").Run()
if err != nil {
return err
}
return exec.Command("systemctl", "daemon-reload").Run()
}
return nil
}
func (s *linuxService) Remove() error {
if s.flavor == initSystemd {
exec.Command("systemctl", "disable", s.name+".service").Run()
}
log.Println("removing: ", s.flavor.ConfigPath(s.name))
if err := os.Remove(s.flavor.ConfigPath(s.name)); err != nil {
return err
}
return nil
}
func (s *linuxService) Run(onStart, onStop func() error) (err error) {
err = onStart()
if err != nil {
return err
}
defer func() {
err = onStop()
}()
sig := make(chan os.Signal, 3)
signal.Notify(sig, os.Interrupt, os.Kill)
<-sig
return nil
}
func (s *linuxService) Start() error {
switch s.flavor {
case initSystemd:
log.Println("exec: systemctl start " + s.name + ".service")
return exec.Command("systemctl", "start", s.name+".service").Run()
case initUpstart:
log.Println("exec: initctl start " + s.name)
return exec.Command("initctl", "start", s.name).Run()
default:
log.Println("exec: service " + s.name + " start")
return exec.Command("service", s.name, "start").Run()
}
}
func (s *linuxService) Stop() error {
switch s.flavor {
case initSystemd:
log.Println("exec: systemctl stop " + s.name + ".service")
return exec.Command("systemctl", "stop", s.name+".service").Start()
case initUpstart:
log.Println("exec: initctl stop " + s.name)
return exec.Command("initctl", "stop", s.name).Start()
default:
log.Println("exec: service " + s.name + " stop")
return exec.Command("service", s.name, "stop").Start()
}
}
func (s *linuxService) Error(format string, a ...interface{}) error {
return s.logger.Err(fmt.Sprintf(format, a...))
}
func (s *linuxService) Warning(format string, a ...interface{}) error {
return s.logger.Warning(fmt.Sprintf(format, a...))
}
func (s *linuxService) Info(format string, a ...interface{}) error {
return s.logger.Info(fmt.Sprintf(format, a...))
}
const systemVScript = `#!/bin/sh
# For RedHat and cousins:
# chkconfig: - 99 01
# description: {{.Description}}
# processname: {{.Path}}
### BEGIN INIT INFO
# Provides: {{.Path}}
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: {{.Display}}
# Description: {{.Description}}
### END INIT INFO
cmd="{{.Path}} {{.Args}}"
name=$(basename $0)
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps $(get_pid) > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
$cmd >> "$stdout_log" 2>> "$stderr_log" &
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill $(get_pid)
for i in {1..10}
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0`
const upstartScript = `# {{.Description}}
description "{{.Display}}"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
# stop the respawn is process fails to start 5 times within 5 minutes
respawn
respawn limit 5 300
umask 022
console none
pre-start script
test -x {{.Path}} {{.Args}} || { stop; exit 0; }
end script
# Start
exec {{.Path}} {{.Args}}
`
const systemdScript = `[Unit]
Description={{.Description}}
ConditionFileIsExecutable={{.Path}}
After=network.target
[Service]
ExecStart={{.Path}} {{.Args}}
# respawn process on crash after a 3s wait
# if fails to start 5 times within 5 minutes, stop trying
Restart=on-failure
RestartSec=3s
StartLimitInterval=300
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
`