From 514d2a825272fb366585ed29ba0d0c5ff79742a0 Mon Sep 17 00:00:00 2001 From: Jamie Strandboge Date: Wed, 9 Mar 2022 16:11:54 -0600 Subject: [PATCH] [disk][linux] add SELF_MOUNTINFO to use /proc/self The changes to gopsutil for reading /proc/1/mountinfo affected applications running under restricted environments that disallows access to /proc/1/mountinfo. #1159 was filed for android but other restricted environments are also affected (eg, snaps)). The fix for #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. --- README.md | 3 +++ disk/disk_linux.go | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 191f3c7fb..b4b40e74c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 18c96c175..19943a48a 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -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 {