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