| 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package cgroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/ |
| 25 | |
| 26 | import ( |
| 27 | "bufioscip-go gomod github.com/golang/go/src go1.18 bufio/" |
| 28 | "osscip-go gomod github.com/golang/go/src go1.18 os/" |
| 29 | "path/filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/" |
| 30 | "strconvscip-go gomod github.com/golang/go/src go1.18 strconv/" |
| 31 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | _mountInfoSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoSep. = " " |
| 36 | _mountInfoOptsSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoOptsSep. = "," |
| 37 | _mountInfoOptionalFieldsSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoOptionalFieldsSep. = "-" |
| 38 | ) |
| 39 | |
| 40 | const ( |
| 41 | _miFieldIDMountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDMountID. = iota |
| 42 | _miFieldIDParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDParentID. |
| 43 | _miFieldIDDeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDDeviceID. |
| 44 | _miFieldIDRootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDRoot. |
| 45 | _miFieldIDMountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDMountPoint. |
| 46 | _miFieldIDOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDOptions. |
| 47 | _miFieldIDOptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDOptionalFields. |
| 48 | |
| 49 | _miFieldCountFirstHalfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountFirstHalf. |
| 50 | ) |
| 51 | |
| 52 | const ( |
| 53 | _miFieldOffsetFSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldOffsetFSType. = iota |
| 54 | _miFieldOffsetMountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldOffsetMountSource. |
| 55 | _miFieldOffsetSuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldOffsetSuperOptions. |
| 56 | |
| 57 | _miFieldCountSecondHalfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountSecondHalf. |
| 58 | ) |
| 59 | |
| 60 | const _miFieldCountMinscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountMin. = _miFieldCountFirstHalfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountFirstHalf. + _miFieldCountSecondHalfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountSecondHalf. |
| 61 | |
| 62 | // MountPoint is the data structure for the mount points in |
| 63 | // `/proc/$PID/mountinfo`. See also proc(5) for more information. |
| 64 | type MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint# struct { |
| 65 | MountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountID. int |
| 66 | ParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#ParentID. int |
| 67 | DeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#DeviceID. string |
| 68 | Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root. string |
| 69 | MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint. string |
| 70 | Optionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Options. []string |
| 71 | OptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#OptionalFields. []string |
| 72 | FSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#FSType. string |
| 73 | MountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountSource. string |
| 74 | SuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#SuperOptions. []string |
| 75 | } |
| 76 | |
| 77 | // NewMountPointFromLine parses a line read from `/proc/$PID/mountinfo` and |
| 78 | // returns a new *MountPoint. |
| 79 | func NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(line string) (*MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#, error) { |
| 80 | fields := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(line, _mountInfoSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoSep.) |
| 81 | |
| 82 | if len(fields) < _miFieldCountMinscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountMin. { |
| 83 | return nil, mountPointFormatInvalidErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/mountPointFormatInvalidError#{line} |
| 84 | } |
| 85 | |
| 86 | mountID, err := strconvscip-go gomod github.com/golang/go/src go1.18 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.18 strconv/Atoi().(fields[_miFieldIDMountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDMountID.]) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | |
| 91 | parentID, err := strconvscip-go gomod github.com/golang/go/src go1.18 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.18 strconv/Atoi().(fields[_miFieldIDParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDParentID.]) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | for i, field := range fields[_miFieldIDOptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDOptionalFields.:] { |
| 97 | if field == _mountInfoOptionalFieldsSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoOptionalFieldsSep. { |
| 98 | // End of optional fields. |
| 99 | fsTypeStart := _miFieldIDOptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDOptionalFields. + i + 1 |
| 100 | |
| 101 | // Now we know where the optional fields end, split the line again with a |
| 102 | // limit to avoid issues with spaces in super options as present on WSL. |
| 103 | fields = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.SplitNscip-go gomod github.com/golang/go/src go1.18 strings/SplitN().(line, _mountInfoSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoSep., fsTypeStart+_miFieldCountSecondHalfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountSecondHalf.) |
| 104 | if len(fields) != fsTypeStart+_miFieldCountSecondHalfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldCountSecondHalf. { |
| 105 | return nil, mountPointFormatInvalidErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/mountPointFormatInvalidError#{line} |
| 106 | } |
| 107 | |
| 108 | miFieldIDFSType := _miFieldOffsetFSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldOffsetFSType. + fsTypeStart |
| 109 | miFieldIDMountSource := _miFieldOffsetMountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldOffsetMountSource. + fsTypeStart |
| 110 | miFieldIDSuperOptions := _miFieldOffsetSuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldOffsetSuperOptions. + fsTypeStart |
| 111 | |
| 112 | return &MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#{ |
| 113 | MountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountID.: mountID, |
| 114 | ParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#ParentID.: parentID, |
| 115 | DeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#DeviceID.: fields[_miFieldIDDeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDDeviceID.], |
| 116 | Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root.: fields[_miFieldIDRootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDRoot.], |
| 117 | MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint.: fields[_miFieldIDMountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDMountPoint.], |
| 118 | Optionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Options.: stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(fields[_miFieldIDOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDOptions.], _mountInfoOptsSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoOptsSep.), |
| 119 | OptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#OptionalFields.: fields[_miFieldIDOptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_miFieldIDOptionalFields.:(fsTypeStart - 1)], |
| 120 | FSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#FSType.: fields[miFieldIDFSType], |
| 121 | MountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountSource.: fields[miFieldIDMountSource], |
| 122 | SuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#SuperOptions.: stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(fields[miFieldIDSuperOptions], _mountInfoOptsSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_mountInfoOptsSep.), |
| 123 | }, nil |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return nil, mountPointFormatInvalidErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/mountPointFormatInvalidError#{line} |
| 128 | } |
| 129 | |
| 130 | // Translate converts an absolute path inside the *MountPoint's file system to |
| 131 | // the host file system path in the mount namespace the *MountPoint belongs to. |
| 132 | func (mp *MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#) Translatescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Translate().(absPath string) (string, error) { |
| 133 | relPath, err := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Rel().(mp.Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root., absPath) |
| 134 | |
| 135 | if err != nil { |
| 136 | return "", err |
| 137 | } |
| 138 | if relPath == ".." || stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(relPath, "../") { |
| 139 | return "", pathNotExposedFromMountPointErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#{ |
| 140 | mountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#mountPoint.: mp.MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint., |
| 141 | rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#root.: mp.Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root., |
| 142 | pathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#path.: absPath, |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(mp.MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint., relPath), nil |
| 147 | } |
| 148 | |
| 149 | // parseMountInfo parses procPathMountInfo (usually at `/proc/$PID/mountinfo`) |
| 150 | // and yields parsed *MountPoint into newMountPoint. |
| 151 | func parseMountInfoscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/parseMountInfo().(procPathMountInfo string, newMountPoint func(*MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#) error) error { |
| 152 | mountInfoFile, err := osscip-go gomod github.com/golang/go/src go1.18 os/.Openscip-go gomod github.com/golang/go/src go1.18 os/Open().(procPathMountInfo) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | defer mountInfoFile.Closescip-go gomod github.com/golang/go/src go1.18 os/File#Close().() |
| 157 | |
| 158 | scanner := bufioscip-go gomod github.com/golang/go/src go1.18 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.18 bufio/NewScanner().(mountInfoFile) |
| 159 | |
| 160 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.18 bufio/Scanner#Scan().() { |
| 161 | mountPoint, err := NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(scanner.Textscip-go gomod github.com/golang/go/src go1.18 bufio/Scanner#Text().()) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | if err := newMountPoint(mountPoint); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return scanner.Errscip-go gomod github.com/golang/go/src go1.18 bufio/Scanner#Err().() |
| 171 | } |