| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package mount |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
mountinfoFormat = "%d %d %d:%d %s %s %s %s" |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
func parseMountTable() ([]*Info, error) { |
| 35 |
f, err := os.Open("/proc/self/mountinfo") |
| 36 |
if err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
defer f.Close() |
| 40 |
|
| 41 |
return parseInfoFile(f) |
| 42 |
} |
| 43 |
|
| 44 |
func parseInfoFile(r io.Reader) ([]*Info, error) { |
| 45 |
var ( |
| 46 |
s = bufio.NewScanner(r) |
| 47 |
out = []*Info{} |
| 48 |
) |
| 49 |
|
| 50 |
for s.Scan() { |
| 51 |
if err := s.Err(); err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
|
| 55 |
var ( |
| 56 |
p = &Info{} |
| 57 |
text = s.Text() |
| 58 |
optionalFields string |
| 59 |
) |
| 60 |
|
| 61 |
if _, err := fmt.Sscanf(text, mountinfoFormat, |
| 62 |
&p.ID, &p.Parent, &p.Major, &p.Minor, |
| 63 |
&p.Root, &p.Mountpoint, &p.Opts, &optionalFields); err != nil { |
| 64 |
return nil, fmt.Errorf("Scanning '%s' failed: %s", text, err) |
| 65 |
} |
| 66 |
|
| 67 |
index := strings.Index(text, " - ") |
| 68 |
postSeparatorFields := strings.Split(text[index+3:], " ") |
| 69 |
if len(postSeparatorFields) < 3 { |
| 70 |
return nil, fmt.Errorf("Error found less than 3 fields post '-' in %q", text) |
| 71 |
} |
| 72 |
|
| 73 |
if optionalFields != "-" { |
| 74 |
p.Optional = optionalFields |
| 75 |
} |
| 76 |
|
| 77 |
p.Fstype = postSeparatorFields[0] |
| 78 |
p.Source = postSeparatorFields[1] |
| 79 |
p.VfsOpts = strings.Join(postSeparatorFields[2:], " ") |
| 80 |
out = append(out, p) |
| 81 |
} |
| 82 |
return out, nil |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func PidMountInfo(pid int) ([]*Info, error) { |
| 89 |
f, err := os.Open(fmt.Sprintf("/proc/%d/mountinfo", pid)) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
defer f.Close() |
| 94 |
|
| 95 |
return parseInfoFile(f) |
| 96 |
} |