Debian Codegraph @

internal/mountinfo/mountinfo.go

raw symbols SCIP
1 // Package mountinfo parses /proc/self/mountinfo to discover filesystem
2 // mounts visible in the calling process's mount namespace.
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 // Entry represents one parsed line from /proc/self/mountinfo.
20 type Entry struct {
21 // MountPoint is the absolute path where the filesystem is mounted,
22 // with octal escapes (\040, \011, \012, \134) decoded.
23 MountPoint string
24
25 // Root is the path within the source filesystem that's exposed at
26 // MountPoint, octal-decoded. It is "/" for a full filesystem mount and
27 // some other path (e.g. "/events") when a subdirectory of the source
28 // filesystem has been bind-mounted onto MountPoint. Callers that want
29 // to use MountPoint as the root of a filesystem should reject entries
30 // where Root != "/".
31 Root string
32
33 // FSType is the filesystem type, e.g. "bpf", "tracefs", "debugfs".
34 FSType string
35 }
36
37 // Read parses /proc/self/mountinfo and returns one Entry per mount line.
38 // The result is cached for the lifetime of the process; mounts that appear
39 // or disappear after the first call are not reflected.
40 //
41 // On non-Linux, returns an error wrapping [internal.ErrNotSupportedOnOS].
42 func Read() ([]Entry, error) {
43 return readOnce()
44 }
45
46 // FindByFSType returns the mount points for filesystems of the given type,
47 // in order of appearance, with duplicates removed.
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 // Number of matching mounts is expected to be very low; linear
63 // search is faster and more cache-friendly than a map at this
64 // scale, and avoids the allocations a map would incur.
65 if !slices.Contains(mounts, e.MountPoint) {
66 mounts = append(mounts, e.MountPoint)
67 }
68 }
69 return mounts
70 }
71
72 // parseEntries parses mountinfo data read from r.
73 func parseEntries(r io.Reader) ([]Entry, error) {
74 var entries []Entry
75 // Format of /proc/self/mountinfo:
76 //
77 // {id} {parent id} {major:minor} {root} {mount point} {mount options} [optional fields...] - {filesystem type} {source} {superblock options}
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 // show_mountinfo in the kernel has the escape set of ' \t\n\\'. Instead of
126 // a full octal unescaper, only replace these specific characters.
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 })