summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-01-08 09:13:50 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2016-01-08 09:13:50 +0000
commite377b742ed26fedd84254ac2c284db33d465fe4d (patch)
tree32fc2cf794f828d685a895aa6afe7bc49e0171aa
parent0dfe4894e64693ed3df948aa34d1176fe5fe1105 (diff)
downloadpsutil-e377b742ed26fedd84254ac2c284db33d465fe4d.tar.gz
#557 (NetBSD): implement process cwd()
-rw-r--r--psutil/_psbsd.py17
-rw-r--r--psutil/_psutil_bsd.c2
-rw-r--r--psutil/arch/bsd/netbsd.c7
-rw-r--r--psutil/arch/bsd/netbsd.h1
-rw-r--r--test/_posix.py6
5 files changed, 23 insertions, 10 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py
index 3449473e..19ea8c3c 100644
--- a/psutil/_psbsd.py
+++ b/psutil/_psbsd.py
@@ -526,8 +526,21 @@ class Process(object):
# sometimes we get an empty string, in which case we turn
# it into None
if OPENBSD and self.pid == 0:
- return None # ...else raises EINVAL
- return cext.proc_cwd(self.pid) or None
+ return None # ...else it would raise EINVAL
+ elif NETBSD:
+ try:
+ return os.readlink("/proc/%s/cwd" % self.pid)
+ except OSError as err:
+ if err.errno == errno.ENOENT:
+ if not pid_exists(self.pid):
+ raise NoSuchProcess(self.pid, self._name)
+ else:
+ raise ZombieProcess(
+ self.pid, self._name, self._ppid)
+ else:
+ raise
+ else:
+ return cext.proc_cwd(self.pid) or None
@wrap_exceptions
def memory_maps(self):
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c
index dcdf80f9..e4be9c84 100644
--- a/psutil/_psutil_bsd.c
+++ b/psutil/_psutil_bsd.c
@@ -936,8 +936,10 @@ PsutilMethods[] = {
"Return process IO counters"},
{"proc_tty_nr", psutil_proc_tty_nr, METH_VARARGS,
"Return process tty (terminal) number"},
+#ifdef __FreeBSD__ || __OpenBSD__
{"proc_cwd", psutil_proc_cwd, METH_VARARGS,
"Return process current working directory."},
+#endif
#if defined(__FreeBSD_version) && __FreeBSD_version >= 800000 || __OpenBSD__ || defined(__NetBSD__)
{"proc_num_fds", psutil_proc_num_fds, METH_VARARGS,
"Return the number of file descriptors opened by this process"},
diff --git a/psutil/arch/bsd/netbsd.c b/psutil/arch/bsd/netbsd.c
index 1efa1147..36977e27 100644
--- a/psutil/arch/bsd/netbsd.c
+++ b/psutil/arch/bsd/netbsd.c
@@ -513,13 +513,6 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) {
}
-PyObject *
-psutil_proc_cwd(PyObject *self, PyObject *args) {
- // Not implemented
- return NULL;
-}
-
-
// see sys/kern/kern_sysctl.c lines 1100 and
// usr.bin/fstat/fstat.c print_inet_details()
static char *
diff --git a/psutil/arch/bsd/netbsd.h b/psutil/arch/bsd/netbsd.h
index 71cca52e..b0b2caaf 100644
--- a/psutil/arch/bsd/netbsd.h
+++ b/psutil/arch/bsd/netbsd.h
@@ -22,7 +22,6 @@ PyObject *psutil_proc_threads(PyObject *self, PyObject *args);
PyObject *psutil_virtual_mem(PyObject *self, PyObject *args);
PyObject *psutil_swap_mem(PyObject *self, PyObject *args);
PyObject *psutil_proc_num_fds(PyObject *self, PyObject *args);
-PyObject *psutil_proc_cwd(PyObject *self, PyObject *args);
PyObject *psutil_proc_connections(PyObject *self, PyObject *args);
PyObject *psutil_per_cpu_times(PyObject *self, PyObject *args);
PyObject* psutil_disk_io_counters(PyObject* self, PyObject* args);
diff --git a/test/_posix.py b/test/_posix.py
index 35b498a7..10243336 100644
--- a/test/_posix.py
+++ b/test/_posix.py
@@ -262,6 +262,12 @@ class PosixSpecificTestCase(unittest.TestCase):
if failures:
self.fail('\n' + '\n'.join(failures))
+ @unittest.skipUnless(os.path.islink("/proc/%s/cwd" % os.getpid()),
+ "/proc fs not available")
+ def test_cwd_proc(self):
+ self.assertEqual(os.readlink("/proc/%s/cwd" % os.getpid()),
+ psutil.Process().cwd())
+
def main():
test_suite = unittest.TestSuite()