Skip to content

Commit

Permalink
implement PID 0 exceptions in C
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 14, 2024
1 parent 0b93503 commit 3175f19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
20 changes: 4 additions & 16 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,14 +911,7 @@ def _not_implemented(self):
@wrap_exceptions
def open_files(self):
"""Return files opened by process as a list of namedtuples."""
if OPENBSD and self.pid == 0:
try:
rawlist = cext.proc_open_files(self.pid)
except ProcessLookupError:
return []
else:
rawlist = cext.proc_open_files(self.pid)

rawlist = cext.proc_open_files(self.pid)
return [_common.popenfile(path, fd) for path, fd in rawlist]

else:
Expand All @@ -931,15 +924,10 @@ def open_files(self):
@wrap_exceptions
def num_fds(self):
"""Return the number of file descriptors opened by this process."""
if OPENBSD and self.pid == 0:
try:
return cext.proc_num_fds(self.pid)
except ProcessLookupError:
return 0
elif NETBSD:
ret = cext.proc_num_fds(self.pid)
if NETBSD:
self._assert_alive()

return cext.proc_num_fds(self.pid)
return ret

else:
num_fds = _not_implemented
Expand Down
10 changes: 9 additions & 1 deletion psutil/arch/bsd/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,16 @@ 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 []`"
);
return py_retlist;
}
#else
psutil_raise_for_pid(pid, "kinfo_getfile()");
#endif
goto error;
Expand Down
11 changes: 10 additions & 1 deletion psutil/arch/openbsd/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,17 @@ 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`"
);
return Py_BuildValue("i", 0);
}
#endif
return NULL;
}

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

0 comments on commit 3175f19

Please sign in to comment.