| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Keep the original Uber license. |
| 5 |
|
| 6 |
// Copyright (c) 2017 Uber Technologies, Inc. |
| 7 |
// |
| 8 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
// of this software and associated documentation files (the "Software"), to deal |
| 10 |
// in the Software without restriction, including without limitation the rights |
| 11 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
// copies of the Software, and to permit persons to whom the Software is |
| 13 |
// furnished to do so, subject to the following conditions: |
| 14 |
// |
| 15 |
// The above copyright notice and this permission notice shall be included in |
| 16 |
// all copies or substantial portions of the Software. |
| 17 |
// |
| 18 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
// THE SOFTWARE. |
| 25 |
|
| 26 |
//go:build linux |
| 27 |
|
| 28 |
package cgroups // import "go.opentelemetry.io/collector/internal/memorylimiter/cgroups" |
| 29 |
|
| 30 |
import ( |
| 31 |
"bufio" |
| 32 |
"os" |
| 33 |
"path/filepath" |
| 34 |
"strconv" |
| 35 |
"strings" |
| 36 |
) |
| 37 |
|
| 38 |
const ( |
| 39 |
_mountInfoSep = " " |
| 40 |
_mountInfoOptsSep = "," |
| 41 |
_mountInfoOptionalFieldsSep = "-" |
| 42 |
) |
| 43 |
|
| 44 |
const ( |
| 45 |
_miFieldIDMountID = iota |
| 46 |
_miFieldIDParentID |
| 47 |
_miFieldIDDeviceID |
| 48 |
_miFieldIDRoot |
| 49 |
_miFieldIDMountPoint |
| 50 |
_miFieldIDOptions |
| 51 |
_miFieldIDOptionalFields |
| 52 |
|
| 53 |
_miFieldCountFirstHalf |
| 54 |
) |
| 55 |
|
| 56 |
const ( |
| 57 |
_miFieldOffsetFSType = iota |
| 58 |
_miFieldOffsetMountSource |
| 59 |
_miFieldOffsetSuperOptions |
| 60 |
|
| 61 |
_miFieldCountSecondHalf |
| 62 |
) |
| 63 |
|
| 64 |
const _miFieldCountMin = _miFieldCountFirstHalf + _miFieldCountSecondHalf |
| 65 |
|
| 66 |
// MountPoint is the data structure for the mount points in |
| 67 |
// `/proc/$PID/mountinfo`. See also proc(5) for more information. |
| 68 |
type MountPoint struct { |
| 69 |
MountID int |
| 70 |
ParentID int |
| 71 |
DeviceID string |
| 72 |
Root string |
| 73 |
MountPoint string |
| 74 |
Options []string |
| 75 |
OptionalFields []string |
| 76 |
FSType string |
| 77 |
MountSource string |
| 78 |
SuperOptions []string |
| 79 |
} |
| 80 |
|
| 81 |
// NewMountPointFromLine parses a line read from `/proc/$PID/mountinfo` and |
| 82 |
// returns a new *MountPoint. |
| 83 |
func NewMountPointFromLine(line string) (*MountPoint, error) { |
| 84 |
fields := strings.Split(line, _mountInfoSep) |
| 85 |
|
| 86 |
if len(fields) < _miFieldCountMin { |
| 87 |
return nil, mountPointFormatInvalidError{line} |
| 88 |
} |
| 89 |
|
| 90 |
mountID, err := strconv.Atoi(fields[_miFieldIDMountID]) |
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
|
| 95 |
parentID, err := strconv.Atoi(fields[_miFieldIDParentID]) |
| 96 |
if err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
|
| 100 |
for i, field := range fields[_miFieldIDOptionalFields:] { |
| 101 |
if field != _mountInfoOptionalFieldsSep { |
| 102 |
continue |
| 103 |
} |
| 104 |
fsTypeStart := _miFieldIDOptionalFields + i + 1 |
| 105 |
|
| 106 |
if len(fields) != fsTypeStart+_miFieldCountSecondHalf { |
| 107 |
return nil, mountPointFormatInvalidError{line} |
| 108 |
} |
| 109 |
|
| 110 |
miFieldIDFSType := _miFieldOffsetFSType + fsTypeStart |
| 111 |
miFieldIDMountSource := _miFieldOffsetMountSource + fsTypeStart |
| 112 |
miFieldIDSuperOptions := _miFieldOffsetSuperOptions + fsTypeStart |
| 113 |
|
| 114 |
return &MountPoint{ |
| 115 |
MountID: mountID, |
| 116 |
ParentID: parentID, |
| 117 |
DeviceID: fields[_miFieldIDDeviceID], |
| 118 |
Root: fields[_miFieldIDRoot], |
| 119 |
MountPoint: fields[_miFieldIDMountPoint], |
| 120 |
Options: strings.Split(fields[_miFieldIDOptions], _mountInfoOptsSep), |
| 121 |
OptionalFields: fields[_miFieldIDOptionalFields:(fsTypeStart - 1)], |
| 122 |
FSType: fields[miFieldIDFSType], |
| 123 |
MountSource: fields[miFieldIDMountSource], |
| 124 |
SuperOptions: strings.Split(fields[miFieldIDSuperOptions], _mountInfoOptsSep), |
| 125 |
}, nil |
| 126 |
} |
| 127 |
|
| 128 |
return nil, mountPointFormatInvalidError{line} |
| 129 |
} |
| 130 |
|
| 131 |
// Translate converts an absolute path inside the *MountPoint's file system to |
| 132 |
// the host file system path in the mount namespace the *MountPoint belongs to. |
| 133 |
func (mp *MountPoint) Translate(absPath string) (string, error) { |
| 134 |
relPath, err := filepath.Rel(mp.Root, absPath) |
| 135 |
if err != nil { |
| 136 |
return "", err |
| 137 |
} |
| 138 |
if relPath == ".." || strings.HasPrefix(relPath, "../") { |
| 139 |
return "", pathNotExposedFromMountPointError{ |
| 140 |
mountPoint: mp.MountPoint, |
| 141 |
root: mp.Root, |
| 142 |
path: absPath, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return filepath.Join(mp.MountPoint, relPath), nil |
| 147 |
} |
| 148 |
|
| 149 |
// parseMountInfo parses procPathMountInfo (usually at `/proc/$PID/mountinfo`) |
| 150 |
// and yields parsed *MountPoint into newMountPoint. |
| 151 |
func parseMountInfo(procPathMountInfo string, newMountPoint func(*MountPoint) error) error { |
| 152 |
mountInfoFile, err := os.Open(filepath.Clean(procPathMountInfo)) |
| 153 |
if err != nil { |
| 154 |
return err |
| 155 |
} |
| 156 |
defer mountInfoFile.Close() |
| 157 |
|
| 158 |
scanner := bufio.NewScanner(mountInfoFile) |
| 159 |
|
| 160 |
for scanner.Scan() { |
| 161 |
mountPoint, err := NewMountPointFromLine(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.Err() |
| 171 |
} |