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