Skip to content

Commit 2dd2eda

Browse files
committed
Cache the systemd handle
Signed-off-by: Alberto Planas <[email protected]>
1 parent 27c4880 commit 2dd2eda

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

psutil/arch/linux/users.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ int (*sd_session_get_start_time)(const char *, uint64_t *);
2121
int (*sd_session_get_tty)(const char *, char **);
2222
int (*sd_session_get_username)(const char *, char **);
2323

24+
// Handle for the libsystemd library
25+
void *HANDLE = NULL;
26+
2427

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

3538
static void *
3639
load_systemd() {
37-
void *handle = dlopen("libsystemd.so.0", RTLD_LAZY);
40+
void *handle = NULL;
41+
42+
if (HANDLE != NULL)
43+
return HANDLE;
44+
45+
handle = dlopen("libsystemd.so.0", RTLD_LAZY);
3846
if (dlerror() != NULL || handle == NULL) {
3947
psutil_debug("can't open libsystemd.so.0");
4048
return NULL;
@@ -53,7 +61,9 @@ load_systemd() {
5361
dlclose(handle);
5462
return NULL;
5563
}
56-
return handle;
64+
65+
HANDLE = handle;
66+
return HANDLE;
5767
}
5868

5969
PyObject *

0 commit comments

Comments
 (0)