diff --git a/kitty/utmp.c b/kitty/utmp.c index 47d06720b..f461edf69 100644 --- a/kitty/utmp.c +++ b/kitty/utmp.c @@ -1,6 +1,14 @@ #include "data-types.h" #if __has_include() #include +#include + +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