-
Notifications
You must be signed in to change notification settings - Fork 2
/
systemd.go
51 lines (45 loc) · 1.2 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
package main
import (
"context"
"fmt"
"os"
"time"
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
)
func createSystemdScope(name string) error {
ctx := context.TODO()
dbusConn, err := systemdDbus.NewUserConnectionContext(ctx)
if err != nil {
// silently skip if there is no user dbus session
// for now we only use the systemd unit for task grouping anyway
return nil
}
defer dbusConn.Close()
statusChan := make(chan string, 1)
pid := os.Getpid()
properties := []systemdDbus.Property{
systemdDbus.PropPids(uint32(pid)),
}
var scopeName string
if name == "" {
scopeName = fmt.Sprintf("runjail-%d.scope", pid)
} else {
scopeName = fmt.Sprintf("runjail-%s-%d.scope", name, pid)
}
_, err = dbusConn.StartTransientUnitContext(ctx, scopeName, "fail", properties, statusChan)
if err != nil {
return fmt.Errorf("failed to start transient unit: %w", err)
}
timeout := time.NewTimer(5 * time.Second)
defer timeout.Stop()
select {
case status := <-statusChan:
close(statusChan)
if status != "done" {
return fmt.Errorf("failed to start transient unit: %w", err)
}
case <-timeout.C:
return fmt.Errorf("timeout waiting for systemd to create a transient unit")
}
return nil
}