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