Clean up previous PR

This commit is contained in:
Kovid Goyal
2021-10-05 11:36:29 +05:30
parent fe42b15ec6
commit 35d4e2d4e0
3 changed files with 72 additions and 110 deletions

View File

@@ -1,50 +1,35 @@
#if defined(__unix__)
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdbool.h>
#include <utmp.h>
#include "data-types.h"
#ifdef __unix__
#include <utmpx.h>
static PyObject*
num_users(PyObject *const self, PyObject *const args) {
(void)self; (void)args;
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
size_t users = 0;
struct utmpx *ut;
Py_BEGIN_ALLOW_THREADS
#ifdef UTENT_REENTRANT
struct utmp *result = NULL;
struct utmp buffer = { 0, };
while (true) {
if (getutent_r(&buffer, &result) == -1) {
Py_BLOCK_THREADS
return PyErr_SetFromErrno(PyExc_OSError);
}
if (result == NULL) { break; }
if (result->ut_type == USER_PROCESS) { users++; }
setutxent();
while ((ut = getutxent())) {
if (ut->ut_type == USER_PROCESS) users++;
}
#else
struct utmp *ut;
setutent();
while ((ut = getutent())) {
if (ut->ut_type == USER_PROCESS) {
users++;
}
}
endutent();
#endif
endutxent();
Py_END_ALLOW_THREADS
return PyLong_FromSize_t(users);
}
#else
static PyObject*
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
PyErr_SetString(PyExc_RuntimeError, "Counting the number of users is not supported");
return NULL;
}
#endif
static PyMethodDef UtmpMethods[] = {
static PyMethodDef methods[] = {
{"num_users", num_users, METH_NOARGS, "Get the number of users using UTMP data" },
{ NULL, NULL, 0, NULL },
};
bool
init_utmp(PyObject *module) {
// 0 = success
return PyModule_AddFunctions(module, UtmpMethods) == 0;
return PyModule_AddFunctions(module, methods) == 0;
}
#endif