-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathsystemd.go
464 lines (382 loc) · 11.5 KB
/
systemd.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package systemd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
)
var (
// the output of "show" must match this for Stop to be done:
isStopDone = regexp.MustCompile(`(?m)\AActiveState=(?:failed|inactive)$`).Match
// how much time should Stop wait between calls to show
stopCheckDelay = 250 * time.Millisecond
// how much time should Stop wait between notifying the user of the waiting
stopNotifyDelay = 20 * time.Second
)
// run calls systemctl with the given args, returning its standard output (and wrapped error)
func run(args ...string) ([]byte, error) {
bs, err := exec.Command("systemctl", args...).CombinedOutput()
if err != nil {
exitCode, _ := osutil.ExitCode(err)
return nil, &Error{cmd: args, exitCode: exitCode, msg: bs}
}
return bs, nil
}
// SystemctlCmd is called from the commands to actually call out to
// systemctl. It's exported so it can be overridden by testing.
var SystemctlCmd = run
// jctl calls journalctl to get the JSON logs of the given services, wrapping the error if any.
func jctl(svcs []string) ([]byte, error) {
cmd := []string{"journalctl", "-o", "json"}
for i := range svcs {
cmd = append(cmd, "-u", svcs[i])
}
bs, err := exec.Command(cmd[0], cmd[1:]...).Output() // journalctl can be messy with its stderr
if err != nil {
exitCode, _ := osutil.ExitCode(err)
return nil, &Error{cmd: cmd, exitCode: exitCode, msg: bs}
}
return bs, nil
}
// JournalctlCmd is called from Logs to run journalctl; exported for testing.
var JournalctlCmd = jctl
// Systemd exposes a minimal interface to manage systemd via the systemctl command.
type Systemd interface {
DaemonReload() error
Enable(service string) error
Disable(service string) error
Start(service string) error
Stop(service string, timeout time.Duration) error
Kill(service, signal string) error
Restart(service string, timeout time.Duration) error
Status(service string) (string, error)
ServiceStatus(service string) (*ServiceStatus, error)
Logs(services []string) ([]Log, error)
WriteMountUnitFile(name, what, where, fstype string) (string, error)
}
// A Log is a single entry in the systemd journal
type Log map[string]interface{}
// RestartCondition encapsulates the different systemd 'restart' options
type RestartCondition string
// These are the supported restart conditions
const (
RestartNever RestartCondition = "never"
RestartOnSuccess RestartCondition = "on-success"
RestartOnFailure RestartCondition = "on-failure"
RestartOnAbnormal RestartCondition = "on-abnormal"
RestartOnAbort RestartCondition = "on-abort"
RestartAlways RestartCondition = "always"
)
var RestartMap = map[string]RestartCondition{
"never": RestartNever,
"on-success": RestartOnSuccess,
"on-failure": RestartOnFailure,
"on-abnormal": RestartOnAbnormal,
"on-abort": RestartOnAbort,
"always": RestartAlways,
}
// ErrUnknownRestartCondition is returned when trying to unmarshal an unknown restart condition
var ErrUnknownRestartCondition = errors.New("invalid restart condition")
func (rc RestartCondition) String() string {
return string(rc)
}
// UnmarshalYAML so RestartCondition implements yaml's Unmarshaler interface
func (rc *RestartCondition) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v string
if err := unmarshal(&v); err != nil {
return err
}
nrc, ok := RestartMap[v]
if !ok {
return ErrUnknownRestartCondition
}
*rc = nrc
return nil
}
const (
// the default target for systemd units that we generate
ServicesTarget = "multi-user.target"
// the default target for systemd units that we generate
SocketsTarget = "sockets.target"
// the location to put system services
snapServicesDir = "/etc/systemd/system"
)
type reporter interface {
Notify(string)
}
// New returns a Systemd that uses the given rootDir
func New(rootDir string, rep reporter) Systemd {
return &systemd{rootDir: rootDir, reporter: rep}
}
type systemd struct {
rootDir string
reporter reporter
}
// DaemonReload reloads systemd's configuration.
func (*systemd) DaemonReload() error {
_, err := SystemctlCmd("daemon-reload")
return err
}
// Enable the given service
func (s *systemd) Enable(serviceName string) error {
_, err := SystemctlCmd("--root", s.rootDir, "enable", serviceName)
return err
}
// Disable the given service
func (s *systemd) Disable(serviceName string) error {
_, err := SystemctlCmd("--root", s.rootDir, "disable", serviceName)
return err
}
// Start the given service
func (*systemd) Start(serviceName string) error {
_, err := SystemctlCmd("start", serviceName)
return err
}
// Logs for the given service
func (*systemd) Logs(serviceNames []string) ([]Log, error) {
bs, err := JournalctlCmd(serviceNames)
if err != nil {
return nil, err
}
const noEntries = "-- No entries --\n"
if len(bs) == len(noEntries) && string(bs) == noEntries {
return nil, nil
}
var logs []Log
dec := json.NewDecoder(bytes.NewReader(bs))
for {
var log Log
err = dec.Decode(&log)
if err != nil {
break
}
logs = append(logs, log)
}
if err != io.EOF {
return nil, err
}
return logs, nil
}
var statusregex = regexp.MustCompile(`(?m)^(?:(.*?)=(.*))?$`)
func (s *systemd) Status(serviceName string) (string, error) {
status, err := s.ServiceStatus(serviceName)
if err != nil {
return "", err
}
return fmt.Sprintf("%s; %s; %s (%s)", status.UnitFileState, status.LoadState, status.ActiveState, status.SubState), nil
}
// A ServiceStatus holds structured service status information.
type ServiceStatus struct {
ServiceFileName string `json:"service-file-name"`
LoadState string `json:"load-state"`
ActiveState string `json:"active-state"`
SubState string `json:"sub-state"`
UnitFileState string `json:"unit-file-state"`
}
func (s *systemd) ServiceStatus(serviceName string) (*ServiceStatus, error) {
bs, err := SystemctlCmd("show", "--property=Id,LoadState,ActiveState,SubState,UnitFileState", serviceName)
if err != nil {
return nil, err
}
status := &ServiceStatus{ServiceFileName: serviceName}
for _, bs := range statusregex.FindAllSubmatch(bs, -1) {
if len(bs[0]) > 0 {
k := string(bs[1])
v := string(bs[2])
switch k {
case "LoadState":
status.LoadState = v
case "ActiveState":
status.ActiveState = v
case "SubState":
status.SubState = v
case "UnitFileState":
status.UnitFileState = v
}
}
}
return status, nil
}
// Stop the given service, and wait until it has stopped.
func (s *systemd) Stop(serviceName string, timeout time.Duration) error {
if _, err := SystemctlCmd("stop", serviceName); err != nil {
return err
}
// and now wait for it to actually stop
giveup := time.NewTimer(timeout)
notify := time.NewTicker(stopNotifyDelay)
defer notify.Stop()
check := time.NewTicker(stopCheckDelay)
defer check.Stop()
firstCheck := true
loop:
for {
select {
case <-giveup.C:
break loop
case <-check.C:
bs, err := SystemctlCmd("show", "--property=ActiveState", serviceName)
if err != nil {
return err
}
if isStopDone(bs) {
return nil
}
if !firstCheck {
continue loop
}
firstCheck = false
case <-notify.C:
}
// after notify delay or after a failed first check
s.reporter.Notify(fmt.Sprintf("Waiting for %s to stop.", serviceName))
}
return &Timeout{action: "stop", service: serviceName}
}
// Kill all processes of the unit with the given signal
func (s *systemd) Kill(serviceName, signal string) error {
_, err := SystemctlCmd("kill", serviceName, "-s", signal)
return err
}
// Restart the service, waiting for it to stop before starting it again.
func (s *systemd) Restart(serviceName string, timeout time.Duration) error {
if err := s.Stop(serviceName, timeout); err != nil {
return err
}
return s.Start(serviceName)
}
// Error is returned if the systemd action failed
type Error struct {
cmd []string
msg []byte
exitCode int
}
func (e *Error) Error() string {
return fmt.Sprintf("%v failed with exit status %d: %s", e.cmd, e.exitCode, e.msg)
}
// Timeout is returned if the systemd action failed to reach the
// expected state in a reasonable amount of time
type Timeout struct {
action string
service string
}
func (e *Timeout) Error() string {
return fmt.Sprintf("%v failed to %v: timeout", e.service, e.action)
}
// IsTimeout checks whether the given error is a Timeout
func IsTimeout(err error) bool {
_, isTimeout := err.(*Timeout)
return isTimeout
}
const myFmt = "2006-01-02T15:04:05.000000Z07:00"
// Timestamp of the Log, formatted like RFC3339 to µs precision.
//
// If no timestamp, the string "-(no timestamp!)-" -- and something is
// wrong with your system. Some other "impossible" error conditions
// also result in "-(errror message)-" timestamps.
func (l Log) Timestamp() string {
t := "-(no timestamp!)-"
if ius, ok := l["__REALTIME_TIMESTAMP"]; ok {
// according to systemd.journal-fields(7) it's microseconds as a decimal string
sus, ok := ius.(string)
if ok {
if us, err := strconv.ParseInt(sus, 10, 64); err == nil {
t = time.Unix(us/1000000, 1000*(us%1000000)).UTC().Format(myFmt)
} else {
t = fmt.Sprintf("-(timestamp not a decimal number: %#v)-", sus)
}
} else {
t = fmt.Sprintf("-(timestamp not a string: %#v)-", ius)
}
}
return t
}
// Message of the Log, if any; otherwise, "-".
func (l Log) Message() string {
if msg, ok := l["MESSAGE"].(string); ok {
return msg
}
return "-"
}
// SID is the syslog identifier of the Log, if any; otherwise, "-".
func (l Log) SID() string {
if sid, ok := l["SYSLOG_IDENTIFIER"].(string); ok {
return sid
}
return "-"
}
func (l Log) String() string {
return fmt.Sprintf("%s %s %s", l.Timestamp(), l.SID(), l.Message())
}
// useFuse detects if we should be using squashfuse instead
func useFuse() bool {
if !osutil.FileExists("/dev/fuse") {
return false
}
_, err := exec.LookPath("squashfuse")
if err != nil {
return false
}
out, err := exec.Command("systemd-detect-virt", "--container").Output()
if err != nil {
return false
}
virt := strings.TrimSpace(string(out))
if virt != "none" {
return true
}
return false
}
// MountUnitPath returns the path of a {,auto}mount unit
func MountUnitPath(baseDir, ext string) string {
escapedPath := EscapeUnitNamePath(baseDir)
return filepath.Join(dirs.SnapServicesDir, fmt.Sprintf("%s.%s", escapedPath, ext))
}
func (s *systemd) WriteMountUnitFile(name, what, where, fstype string) (string, error) {
extra := ""
if osutil.IsDirectory(what) {
extra = "Options=bind\n"
fstype = "none"
}
if fstype == "squashfs" && useFuse() {
fstype = "fuse.squashfuse"
}
c := fmt.Sprintf(`[Unit]
Description=Mount unit for %s
[Mount]
What=%s
Where=%s
Type=%s
%s
[Install]
WantedBy=multi-user.target
`, name, what, where, fstype, extra)
mu := MountUnitPath(where, "mount")
return filepath.Base(mu), osutil.AtomicWriteFile(mu, []byte(c), 0644, 0)
}