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