Skip to content

Commit

Permalink
Implement psutil_proc_exe for NetBSD
Browse files Browse the repository at this point in the history
Keep fallback for versions <8.0 without KERN_PROC_PATHNAME.
  • Loading branch information
krytarowski committed Jun 12, 2019
1 parent 5ebd0ba commit 39373d0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
8 changes: 1 addition & 7 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,8 @@ def name(self):

@wrap_exceptions
def exe(self):
if FREEBSD:
if FREEBSD or NETBSD:
return cext.proc_exe(self.pid)
elif NETBSD:
if self.pid == 0:
# /proc/0 dir exists but /proc/0/exe doesn't
return ""
with wrap_exceptions_procfs(self):
return os.readlink("/proc/%s/exe" % self.pid)
else:
# OpenBSD: exe cannot be determined; references:
# https://chromium.googlesource.com/chromium/src/base/+/
Expand Down
4 changes: 3 additions & 1 deletion psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,11 @@ PsutilMethods[] = {
{"proc_num_threads", psutil_proc_num_threads, METH_VARARGS,
"Return number of threads used by process"},
#endif
#if defined(PSUTIL_FREEBSD)
#if defined(PSUTIL_FREEBSD) || defined(PSUTIL_NETBSD)
{"proc_exe", psutil_proc_exe, METH_VARARGS,
"Return process pathname executable"},
#endif
#if defined(PSUTIL_FREEBSD)
{"proc_memory_maps", psutil_proc_memory_maps, METH_VARARGS,
"Return a list of tuples for every process's memory map"},
{"proc_cpu_affinity_get", psutil_proc_cpu_affinity_get, METH_VARARGS,
Expand Down
35 changes: 35 additions & 0 deletions psutil/arch/netbsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,38 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
uv.forks // forks
);
}

PyObject *
psutil_proc_exe(PyObject *self, PyObject *args) {
long pid;

char path[MAXPATHLEN];
size_t pathlen = sizeof path;

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

#ifdef KERN_PROC_PATHNAME
int name[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_PATHNAME};
if (sysctl(name, 4, path, &pathlen, NULL, 0) != 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
#else
char *buf;
if (asprintf(&buf, "/proc/%d/exe", (int)pid) < 0) {
PyErr_NoMemory();
return NULL;
}

ssize_t len = readlink(buf, path, pathlen - 1);
free(buf);
if (len == -1) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
path[len] = '\0';
#endif

return PyUnicode_DecodeFSDefault(path);
}
1 change: 1 addition & 0 deletions psutil/arch/netbsd/specific.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ 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);
PyObject *psutil_proc_cwd(PyObject *self, PyObject *args);
PyObject *psutil_proc_exe(PyObject *self, PyObject *args);

0 comments on commit 39373d0

Please sign in to comment.