]> git.sesse.net Git - vlc/blob - src/posix/plugin.c
Fix Metacube header handling with multiple header blocks.
[vlc] / src / posix / plugin.c
1 /*****************************************************************************
2  * plugin.c : Low-level dynamic library handling
3  *****************************************************************************
4  * Copyright (C) 2001-2007 VLC authors and VideoLAN
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 it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * 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 "modules/modules.h"
33
34 #include <sys/types.h>
35 #include <dlfcn.h>
36
37 #ifdef HAVE_VALGRIND_VALGRIND_H
38 # include <valgrind/valgrind.h>
39 #endif
40
41 /**
42  * Load a dynamically linked library using a system dependent method.
43  *
44  * \param p_this vlc object
45  * \param path library file
46  * \param p_handle the module handle returned
47  * \return 0 on success as well as the module handle.
48  */
49 int module_Load (vlc_object_t *p_this, const char *path,
50                  module_handle_t *p_handle, bool lazy)
51 {
52 #if defined (RTLD_NOW)
53     const int flags = lazy ? RTLD_LAZY : RTLD_NOW;
54 #elif defined (DL_LAZY)
55     const int flags = DL_LAZY;
56 #else
57     const int flags = 0;
58 #endif
59
60     module_handle_t handle = dlopen (path, flags);
61     if( handle == NULL )
62     {
63         msg_Warn( p_this, "cannot load module `%s' (%s)", path, dlerror() );
64         return -1;
65     }
66     *p_handle = handle;
67     return 0;
68 }
69
70 /**
71  * CloseModule: unload a dynamic library
72  *
73  * This function unloads a previously opened dynamically linked library
74  * using a system dependent method. No return value is taken in consideration,
75  * since some libraries sometimes refuse to close properly.
76  * \param handle handle of the library
77  * \return nothing
78  */
79 void module_Unload( module_handle_t handle )
80 {
81 #ifdef HAVE_VALGRIND_VALGRIND_H
82     if( RUNNING_ON_VALGRIND > 0 )
83         return; /* do not dlclose() so that we get proper stack traces */
84 #endif
85     dlclose( handle );
86 }
87
88 /**
89  * Looks up a symbol from a dynamically loaded library
90  *
91  * This function queries a loaded library for a symbol specified in a
92  * string, and returns a pointer to it. We don't check for dlerror() or
93  * similar functions, since we want a non-NULL symbol anyway.
94  *
95  * @param handle handle to the module
96  * @param psz_function function name
97  * @return NULL on error, or the address of the symbol
98  */
99 void *module_Lookup( module_handle_t handle, const char *psz_function )
100 {
101     return dlsym( handle, psz_function );
102 }