Skip to content

Commit 9e6e627

Browse files
committed
[disk][linux] add HOST_PROC_MOUNTINFO, closes #1271
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 HOST_PROC_MOUNTINFO to force reading from the parent dir of the specified path instead of first trying /proc/1. When unset, retain the current behavior with fallback. This allows people, for example, to set HOST_PROC_MOUNTINFO=/proc/self/mountinfo when gopsutil is running under these restricted environments. This change updates the private readMountFile() to use a root path instead of a root subpath, and adjusts PartitionsWithContext() to set the root path to /proc/1 initially and falling back to /proc/self. When HOST_PROC_MOUNTINFO is not empty, set the root path to the parent directory of HOST_PROC_MOUNTINFO.
1 parent 78577a7 commit 9e6e627

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ environment variable.
9090
You can set an alternative location to `/dev` by setting the `HOST_DEV`
9191
environment variable.
9292

93+
You can set an alternative location to `/proc/N/mountinfo` by setting the
94+
`HOST_PROC_MOUNTINFO` environment variable.
95+
9396
## Documentation
9497

9598
see http://godoc.org/github.com/shirou/gopsutil

disk/disk_linux.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ var fsTypeMap = map[int64]string{
235235
ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */
236236
}
237237

238-
// readMountFile reads mountinfo or mounts file under /proc/1 or /proc/self
238+
// readMountFile reads mountinfo or mounts file under the specified root path
239+
// (eg, /proc/1, /proc/self, etc)
239240
func readMountFile(root string) (lines []string, useMounts bool, filename string, err error) {
240-
filename = common.HostProc(path.Join(root, "mountinfo"))
241+
filename = path.Join(root, "mountinfo")
241242
lines, err = common.ReadLines(filename)
242243
if err != nil {
243244
var pathErr *os.PathError
@@ -246,7 +247,7 @@ func readMountFile(root string) (lines []string, useMounts bool, filename string
246247
}
247248
// if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26)
248249
useMounts = true
249-
filename = common.HostProc(path.Join(root, "mounts"))
250+
filename = path.Join(root, "mounts")
250251
lines, err = common.ReadLines(filename)
251252
if err != nil {
252253
return
@@ -257,10 +258,22 @@ func readMountFile(root string) (lines []string, useMounts bool, filename string
257258
}
258259

259260
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
260-
lines, useMounts, filename, err := readMountFile("1")
261+
// by default, try "/proc/1/..." first
262+
root := common.HostProc(path.Join("1"))
263+
264+
// force preference for dirname of HOST_PROC_MOUNTINFO, if set #1271
265+
hpmPath := os.Getenv("HOST_PROC_MOUNTINFO")
266+
if hpmPath != "" {
267+
root = filepath.Dir(hpmPath)
268+
}
269+
270+
lines, useMounts, filename, err := readMountFile(root)
261271
if err != nil {
262-
// fallback to "/proc/self/mountinfo" #1159
263-
lines, useMounts, filename, err = readMountFile("self")
272+
if hpmPath != "" { // don't fallback with HOST_PROC_MOUNTINFO
273+
return nil, err
274+
}
275+
// fallback to "/proc/self/..." #1159
276+
lines, useMounts, filename, err = readMountFile(common.HostProc(path.Join("self")))
264277
if err != nil {
265278
return nil, err
266279
}

0 commit comments

Comments
 (0)