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