-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (76 loc) · 1.93 KB
/
main.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
// Program units prints systemd units
// to show how the package can be configured if needed.
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/marselester/systemd"
)
func main() {
// By default an exit code is set to indicate a failure
// since there are more failure scenarios to begin with.
exitCode := 1
defer func() { os.Exit(exitCode) }()
addr := flag.String("addr", "", "bus address")
onlyServices := flag.Bool("svc", false, "show only services")
checkSerial := flag.Bool("serial", false, "check message serial")
timeout := flag.Duration("timeout", time.Second, "connection read/write timeout")
flag.Parse()
opts := []systemd.Option{
systemd.WithTimeout(*timeout),
}
if *checkSerial {
opts = append(opts, systemd.WithSerialCheck())
}
if *addr != "" {
opts = append(opts, systemd.WithAddress(*addr))
}
c, err := systemd.New(opts...)
if err != nil {
log.Print(err)
return
}
defer func() {
if err = c.Close(); err != nil {
log.Print(err)
}
}()
if *onlyServices {
err = printServices(c)
} else {
err = c.ListUnits(nil, printAll)
}
if err != nil {
log.Print(err)
return
}
// The program terminates successfully.
exitCode = 0
}
func printAll(u *systemd.Unit) {
fmt.Printf("%s %s\n", u.Name, u.ActiveState)
}
// printServices prints service names along with their PIDs.
// It ignores non-service units.
func printServices(c *systemd.Client) error {
var services []systemd.Unit
err := c.ListUnits(systemd.IsService, func(u *systemd.Unit) {
// Must copy a unit,
// otherwise it will be modified on the next function call.
services = append(services, *u)
})
if err != nil {
return fmt.Errorf("failed to get systemd units: %w", err)
}
var pid uint32
for _, s := range services {
if pid, err = c.MainPID(s.Name); err != nil {
return fmt.Errorf("failed to get PID for service %q: %w", s.Name, err)
}
fmt.Printf("%d %s %s\n", pid, s.Name, s.ActiveState)
}
return nil
}