3131#include <stdlib.h>
3232#include <string.h>
3333
34+ #define LENGTHOF (array ) (sizeof(array) / sizeof(array[0]))
35+
3436static void * s_impl_library = 0 ;
3537static struct wpe_loader_interface * s_impl_loader = 0 ;
3638
3739#ifndef WPE_BACKEND
38- #define IMPL_LIBRARY_NAME_BUFFER_SIZE 512
39- static char * s_impl_library_name ;
40- static char s_impl_library_name_buffer [IMPL_LIBRARY_NAME_BUFFER_SIZE ];
40+ static char * s_impl_library_path = NULL ;
41+ static char s_impl_library_path_buffer [512 ];
4142#endif
4243
4344#ifndef WPE_BACKEND
4445static void
45- wpe_loader_set_impl_library_name (const char * impl_library_name )
46+ wpe_loader_set_impl_library_path (const char * impl_library_path )
4647{
47- size_t len ;
48-
49- if (!impl_library_name )
48+ if (!impl_library_path )
5049 return ;
5150
52- len = strlen (impl_library_name ) + 1 ;
51+ size_t len = strlen (impl_library_path ) + 1 ;
5352 if (len == 1 )
5453 return ;
5554
56- if (len > IMPL_LIBRARY_NAME_BUFFER_SIZE )
57- s_impl_library_name = ( char * ) malloc (len );
55+ if (len > LENGTHOF ( s_impl_library_path_buffer ) )
56+ s_impl_library_path = malloc (len );
5857 else
59- s_impl_library_name = s_impl_library_name_buffer ;
60- memcpy (s_impl_library_name , impl_library_name , len );
58+ s_impl_library_path = s_impl_library_path_buffer ;
59+ memcpy (s_impl_library_path , impl_library_path , len );
6160}
62- #endif
61+ #endif /* !WPE_BACKEND */
6362
6463void
65- load_impl_library ()
64+ load_impl_library (void )
6665{
6766#ifdef WPE_BACKEND
6867 s_impl_library = dlopen (WPE_BACKEND , RTLD_NOW );
6968 if (!s_impl_library ) {
7069 fprintf (stderr , "wpe: could not load compile-time defined WPE_BACKEND: %s\n" , dlerror ());
7170 abort ();
7271 }
73- #else
72+ #else /* !WPE_BACKEND */
7473#ifndef NDEBUG
7574 // Get the impl library from an environment variable, if available.
76- char * env_library_name = getenv ("WPE_BACKEND_LIBRARY" );
77- if (env_library_name ) {
78- s_impl_library = dlopen (env_library_name , RTLD_NOW );
75+ const char * env_library_path = getenv ("WPE_BACKEND_LIBRARY" );
76+ if (env_library_path ) {
77+ s_impl_library = dlopen (env_library_path , RTLD_NOW );
7978 if (!s_impl_library ) {
8079 fprintf (stderr , "wpe: could not load specified WPE_BACKEND_LIBRARY: %s\n" , dlerror ());
8180 abort ();
8281 }
83- wpe_loader_set_impl_library_name ( env_library_name );
82+ wpe_loader_set_impl_library_path ( env_library_path );
8483 }
85- #endif
84+ #endif /* !NDEBUG */
8685 if (!s_impl_library ) {
8786 // Load libWPEBackend-default.so by ... default.
88- s_impl_library = dlopen (" libWPEBackend-default.so" , RTLD_NOW );
87+ s_impl_library = dlopen (WPE_BACKENDS_DIR "/ libWPEBackend-default.so" , RTLD_NOW );
8988 if (!s_impl_library ) {
9089 fprintf (stderr , "wpe: could not load the impl library. Is there any backend installed?: %s\n" , dlerror ());
9190 abort ();
9291 }
93- wpe_loader_set_impl_library_name ( " libWPEBackend-default.so" );
92+ wpe_loader_set_impl_library_path ( WPE_BACKENDS_DIR "/ libWPEBackend-default.so" );
9493 }
95- #endif
94+ #endif /* WPE_BACKEND */
9695
9796 s_impl_loader = dlsym (s_impl_library , "_wpe_loader_interface" );
9897}
@@ -101,41 +100,51 @@ bool
101100wpe_loader_init (const char * impl_library_name )
102101{
103102#ifndef WPE_BACKEND
104- if (!impl_library_name ) {
103+ if (!( impl_library_name && impl_library_name [ 0 ] != '\0' ) ) {
105104 fprintf (stderr , "wpe_loader_init: invalid implementation library name\n" );
106105 abort ();
107106 }
108107
108+ const bool relative_path = (impl_library_name [0 ] != '/' );
109+
110+ size_t len = strlen (impl_library_name ) + 1 + (relative_path ? LENGTHOF (WPE_BACKENDS_DIR ) : 0 );
111+ char impl_library_path [len ];
112+
113+ if (relative_path )
114+ snprintf (impl_library_path , len , WPE_BACKENDS_DIR "/%s" , impl_library_name );
115+ else
116+ strncpy (impl_library_path , impl_library_name , len );
117+
109118 if (s_impl_library ) {
110- if (!s_impl_library_name || strcmp (s_impl_library_name , impl_library_name ) != 0 ) {
119+ if (!s_impl_library_path || strcmp (s_impl_library_path , impl_library_path ) != 0 ) {
111120 fprintf (stderr , "wpe_loader_init: already initialized\n" );
112121 return false;
113122 }
114123 return true;
115124 }
116125
117- s_impl_library = dlopen (impl_library_name , RTLD_NOW );
126+ s_impl_library = dlopen (impl_library_path , RTLD_NOW );
118127 if (!s_impl_library ) {
119- fprintf (stderr , "wpe_loader_init could not load the library '%s': %s\n" , impl_library_name , dlerror ());
128+ fprintf (stderr , "wpe_loader_init could not load the library '%s': %s\n" , impl_library_path , dlerror ());
120129 return false;
121130 }
122- wpe_loader_set_impl_library_name ( impl_library_name );
131+ wpe_loader_set_impl_library_path ( impl_library_path );
123132
124133 s_impl_loader = dlsym (s_impl_library , "_wpe_loader_interface" );
125134 return true;
126- #else
135+ #else /* WPE_BACKEND */
127136 return false;
128- #endif
137+ #endif /* !WPE_BACKEND */
129138}
130139
131140const char *
132141wpe_loader_get_loaded_implementation_library_name (void )
133142{
134143#ifdef WPE_BACKEND
135144 return s_impl_library ? WPE_BACKEND : NULL ;
136- #else
137- return s_impl_library_name ;
138- #endif
145+ #else /* !WPE_BACKEND */
146+ return s_impl_library_path ;
147+ #endif /* WPE_BACKEND */
139148}
140149
141150void *
0 commit comments