31
31
#include <stdlib.h>
32
32
#include <string.h>
33
33
34
+ #define LENGTHOF (array ) (sizeof(array) / sizeof(array[0]))
35
+
34
36
static void * s_impl_library = 0 ;
35
37
static struct wpe_loader_interface * s_impl_loader = 0 ;
36
38
37
39
#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 ];
41
42
#endif
42
43
43
44
#ifndef WPE_BACKEND
44
45
static 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 )
46
47
{
47
- size_t len ;
48
-
49
- if (!impl_library_name )
48
+ if (!impl_library_path )
50
49
return ;
51
50
52
- len = strlen (impl_library_name ) + 1 ;
51
+ size_t len = strlen (impl_library_path ) + 1 ;
53
52
if (len == 1 )
54
53
return ;
55
54
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 );
58
57
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 );
61
60
}
62
- #endif
61
+ #endif /* !WPE_BACKEND */
63
62
64
63
void
65
- load_impl_library ()
64
+ load_impl_library (void )
66
65
{
67
66
#ifdef WPE_BACKEND
68
67
s_impl_library = dlopen (WPE_BACKEND , RTLD_NOW );
69
68
if (!s_impl_library ) {
70
69
fprintf (stderr , "wpe: could not load compile-time defined WPE_BACKEND: %s\n" , dlerror ());
71
70
abort ();
72
71
}
73
- #else
72
+ #else /* !WPE_BACKEND */
74
73
#ifndef NDEBUG
75
74
// 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 );
79
78
if (!s_impl_library ) {
80
79
fprintf (stderr , "wpe: could not load specified WPE_BACKEND_LIBRARY: %s\n" , dlerror ());
81
80
abort ();
82
81
}
83
- wpe_loader_set_impl_library_name ( env_library_name );
82
+ wpe_loader_set_impl_library_path ( env_library_path );
84
83
}
85
- #endif
84
+ #endif /* !NDEBUG */
86
85
if (!s_impl_library ) {
87
86
// 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 );
89
88
if (!s_impl_library ) {
90
89
fprintf (stderr , "wpe: could not load the impl library. Is there any backend installed?: %s\n" , dlerror ());
91
90
abort ();
92
91
}
93
- wpe_loader_set_impl_library_name ( " libWPEBackend-default.so" );
92
+ wpe_loader_set_impl_library_path ( WPE_BACKENDS_DIR "/ libWPEBackend-default.so" );
94
93
}
95
- #endif
94
+ #endif /* WPE_BACKEND */
96
95
97
96
s_impl_loader = dlsym (s_impl_library , "_wpe_loader_interface" );
98
97
}
@@ -101,41 +100,51 @@ bool
101
100
wpe_loader_init (const char * impl_library_name )
102
101
{
103
102
#ifndef WPE_BACKEND
104
- if (!impl_library_name ) {
103
+ if (!( impl_library_name && impl_library_name [ 0 ] != '\0' ) ) {
105
104
fprintf (stderr , "wpe_loader_init: invalid implementation library name\n" );
106
105
abort ();
107
106
}
108
107
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
+
109
118
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 ) {
111
120
fprintf (stderr , "wpe_loader_init: already initialized\n" );
112
121
return false;
113
122
}
114
123
return true;
115
124
}
116
125
117
- s_impl_library = dlopen (impl_library_name , RTLD_NOW );
126
+ s_impl_library = dlopen (impl_library_path , RTLD_NOW );
118
127
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 ());
120
129
return false;
121
130
}
122
- wpe_loader_set_impl_library_name ( impl_library_name );
131
+ wpe_loader_set_impl_library_path ( impl_library_path );
123
132
124
133
s_impl_loader = dlsym (s_impl_library , "_wpe_loader_interface" );
125
134
return true;
126
- #else
135
+ #else /* WPE_BACKEND */
127
136
return false;
128
- #endif
137
+ #endif /* !WPE_BACKEND */
129
138
}
130
139
131
140
const char *
132
141
wpe_loader_get_loaded_implementation_library_name (void )
133
142
{
134
143
#ifdef WPE_BACKEND
135
144
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 */
139
148
}
140
149
141
150
void *
0 commit comments