Add fallback for missing /proc/1/mounts (#1172)

* Add fallback for missing /proc/1/mounts

On some systems, `/proc/1/mounts` is hidden from non-root users due to
the `hidepid` procfs feature. Attempt to fallback to `/proc/mounts` if
`/proc/1/mounts` is not found.

Signed-off-by: Ben Kochie <superq@gmail.com>

* Add tests.

Signed-off-by: Ben Kochie <superq@gmail.com>

* Add CHANGELOG entry.

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2018-11-30 14:01:55 +01:00 committed by GitHub
parent 0cb0c4d911
commit 4abc6fba7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View File

@ -5,6 +5,7 @@
### Changes ### Changes
* [BUGFIX] * [BUGFIX]
* [BUGFIX] Add fallback for missing /proc/1/mounts #1172
* [CHANGE] Add TCPSynRetrans to netstat default filter #1143 * [CHANGE] Add TCPSynRetrans to netstat default filter #1143
* [ENHANCEMENT] Add Infiniband counters #1120 * [ENHANCEMENT] Add Infiniband counters #1120
* [FEATURE] Add a flag to disable exporter metrics #1148 * [FEATURE] Add a flag to disable exporter metrics #1148

View File

@ -135,6 +135,11 @@ func stuckMountWatcher(mountPoint string, success chan struct{}) {
func mountPointDetails() ([]filesystemLabels, error) { func mountPointDetails() ([]filesystemLabels, error) {
file, err := os.Open(procFilePath("1/mounts")) file, err := os.Open(procFilePath("1/mounts"))
if os.IsNotExist(err) {
// Fallback to `/proc/mounts` if `/proc/1/mounts` is missing due hidepid.
log.Debugf("Got %q reading root mounts, falling back to system mounts", err)
file, err = os.Open(procFilePath("mounts"))
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -91,3 +91,24 @@ func TestMountPointDetails(t *testing.T) {
} }
} }
} }
func TestMountsFallback(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_hidepid/proc"}); err != nil {
t.Fatal(err)
}
expected := map[string]string{
"/": "",
}
filesystems, err := mountPointDetails()
if err != nil {
t.Log(err)
}
for _, fs := range filesystems {
if _, ok := expected[fs.mountPoint]; !ok {
t.Errorf("Got unexpected %s", fs.mountPoint)
}
}
}

View File

@ -0,0 +1 @@
rootfs / rootfs rw 0 0