This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
cgroup_util.go
183 lines (148 loc) · 3.6 KB
/
cgroup_util.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
// Copyright 2014 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//+build linux
package common
// adapted from systemd/src/shared/cgroup-util.c
// TODO this should be moved to go-systemd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
unitNameMax = 256
)
var (
validChars = regexp.MustCompile(`[a-zA-Z0-9:\-_\.\\]+`)
)
// cgEscape implements very minimal escaping for names to be used as file names
// in the cgroup tree: any name which might conflict with a kernel name or is
// prefixed with '_' is prefixed with a '_'. That way, when reading cgroup
// names it is sufficient to remove a single prefixing underscore if there is
// one.
func cgEscape(p string) string {
needPrefix := false
switch {
case strings.HasPrefix(p, "_"):
fallthrough
case strings.HasPrefix(p, "."):
fallthrough
case p == "notify_on_release":
fallthrough
case p == "release_agent":
fallthrough
case p == "tasks":
needPrefix = true
case strings.Contains(p, "."):
sp := strings.Split(p, ".")
if sp[0] == "cgroup" {
needPrefix = true
} else {
n := sp[0]
if checkHierarchy(n) {
needPrefix = true
}
}
}
if needPrefix {
return "_" + p
}
return p
}
func filenameIsValid(p string) bool {
switch {
case p == "", p == ".", p == "..", strings.Contains(p, "/"):
return false
default:
return true
}
}
func checkHierarchy(p string) bool {
if !filenameIsValid(p) {
return true
}
cc := filepath.Join("/sys/fs/cgroup", p)
if _, err := os.Stat(cc); os.IsNotExist(err) {
return false
}
return true
}
func cgUnescape(p string) string {
if p[0] == '_' {
return p[1:]
}
return p
}
func sliceNameIsValid(n string) bool {
if n == "" {
return false
}
if len(n) >= unitNameMax {
return false
}
if !strings.Contains(n, ".") {
return false
}
if validChars.FindString(n) != n {
return false
}
if strings.Contains(n, "@") {
return false
}
return true
}
// SliceToPath explodes a slice name to its corresponding path in the cgroup
// hierarchy. For example, a slice named "foo-bar-baz.slice" corresponds to the
// path "foo.slice/foo-bar.slice/foo-bar-baz.slice". See systemd.slice(5)
func SliceToPath(unit string) (string, error) {
if unit == "-.slice" {
return "", nil
}
if !strings.HasSuffix(unit, ".slice") {
return "", fmt.Errorf("not a slice")
}
if !sliceNameIsValid(unit) {
return "", fmt.Errorf("invalid slice name")
}
prefix := unitnameToPrefix(unit)
// don't allow initial dashes
if prefix[0] == '-' {
return "", fmt.Errorf("initial dash")
}
prefixParts := strings.Split(prefix, "-")
var curSlice string
var slicePath string
for _, slicePart := range prefixParts {
if slicePart == "" {
return "", fmt.Errorf("trailing or double dash")
}
if curSlice != "" {
curSlice = curSlice + "-"
}
curSlice = curSlice + slicePart
curSliceDir := curSlice + ".slice"
escaped := cgEscape(curSliceDir)
slicePath = filepath.Join(slicePath, escaped)
}
return slicePath, nil
}
func unitnameToPrefix(unit string) string {
idx := strings.Index(unit, "@")
if idx == -1 {
idx = strings.LastIndex(unit, ".")
}
return unit[:idx]
}