]> git.sesse.net Git - vlc/blob - src/modules/os.c
AllocatePluginDir: remove dummy b_die check
[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_BEOS)
47 #   if defined(HAVE_IMAGE_H)
48 #       include <image.h>
49 #   endif
50 #elif defined(__APPLE__)
51 #   include <dlfcn.h>
52 #elif defined(HAVE_DL_WINDOWS)
53 #   include <windows.h>
54 #elif defined(HAVE_DL_DLOPEN)
55 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
56 #       include <dlfcn.h>
57 #   endif
58 #   if defined(HAVE_SYS_DL_H)
59 #       include <sys/dl.h>
60 #   endif
61 #elif defined(HAVE_DL_SHL_LOAD)
62 #   if defined(HAVE_DL_H)
63 #       include <dl.h>
64 #   endif
65 #endif
66 #ifdef HAVE_VALGRIND_VALGRIND_H
67 # include <valgrind/valgrind.h>
68 #endif
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 #ifdef HAVE_DYNAMIC_PLUGINS
74 static void *module_Lookup( module_handle_t, const char * );
75
76 #if defined(HAVE_DL_WINDOWS)
77 static char * GetWindowsError  ( void );
78 #endif
79
80 /**
81  * module Call
82  *
83  * Call a symbol given its name and a module structure. The symbol MUST
84  * refer to a function returning int and taking a module_t* as an argument.
85  * \param p_module the modules
86  * \return 0 if it pass and -1 in case of a failure
87  */
88 int module_Call( vlc_object_t *obj, module_t *p_module )
89 {
90     static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
91     int (* pf_symbol) ( module_t * p_module );
92
93     /* Try to resolve the symbol */
94     pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
95                                                      psz_name );
96
97     if( pf_symbol == NULL )
98     {
99 #if defined(HAVE_DL_BEOS)
100         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
101                   psz_name, p_module->psz_filename );
102 #elif defined(HAVE_DL_WINDOWS)
103         char *psz_error = GetWindowsError();
104         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
105                   psz_name, p_module->psz_filename, psz_error );
106         free( psz_error );
107 #elif defined(HAVE_DL_DLOPEN)
108         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
109                   psz_name, p_module->psz_filename, dlerror() );
110 #elif defined(HAVE_DL_SHL_LOAD)
111         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
112                   psz_name, p_module->psz_filename );
113 #else
114 #   error "Something is wrong in modules.c"
115 #endif
116         return -1;
117     }
118
119     /* We can now try to call the symbol */
120     if( pf_symbol( p_module ) != 0 )
121     {
122         /* With a well-written module we shouldn't have to print an
123          * additional error message here, but just make sure. */
124         msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
125                  psz_name, p_module->psz_filename );
126         return -1;
127     }
128
129     /* Everything worked fine, we can return */
130     return 0;
131 }
132
133 /**
134  * Load a dynamically linked library using a system dependent method.
135  *
136  * \param p_this vlc object
137  * \param psz_file library file
138  * \param p_handle the module handle returned
139  * \return 0 on success as well as the module handle.
140  */
141 int module_Load( vlc_object_t *p_this, const char *psz_file,
142                  module_handle_t *p_handle )
143 {
144     module_handle_t handle;
145
146 #if defined(HAVE_DL_BEOS)
147     handle = load_add_on( psz_file );
148     if( handle < 0 )
149     {
150         msg_Warn( p_this, "cannot load module `%s'", psz_file );
151         return -1;
152     }
153
154 #elif defined(HAVE_DL_WINDOWS)
155     wchar_t psz_wfile[MAX_PATH];
156     MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
157
158 #ifndef UNDER_CE
159     /* FIXME: this is not thread-safe -- Courmisch */
160     UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
161     SetErrorMode (mode|SEM_FAILCRITICALERRORS);
162 #endif
163
164     handle = LoadLibraryW( psz_wfile );
165
166 #ifndef UNDER_CE
167     SetErrorMode (mode);
168 #endif
169
170     if( handle == NULL )
171     {
172         char *psz_err = GetWindowsError();
173         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
174         free( psz_err );
175         return -1;
176     }
177
178 #elif defined(HAVE_DL_DLOPEN)
179
180 # if defined (RTLD_NOW)
181     const int flags = RTLD_NOW;
182 # elif defined (DL_LAZY)
183     const int flags = DL_LAZY;
184 # else
185     const int flags = 0;
186 # endif
187
188     handle = dlopen( psz_file, flags );
189     if( handle == NULL )
190     {
191         msg_Warn( p_this, "cannot load module `%s' (%s)",
192                           psz_file, dlerror() );
193         return -1;
194     }
195
196 #elif defined(HAVE_DL_SHL_LOAD)
197     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
198     if( handle == NULL )
199     {
200         msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
201         return -1;
202     }
203
204 #else
205 #   error "Something is wrong in modules.c"
206
207 #endif
208
209     *p_handle = handle;
210     return 0;
211 }
212
213 /**
214  * CloseModule: unload a dynamic library
215  *
216  * This function unloads a previously opened dynamically linked library
217  * using a system dependent method. No return value is taken in consideration,
218  * since some libraries sometimes refuse to close properly.
219  * \param handle handle of the library
220  * \return nothing
221  */
222 void module_Unload( module_handle_t handle )
223 {
224 #if defined(HAVE_DL_BEOS)
225     unload_add_on( handle );
226
227 #elif defined(HAVE_DL_WINDOWS)
228     FreeLibrary( handle );
229
230 #elif defined(HAVE_DL_DLOPEN)
231 # ifdef HAVE_VALGRIND_VALGRIND_H
232     if( RUNNING_ON_VALGRIND > 0 )
233         return; /* do not dlclose() so that we get proper stack traces */
234 # endif
235     dlclose( handle );
236
237 #elif defined(HAVE_DL_SHL_LOAD)
238     shl_unload( handle );
239
240 #endif
241     return;
242 }
243
244 /**
245  * Looks up a symbol from a dynamically loaded library
246  *
247  * This function queries a loaded library for a symbol specified in a
248  * string, and returns a pointer to it. We don't check for dlerror() or
249  * similar functions, since we want a non-NULL symbol anyway.
250  *
251  * @param handle handle to the module
252  * @param psz_function function name
253  * @return NULL on error, or the address of the symbol
254  */
255 static void *module_Lookup( module_handle_t handle, const char *psz_function )
256 {
257 #if defined(HAVE_DL_BEOS)
258     void * p_symbol;
259     if( B_OK == get_image_symbol( handle, psz_function,
260                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
261     {
262         return p_symbol;
263     }
264     else
265     {
266         return NULL;
267     }
268
269 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
270     wchar_t wide[strlen( psz_function ) + 1];
271     size_t i = 0;
272     do
273         wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
274     while( psz_function[i++] );
275
276     return (void *)GetProcAddress( handle, wide );
277
278 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
279     return (void *)GetProcAddress( handle, (char *)psz_function );
280
281 #elif defined(HAVE_DL_DLOPEN)
282     return dlsym( handle, psz_function );
283
284 #elif defined(HAVE_DL_SHL_LOAD)
285     void *p_sym;
286     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
287     return p_sym;
288
289 #endif
290 }
291
292 #if defined(HAVE_DL_WINDOWS)
293 static char * GetWindowsError( void )
294 {
295 #if defined(UNDER_CE)
296     wchar_t psz_tmp[MAX_PATH];
297     char * psz_buffer = malloc( MAX_PATH );
298 #else
299     char * psz_tmp = malloc( MAX_PATH );
300 #endif
301     int i = 0, i_error = GetLastError();
302
303     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
304                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
305                    (LPTSTR)psz_tmp, MAX_PATH, NULL );
306
307     /* Go to the end of the string */
308     while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
309     {
310         i++;
311     }
312
313     if( psz_tmp[i] )
314     {
315 #if defined(UNDER_CE)
316         swprintf( psz_tmp + i, L" (error %i)", i_error );
317         psz_tmp[ 255 ] = L'\0';
318 #else
319         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
320         psz_tmp[ 255 ] = '\0';
321 #endif
322     }
323
324 #if defined(UNDER_CE)
325     wcstombs( psz_buffer, psz_tmp, MAX_PATH );
326     return psz_buffer;
327 #else
328     return psz_tmp;
329 #endif
330 }
331 #endif /* HAVE_DL_WINDOWS */
332 #endif /* HAVE_DYNAMIC_PLUGINS */