summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2018-10-19 14:27:56 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2018-10-19 14:27:56 +0200
commitfbece8e45991e8cc070bfd5e9efcd4974227abcd (patch)
tree043ad8d23109a3c27d52349626f83c004d378498
parent6db8d2e41dc67621eae9d8eeab3c39fef7e2ddf4 (diff)
downloadpsutil-fbece8e45991e8cc070bfd5e9efcd4974227abcd.tar.gz
fix #1307: [Linux] disk_partitions() does not honour PROCFS_PATH
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/_pslinux.py11
-rw-r--r--psutil/_psutil_linux.c9
-rwxr-xr-xpsutil/tests/test_connections.py1
-rwxr-xr-xpsutil/tests/test_linux.py12
-rwxr-xr-xpsutil/tests/test_system.py6
6 files changed, 30 insertions, 10 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 44837094..a02b8e2c 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -22,6 +22,7 @@ XXXX-XX-XX
not accurate.
- 1294_: [Windows] psutil.Process().connections() may sometimes fail with
intermittent 0xC0000001. (patch by Sylvain Duchesne)
+- 1307_: [Linux] disk_partitions() does not honour PROCFS_PATH.
- 1320_: [AIX] system CPU times (psutil.cpu_times()) were being reported with
ticks unit as opposed to seconds. (patch by Jaime Fullaondo)
- 1332_: [OSX] psutil debug messages are erroneously printed all the time.
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 236934fc..82e32189 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -1140,7 +1140,8 @@ def disk_io_counters(perdisk=False):
def disk_partitions(all=False):
"""Return mounted disk partitions as a list of namedtuples."""
fstypes = set()
- with open_text("%s/filesystems" % get_procfs_path()) as f:
+ procfs_path = get_procfs_path()
+ with open_text("%s/filesystems" % procfs_path) as f:
for line in f:
line = line.strip()
if not line.startswith("nodev"):
@@ -1151,8 +1152,14 @@ def disk_partitions(all=False):
if fstype == "zfs":
fstypes.add("zfs")
+ # See: https://github.com/giampaolo/psutil/issues/1307
+ if procfs_path == "/proc":
+ mtab_path = os.path.realpath("/etc/mtab")
+ else:
+ mtab_path = os.path.realpath("%s/self/mounts" % procfs_path)
+
retlist = []
- partitions = cext.disk_partitions()
+ partitions = cext.disk_partitions(mtab_path)
for partition in partitions:
device, mountpoint, fstype, opts = partition
if device == 'none':
diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c
index d1f0d145..bd27b5f9 100644
--- a/psutil/_psutil_linux.c
+++ b/psutil/_psutil_linux.c
@@ -195,6 +195,7 @@ static PyObject *
psutil_disk_partitions(PyObject *self, PyObject *args) {
FILE *file = NULL;
struct mntent *entry;
+ const char *mtab_path;
PyObject *py_dev = NULL;
PyObject *py_mountp = NULL;
PyObject *py_tuple = NULL;
@@ -203,12 +204,14 @@ psutil_disk_partitions(PyObject *self, PyObject *args) {
if (py_retlist == NULL)
return NULL;
- // MOUNTED constant comes from mntent.h and it's == '/etc/mtab'
+ if (!PyArg_ParseTuple(args, "s", &mtab_path))
+ return NULL;
+
Py_BEGIN_ALLOW_THREADS
- file = setmntent(MOUNTED, "r");
+ file = setmntent(mtab_path, "r");
Py_END_ALLOW_THREADS
if ((file == 0) || (file == NULL)) {
- PyErr_SetFromErrnoWithFilename(PyExc_OSError, MOUNTED);
+ PyErr_SetFromErrnoWithFilename(PyExc_OSError, mtab_path);
goto error;
}
diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py
index cba835e1..7f59a74c 100755
--- a/psutil/tests/test_connections.py
+++ b/psutil/tests/test_connections.py
@@ -53,6 +53,7 @@ thisproc = psutil.Process()
class Base(object):
def setUp(self):
+ safe_rmpath(TESTFN)
if not NETBSD:
# NetBSD opens a UNIX socket to /var/log/run.
cons = thisproc.connections(kind='all')
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 115a6af8..4b72f725 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -1011,6 +1011,18 @@ class TestSystemDisks(unittest.TestCase):
assert ret
self.assertEqual(ret[0].fstype, 'zfs')
+ def test_disk_partitions_procfs(self):
+ # See: https://github.com/giampaolo/psutil/issues/1307
+ try:
+ with mock.patch('os.path.realpath',
+ return_value='/non/existent') as m:
+ with self.assertRaises(OSError) as cm:
+ psutil.disk_partitions()
+ assert m.called
+ self.assertEqual(cm.exception.errno, errno.ENOENT)
+ finally:
+ psutil.PROCFS_PATH = "/proc"
+
def test_disk_io_counters_kernel_2_4_mocked(self):
# Tests /proc/diskstats parsing format for 2.4 kernels, see:
# https://github.com/giampaolo/psutil/issues/767
diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py
index 8b07caff..7cc678f0 100755
--- a/psutil/tests/test_system.py
+++ b/psutil/tests/test_system.py
@@ -522,11 +522,7 @@ class TestSystemAPIs(unittest.TestCase):
if err.errno not in (errno.EPERM, errno.EACCES):
raise
else:
- if SUNOS or TRAVIS:
- # on solaris apparently mount points can also be files
- assert os.path.exists(disk.mountpoint), disk
- else:
- assert os.path.isdir(disk.mountpoint), disk
+ assert os.path.exists(disk.mountpoint), disk
self.assertIsInstance(disk.fstype, str)
self.assertIsInstance(disk.opts, str)