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