Skip to content

Commit

Permalink
[disk][linux] add SELF_MOUNTINFO to use /proc/self
Browse files Browse the repository at this point in the history
The changes to gopsutil for reading /proc/1/mountinfo affected
applications running under restricted environments that disallows access
to /proc/1/mountinfo. shirou#1159 was filed for android but other restricted
environments are also affected (eg, snaps)). The fix for shirou#1159 addressed
the application behavior to work under confinement for non-android as
well. However, depending on the system, the attempt to read
/proc/1/mountinfo could cause a sandbox denial in the logs which can be
quite noisy if using gopsutil as part of a monitoring solution that
polls often.

This introduces the SELF_MOUNTINFO to force reading from /proc/self
instead of first trying /proc/1. When unset or set to anything other
than '1', retain the current behavior with fallback. This allows people
to set SELF_MOUNTINFO=1 when gopsutil is running under these restricted
environments.
  • Loading branch information
jdstrand committed Mar 9, 2022
1 parent 4b988f3 commit 514d2a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ environment variable.
You can set an alternative location to `/dev` by setting the `HOST_DEV`
environment variable.

You can force use of `/proc/self` for mount info by setting the
`SELF_MOUNTINFO` environment variable.

## Documentation

see http://godoc.org/github.com/shirou/gopsutil
Expand Down
11 changes: 10 additions & 1 deletion disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,17 @@ func readMountFile(root string) (lines []string, useMounts bool, filename string
}

func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
lines, useMounts, filename, err := readMountFile("1")
root := "1"
if os.Getenv("SELF_MOUNTINFO") == "1" {
// force preference for "/proc/self/mountinfo" #1271
root = "self"
}

lines, useMounts, filename, err := readMountFile(root)
if err != nil {
if root == "self" {
return nil, err
}
// fallback to "/proc/self/mountinfo" #1159
lines, useMounts, filename, err = readMountFile("self")
if err != nil {
Expand Down

0 comments on commit 514d2a8

Please sign in to comment.