Match behavior of who when getting num_users by ignoring zombie pids

This commit is contained in:
Kovid Goyal 2022-07-03 14:46:54 +05:30
parent a8b756f040
commit 4b1fa2609d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,6 +1,14 @@
#include "data-types.h"
#if __has_include(<utmpx.h>)
#include <utmpx.h>
#include <signal.h>
static bool
pid_exists(pid_t pid) {
if (pid < 1) return false;
if (kill(pid, 0) >= 0) return true;
return errno != ESRCH;
}
static PyObject*
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
@ -9,7 +17,7 @@ num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
Py_BEGIN_ALLOW_THREADS
setutxent();
while ((ut = getutxent())) {
if (ut->ut_type == USER_PROCESS) users++;
if (ut->ut_type == USER_PROCESS && ut->ut_user[0] && pid_exists(ut->ut_pid)) users++;
}
endutxent();
Py_END_ALLOW_THREADS