Skip to content

Commit 9020fae

Browse files
committed
Finding libX11.so.6 in a slightly more intelligent way: Check that dlopen actually succeeds, if not found in one of the fixed paths, look in the LD_LIBRARY_PATH.
1 parent 5aac1e2 commit 9020fae

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

cpp/linux-specific/x_ignore_nofocus.c

+56-9
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,29 @@ int is_library_for_architecture(const char* lib_path, uint16_t arch)
539539
return FALSE;
540540
}
541541

542+
int is_usable_library(const char *candidate_library, uint16_t desired_architecture)
543+
{
544+
if (access(candidate_library, F_OK) == 0 &&
545+
is_library_for_architecture(candidate_library, desired_architecture) == TRUE) {
546+
void *ret_handle = dlopen(candidate_library, RTLD_LAZY);
547+
if (ret_handle == NULL) {
548+
return FALSE;
549+
}
550+
dlclose(ret_handle);
551+
552+
return TRUE;
553+
}
554+
return FALSE;
555+
}
556+
542557
int find_xlib_by_arch(const char* possible_locations[],
543558
int locations_length, uint16_t desired_architecture)
544559
{
545560
int i;
546561
for (i = 0; i < locations_length; i++) {
547562
const char* possible_location = possible_locations[i];
548563

549-
if (access(possible_location, F_OK) == 0 &&
550-
is_library_for_architecture(possible_location, desired_architecture) == TRUE)
564+
if (is_usable_library(possible_location, desired_architecture))
551565
{
552566
return i;
553567
}
@@ -556,6 +570,34 @@ int find_xlib_by_arch(const char* possible_locations[],
556570
return -1;
557571
}
558572

573+
574+
int find_xlib_by_env(char *library, uint16_t desired_architecture)
575+
{
576+
char *ld_env = getenv("LD_LIBRARY_PATH");
577+
if (ld_env == 0) {
578+
return FALSE;
579+
}
580+
581+
char *ld_to_parse = strdup(ld_env);
582+
583+
int found_library = FALSE;
584+
char *t = strtok(ld_to_parse, ":");
585+
char potential_library[MAX_LIBRARY_PATH + 1];
586+
587+
while ((t != NULL) && (!found_library)) {
588+
snprintf(potential_library, MAX_LIBRARY_PATH, "%s/libX11.so.6", t);
589+
if (is_usable_library(potential_library, desired_architecture)) {
590+
strcpy(library, potential_library);
591+
fprintf(stderr, "Found libX11.so.6 at: %s\n", potential_library);
592+
found_library = TRUE;
593+
}
594+
t = strtok(NULL, ":");
595+
}
596+
597+
free(ld_to_parse);
598+
return found_library;
599+
}
600+
559601
void* get_xlib_handle()
560602
{
561603
void* ret_handle = NULL;
@@ -577,22 +619,27 @@ void* get_xlib_handle()
577619
required_lib_arch = EM_X86_64;
578620
}
579621
int suitable_xlib_index = find_xlib_by_arch(possible_locations, locations_len, required_lib_arch);
580-
if (suitable_xlib_index < 0) {
622+
int found_library = FALSE;
623+
if (suitable_xlib_index >= 0) {
624+
snprintf(library, MAX_LIBRARY_PATH, "%s", possible_locations[suitable_xlib_index]);
625+
found_library = TRUE;
626+
} else {
627+
found_library = find_xlib_by_env(library, required_lib_arch);
628+
}
629+
if (found_library == FALSE) {
581630
const char* desired_arch = (required_lib_arch == EM_386 ? "32-bit" : "64-bit");
582-
fprintf(stderr, "None of the following is a %s version of Xlib:", desired_arch);
631+
fprintf(stderr, "None of the following is a %s version of Xlib:", desired_arch);
583632
int i;
584633
for (i = 0; i < locations_len; i++) {
585-
fprintf(stderr, " %s\n", possible_locations[i]);
634+
fprintf(stderr, " %s\n", possible_locations[i]);
586635
}
587636
return NULL;
588637
}
589638

590-
snprintf(library, MAX_LIBRARY_PATH, possible_locations[suitable_xlib_index]);
591-
592-
ret_handle = dlopen(library, RTLD_LAZY);
639+
ret_handle = dlopen(library, RTLD_LAZY);
593640
if (ret_handle == NULL) {
594641
fprintf(stderr, "Failed to dlopen %s\n", library);
595-
fprintf(stderr, "dlerror says: %s\n", dlerror());
642+
fprintf(stderr, "dlerror says: %s\n", dlerror());
596643
}
597644

598645
return ret_handle;

0 commit comments

Comments
 (0)