...
1 package manager
2
3 import (
4 "errors"
5 "fmt"
6 "path/filepath"
7
8 "github.com/opencontainers/runc/libcontainer/cgroups"
9 "github.com/opencontainers/runc/libcontainer/cgroups/fs"
10 "github.com/opencontainers/runc/libcontainer/cgroups/fs2"
11 "github.com/opencontainers/runc/libcontainer/cgroups/systemd"
12 "github.com/opencontainers/runc/libcontainer/configs"
13 )
14
15
16
17
18 func New(config *configs.Cgroup) (cgroups.Manager, error) {
19 return NewWithPaths(config, nil)
20 }
21
22
23
24
25
26
27
28
29
30 func NewWithPaths(config *configs.Cgroup, paths map[string]string) (cgroups.Manager, error) {
31 if config == nil {
32 return nil, errors.New("cgroups/manager.New: config must not be nil")
33 }
34 if config.Systemd && !systemd.IsRunningSystemd() {
35 return nil, errors.New("systemd not running on this host, cannot use systemd cgroups manager")
36 }
37
38
39 if cgroups.IsCgroup2UnifiedMode() {
40 path, err := getUnifiedPath(paths)
41 if err != nil {
42 return nil, fmt.Errorf("manager.NewWithPaths: inconsistent paths: %w", err)
43 }
44 if config.Systemd {
45 return systemd.NewUnifiedManager(config, path)
46 }
47 return fs2.NewManager(config, path)
48 }
49
50
51 if config.Systemd {
52 return systemd.NewLegacyManager(config, paths)
53 }
54
55 return fs.NewManager(config, paths)
56 }
57
58
59
60
61
62
63
64
65 func getUnifiedPath(paths map[string]string) (string, error) {
66 if len(paths) > 1 {
67 return "", fmt.Errorf("expected a single path, got %+v", paths)
68 }
69 path := paths[""]
70
71 if path != "" {
72 if filepath.Clean(path) != path || !filepath.IsAbs(path) {
73 return "", fmt.Errorf("invalid path: %q", path)
74 }
75 }
76
77 return path, nil
78 }
79
View as plain text