Skip to content

Commit 12a7c8f

Browse files
author
Mrunal Patel
authored
Merge pull request #2411 from kolyshkin/v1-specific
libct/cgroups/utils: fix/separate cgroupv1 code
2 parents 82d2fa4 + 8c5a19f commit 12a7c8f

File tree

8 files changed

+350
-318
lines changed

8 files changed

+350
-318
lines changed

libcontainer/cgroups/cgroups.go

-24
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package cgroups
44

55
import (
6-
"fmt"
7-
86
"github.com/opencontainers/runc/libcontainer/configs"
97
)
108

@@ -51,25 +49,3 @@ type Manager interface {
5149
// Whether the cgroup path exists or not
5250
Exists() bool
5351
}
54-
55-
type NotFoundError struct {
56-
Subsystem string
57-
}
58-
59-
func (e *NotFoundError) Error() string {
60-
return fmt.Sprintf("mountpoint for %s not found", e.Subsystem)
61-
}
62-
63-
func NewNotFoundError(sub string) error {
64-
return &NotFoundError{
65-
Subsystem: sub,
66-
}
67-
}
68-
69-
func IsNotFound(err error) bool {
70-
if err == nil {
71-
return false
72-
}
73-
_, ok := err.(*NotFoundError)
74-
return ok
75-
}

libcontainer/cgroups/fs/apply_raw.go libcontainer/cgroups/fs/fs.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
package fs
44

55
import (
6+
"bufio"
67
"fmt"
78
"os"
89
"path/filepath"
10+
"strings"
911
"sync"
1012

1113
"github.com/opencontainers/runc/libcontainer/cgroups"
@@ -88,10 +90,43 @@ func getCgroupRoot() (string, error) {
8890
return cgroupRoot, nil
8991
}
9092

91-
root, err := cgroups.FindCgroupMountpointDir()
93+
f, err := os.Open("/proc/self/mountinfo")
9294
if err != nil {
9395
return "", err
9496
}
97+
defer f.Close()
98+
99+
var root string
100+
scanner := bufio.NewScanner(f)
101+
for scanner.Scan() {
102+
text := scanner.Text()
103+
fields := strings.Split(text, " ")
104+
// Safe as mountinfo encodes mountpoints with spaces as \040.
105+
index := strings.Index(text, " - ")
106+
postSeparatorFields := strings.Fields(text[index+3:])
107+
numPostFields := len(postSeparatorFields)
108+
109+
// This is an error as we can't detect if the mount is for "cgroup"
110+
if numPostFields == 0 {
111+
return "", fmt.Errorf("mountinfo: found no fields post '-' in %q", text)
112+
}
113+
114+
if postSeparatorFields[0] == "cgroup" {
115+
// Check that the mount is properly formatted.
116+
if numPostFields < 3 {
117+
return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
118+
}
119+
120+
root = filepath.Dir(fields[4])
121+
break
122+
}
123+
}
124+
if err := scanner.Err(); err != nil {
125+
return "", err
126+
}
127+
if root == "" {
128+
return "", errors.New("no cgroup mount found in mountinfo")
129+
}
95130

96131
if _, err := os.Stat(root); err != nil {
97132
return "", err
File renamed without changes.

0 commit comments

Comments
 (0)