Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpenBSD] Process num_fds() and open_files() may raise NSP for PID 0 #2460

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ XXXX-XX-XX
Python 3.13. (patch by Sam Gross)
- 2455_, [Linux]: ``IndexError`` may occur when reading /proc/pid/stat and
field 40 (blkio_ticks) is missing.
- 2460_, [OpenBSD]: `Process.num_fds()`_ and `Process.open_files()`_ may fail
with `NoSuchProcess`_ for PID 0. Instead, we now return "null" values (0 and
[] respectively).

6.0.0
======
Expand Down
4 changes: 2 additions & 2 deletions INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ OpenBSD

::

export PKG_PATH=http://ftp.eu.openbsd.org/pub/OpenBSD/`uname -r`/packages/`uname -m`/
export PKG_PATH=https://cdn.openbsd.org/pub/OpenBSD/`uname -r`/packages/`uname -m`/
pkg_add -v python3 gcc
pip install psutil

Expand All @@ -93,7 +93,7 @@ Assuming Python 3.11 (the most recent at the time of writing):

::

export PKG_PATH="http://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/`uname -m`/`uname -r`/All"
export PKG_PATH="https://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/`uname -m`/`uname -r`/All"
pkg_add -v pkgin
pkgin install python311-* gcc12-* py311-setuptools-* py311-pip-*
python3.11 -m pip install psutil
Expand Down
11 changes: 10 additions & 1 deletion psutil/arch/bsd/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,17 @@ psutil_proc_open_files(PyObject *self, PyObject *args) {

errno = 0;
freep = kinfo_getfile(pid, &cnt);

if (freep == NULL) {
#if !defined(PSUTIL_OPENBSD)
#if defined(PSUTIL_OPENBSD)
if ((pid == 0) && (errno == ESRCH)) {
psutil_debug(
"open_files() returned ESRCH for PID 0; forcing `return []`"
);
PyErr_Clear();
return py_retlist;
}
#else
psutil_raise_for_pid(pid, "kinfo_getfile()");
#endif
goto error;
Expand Down
19 changes: 15 additions & 4 deletions psutil/arch/openbsd/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ psutil_kinfo_proc(pid_t pid, struct kinfo_proc *proc) {

ret = sysctl((int*)mib, 6, proc, &size, NULL, 0);
if (ret == -1) {
PyErr_SetFromErrno(PyExc_OSError);
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl(kinfo_proc)");
return -1;
}
// sysctl stores 0 in the size if we can't find the process information.
Expand Down Expand Up @@ -69,7 +69,7 @@ kinfo_getfile(pid_t pid, int* cnt) {

/* get the size of what would be returned */
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl(kinfo_file) (1/2)");
return NULL;
}
if ((kf = malloc(len)) == NULL) {
Expand All @@ -79,7 +79,7 @@ kinfo_getfile(pid_t pid, int* cnt) {
mib[5] = (int)(len / sizeof(struct kinfo_file));
if (sysctl(mib, 6, kf, &len, NULL, 0) < 0) {
free(kf);
PyErr_SetFromErrno(PyExc_OSError);
psutil_PyErr_SetFromOSErrnoWithSyscall("sysctl(kinfo_file) (2/2)");
return NULL;
}

Expand Down Expand Up @@ -288,8 +288,19 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) {
return NULL;

freep = kinfo_getfile(pid, &cnt);
if (freep == NULL)

if (freep == NULL) {
#if defined(PSUTIL_OPENBSD)
if ((pid == 0) && (errno == ESRCH)) {
psutil_debug(
"num_fds() returned ESRCH for PID 0; forcing `return 0`"
);
PyErr_Clear();
return Py_BuildValue("i", 0);
}
#endif
return NULL;
}

free(freep);
return Py_BuildValue("i", cnt);
Expand Down
Loading