]> git.sesse.net Git - vlc/blobdiff - src/modules/os.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / modules / os.c
index 997e780b523a0db418688400817d51087a3386af..9110c79e0bbffd889810eaf935e1157325fb6667 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <vlc_common.h>
 #include <vlc_plugin.h> /* MODULE_SUFFIX */
+#include <vlc_charset.h>
 #include "libvlc.h"
 #include "modules.h"
 
 #include <stdio.h>                                              /* sprintf() */
 #include <string.h>                                              /* strdup() */
 
-#ifdef HAVE_SYS_TYPES_H
-#   include <sys/types.h>
-#endif
+#include <sys/types.h>
 
 #if !defined(HAVE_DYNAMIC_PLUGINS)
     /* no support for plugins */
-#elif defined(HAVE_DL_DYLD)
-#   if defined(HAVE_MACH_O_DYLD_H)
-#       include <mach-o/dyld.h>
-#   endif
 #elif defined(HAVE_DL_BEOS)
 #   if defined(HAVE_IMAGE_H)
 #       include <image.h>
 #   endif
+#elif defined(__APPLE__)
+#   include <dlfcn.h>
 #elif defined(HAVE_DL_WINDOWS)
 #   include <windows.h>
 #elif defined(HAVE_DL_DLOPEN)
 #       include <dl.h>
 #   endif
 #endif
+#ifdef HAVE_VALGRIND_VALGRIND_H
+# include <valgrind/valgrind.h>
+#endif
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
 #ifdef HAVE_DYNAMIC_PLUGINS
-static void * GetSymbol        ( module_handle_t, const char * );
+static void *module_Lookup( module_handle_t, const char * );
 
 #if defined(HAVE_DL_WINDOWS)
 static char * GetWindowsError  ( void );
@@ -90,11 +90,12 @@ int module_Call( vlc_object_t *obj, module_t *p_module )
     int (* pf_symbol) ( module_t * p_module );
 
     /* Try to resolve the symbol */
-    pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
+    pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
+                                                     psz_name );
 
     if( pf_symbol == NULL )
     {
-#if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
+#if defined(HAVE_DL_BEOS)
         msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
                   psz_name, p_module->psz_filename );
 #elif defined(HAVE_DL_WINDOWS)
@@ -128,16 +129,6 @@ int module_Call( vlc_object_t *obj, module_t *p_module )
     return 0;
 }
 
