mountpoint.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. //go:build linux
  21. // +build linux
  22. package cgroups
  23. import (
  24. "bufio"
  25. "os"
  26. "path/filepath"
  27. "strconv"
  28. "strings"
  29. )
  30. const (
  31. _mountInfoSep = " "
  32. _mountInfoOptsSep = ","
  33. _mountInfoOptionalFieldsSep = "-"
  34. )
  35. const (
  36. _miFieldIDMountID = iota
  37. _miFieldIDParentID
  38. _miFieldIDDeviceID
  39. _miFieldIDRoot
  40. _miFieldIDMountPoint
  41. _miFieldIDOptions
  42. _miFieldIDOptionalFields
  43. _miFieldCountFirstHalf
  44. )
  45. const (
  46. _miFieldOffsetFSType = iota
  47. _miFieldOffsetMountSource
  48. _miFieldOffsetSuperOptions
  49. _miFieldCountSecondHalf
  50. )
  51. const _miFieldCountMin = _miFieldCountFirstHalf + _miFieldCountSecondHalf
  52. // MountPoint is the data structure for the mount points in
  53. // `/proc/$PID/mountinfo`. See also proc(5) for more information.
  54. type MountPoint struct {
  55. MountID int
  56. ParentID int
  57. DeviceID string
  58. Root string
  59. MountPoint string
  60. Options []string
  61. OptionalFields []string
  62. FSType string
  63. MountSource string
  64. SuperOptions []string
  65. }
  66. // NewMountPointFromLine parses a line read from `/proc/$PID/mountinfo` and
  67. // returns a new *MountPoint.
  68. func NewMountPointFromLine(line string) (*MountPoint, error) {
  69. fields := strings.Split(line, _mountInfoSep)
  70. if len(fields) < _miFieldCountMin {
  71. return nil, mountPointFormatInvalidError{line}
  72. }
  73. mountID, err := strconv.Atoi(fields[_miFieldIDMountID])
  74. if err != nil {
  75. return nil, err
  76. }
  77. parentID, err := strconv.Atoi(fields[_miFieldIDParentID])
  78. if err != nil {
  79. return nil, err
  80. }
  81. for i, field := range fields[_miFieldIDOptionalFields:] {
  82. if field == _mountInfoOptionalFieldsSep {
  83. // End of optional fields.
  84. fsTypeStart := _miFieldIDOptionalFields + i + 1
  85. // Now we know where the optional fields end, split the line again with a
  86. // limit to avoid issues with spaces in super options as present on WSL.
  87. fields = strings.SplitN(line, _mountInfoSep, fsTypeStart+_miFieldCountSecondHalf)
  88. if len(fields) != fsTypeStart+_miFieldCountSecondHalf {
  89. return nil, mountPointFormatInvalidError{line}
  90. }
  91. miFieldIDFSType := _miFieldOffsetFSType + fsTypeStart
  92. miFieldIDMountSource := _miFieldOffsetMountSource + fsTypeStart
  93. miFieldIDSuperOptions := _miFieldOffsetSuperOptions + fsTypeStart
  94. return &MountPoint{
  95. MountID: mountID,
  96. ParentID: parentID,
  97. DeviceID: fields[_miFieldIDDeviceID],
  98. Root: fields[_miFieldIDRoot],
  99. MountPoint: fields[_miFieldIDMountPoint],
  100. Options: strings.Split(fields[_miFieldIDOptions], _mountInfoOptsSep),
  101. OptionalFields: fields[_miFieldIDOptionalFields:(fsTypeStart - 1)],
  102. FSType: fields[miFieldIDFSType],
  103. MountSource: fields[miFieldIDMountSource],
  104. SuperOptions: strings.Split(fields[miFieldIDSuperOptions], _mountInfoOptsSep),
  105. }, nil
  106. }
  107. }
  108. return nil, mountPointFormatInvalidError{line}
  109. }
  110. // Translate converts an absolute path inside the *MountPoint's file system to
  111. // the host file system path in the mount namespace the *MountPoint belongs to.
  112. func (mp *MountPoint) Translate(absPath string) (string, error) {
  113. relPath, err := filepath.Rel(mp.Root, absPath)
  114. if err != nil {
  115. return "", err
  116. }
  117. if relPath == ".." || strings.HasPrefix(relPath, "../") {
  118. return "", pathNotExposedFromMountPointError{
  119. mountPoint: mp.MountPoint,
  120. root: mp.Root,
  121. path: absPath,
  122. }
  123. }
  124. return filepath.Join(mp.MountPoint, relPath), nil
  125. }
  126. // parseMountInfo parses procPathMountInfo (usually at `/proc/$PID/mountinfo`)
  127. // and yields parsed *MountPoint into newMountPoint.
  128. func parseMountInfo(procPathMountInfo string, newMountPoint func(*MountPoint) error) error {
  129. mountInfoFile, err := os.Open(procPathMountInfo)
  130. if err != nil {
  131. return err
  132. }
  133. defer mountInfoFile.Close()
  134. scanner := bufio.NewScanner(mountInfoFile)
  135. for scanner.Scan() {
  136. mountPoint, err := NewMountPointFromLine(scanner.Text())
  137. if err != nil {
  138. return err
  139. }
  140. if err := newMountPoint(mountPoint); err != nil {
  141. return err
  142. }
  143. }
  144. return scanner.Err()
  145. }