]> git.sesse.net Git - vlc/blob - src/modules/os.c
Win32: support loading plugins from directories with extra-ACP characters
[vlc] / src / modules / os.c
1 /*****************************************************************************
2  * os.c : Low-level dynamic library handling
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *          Hans-Peter Jansen <hpj@urpla.net>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h> /* MODULE_SUFFIX */
33 #include <vlc_charset.h>
34 #include "libvlc.h"
35 #include "modules.h"
36
37 #include <stdlib.h>                                      /* free(), strtol() */
38 #include <stdio.h>                                              /* sprintf() */
39 #include <string.h>                                              /* strdup() */
40
41 #ifdef HAVE_SYS_TYPES_H
42 #   include <sys/types.h>
43 #endif
44
45 #if !defined(HAVE_DYNAMIC_PLUGINS)
46     /* no support for plugins */
47 #elif defined(HAVE_DL_BEOS)
48 #   if defined(HAVE_IMAGE_H)
49 #       include <image.h>
50 #   endif
51 #elif defined(__APPLE__)
52 #   include <dlfcn.h>
53 #elif defined(HAVE_DL_WINDOWS)
54 #   include <windows.h>
55 #elif defined(HAVE_DL_DLOPEN)
56 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
57 #       include <dlfcn.h>
58 #   endif
59 #   if defined(HAVE_SYS_DL_H)
60 #       include <sys/dl.h>
61 #   endif
62 #elif defined(HAVE_DL_SHL_LOAD)
63 #   if defined(HAVE_DL_H)
64 #       include <dl.h>
65 #   endif
66 #endif
67 #ifdef HAVE_VALGRIND_VALGRIND_H
68 # include <valgrind/valgrind.h>
69 #endif
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74 #ifdef HAVE_DYNAMIC_PLUGINS
75 static void *module_Lookup( module_handle_t, const char * );
76
77 #if defined(HAVE_DL_WINDOWS)
78 static char * GetWindowsError  ( void );
79 #endif
80
81 /**
82  * module Call
83  *
84  * Call a symbol given its name and a module structure. The symbol MUST
85  * refer to a function returning int and taking a module_t* as an argument.
86  * \param p_module the modules
87  * \return 0 if it pass and -1 in case of a failure
88  */
89 int module_Call( vlc_object_t *obj, module_t *p_module )
90 {
91     static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
92     int (* pf_symbol) ( module_t * p_module );
93
94     /* Try to resolve the symbol */
95     pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
96                                                      psz_name );
97
98     if( pf_symbol == NULL )
99     {
100 #if defined(HAVE_DL_BEOS)
101         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
102                   psz_name, p_module->psz_filename );
103 #elif defined(HAVE_DL_WINDOWS)
104         char *psz_error = GetWindowsError();
105         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
106                   psz_name, p_module->psz_filename, psz_error );
107         free( psz_error );
108 #elif defined(HAVE_DL_DLOPEN)
109         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
110                   psz_name, p_module->psz_filename, dlerror() );
111 #elif defined(HAVE_DL_SHL_LOAD)
112         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
113                   psz_name, p_module->psz_filename );
114 #else
115 #   error "Something is wrong in modules.c"
116 #endif
117         return -1;
118     }
119
120     /* We can now try to call the symbol */
121     if( pf_symbol( p_module ) != 0 )
122     {
123         /* With a well-written module we shouldn't have to print an
124          * additional error message here, but just make sure. */
125         msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
126                  psz_name, p_module->psz_filename );
127         return -1;
128     }
129
130     /* Everything worked fine, we can return */
131     return 0;
132 }
133
134 /**
135  * Load a dynamically linked library using a system dependent method.
136  *
137  * \param p_this vlc object
138  * \param psz_file library file
139  * \param p_handle the module handle returned
140  * \return 0 on success as well as the module handle.
141  */
142 int module_Load( vlc_object_t *p_this, const char *psz_file,
143                  module_handle_t *p_handle )
144 {
145     module_handle_t handle;
146
147 #if defined(HAVE_DL_BEOS)
148     handle = load_add_on( psz_file );
149     if( handle < 0 )
150     {
151         msg_Warn( p_this, "cannot load module `%s'", psz_file );
152         return -1;
153     }
154
155 #elif defined(HAVE_DL_WINDOWS)
156     wchar_t psz_wfile[MAX_PATH];
157     MultiByteToWideChar( CP_UTF8, 0, psz_file, -1, psz_wfile, MAX_PATH );
158
159 #ifndef UNDER_CE
160     /* FIXME: this is not thread-safe -- Courmisch */
161     UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
162     SetErrorMode (mode|SEM_FAILCRITICALERRORS);
163 #endif
164
165     handle = LoadLibraryW( psz_wfile );
166
167 #ifndef UNDER_CE
168     SetErrorMode (mode);
169 #endif
170
171     if( handle == NULL )
172     {
173         char *psz_err = GetWindowsError();
174         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
175         free( psz_err );
176         return -1;
177     }
178
179 #elif defined(HAVE_DL_DLOPEN)
180
181 # if defined (RTLD_NOW)
182     const int flags = RTLD_NOW;
183 # elif defined (DL_LAZY)
184     const int flags = DL_LAZY;
185 # else
186     const int flags = 0;
187 # endif
188     char *path = ToLocale( psz_file );
189
190     handle = dlopen( path, flags );
191     LocaleFree( path );
192     if( handle == NULL )
193     {
194         msg_Warn( p_this, "cannot load module `%s' (%s)", path, dlerror() );
195         return -1;
196     }
197
198 #elif defined(HAVE_DL_SHL_LOAD)
199     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
200     if( handle == NULL )
201     {
202         msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
203         return -1;
204     }
205
206 #else
207 #   error "Something is wrong in modules.c"
208
209 #endif
210
211     *p_handle = handle;
212     return 0;
213 }
214
215 /**
216  * CloseModule: unload a dynamic library
217  *
218  * This function unloads a previously opened dynamically linked library
219  * using a system dependent method. No return value is taken in consideration,
220  * since some libraries sometimes refuse to close properly.
221  * \param handle handle of the library
222  * \return nothing
223  */
224 void module_Unload( module_handle_t handle )
225 {
226 #if defined(HAVE_DL_BEOS)
227     unload_add_on( handle );
228
229 #elif defined(HAVE_DL_WINDOWS)
230     FreeLibrary( handle );
231
232 #elif defined(HAVE_DL_DLOPEN)
233 # ifdef HAVE_VALGRIND_VALGRIND_H
234     if( RUNNING_ON_VALGRIND > 0 )
235         return; /* do not dlclose() so that we get proper stack traces */
236 # endif
237     dlclose( handle );
238
239 #elif defined(HAVE_DL_SHL_LOAD)
240     shl_unload( handle );
241
242 #endif
243     return;
244 }
245
246 /**
247  * Looks up a symbol from a dynamically loaded library
248  *
249  * This function queries a loaded library for a symbol specified in a
250  * string, and returns a pointer to it. We don't check for dlerror() or
251  * similar functions, since we want a non-NULL symbol anyway.
252  *
253  * @param handle handle to the module
254  * @param psz_function function name
255  * @return NULL on error, or the address of the symbol
256  */
257 static void *module_Lookup( module_handle_t handle, const char *psz_function )
258 {
259 #if defined(HAVE_DL_BEOS)
260     void * p_symbol;
261     if( B_OK == get_image_symbol( handle, psz_function,
262                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
263     {
264         return p_symbol;
265     }
266     else
267     {
268         return NULL;
269     }
270
271 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
272     wchar_t wide[strlen( psz_function ) + 1];
273     size_t i = 0;
274     do
275         wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
276     while( psz_function[i++] );
277
278     return (void *)GetProcAddress( handle, wide );
279
280 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
281     return (void *)GetProcAddress( handle, (char *)psz_function );
282
283 #elif defined(HAVE_DL_DLOPEN)
284     return dlsym( handle, psz_function );
285
286 #elif defined(HAVE_DL_SHL_LOAD)
287     void *p_sym;
288     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
289     return p_sym;
290
291 #endif
292 }
293
294 #if defined(HAVE_DL_WINDOWS)
295 static char * GetWindowsError( void )
296 {
297 #if defined(UNDER_CE)
298     wchar_t psz_tmp[MAX_PATH];
299     char * psz_buffer = malloc( MAX_PATH );
300 #else
301     char * psz_tmp = malloc( MAX_PATH );
302 #endif
303     int i = 0, i_error = GetLastError();
304
305     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
306                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
307                    (LPTSTR)psz_tmp, MAX_PATH, NULL );
308
309     /* Go to the end of the string */
310     while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
311     {
312         i++;
313     }
314
315     if( psz_tmp[i] )
316     {
317 #if defined(UNDER_CE)
318         swprintf( psz_tmp + i, L" (error %i)", i_error );
319         psz_tmp[ 255 ] = L'\0';
320 #else
321         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
322         psz_tmp[ 255 ] = '\0';
323 #endif
324     }
325
326 #if defined(UNDER_CE)
327     wcstombs( psz_buffer, psz_tmp, MAX_PATH );
328     return psz_buffer;
329 #else
330     return psz_tmp;
331 #endif
332 }
333 #endif /* HAVE_DL_WINDOWS */
334 #endif /* HAVE_DYNAMIC_PLUGINS */