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