| 1 |
|
| 2 |
|
| 3 |
package mountinfo |
| 4 |
|
| 5 |
import ( |
| 6 |
"bufio" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"slices" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
|
| 15 |
"github.com/cilium/ebpf/internal" |
| 16 |
"github.com/cilium/ebpf/internal/platform" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
type Entry struct { |
| 21 |
|
| 22 |
|
| 23 |
MountPoint string |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
Root string |
| 32 |
|
| 33 |
|
| 34 |
FSType string |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func Read() ([]Entry, error) { |
| 43 |
return readOnce() |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func FindByFSType(fstype string) ([]string, error) { |
| 49 |
entries, err := Read() |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return filterByFSType(entries, fstype), nil |
| 54 |
} |
| 55 |
|
| 56 |
func filterByFSType(entries []Entry, fstype string) []string { |
| 57 |
var mounts []string |
| 58 |
for _, e := range entries { |
| 59 |
if e.FSType != fstype { |
| 60 |
continue |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
if !slices.Contains(mounts, e.MountPoint) { |
| 66 |
mounts = append(mounts, e.MountPoint) |
| 67 |
} |
| 68 |
} |
| 69 |
return mounts |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func parseEntries(r io.Reader) ([]Entry, error) { |
| 74 |
var entries []Entry |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
br := bufio.NewReader(r) |
| 79 |
for { |
| 80 |
line, err := br.ReadString('\n') |
| 81 |
if line != "" { |
| 82 |
line = strings.TrimRight(line, "\n") |
| 83 |
if line != "" { |
| 84 |
entry, perr := parseLine(line) |
| 85 |
if perr != nil { |
| 86 |
return nil, perr |
| 87 |
} |
| 88 |
entries = append(entries, entry) |
| 89 |
} |
| 90 |
} |
| 91 |
if errors.Is(err, io.EOF) { |
| 92 |
break |
| 93 |
} |
| 94 |
if err != nil { |
| 95 |
return nil, fmt.Errorf("read mountinfo: %w", err) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return entries, nil |
| 100 |
} |
| 101 |
|
| 102 |
func parseLine(line string) (Entry, error) { |
| 103 |
firstHalfStr, secondHalfStr, ok := strings.Cut(line, " - ") |
| 104 |
if !ok { |
| 105 |
return Entry{}, fmt.Errorf("invalid mountinfo line, missing dash: %q", line) |
| 106 |
} |
| 107 |
|
| 108 |
secondHalf := strings.Fields(strings.TrimSpace(secondHalfStr)) |
| 109 |
if len(secondHalf) == 0 { |
| 110 |
return Entry{}, fmt.Errorf("invalid mountinfo line, too few fields after dash: %q", line) |
| 111 |
} |
| 112 |
|
| 113 |
firstHalf := strings.Fields(strings.TrimSpace(firstHalfStr)) |
| 114 |
if len(firstHalf) < 6 { |
| 115 |
return Entry{}, fmt.Errorf("invalid mountinfo line, too few fields: %q", line) |
| 116 |
} |
| 117 |
|
| 118 |
return Entry{ |
| 119 |
Root: unescape(firstHalf[3]), |
| 120 |
MountPoint: unescape(firstHalf[4]), |
| 121 |
FSType: secondHalf[0], |
| 122 |
}, nil |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
var unescaper = strings.NewReplacer( |
| 128 |
`\040`, " ", |
| 129 |
`\011`, "\t", |
| 130 |
`\012`, "\n", |
| 131 |
`\134`, `\`, |
| 132 |
) |
| 133 |
|
| 134 |
func unescape(s string) string { |
| 135 |
return unescaper.Replace(s) |
| 136 |
} |
| 137 |
|
| 138 |
var readOnce = sync.OnceValues(func() ([]Entry, error) { |
| 139 |
if !platform.IsLinux { |
| 140 |
return nil, fmt.Errorf("mountinfo: %w", internal.ErrNotSupportedOnOS) |
| 141 |
} |
| 142 |
|
| 143 |
f, err := os.Open("/proc/self/mountinfo") |
| 144 |
if err != nil { |
| 145 |
return nil, fmt.Errorf("open mountinfo: %w", err) |
| 146 |
} |
| 147 |
defer f.Close() |
| 148 |
|
| 149 |
return parseEntries(f) |
| 150 |
}) |