-#if defined (RTLD_NOLOAD)
-/* Make sure libvlccore is in the global namespace */
-static void load_libvlccore( void )
-{
-    if( !dlsym( RTLD_DEFAULT, "vlc_module_create" )
-     && !dlopen( "libvlccore.so", RTLD_GLOBAL|RTLD_NOLOAD ) )
-        fprintf( stderr, "ERROR: failed loading libvlccore\n" );
-}
-#endif
-
 /**
  * Load a dynamically linked library using a system dependent method.
  *
@@ -151,37 +142,7 @@ int module_Load( vlc_object_t *p_this, const char *psz_file,
 {
     module_handle_t handle;
 
-#if defined(HAVE_DL_DYLD)
-    NSObjectFileImage image;
-    NSObjectFileImageReturnCode ret;
-
-    ret = NSCreateObjectFileImageFromFile( psz_file, &image );
-
-    if( ret != NSObjectFileImageSuccess )
-    {
-        msg_Warn( p_this, "cannot create image from `%s'", psz_file );
-        return -1;
-    }
-
-    /* Open the dynamic module */
-    handle = NSLinkModule( image, psz_file,
-                           NSLINKMODULE_OPTION_RETURN_ON_ERROR );
-
-    if( !handle )
-    {
-        NSLinkEditErrors errors;
-        const char *psz_file, *psz_err;
-        int i_errnum;
-        NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
-        msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
-        NSDestroyObjectFileImage( image );
-        return -1;
-    }
-
-    /* Destroy our image, we won't need it */
-    NSDestroyObjectFileImage( image );
-
-#elif defined(HAVE_DL_BEOS)
+#if defined(HAVE_DL_BEOS)
     handle = load_add_on( psz_file );
     if( handle < 0 )
     {
@@ -191,7 +152,7 @@ int module_Load( vlc_object_t *p_this, const char *psz_file,
 
 #elif defined(HAVE_DL_WINDOWS)
     wchar_t psz_wfile[MAX_PATH];
-    MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
+    MultiByteToWideChar( CP_UTF8, 0, psz_file, -1, psz_wfile, MAX_PATH );
 
 #ifndef UNDER_CE
     /* FIXME: this is not thread-safe -- Courmisch */
@@ -214,10 +175,6 @@ int module_Load( vlc_object_t *p_this, const char *psz_file,
     }
 
 #elif defined(HAVE_DL_DLOPEN)
-# if defined (RTLD_NOLOAD)
-    static pthread_once_t once = PTHREAD_ONCE_INIT;
-    pthread_once( &once, &load_libvlccore );
-# endif
 
 # if defined (RTLD_NOW)
     const int flags = RTLD_NOW;
@@ -226,14 +183,16 @@ int module_Load( vlc_object_t *p_this, const char *psz_file,
 # else
     const int flags = 0;
 # endif
+    char *path = ToLocale( psz_file );
 
-    handle = dlopen( psz_file, RTLD_NOW );
+    handle = dlopen( path, flags );
     if( handle == NULL )
     {
-        msg_Warn( p_this, "cannot load module `%s' (%s)",
-                          psz_file, dlerror() );
+        msg_Warn( p_this, "cannot load module `%s' (%s)", path, dlerror() );
+        LocaleFree( path );
         return -1;
     }
+    LocaleFree( path );
 
 #elif defined(HAVE_DL_SHL_LOAD)
     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
@@ -263,21 +222,18 @@ int module_Load( vlc_object_t *p_this, const char *psz_file,
  */
 void module_Unload( module_handle_t handle )
 {
-#if defined(HAVE_DL_DYLD)
-    NSUnLinkModule( handle, FALSE );
-
-#elif defined(HAVE_DL_BEOS)
+#if defined(HAVE_DL_BEOS)
     unload_add_on( handle );
 
 #elif defined(HAVE_DL_WINDOWS)
     FreeLibrary( handle );
 
 #elif defined(HAVE_DL_DLOPEN)
-# ifdef NDEBUG
-    dlclose( handle );
-# else
-    (void)handle;
+# ifdef HAVE_VALGRIND_VALGRIND_H
+    if( RUNNING_ON_VALGRIND > 0 )
+        return; /* do not dlclose() so that we get proper stack traces */
 # endif
+    dlclose( handle );
 
 #elif defined(HAVE_DL_SHL_LOAD)
     shl_unload( handle );
@@ -287,44 +243,19 @@ void module_Unload( module_handle_t handle )
 }
 
 /**
- * GetSymbol: get a symbol from a dynamic library
+ * Looks up a symbol from a dynamically loaded library
  *
  * This function queries a loaded library for a symbol specified in a
  * string, and returns a pointer to it. We don't check for dlerror() or
  * similar functions, since we want a non-NULL symbol anyway.
- * \param handle handle to the module
- * \param psz_function function name
- * \return nothing
+ *
+ * @param handle handle to the module
+ * @param psz_function function name
+ * @return NULL on error, or the address of the symbol
  */
-static void * _module_getsymbol( module_handle_t, const char * );
-
-static void * GetSymbol( module_handle_t handle, const char * psz_function )
+static void *module_Lookup( module_handle_t handle, const char *psz_function )
 {
-    void * p_symbol = _module_getsymbol( handle, psz_function );
-
-    /* MacOS X dl library expects symbols to begin with "_". So do
-     * some other operating systems. That's really lame, but hey, what
-     * can we do ? */
-    if( p_symbol == NULL )
-    {
-        char psz_call[strlen( psz_function ) + 2];
-
-        psz_call[ 0 ] = '_';
-        memcpy( psz_call + 1, psz_function, sizeof (psz_call) - 1 );
-        p_symbol = _module_getsymbol( handle, psz_call );
-    }
-
-    return p_symbol;
-}
-
-static void * _module_getsymbol( module_handle_t handle,
-                                 const char * psz_function )
-{
-#if defined(HAVE_DL_DYLD)
-    NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
-    return NSAddressOfSymbol( sym );
-
-#elif defined(HAVE_DL_BEOS)
+#if defined(HAVE_DL_BEOS)
     void * p_symbol;
     if( B_OK == get_image_symbol( handle, psz_function,
                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
@@ -337,10 +268,13 @@ static void * _module_getsymbol( module_handle_t handle,
     }
 
 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
-    wchar_t psz_real[256];
-    MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
+    wchar_t wide[strlen( psz_function ) + 1];
+    size_t i = 0;
+    do
+        wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
+    while( psz_function[i++] );
 
-    return (void *)GetProcAddress( handle, psz_real );
+    return (void *)GetProcAddress( handle, wide );
 
 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
     return (void *)GetProcAddress( handle, (char *)psz_function );
@@ -357,43 +291,23 @@ static void * _module_getsymbol( module_handle_t handle,
 }
 
 #if defined(HAVE_DL_WINDOWS)
+# include <wchar.h>
+
 static char * GetWindowsError( void )
 {
-#if defined(UNDER_CE)
-    wchar_t psz_tmp[MAX_PATH];
-    char * psz_buffer = malloc( MAX_PATH );
-#else
-    char * psz_tmp = malloc( MAX_PATH );
-#endif
+    wchar_t wmsg[256];
     int i = 0, i_error = GetLastError();
 
-    FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
-                   NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-                   (LPTSTR)psz_tmp, MAX_PATH, NULL );
+    FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
+                    wmsg, 256, NULL );
 
     /* Go to the end of the string */
-    while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
-    {
+    while( !wmemchr( L"\r\n\0", wmsg[i], 3 ) )
         i++;
-    }
-
-    if( psz_tmp[i] )
-    {
-#if defined(UNDER_CE)
-        swprintf( psz_tmp + i, L" (error %i)", i_error );
-        psz_tmp[ 255 ] = L'\0';
-#else
-        snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
-        psz_tmp[ 255 ] = '\0';
-#endif
-    }
 
-#if defined(UNDER_CE)
-    wcstombs( psz_buffer, psz_tmp, MAX_PATH );
-    return psz_buffer;
-#else
-    return psz_tmp;
-#endif
+    snwprintf( wmsg + i, 256 - i, L" (error %i)", i_error );
+    return FromWide( wmsg );
 }
 #endif /* HAVE_DL_WINDOWS */
 #endif /* HAVE_DYNAMIC_PLUGINS */