1 /*****************************************************************************
2 * os.c : Low-level dynamic library handling
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "modules/modules.h"
35 #include <sys/types.h>
38 #ifdef HAVE_VALGRIND_VALGRIND_H
39 # include <valgrind/valgrind.h>
43 * Load a dynamically linked library using a system dependent method.
45 * \param p_this vlc object
46 * \param psz_file library file
47 * \param p_handle the module handle returned
48 * \return 0 on success as well as the module handle.
50 int module_Load( vlc_object_t *p_this, const char *psz_file,
51 module_handle_t *p_handle, bool lazy )
53 #if defined (RTLD_NOW)
54 const int flags = lazy ? RTLD_LAZY : RTLD_NOW;
55 #elif defined (DL_LAZY)
56 const int flags = DL_LAZY;
60 char *path = ToLocale( psz_file );
62 module_handle_t handle = dlopen( path, flags );
65 msg_Warn( p_this, "cannot load module `%s' (%s)", path, dlerror() );
75 * CloseModule: unload a dynamic library
77 * This function unloads a previously opened dynamically linked library
78 * using a system dependent method. No return value is taken in consideration,
79 * since some libraries sometimes refuse to close properly.
80 * \param handle handle of the library
83 void module_Unload( module_handle_t handle )
85 #ifdef HAVE_VALGRIND_VALGRIND_H
86 if( RUNNING_ON_VALGRIND > 0 )
87 return; /* do not dlclose() so that we get proper stack traces */
93 * Looks up a symbol from a dynamically loaded library
95 * This function queries a loaded library for a symbol specified in a
96 * string, and returns a pointer to it. We don't check for dlerror() or
97 * similar functions, since we want a non-NULL symbol anyway.
99 * @param handle handle to the module
100 * @param psz_function function name
101 * @return NULL on error, or the address of the symbol
103 void *module_Lookup( module_handle_t handle, const char *psz_function )
105 return dlsym( handle, psz_function );