]> git.sesse.net Git - vlc/blob - src/modules/os.c
Add missing dlopen flag (if and only if _needed_).
[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 "libvlc.h"
34 #include "modules.h"
35
36 #include <stdlib.h>                                      /* free(), strtol() */
37 #include <stdio.h>                                              /* sprintf() */
38 #include <string.h>                                              /* strdup() */
39
40 #ifdef HAVE_SYS_TYPES_H
41 #   include <sys/types.h>
42 #endif
43
44 #if !defined(HAVE_DYNAMIC_PLUGINS)
45     /* no support for plugins */
46 #elif defined(HAVE_DL_DYLD)
47 #   if defined(HAVE_MACH_O_DYLD_H)
48 #       include <mach-o/dyld.h>
49 #   endif
50 #elif defined(HAVE_DL_BEOS)
51 #   if defined(HAVE_IMAGE_H)
52 #       include <image.h>
53 #   endif
54 #elif defined(HAVE_DL_WINDOWS)
55 #   include <windows.h>
56 #elif defined(HAVE_DL_DLOPEN)
57 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
58 #       include <dlfcn.h>
59 #   endif
60 #   if defined(HAVE_SYS_DL_H)
61 #       include <sys/dl.h>
62 #   endif
63 #elif defined(HAVE_DL_SHL_LOAD)
64 #   if defined(HAVE_DL_H)
65 #       include <dl.h>
66 #   endif
67 #endif
68 #ifdef HAVE_VALGRIND_VALGRIND_H
69 # include <valgrind/valgrind.h>
70 #endif
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 #ifdef HAVE_DYNAMIC_PLUGINS
76 static void *module_Lookup( module_handle_t, const char * );
77
78 #if defined(HAVE_DL_WINDOWS)
79 static char * GetWindowsError  ( void );
80 #endif
81
82 /**
83  * module Call
84  *
85  * Call a symbol given its name and a module structure. The symbol MUST
86  * refer to a function returning int and taking a module_t* as an argument.
87  * \param p_module the modules
88  * \return 0 if it pass and -1 in case of a failure
89  */
90 int module_Call( vlc_object_t *obj, module_t *p_module )
91 {
92     static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
93     int (* pf_symbol) ( module_t * p_module );
94
95     /* Try to resolve the symbol */
96     pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
97                                                      psz_name );
98
99     if( pf_symbol == NULL )
100     {
101 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
102         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
103                   psz_name, p_module->psz_filename );
104 #elif defined(HAVE_DL_WINDOWS)
105         char *psz_error = GetWindowsError();
106         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
107                   psz_name, p_module->psz_filename, psz_error );
108         free( psz_error );
109 #elif defined(HAVE_DL_DLOPEN)
110         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
111                   psz_name, p_module->psz_filename, dlerror() );
112 #elif defined(HAVE_DL_SHL_LOAD)
113         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
114                   psz_name, p_module->psz_filename );
115 #else
116 #   error "Something is wrong in modules.c"
117 #endif
118         return -1;
119     }
120
121     /* We can now try to call the symbol */
122     if( pf_symbol( p_module ) != 0 )
123     {
124         /* With a well-written module we shouldn't have to print an
125          * additional error message here, but just make sure. */
126         msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
127                  psz_name, p_module->psz_filename );
128         return -1;
129     }
130
131     /* Everything worked fine, we can return */
132     return 0;
133 }
134
135 #if defined (RTLD_NOLOAD)
136 /* Make sure libvlccore is in the global namespace */
137 static void load_libvlccore( void )
138 {
139     if( !dlsym( RTLD_DEFAULT, "libvlc_Quit" )
140      && !dlopen( "libvlccore.so", RTLD_LAZY|RTLD_GLOBAL|RTLD_NOLOAD ) )
141         fprintf( stderr, "ERROR: failed loading libvlccore\n" );
142 }
143 #endif
144
145 /**
146  * Load a dynamically linked library using a system dependent method.
147  *
148  * \param p_this vlc object
149  * \param psz_file library file
150  * \param p_handle the module handle returned
151  * \return 0 on success as well as the module handle.
152  */
153 int module_Load( vlc_object_t *p_this, const char *psz_file,
154                  module_handle_t *p_handle )
155 {
156     module_handle_t handle;
157
158 #if defined(HAVE_DL_DYLD)
159     NSObjectFileImage image;
160     NSObjectFileImageReturnCode ret;
161
162     ret = NSCreateObjectFileImageFromFile( psz_file, &image );
163
164     if( ret != NSObjectFileImageSuccess )
165     {
166         msg_Warn( p_this, "cannot create image from `%s'", psz_file );
167         return -1;
168     }
169
170     /* Open the dynamic module */
171     handle = NSLinkModule( image, psz_file,
172                            NSLINKMODULE_OPTION_RETURN_ON_ERROR );
173
174     if( !handle )
175     {
176         NSLinkEditErrors errors;
177         const char *psz_file, *psz_err;
178         int i_errnum;
179         NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
180         msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
181         NSDestroyObjectFileImage( image );
182         return -1;
183     }
184
185     /* Destroy our image, we won't need it */
186     NSDestroyObjectFileImage( image );
187
188 #elif defined(HAVE_DL_BEOS)
189     handle = load_add_on( psz_file );
190     if( handle < 0 )
191     {
192         msg_Warn( p_this, "cannot load module `%s'", psz_file );
193         return -1;
194     }
195
196 #elif defined(HAVE_DL_WINDOWS)
197     wchar_t psz_wfile[MAX_PATH];
198     MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
199
200 #ifndef UNDER_CE
201     /* FIXME: this is not thread-safe -- Courmisch */
202     UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
203     SetErrorMode (mode|SEM_FAILCRITICALERRORS);
204 #endif
205
206     handle = LoadLibraryW( psz_wfile );
207
208 #ifndef UNDER_CE
209     SetErrorMode (mode);
210 #endif
211
212     if( handle == NULL )
213     {
214         char *psz_err = GetWindowsError();
215         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
216         free( psz_err );
217         return -1;
218     }
219
220 #elif defined(HAVE_DL_DLOPEN)
221 # if defined (RTLD_NOLOAD)
222     static pthread_once_t once = PTHREAD_ONCE_INIT;
223     pthread_once( &once, &load_libvlccore );
224 # endif
225
226 # if defined (RTLD_NOW)
227     const int flags = RTLD_NOW;
228 # elif defined (DL_LAZY)
229     const int flags = DL_LAZY;
230 # else
231     const int flags = 0;
232 # endif
233
234     handle = dlopen( psz_file, flags );
235     if( handle == NULL )
236     {
237         msg_Warn( p_this, "cannot load module `%s' (%s)",
238                           psz_file, dlerror() );
239         return -1;
240     }
241
242 #elif defined(HAVE_DL_SHL_LOAD)
243     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
244     if( handle == NULL )
245     {
246         msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
247         return -1;
248     }
249
250 #else
251 #   error "Something is wrong in modules.c"
252
253 #endif
254
255     *p_handle = handle;
256     return 0;
257 }
258
259 /**
260  * CloseModule: unload a dynamic library
261  *
262  * This function unloads a previously opened dynamically linked library
263  * using a system dependent method. No return value is taken in consideration,
264  * since some libraries sometimes refuse to close properly.
265  * \param handle handle of the library
266  * \return nothing
267  */
268 void module_Unload( module_handle_t handle )
269 {
270 #if defined(HAVE_DL_DYLD)
271     NSUnLinkModule( handle, FALSE );
272
273 #elif defined(HAVE_DL_BEOS)
274     unload_add_on( handle );
275
276 #elif defined(HAVE_DL_WINDOWS)
277     FreeLibrary( handle );
278
279 #elif defined(HAVE_DL_DLOPEN)
280 # ifdef HAVE_VALGRIND_VALGRIND_H
281     if( RUNNING_ON_VALGRIND > 0 )
282         return; /* do not dlclose() so that we get proper stack traces */
283 # endif
284     dlclose( handle );
285
286 #elif defined(HAVE_DL_SHL_LOAD)
287     shl_unload( handle );
288
289 #endif
290     return;
291 }
292
293 /**
294  * Looks up a symbol from a dynamically loaded library
295  *
296  * This function queries a loaded library for a symbol specified in a
297  * string, and returns a pointer to it. We don't check for dlerror() or
298  * similar functions, since we want a non-NULL symbol anyway.
299  *
300  * @param handle handle to the module
301  * @param psz_function function name
302  * @return NULL on error, or the address of the symbol
303  */
304 static void *module_Lookup( module_handle_t handle, const char *psz_function )
305 {
306 #if defined(HAVE_DL_DYLD)
307     char psz_call[strlen( psz_function ) + 2];
308     psz_call[0] = '_';
309     memcpy( psz_call + 1, psz_function, sizeof( psz_call ) - 1 );
310
311     NSSymbol sym = NSLookupSymbolInModule( handle, psz_call );
312     return NSAddressOfSymbol( sym );
313
314 #elif defined(HAVE_DL_BEOS)
315     void * p_symbol;
316     if( B_OK == get_image_symbol( handle, psz_function,
317                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
318     {
319         return p_symbol;
320     }
321     else
322     {
323         return NULL;
324     }
325
326 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
327     wchar_t wide[strlen( psz_function ) + 1];
328     size_t i = 0;
329     do
330         wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
331     while( psz_function[i++] );
332
333     return (void *)GetProcAddress( handle, wide );
334
335 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
336     return (void *)GetProcAddress( handle, (char *)psz_function );
337
338 #elif defined(HAVE_DL_DLOPEN)
339     return dlsym( handle, psz_function );
340
341 #elif defined(HAVE_DL_SHL_LOAD)
342     void *p_sym;
343     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
344     return p_sym;
345
346 #endif
347 }
348
349 #if defined(HAVE_DL_WINDOWS)
350 static char * GetWindowsError( void )
351 {
352 #if defined(UNDER_CE)
353     wchar_t psz_tmp[MAX_PATH];
354     char * psz_buffer = malloc( MAX_PATH );
355 #else
356     char * psz_tmp = malloc( MAX_PATH );
357 #endif
358     int i = 0, i_error = GetLastError();
359
360     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
361                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
362                    (LPTSTR)psz_tmp, MAX_PATH, NULL );
363
364     /* Go to the end of the string */
365     while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
366     {
367         i++;
368     }
369
370     if( psz_tmp[i] )
371     {
372 #if defined(UNDER_CE)
373         swprintf( psz_tmp + i, L" (error %i)", i_error );
374         psz_tmp[ 255 ] = L'\0';
375 #else
376         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
377         psz_tmp[ 255 ] = '\0';
378 #endif
379     }
380
381 #if defined(UNDER_CE)
382     wcstombs( psz_buffer, psz_tmp, MAX_PATH );
383     return psz_buffer;
384 #else
385     return psz_tmp;
386 #endif
387 }
388 #endif /* HAVE_DL_WINDOWS */
389 #endif /* HAVE_DYNAMIC_PLUGINS */