commit aebb4e5d8d021a1dbac3dd925e3ec3535ea84a68 Author: Kamil Rytarowski Date: Fri Jun 7 23:42:03 2019 +0200 Implement native proc_cwd for NetBSD without /proc (Linux-compat) KERN_PROC_CWD has been introduced in NetBSD 8.99.42. diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 3d9dfdab..1ce1a5ff 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -105,6 +105,7 @@ HAS_PER_CPU_TIMES = hasattr(cext, "per_cpu_times") HAS_PROC_NUM_THREADS = hasattr(cext, "proc_num_threads") HAS_PROC_OPEN_FILES = hasattr(cext, 'proc_open_files') HAS_PROC_NUM_FDS = hasattr(cext, 'proc_num_fds') +HAS_PROC_CWD = hasattr(cext, 'proc_cwd') kinfo_proc_map = dict( ppid=0, @@ -845,6 +846,8 @@ class Process(object): if OPENBSD and self.pid == 0: return None # ...else it would raise EINVAL elif NETBSD: + if HAS_PROC_CWD: + return cext.proc_cwd(self.pid) or None with wrap_exceptions_procfs(self): return os.readlink("/proc/%s/cwd" % self.pid) elif HAS_PROC_OPEN_FILES: diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 0f899ef5..88f168d3 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -921,6 +921,8 @@ PsutilMethods[] = { #if defined(PSUTIL_FREEBSD) || defined(PSUTIL_OPENBSD) {"proc_connections", psutil_proc_connections, METH_VARARGS, "Return connections opened by process"}, +#endif +#if defined(PSUTIL_FREEBSD) || defined(PSUTIL_OPENBSD) || (defined(PSUTIL_NETBSD) && __NetBSD_Version__ >= 899004200) {"proc_cwd", psutil_proc_cwd, METH_VARARGS, "Return process current working directory."}, #endif diff --git a/psutil/arch/netbsd/specific.c b/psutil/arch/netbsd/specific.c index 195896f2..c4edad31 100644 --- a/psutil/arch/netbsd/specific.c +++ b/psutil/arch/netbsd/specific.c @@ -111,6 +111,24 @@ kinfo_getfile(pid_t pid, int* cnt) { return kf; } +#ifdef KERN_PROC_CWD /* Introduced in NetBSD-8.99.42 */ +PyObject * +psutil_proc_cwd(PyObject *self, PyObject *args) { + long pid; + char path[MAXPATHLEN]; + size_t pathlen = sizeof path; + + if (! PyArg_ParseTuple(args, "l", &pid)) + return NULL; + + int name[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_CWD}; + if (sysctl(name, 4, path, &pathlen, NULL, 0) != 0) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + return PyUnicode_DecodeFSDefault(path); +} +#endif // XXX: This is no longer used as per // https://github.com/giampaolo/psutil/pull/557#issuecomment-171912820 diff --git a/psutil/arch/netbsd/specific.h b/psutil/arch/netbsd/specific.h index 96ad9f7d..7dac88f7 100644 --- a/psutil/arch/netbsd/specific.h +++ b/psutil/arch/netbsd/specific.h @@ -26,3 +26,6 @@ PyObject* psutil_disk_io_counters(PyObject* self, PyObject* args); PyObject* psutil_proc_exe(PyObject* self, PyObject* args); PyObject* psutil_proc_num_threads(PyObject* self, PyObject* args); PyObject* psutil_cpu_stats(PyObject* self, PyObject* args); +#if (__NetBSD_Version__ - 0) >= 899004200 +PyObject *psutil_proc_cwd(PyObject *self, PyObject *args); +#endif