Skip to content

Commit

Permalink
#557 (NetBSD): implement process cwd()
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jan 8, 2016
1 parent 0dfe489 commit e377b74
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
17 changes: 15 additions & 2 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,21 @@ def cwd(self):
# 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):
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
7 changes: 0 additions & 7 deletions psutil/arch/bsd/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
1 change: 0 additions & 1 deletion psutil/arch/bsd/netbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ def call(p, attr):
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()
Expand Down

0 comments on commit e377b74

Please sign in to comment.