Merge branch '18947-githttpd'
[arvados.git] / lib / crunchrun / cgroup.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package crunchrun
6
7 import (
8         "bytes"
9         "fmt"
10         "io/ioutil"
11 )
12
13 // Return the current process's cgroup for the given subsystem.
14 func findCgroup(subsystem string) (string, error) {
15         subsys := []byte(subsystem)
16         cgroups, err := ioutil.ReadFile("/proc/self/cgroup")
17         if err != nil {
18                 return "", err
19         }
20         for _, line := range bytes.Split(cgroups, []byte("\n")) {
21                 toks := bytes.SplitN(line, []byte(":"), 4)
22                 if len(toks) < 3 {
23                         continue
24                 }
25                 for _, s := range bytes.Split(toks[1], []byte(",")) {
26                         if bytes.Compare(s, subsys) == 0 {
27                                 return string(toks[2]), nil
28                         }
29                 }
30         }
31         return "", fmt.Errorf("subsystem %q not found in /proc/self/cgroup", subsystem)
32 }