Match behavior of who when getting num_users by ignoring zombie pids
This commit is contained in:
parent
a8b756f040
commit
4b1fa2609d
10
kitty/utmp.c
10
kitty/utmp.c
@ -1,6 +1,14 @@
|
|||||||
#include "data-types.h"
|
#include "data-types.h"
|
||||||
#if __has_include(<utmpx.h>)
|
#if __has_include(<utmpx.h>)
|
||||||
#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*
|
static PyObject*
|
||||||
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
|
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
|
Py_BEGIN_ALLOW_THREADS
|
||||||
setutxent();
|
setutxent();
|
||||||
while ((ut = getutxent())) {
|
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();
|
endutxent();
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user