Skip to content

Commit

Permalink
fix compilation errors
Browse files Browse the repository at this point in the history
Signed-off-by: Giampaolo Rodola <[email protected]>
  • Loading branch information
giampaolo authored and aplanas committed Oct 24, 2023
1 parent 9bdfa1e commit 6fb4801
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions psutil/arch/linux/users.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

#include "../../_psutil_common.h"

/* Systemd function signatures that will be loaded */

// Systemd function signatures that will be loaded dynamically.
int (*sd_booted)(void);
int (*sd_get_sessions)(char ***);
int (*sd_session_get_leader)(const char *, pid_t *);
Expand All @@ -20,6 +21,7 @@ int (*sd_session_get_start_time)(const char *, uint64_t *);
int (*sd_session_get_tty)(const char *, char **);
int (*sd_session_get_username)(const char *, char **);


#define dlsym_check(__h, __fn, __name) do { \
__fn = dlsym(__h, #__fn); \
if (dlerror() != NULL || __fn == NULL) { \
Expand All @@ -29,6 +31,7 @@ int (*sd_session_get_username)(const char *, char **);
} \
} while (0)


static void *
load_systemd() {
void *handle = dlopen("libsystemd.so.0", RTLD_LAZY);
Expand All @@ -37,13 +40,13 @@ load_systemd() {
return NULL;
}

dlsym_check(handle, sd_booted);
dlsym_check(handle, sd_get_sessions);
dlsym_check(handle, sd_session_get_leader);
dlsym_check(handle, sd_session_get_remote_host);
dlsym_check(handle, sd_session_get_start_time);
dlsym_check(handle, sd_session_get_tty);
dlsym_check(handle, sd_session_get_username);
dlsym_check(handle, sd_booted, "sd_booted");
dlsym_check(handle, sd_get_sessions, "sd_get_sessions");
dlsym_check(handle, sd_session_get_leader, "sd_session_get_leader");
dlsym_check(handle, sd_session_get_remote_host, "sd_session_get_remote_host");
dlsym_check(handle, sd_session_get_start_time, "sd_session_get_start_time");
dlsym_check(handle, sd_session_get_tty, "sd_session_get_tty");
dlsym_check(handle, sd_session_get_username, "sd_session_get_username");

if (! sd_booted()) {
psutil_debug("systemd not booted");
Expand All @@ -55,8 +58,7 @@ load_systemd() {

PyObject *
psutil_users_systemd(PyObject *self, PyObject *args) {
char **sessions_list = NULL;
PyObject *py_retlist = PyList_New(0);
PyObject *py_retlist = NULL;
PyObject *py_tuple = NULL;
PyObject *py_username = NULL;
PyObject *py_tty = NULL;
Expand All @@ -65,6 +67,7 @@ psutil_users_systemd(PyObject *self, PyObject *args) {
double tstamp = 0.0;
pid_t pid = 0;
int sessions;
char **sessions_list = NULL;
const char *session_id = NULL;
char *username = NULL;
char *tty = NULL;
Expand All @@ -73,10 +76,12 @@ psutil_users_systemd(PyObject *self, PyObject *args) {
void *handle = load_systemd();

if (! handle)
return Py_RETURN_NONE;
Py_RETURN_NONE;

py_retlist = PyList_New(0);
if (py_retlist == NULL)
return NULL;
goto error;

sessions = sd_get_sessions(&sessions_list);
for (int i = 0; i < sessions; i++) {
session_id = sessions_list[i];
Expand All @@ -95,7 +100,8 @@ psutil_users_systemd(PyObject *self, PyObject *args) {
tty = NULL;
if (sd_session_get_tty(session_id, &tty) < 0) {
py_tty = PyUnicode_DecodeFSDefault("");
} else {
}
else {
py_tty = PyUnicode_DecodeFSDefault(tty);
free(tty);
}
Expand Down Expand Up @@ -153,6 +159,7 @@ psutil_users_systemd(PyObject *self, PyObject *args) {
return NULL;
}


PyObject *
psutil_users_utmp(PyObject *self, PyObject *args) {
struct utmp *ut;
Expand Down

0 comments on commit 6fb4801

Please sign in to comment.