Skip to content

Commit b444813

Browse files
authored
Merge pull request containerd#151 from kolyshkin/mountinfo
bufio.Scanner.Err nits; mountinfo parser fixes
2 parents 6c3dec4 + 77aed01 commit b444813

7 files changed

Lines changed: 41 additions & 41 deletions

File tree

blkio.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ func (b *blkioController) readEntry(devices map[deviceKey]string, path, name str
181181
defer f.Close()
182182
sc := bufio.NewScanner(f)
183183
for sc.Scan() {
184-
if err := sc.Err(); err != nil {
185-
return err
186-
}
187184
// format: dev type amount
188185
fields := strings.FieldsFunc(sc.Text(), splitBlkIOStatLine)
189186
if len(fields) < 3 {
@@ -220,7 +217,7 @@ func (b *blkioController) readEntry(devices map[deviceKey]string, path, name str
220217
Value: v,
221218
})
222219
}
223-
return nil
220+
return sc.Err()
224221
}
225222

226223
func createBlkioSettings(blkio *specs.LinuxBlockIO) []blkioSettings {

cpu.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ func (c *cpuController) Stat(path string, stats *v1.Metrics) error {
110110
// get or create the cpu field because cpuacct can also set values on this struct
111111
sc := bufio.NewScanner(f)
112112
for sc.Scan() {
113-
if err := sc.Err(); err != nil {
114-
return err
115-
}
116113
key, v, err := parseKV(sc.Text())
117114
if err != nil {
118115
return err
@@ -126,5 +123,5 @@ func (c *cpuController) Stat(path string, stats *v1.Metrics) error {
126123
stats.CPU.Throttling.ThrottledTime = v
127124
}
128125
}
129-
return nil
126+
return sc.Err()
130127
}

memory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,16 @@ func (m *memoryController) parseStats(r io.Reader, stat *v1.MemoryStat) error {
236236
line int
237237
)
238238
for sc.Scan() {
239-
if err := sc.Err(); err != nil {
240-
return err
241-
}
242239
key, v, err := parseKV(sc.Text())
243240
if err != nil {
244241
return fmt.Errorf("%d: %v", line, err)
245242
}
246243
raw[key] = v
247244
line++
248245
}
246+
if err := sc.Err(); err != nil {
247+
return err
248+
}
249249
stat.Cache = raw["cache"]
250250
stat.RSS = raw["rss"]
251251
stat.RSSHuge = raw["rss_huge"]

utils.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ func readPids(path string, subsystem Name) ([]Process, error) {
181181
})
182182
}
183183
}
184+
if err := s.Err(); err != nil {
185+
// failed to read all pids?
186+
return nil, err
187+
}
184188
return out, nil
185189
}
186190

@@ -208,6 +212,9 @@ func readTasksPids(path string, subsystem Name) ([]Task, error) {
208212
})
209213
}
210214
}
215+
if err := s.Err(); err != nil {
216+
return nil, err
217+
}
211218
return out, nil
212219
}
213220

@@ -286,9 +293,6 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
286293
s = bufio.NewScanner(r)
287294
)
288295
for s.Scan() {
289-
if err := s.Err(); err != nil {
290-
return nil, err
291-
}
292296
var (
293297
text = s.Text()
294298
parts = strings.SplitN(text, ":", 3)
@@ -302,6 +306,9 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
302306
}
303307
}
304308
}
309+
if err := s.Err(); err != nil {
310+
return nil, err
311+
}
305312
return cgroups, nil
306313
}
307314

@@ -313,16 +320,23 @@ func getCgroupDestination(subsystem string) (string, error) {
313320
defer f.Close()
314321
s := bufio.NewScanner(f)
315322
for s.Scan() {
316-
if err := s.Err(); err != nil {
317-
return "", err
323+
fields := strings.Split(s.Text(), " ")
324+
if len(fields) < 10 {
325+
// broken mountinfo?
326+
continue
327+
}
328+
if fields[len(fields)-3] != "cgroup" {
329+
continue
318330
}
319-
fields := strings.Fields(s.Text())
320331
for _, opt := range strings.Split(fields[len(fields)-1], ",") {
321332
if opt == subsystem {
322333
return fields[3], nil
323334
}
324335
}
325336
}
337+
if err := s.Err(); err != nil {
338+
return "", err
339+
}
326340
return "", ErrNoCgroupMountDestination
327341
}
328342

v1.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,20 @@ func v1MountPoint() (string, error) {
5454
defer f.Close()
5555
scanner := bufio.NewScanner(f)
5656
for scanner.Scan() {
57-
if err := scanner.Err(); err != nil {
58-
return "", err
59-
}
6057
var (
61-
text = scanner.Text()
62-
fields = strings.Split(text, " ")
63-
// safe as mountinfo encodes mountpoints with spaces as \040.
64-
index = strings.Index(text, " - ")
65-
postSeparatorFields = strings.Fields(text[index+3:])
66-
numPostFields = len(postSeparatorFields)
58+
text = scanner.Text()
59+
fields = strings.Split(text, " ")
60+
numFields = len(fields)
6761
)
68-
// this is an error as we can't detect if the mount is for "cgroup"
69-
if numPostFields == 0 {
70-
return "", fmt.Errorf("Found no fields post '-' in %q", text)
62+
if numFields < 10 {
63+
return "", fmt.Errorf("mountinfo: bad entry %q", text)
7164
}
72-
if postSeparatorFields[0] == "cgroup" {
73-
// check that the mount is properly formated.
74-
if numPostFields < 3 {
75-
return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
76-
}
65+
if fields[numFields-3] == "cgroup" {
7766
return filepath.Dir(fields[4]), nil
7867
}
7968
}
69+
if err := scanner.Err(); err != nil {
70+
return "", err
71+
}
8072
return "", ErrMountPointNotExist
8173
}

v2/manager.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,16 +496,13 @@ func readKVStatsFile(path string, file string, out map[string]interface{}) error
496496

497497
s := bufio.NewScanner(f)
498498
for s.Scan() {
499-
if err := s.Err(); err != nil {
500-
return err
501-
}
502499
name, value, err := parseKV(s.Text())
503500
if err != nil {
504501
return errors.Wrapf(err, "error while parsing %s (line=%q)", filepath.Join(path, file), s.Text())
505502
}
506503
out[name] = value
507504
}
508-
return nil
505+
return s.Err()
509506
}
510507

511508
func (c *Manager) Freeze() error {

v2/utils.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func parseCgroupProcsFile(path string) ([]uint64, error) {
8585
out = append(out, pid)
8686
}
8787
}
88+
if err := s.Err(); err != nil {
89+
return nil, err
90+
}
8891
return out, nil
8992
}
9093

@@ -144,9 +147,6 @@ func parseCgroupFromReader(r io.Reader) (string, error) {
144147
s = bufio.NewScanner(r)
145148
)
146149
for s.Scan() {
147-
if err := s.Err(); err != nil {
148-
return "", err
149-
}
150150
var (
151151
text = s.Text()
152152
parts = strings.SplitN(text, ":", 3)
@@ -159,6 +159,9 @@ func parseCgroupFromReader(r io.Reader) (string, error) {
159159
return parts[2], nil
160160
}
161161
}
162+
if err := s.Err(); err != nil {
163+
return "", err
164+
}
162165
return "", fmt.Errorf("cgroup path not found")
163166
}
164167

0 commit comments

Comments
 (0)