Skip to content

Commit

Permalink
Cache the systemd handle
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Planas <aplanas@suse.com>
  • Loading branch information
aplanas committed Oct 24, 2023
1 parent 27c4880 commit 2dd2eda
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions psutil/arch/linux/users.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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 **);

// Handle for the libsystemd library
void *HANDLE = NULL;


#define dlsym_check(__h, __fn, __name) do { \
__fn = dlsym(__h, #__fn); \
Expand All @@ -34,7 +37,12 @@ int (*sd_session_get_username)(const char *, char **);

static void *
load_systemd() {
void *handle = dlopen("libsystemd.so.0", RTLD_LAZY);
void *handle = NULL;

if (HANDLE != NULL)
return HANDLE;

handle = dlopen("libsystemd.so.0", RTLD_LAZY);
if (dlerror() != NULL || handle == NULL) {
psutil_debug("can't open libsystemd.so.0");
return NULL;
Expand All @@ -53,7 +61,9 @@ load_systemd() {
dlclose(handle);
return NULL;
}
return handle;

HANDLE = handle;
return HANDLE;
}

PyObject *
Expand Down

0 comments on commit 2dd2eda

Please sign in to comment.