1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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 *****************************************************************************/
29 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
30 * is set to 64. Don't try to be cleverer. */
31 #ifdef _FILE_OFFSET_BITS
32 #undef _FILE_OFFSET_BITS
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* sprintf() */
37 #include <string.h> /* strdup() */
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
53 #if !defined(HAVE_DYNAMIC_PLUGINS)
54 /* no support for plugins */
55 #elif defined(HAVE_DL_DYLD)
56 # if defined(HAVE_MACH_O_DYLD_H)
57 # include <mach-o/dyld.h>
59 #elif defined(HAVE_DL_BEOS)
60 # if defined(HAVE_IMAGE_H)
63 #elif defined(HAVE_DL_WINDOWS)
65 #elif defined(HAVE_DL_DLOPEN)
66 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
69 # if defined(HAVE_SYS_DL_H)
72 #elif defined(HAVE_DL_SHL_LOAD)
73 # if defined(HAVE_DL_H)
78 #include "misc/configuration.h"
80 #include "vlc_interface.h"
81 #include "vlc_playlist.h"
83 #include "vlc_stream.h"
84 #include "vlc_access.h"
85 #include "vlc_demux.h"
88 #include "vlc_vout_synchro.h"
93 #include "vlc_httpd.h"
100 #include "iso_lang.h"
101 #include "vlc_charset.h"
103 #include "vlc_block.h"
107 #include "vlc_image.h"
110 #include "vlc_update.h"
111 #include "vlc_strings.h"
112 #include "vlc_streaming.h"
116 #if defined( _MSC_VER ) && defined( UNDER_CE )
117 # include "modules_builtin_evc.h"
118 #elif defined( _MSC_VER )
119 # include "modules_builtin_msvc.h"
121 # include "modules_builtin.h"
123 #include "vlc_network.h"
125 #if defined( WIN32 ) || defined( UNDER_CE )
126 /* Avoid name collisions */
127 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
130 /*****************************************************************************
132 *****************************************************************************/
133 #ifdef HAVE_DYNAMIC_PLUGINS
134 static void AllocateAllPlugins ( vlc_object_t * );
135 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
136 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
137 static module_t * AllocatePlugin( vlc_object_t *, char * );
139 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
140 static int DeleteModule ( module_t * );
141 #ifdef HAVE_DYNAMIC_PLUGINS
142 static void DupModule ( module_t * );
143 static void UndupModule ( module_t * );
144 static int CallEntry ( module_t * );
145 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
146 static void CloseModule ( module_handle_t );
147 static void * GetSymbol ( module_handle_t, const char * );
148 static void CacheLoad ( vlc_object_t * );
149 static int CacheLoadConfig ( module_t *, FILE * );
150 static void CacheSave ( vlc_object_t * );
151 static void CacheSaveConfig ( module_t *, FILE * );
152 static char * CacheName ( void );
153 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
154 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
156 #if defined(HAVE_DL_WINDOWS)
157 static char * GetWindowsError ( void );
161 static void module_LoadMain( vlc_object_t *p_this );
164 /* Sub-version number
165 * (only used to avoid breakage in dev version when cache structure changes) */
166 #define CACHE_SUBVERSION_NUM 1
168 /*****************************************************************************
169 * module_InitBank: create the module bank.
170 *****************************************************************************
171 * This function creates a module bank structure which will be filled later
172 * on with all the modules found.
173 *****************************************************************************/
174 void __module_InitBank( vlc_object_t *p_this )
176 module_bank_t *p_bank;
179 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
180 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
181 vlc_mutex_lock( lockval.p_address );
182 if( p_this->p_libvlc_global->p_module_bank )
184 p_this->p_libvlc_global->p_module_bank->i_usage++;
185 vlc_mutex_unlock( lockval.p_address );
186 var_Destroy( p_this->p_libvlc_global, "libvlc" );
189 vlc_mutex_unlock( lockval.p_address );
190 var_Destroy( p_this->p_libvlc_global, "libvlc" );
192 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
193 p_bank->psz_object_name = "module bank";
195 p_bank->i_cache = p_bank->i_loaded_cache = 0;
196 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
197 p_bank->b_cache = p_bank->b_cache_dirty =
198 p_bank->b_cache_delete = VLC_FALSE;
201 * Store the symbols to be exported
203 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
204 STORE_SYMBOLS( &p_bank->symbols );
207 /* Everything worked, attach the object */
208 p_this->p_libvlc_global->p_module_bank = p_bank;
209 vlc_object_attach( p_bank, p_this->p_libvlc_global );
211 module_LoadMain( p_this );
216 /*****************************************************************************
217 * module_ResetBank: reset the module bank.
218 *****************************************************************************
219 * This function resets the module bank by unloading all unused plugin
221 *****************************************************************************/
222 void __module_ResetBank( vlc_object_t *p_this )
224 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
228 /*****************************************************************************
229 * module_EndBank: empty the module bank.
230 *****************************************************************************
231 * This function unloads all unused plugin modules and empties the module
232 * bank in case of success.
233 *****************************************************************************/
234 void __module_EndBank( vlc_object_t *p_this )
239 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
240 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
241 vlc_mutex_lock( lockval.p_address );
242 if( !p_this->p_libvlc_global->p_module_bank )
244 vlc_mutex_unlock( lockval.p_address );
245 var_Destroy( p_this->p_libvlc_global, "libvlc" );
248 if( --p_this->p_libvlc_global->p_module_bank->i_usage )
250 vlc_mutex_unlock( lockval.p_address );
251 var_Destroy( p_this->p_libvlc_global, "libvlc" );
254 vlc_mutex_unlock( lockval.p_address );
255 var_Destroy( p_this->p_libvlc_global, "libvlc" );
257 config_AutoSaveConfigFile( p_this );
259 #ifdef HAVE_DYNAMIC_PLUGINS
260 #define p_bank p_this->p_libvlc_global->p_module_bank
261 if( p_bank->b_cache ) CacheSave( p_this );
262 while( p_bank->i_loaded_cache-- )
264 DeleteModule (p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module);
265 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
266 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
268 if( p_bank->pp_loaded_cache )
269 free( p_bank->pp_loaded_cache );
271 while( p_bank->i_cache-- )
273 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
274 free( p_bank->pp_cache[p_bank->i_cache] );
276 if( p_bank->pp_cache )
277 free( p_bank->pp_cache );
281 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
283 while( p_this->p_libvlc_global->p_module_bank->i_children )
285 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
287 if( DeleteModule( p_next ) )
289 /* Module deletion failed */
290 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
291 p_next->psz_object_name );
293 /* We just free the module by hand. Niahahahahaha. */
294 vlc_object_detach( p_next );
295 vlc_object_destroy( p_next );
299 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
300 p_this->p_libvlc_global->p_module_bank = NULL;
305 /*****************************************************************************
306 * module_LoadMain: load the main program info into the module bank.
307 *****************************************************************************
308 * This function fills the module bank structure with the main module infos.
309 * This is very useful as it will allow us to consider the main program just
310 * as another module, and for instance the configuration options of main will
311 * be available in the module bank structure just as for every other module.
312 *****************************************************************************/
313 static void module_LoadMain( vlc_object_t *p_this )
317 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
318 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
319 vlc_mutex_lock( lockval.p_address );
320 if( p_this->p_libvlc_global->p_module_bank->b_main )
322 vlc_mutex_unlock( lockval.p_address );
323 var_Destroy( p_this->p_libvlc_global, "libvlc" );
326 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
327 vlc_mutex_unlock( lockval.p_address );
328 var_Destroy( p_this->p_libvlc_global, "libvlc" );
330 AllocateBuiltinModule( p_this, vlc_entry__main );
333 /*****************************************************************************
334 * module_LoadBuiltins: load all modules which we built with.
335 *****************************************************************************
336 * This function fills the module bank structure with the builtin modules.
337 *****************************************************************************/
338 void __module_LoadBuiltins( vlc_object_t * p_this )
342 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
343 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
344 vlc_mutex_lock( lockval.p_address );
345 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
347 vlc_mutex_unlock( lockval.p_address );
348 var_Destroy( p_this->p_libvlc_global, "libvlc" );
351 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
352 vlc_mutex_unlock( lockval.p_address );
353 var_Destroy( p_this->p_libvlc_global, "libvlc" );
355 msg_Dbg( p_this, "checking builtin modules" );
356 ALLOCATE_ALL_BUILTINS();
359 /*****************************************************************************
360 * module_LoadPlugins: load all plugin modules we can find.
361 *****************************************************************************
362 * This function fills the module bank structure with the plugin modules.
363 *****************************************************************************/
364 void __module_LoadPlugins( vlc_object_t * p_this )
366 #ifdef HAVE_DYNAMIC_PLUGINS
369 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
370 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
371 vlc_mutex_lock( lockval.p_address );
372 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
374 vlc_mutex_unlock( lockval.p_address );
375 var_Destroy( p_this->p_libvlc_global, "libvlc" );
378 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
379 vlc_mutex_unlock( lockval.p_address );
380 var_Destroy( p_this->p_libvlc_global, "libvlc" );
382 msg_Dbg( p_this, "checking plugin modules" );
384 if( config_GetInt( p_this, "plugins-cache" ) )
385 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
387 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
388 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
390 AllocateAllPlugins( p_this );
394 /*****************************************************************************
395 * module_Need: return the best module function, given a capability list.
396 *****************************************************************************
397 * This function returns the module that best fits the asked capabilities.
398 *****************************************************************************/
399 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
400 const char *psz_name, vlc_bool_t b_strict )
402 typedef struct module_list_t module_list_t;
409 module_list_t *p_next;
412 module_list_t *p_list, *p_first, *p_tmp;
415 int i_which_module, i_index = 0;
416 vlc_bool_t b_intf = VLC_FALSE;
421 char *psz_shortcuts = NULL, *psz_var = NULL;
422 vlc_bool_t b_force_backup = p_this->b_force;
425 /* Deal with variables */
426 if( psz_name && psz_name[0] == '$' )
429 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
430 var_Get( p_this, psz_name + 1, &val );
431 psz_var = val.psz_string;
435 /* Count how many different shortcuts were asked for */
436 if( psz_name && *psz_name )
438 char *psz_parser, *psz_last_shortcut;
440 /* If the user wants none, give him none. */
441 if( !strcmp( psz_name, "none" ) )
443 if( psz_var ) free( psz_var );
448 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
450 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
452 if( *psz_parser == ',' )
456 psz_last_shortcut = psz_parser + 1;
460 /* Check if the user wants to override the "strict" mode */
461 if( psz_last_shortcut )
463 if( !strcmp(psz_last_shortcut, "none") )
468 else if( !strcmp(psz_last_shortcut, "any") )
470 b_strict = VLC_FALSE;
476 /* Sort the modules and test them */
477 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
478 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
481 /* Parse the module list for capabilities and probe each of them */
482 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
484 int i_shortcut_bonus = 0;
486 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
488 /* Test that this module can do what we need */
489 if( strcmp( p_module->psz_capability, psz_capability ) )
491 /* Don't recurse through the sub-modules because vlc_list_find()
492 * will list them anyway. */
496 /* Test if we have the required CPU */
497 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
502 /* If we required a shortcut, check this plugin provides it. */
503 if( i_shortcuts > 0 )
506 const char *psz_name = psz_shortcuts;
508 /* Let's drop modules with a <= 0 score (unless they are
509 * explicitly requested) */
510 b_trash = p_module->i_score <= 0;
512 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
514 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
516 if( !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
519 i_shortcut_bonus = i_short * 10000;
524 /* Go to the next shortcut... This is so lame! */
525 psz_name += strlen( psz_name ) + 1;
528 /* If we are in "strict" mode and we couldn't
529 * find the module in the list of provided shortcuts,
530 * then kick the bastard out of here!!! */
534 /* If we didn't require a shortcut, trash <= 0 scored plugins */
535 else if( p_module->i_score <= 0 )
542 /* Special case: test if we requested a particular intf plugin */
543 if( !i_shortcuts && p_module->psz_program
544 && !strcmp( psz_capability, "interface" )
545 && !strcmp( p_module->psz_program,
546 p_this->p_libvlc->psz_object_name ) )
550 /* Remove previous non-matching plugins */
557 /* This one doesn't match */
561 /* Store this new module */
562 p_list[ i_index ].p_module = p_module;
563 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
564 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
566 /* Add it to the modules-to-probe list */
569 p_list[ 0 ].p_next = NULL;
574 /* Ok, so at school you learned that quicksort is quick, and
575 * bubble sort sucks raw eggs. But that's when dealing with
576 * thousands of items. Here we have barely 50. */
577 module_list_t *p_newlist = p_first;
579 if( p_first->i_score < p_list[ i_index ].i_score )
581 p_list[ i_index ].p_next = p_first;
582 p_first = &p_list[ i_index ];
586 while( p_newlist->p_next != NULL &&
587 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
589 p_newlist = p_newlist->p_next;
592 p_list[ i_index ].p_next = p_newlist->p_next;
593 p_newlist->p_next = &p_list[ i_index ];
600 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
601 i_index, i_index == 1 ? "" : "s" );
603 /* Lock all candidate modules */
605 while( p_tmp != NULL )
607 vlc_object_yield( p_tmp->p_module );
608 p_tmp = p_tmp->p_next;
611 /* We can release the list, interesting modules were yielded */
612 vlc_list_release( p_all );
614 /* Parse the linked list and use the first successful module */
616 while( p_tmp != NULL )
618 #ifdef HAVE_DYNAMIC_PLUGINS
619 /* Make sure the module is loaded in mem */
620 module_t *p_module = p_tmp->p_module;
621 if( p_module->b_submodule )
622 p_module = (module_t *)p_module->p_parent;
624 if( !p_module->b_builtin && !p_module->b_loaded )
626 module_t *p_new_module =
627 AllocatePlugin( p_this, p_module->psz_filename );
630 CacheMerge( p_this, p_module, p_new_module );
631 vlc_object_attach( p_new_module, p_module );
632 DeleteModule( p_new_module );
637 p_this->b_force = p_tmp->b_force;
638 if( p_tmp->p_module->pf_activate
639 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
644 vlc_object_release( p_tmp->p_module );
645 p_tmp = p_tmp->p_next;
648 /* Store the locked module value */
651 p_module = p_tmp->p_module;
652 p_tmp = p_tmp->p_next;
659 /* Unlock the remaining modules */
660 while( p_tmp != NULL )
662 vlc_object_release( p_tmp->p_module );
663 p_tmp = p_tmp->p_next;
667 p_this->b_force = b_force_backup;
669 if( p_module != NULL )
671 msg_Dbg( p_this, "using %s module \"%s\"",
672 psz_capability, p_module->psz_object_name );
674 else if( p_first == NULL )
676 if( !strcmp( psz_capability, "access_demux" ) )
678 msg_Warn( p_this, "no %s module matched \"%s\"",
679 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
683 msg_Err( p_this, "no %s module matched \"%s\"",
684 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
686 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
687 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
690 else if( psz_name != NULL && *psz_name )
692 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
693 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
696 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
700 free( psz_shortcuts );
708 /* Don't forget that the module is still locked */
712 /*****************************************************************************
713 * module_Unneed: decrease the usage count of a module.
714 *****************************************************************************
715 * This function must be called by the thread that called module_Need, to
716 * decrease the reference count and allow for hiding of modules.
717 *****************************************************************************/
718 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
720 /* Use the close method */
721 if( p_module->pf_deactivate )
723 p_module->pf_deactivate( p_this );
726 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
728 vlc_object_release( p_module );
733 /*****************************************************************************
734 * Following functions are local.
735 *****************************************************************************/
737 /*****************************************************************************
738 * AllocateAllPlugins: load all plugin modules we can find.
739 *****************************************************************************/
740 #ifdef HAVE_DYNAMIC_PLUGINS
741 static void AllocateAllPlugins( vlc_object_t *p_this )
743 /* Yes, there are two NULLs because we replace one with "plugin-path". */
744 #if defined( WIN32 ) || defined( UNDER_CE )
745 const char *path[] = { "modules", "", "plugins", NULL, NULL };
747 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
750 const char *const *ppsz_path;
752 /* If the user provided a plugin path, we add it to the list */
753 char *userpath = config_GetPsz( p_this, "plugin-path" );
754 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
756 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
760 if (!**ppsz_path) continue;
762 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
764 /* Handle relative as well as absolute paths */
766 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
767 (*ppsz_path)[1] != ':' )
769 if( (*ppsz_path)[0] != '/' )
772 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
773 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
778 psz_fullpath = strdup( *ppsz_path );
780 if( psz_fullpath == NULL )
783 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
785 /* Don't go deeper than 5 subdirectories */
786 AllocatePluginDir( p_this, psz_fullpath, 5 );
788 free( psz_fullpath );
791 /* Free plugin-path */
792 if( userpath != NULL )
796 /*****************************************************************************
797 * AllocatePluginDir: recursively parse a directory to look for plugins
798 *****************************************************************************/
799 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
802 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
808 #if defined( UNDER_CE ) || defined( _MSC_VER )
810 wchar_t psz_wpath[MAX_PATH + 256];
811 wchar_t psz_wdir[MAX_PATH];
813 char psz_path[MAX_PATH + 256];
814 WIN32_FIND_DATA finddata;
820 struct dirent * file;
824 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
829 #if defined( UNDER_CE ) || defined( _MSC_VER )
831 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
833 rc = GetFileAttributes( psz_wdir );
834 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
836 /* Parse all files in the directory */
837 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
839 rc = GetFileAttributes( psz_dir );
840 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
843 /* Parse all files in the directory */
844 sprintf( psz_path, "%s\\*", psz_dir );
847 handle = FindFirstFile( psz_wpath, &finddata );
849 handle = FindFirstFile( psz_path, &finddata );
851 if( handle == INVALID_HANDLE_VALUE )
853 /* Empty directory */
857 /* Parse the directory and try to load all files it contains. */
861 unsigned int i_len = wcslen( finddata.cFileName );
862 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
863 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
865 unsigned int i_len = strlen( finddata.cFileName );
866 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
870 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
871 || !strcmp( finddata.cFileName, ".." ) )
873 if( !FindNextFile( handle, &finddata ) ) break;
878 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
880 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
883 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
885 else if( i_len > strlen( LIBEXT )
886 /* We only load files ending with LIBEXT */
887 && !strncasecmp( psz_path + strlen( psz_path)
889 LIBEXT, strlen( LIBEXT ) ) )
891 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
892 int64_t i_time = 0, i_size = 0;
895 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
898 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
902 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
904 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
905 i_size = attrbuf.nFileSizeHigh;
907 i_size |= attrbuf.nFileSizeLow;
911 AllocatePluginFile( p_this, psz_file, i_time, i_size );
914 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
916 /* Close the directory */
920 dir = opendir( psz_dir );
926 i_dirlen = strlen( psz_dir );
928 /* Parse the directory and try to load all files it contains. */
929 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
936 if( !*file->d_name || !strcmp( file->d_name, "." )
937 || !strcmp( file->d_name, ".." ) )
942 i_len = strlen( file->d_name );
943 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
945 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
947 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
950 i_stat = stat( psz_file, &statbuf );
951 if( !i_stat && statbuf.st_mode & S_IFDIR )
953 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
955 else if( i_len > strlen( LIBEXT )
956 /* We only load files ending with LIBEXT */
957 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
958 LIBEXT, strlen( LIBEXT ) ) )
960 int64_t i_time = 0, i_size = 0;
964 i_time = statbuf.st_mtime;
965 i_size = statbuf.st_size;
968 AllocatePluginFile( p_this, psz_file, i_time, i_size );
974 /* Close the directory */
980 /*****************************************************************************
981 * AllocatePluginFile: load a module into memory and initialize it.
982 *****************************************************************************
983 * This function loads a dynamically loadable module and allocates a structure
984 * for its information data. The module can then be handled by module_Need
985 * and module_Unneed. It can be removed by DeleteModule.
986 *****************************************************************************/
987 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
988 int64_t i_file_time, int64_t i_file_size )
991 module_cache_t *p_cache_entry = NULL;
994 * Check our plugins cache first then load plugin if needed
997 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1001 p_module = AllocatePlugin( p_this, psz_file );
1005 /* If junk dll, don't try to load it */
1006 if( p_cache_entry->b_junk )
1012 module_config_t *p_item, *p_end;
1014 p_module = p_cache_entry->p_module;
1015 p_module->b_loaded = VLC_FALSE;
1017 /* For now we force loading if the module's config contains
1018 * callbacks or actions.
1019 * Could be optimized by adding an API call.*/
1020 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1021 p_item < p_end; p_item++ )
1023 if( p_item->pf_callback || p_item->i_action )
1024 p_module = AllocatePlugin( p_this, psz_file );
1031 /* Everything worked fine !
1032 * The module is ready to be added to the list. */
1033 p_module->b_builtin = VLC_FALSE;
1035 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1036 p_module->psz_object_name, p_module->psz_longname ); */
1038 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1041 if( !p_this->p_libvlc_global->p_module_bank->b_cache ) return 0;
1043 /* Add entry to cache */
1044 #define p_bank p_this->p_libvlc_global->p_module_bank
1046 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1047 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1048 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1049 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1050 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1051 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1052 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1055 return p_module ? 0 : -1;
1058 /*****************************************************************************
1059 * AllocatePlugin: load a module into memory and initialize it.
1060 *****************************************************************************
1061 * This function loads a dynamically loadable module and allocates a structure
1062 * for its information data. The module can then be handled by module_Need
1063 * and module_Unneed. It can be removed by DeleteModule.
1064 *****************************************************************************/
1065 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1067 module_t * p_module;
1068 module_handle_t handle;
1070 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1072 /* Now that we have successfully loaded the module, we can
1073 * allocate a structure for it */
1074 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1075 if( p_module == NULL )
1077 msg_Err( p_this, "out of memory" );
1078 CloseModule( handle );
1082 /* We need to fill these since they may be needed by CallEntry() */
1083 p_module->psz_filename = psz_file;
1084 p_module->handle = handle;
1085 #ifndef HAVE_SHARED_LIBVLC
1086 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1088 p_module->b_loaded = VLC_TRUE;
1090 /* Initialize the module: fill p_module, default config */
1091 if( CallEntry( p_module ) != 0 )
1093 /* We couldn't call module_init() */
1094 vlc_object_destroy( p_module );
1095 CloseModule( handle );
1099 DupModule( p_module );
1100 p_module->psz_filename = strdup( p_module->psz_filename );
1102 /* Everything worked fine ! The module is ready to be added to the list. */
1103 p_module->b_builtin = VLC_FALSE;
1108 /*****************************************************************************
1109 * DupModule: make a plugin module standalone.
1110 *****************************************************************************
1111 * This function duplicates all strings in the module, so that the dynamic
1112 * object can be unloaded. It acts recursively on submodules.
1113 *****************************************************************************/
1114 static void DupModule( module_t *p_module )
1116 const char **pp_shortcut;
1119 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1121 *pp_shortcut = strdup( *pp_shortcut );
1124 /* We strdup() these entries so that they are still valid when the
1125 * module is unloaded. */
1126 p_module->psz_object_name = strdup( p_module->psz_object_name );
1127 p_module->psz_capability = strdup( p_module->psz_capability );
1128 p_module->psz_shortname = p_module->psz_shortname ?
1129 strdup( p_module->psz_shortname ) : NULL;
1130 p_module->psz_longname = strdup( p_module->psz_longname );
1132 if( p_module->psz_program != NULL )
1134 p_module->psz_program = strdup( p_module->psz_program );
1137 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1139 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1143 /*****************************************************************************
1144 * UndupModule: free a duplicated module.
1145 *****************************************************************************
1146 * This function frees the allocations done in DupModule().
1147 *****************************************************************************/
1148 static void UndupModule( module_t *p_module )
1150 const char **pp_shortcut;
1153 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1155 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1158 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1160 free( (void *)*pp_shortcut );
1163 free( (void *)p_module->psz_object_name );
1164 free( (void *)p_module->psz_capability );
1165 if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1166 free( (void *)p_module->psz_longname );
1168 if( p_module->psz_program != NULL )
1170 free( (void *)p_module->psz_program );
1174 #endif /* HAVE_DYNAMIC_PLUGINS */
1176 /*****************************************************************************
1177 * AllocateBuiltinModule: initialize a builtin module.
1178 *****************************************************************************
1179 * This function registers a builtin module and allocates a structure
1180 * for its information data. The module can then be handled by module_Need
1181 * and module_Unneed. It can be removed by DeleteModule.
1182 *****************************************************************************/
1183 static int AllocateBuiltinModule( vlc_object_t * p_this,
1184 int ( *pf_entry ) ( module_t * ) )
1186 module_t * p_module;
1188 /* Now that we have successfully loaded the module, we can
1189 * allocate a structure for it */
1190 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1191 if( p_module == NULL )
1193 msg_Err( p_this, "out of memory" );
1197 /* Initialize the module : fill p_module->psz_object_name, etc. */
1198 if( pf_entry( p_module ) != 0 )
1200 /* With a well-written module we shouldn't have to print an
1201 * additional error message here, but just make sure. */
1202 msg_Err( p_this, "failed calling entry point in builtin module" );
1203 vlc_object_destroy( p_module );
1207 /* Everything worked fine ! The module is ready to be added to the list. */
1208 p_module->b_builtin = VLC_TRUE;
1210 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1211 p_module->psz_object_name, p_module->psz_longname ); */
1213 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1218 /*****************************************************************************
1219 * DeleteModule: delete a module and its structure.
1220 *****************************************************************************
1221 * This function can only be called if the module isn't being used.
1222 *****************************************************************************/
1223 static int DeleteModule( module_t * p_module )
1225 vlc_object_detach( p_module );
1227 /* We free the structures that we strdup()ed in Allocate*Module(). */
1228 #ifdef HAVE_DYNAMIC_PLUGINS
1229 if( !p_module->b_builtin )
1231 if( p_module->b_loaded && p_module->b_unloadable )
1233 CloseModule( p_module->handle );
1235 UndupModule( p_module );
1236 free( p_module->psz_filename );
1240 /* Free and detach the object's children */
1241 while( p_module->i_children )
1243 vlc_object_t *p_this = p_module->pp_children[0];
1244 vlc_object_detach( p_this );
1245 vlc_object_destroy( p_this );
1248 config_Free( p_module );
1249 vlc_object_destroy( p_module );
1254 #ifdef HAVE_DYNAMIC_PLUGINS
1255 /*****************************************************************************
1256 * CallEntry: call an entry point.
1257 *****************************************************************************
1258 * This function calls a symbol given its name and a module structure. The
1259 * symbol MUST refer to a function returning int and taking a module_t* as
1261 *****************************************************************************/
1262 static int CallEntry( module_t * p_module )
1264 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1265 int (* pf_symbol) ( module_t * p_module );
1267 /* Try to resolve the symbol */
1268 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1270 if( pf_symbol == NULL )
1272 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1273 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1274 psz_name, p_module->psz_filename );
1275 #elif defined(HAVE_DL_WINDOWS)
1276 char *psz_error = GetWindowsError();
1277 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1278 psz_name, p_module->psz_filename, psz_error );
1280 #elif defined(HAVE_DL_DLOPEN)
1281 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1282 psz_name, p_module->psz_filename, dlerror() );
1283 #elif defined(HAVE_DL_SHL_LOAD)
1284 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1285 psz_name, p_module->psz_filename, strerror(errno) );
1287 # error "Something is wrong in modules.c"
1292 /* We can now try to call the symbol */
1293 if( pf_symbol( p_module ) != 0 )
1295 /* With a well-written module we shouldn't have to print an
1296 * additional error message here, but just make sure. */
1297 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1298 psz_name, p_module->psz_filename );
1302 /* Everything worked fine, we can return */
1306 /*****************************************************************************
1307 * LoadModule: loads a dynamic library
1308 *****************************************************************************
1309 * This function loads a dynamically linked library using a system dependant
1310 * method. Will return 0 on success as well as the module handle.
1311 *****************************************************************************/
1312 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1313 module_handle_t *p_handle )
1315 module_handle_t handle;
1317 #if defined(HAVE_DL_DYLD)
1318 NSObjectFileImage image;
1319 NSObjectFileImageReturnCode ret;
1321 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1323 if( ret != NSObjectFileImageSuccess )
1325 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1329 /* Open the dynamic module */
1330 handle = NSLinkModule( image, psz_file,
1331 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1335 NSLinkEditErrors errors;
1336 const char *psz_file, *psz_err;
1338 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1339 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1340 NSDestroyObjectFileImage( image );
1344 /* Destroy our image, we won't need it */
1345 NSDestroyObjectFileImage( image );
1347 #elif defined(HAVE_DL_BEOS)
1348 handle = load_add_on( psz_file );
1351 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1355 #elif defined(HAVE_DL_WINDOWS)
1358 wchar_t psz_wfile[MAX_PATH];
1359 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1360 handle = LoadLibrary( psz_wfile );
1363 handle = LoadLibrary( psz_file );
1365 if( handle == NULL )
1367 char *psz_err = GetWindowsError();
1368 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1373 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1374 /* static is OK, we are called atomically */
1376 # if defined(SYS_LINUX)
1377 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1378 * are going to get namespace collisions when two modules have common
1379 * public symbols, but ALSA is being a pest here. */
1380 if( strstr( psz_file, "alsa_plugin" ) )
1382 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1383 if( handle == NULL )
1385 msg_Warn( p_this, "cannot load module `%s' (%s)",
1386 psz_file, dlerror() );
1392 handle = dlopen( psz_file, RTLD_NOW );
1393 if( handle == NULL )
1395 msg_Warn( p_this, "cannot load module `%s' (%s)",
1396 psz_file, dlerror() );
1400 #elif defined(HAVE_DL_DLOPEN)
1401 # if defined(DL_LAZY)
1402 handle = dlopen( psz_file, DL_LAZY );
1404 handle = dlopen( psz_file, 0 );
1406 if( handle == NULL )
1408 msg_Warn( p_this, "cannot load module `%s' (%s)",
1409 psz_file, dlerror() );
1413 #elif defined(HAVE_DL_SHL_LOAD)
1414 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1415 if( handle == NULL )
1417 msg_Warn( p_this, "cannot load module `%s' (%s)",
1418 psz_file, strerror(errno) );
1423 # error "Something is wrong in modules.c"
1431 /*****************************************************************************
1432 * CloseModule: unload a dynamic library
1433 *****************************************************************************
1434 * This function unloads a previously opened dynamically linked library
1435 * using a system dependant method. No return value is taken in consideration,
1436 * since some libraries sometimes refuse to close properly.
1437 *****************************************************************************/
1438 static void CloseModule( module_handle_t handle )
1440 #if defined(HAVE_DL_DYLD)
1441 NSUnLinkModule( handle, FALSE );
1443 #elif defined(HAVE_DL_BEOS)
1444 unload_add_on( handle );
1446 #elif defined(HAVE_DL_WINDOWS)
1447 FreeLibrary( handle );
1449 #elif defined(HAVE_DL_DLOPEN)
1452 #elif defined(HAVE_DL_SHL_LOAD)
1453 shl_unload( handle );
1459 /*****************************************************************************
1460 * GetSymbol: get a symbol from a dynamic library
1461 *****************************************************************************
1462 * This function queries a loaded library for a symbol specified in a
1463 * string, and returns a pointer to it. We don't check for dlerror() or
1464 * similar functions, since we want a non-NULL symbol anyway.
1465 *****************************************************************************/
1466 static void * _module_getsymbol( module_handle_t, const char * );
1468 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1470 void * p_symbol = _module_getsymbol( handle, psz_function );
1472 /* MacOS X dl library expects symbols to begin with "_". So do
1473 * some other operating systems. That's really lame, but hey, what
1475 if( p_symbol == NULL )
1477 char *psz_call = malloc( strlen( psz_function ) + 2 );
1479 strcpy( psz_call + 1, psz_function );
1480 psz_call[ 0 ] = '_';
1481 p_symbol = _module_getsymbol( handle, psz_call );
1488 static void * _module_getsymbol( module_handle_t handle,
1489 const char * psz_function )
1491 #if defined(HAVE_DL_DYLD)
1492 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1493 return NSAddressOfSymbol( sym );
1495 #elif defined(HAVE_DL_BEOS)
1497 if( B_OK == get_image_symbol( handle, psz_function,
1498 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1507 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1508 wchar_t psz_real[256];
1509 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1511 return (void *)GetProcAddress( handle, psz_real );
1513 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1514 return (void *)GetProcAddress( handle, (char *)psz_function );
1516 #elif defined(HAVE_DL_DLOPEN)
1517 return dlsym( handle, psz_function );
1519 #elif defined(HAVE_DL_SHL_LOAD)
1521 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1527 #if defined(HAVE_DL_WINDOWS)
1528 static char * GetWindowsError( void )
1530 #if defined(UNDER_CE)
1531 wchar_t psz_tmp[MAX_PATH];
1532 char * psz_buffer = malloc( MAX_PATH );
1534 char * psz_tmp = malloc( MAX_PATH );
1536 int i = 0, i_error = GetLastError();
1538 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1539 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1540 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1542 /* Go to the end of the string */
1543 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1550 #if defined(UNDER_CE)
1551 swprintf( psz_tmp + i, L" (error %i)", i_error );
1552 psz_tmp[ 255 ] = L'\0';
1554 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1555 psz_tmp[ 255 ] = '\0';
1559 #if defined(UNDER_CE)
1560 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1566 #endif /* HAVE_DL_WINDOWS */
1568 /*****************************************************************************
1569 * LoadPluginsCache: loads the plugins cache file
1570 *****************************************************************************
1571 * This function will load the plugin cache if present and valid. This cache
1572 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1573 * actually load the dynamically loadable module.
1574 * This allows us to only fully load plugins when they are actually used.
1575 *****************************************************************************/
1576 static void CacheLoad( vlc_object_t *p_this )
1578 char *psz_filename, *psz_homedir;
1580 int i, j, i_size, i_read;
1581 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1582 char p_cachelang[6], p_lang[6];
1584 module_cache_t **pp_cache = 0;
1585 int32_t i_file_size, i_marker;
1587 psz_homedir = p_this->p_libvlc->psz_homedir;
1590 msg_Err( p_this, "psz_homedir is null" );
1594 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1595 PLUGINSCACHE_DIR, CacheName() );
1598 msg_Err( p_this, "out of memory" );
1602 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1604 #if !defined( UNDER_CE )
1605 unlink( psz_filename );
1607 wchar_t psz_wf[MAX_PATH];
1608 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1609 DeleteFile( psz_wf );
1611 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1612 free( psz_filename );
1616 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1618 file = utf8_fopen( psz_filename, "rb" );
1621 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1623 free( psz_filename );
1626 free( psz_filename );
1628 /* Check the file size */
1629 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1630 if( i_read != sizeof(i_file_size) )
1632 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1638 fseek( file, 0, SEEK_END );
1639 if( ftell( file ) != i_file_size )
1641 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1642 "(corrupted size)" );
1646 fseek( file, sizeof(i_file_size), SEEK_SET );
1648 /* Check the file is a plugins cache */
1649 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1650 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1651 if( i_read != i_size ||
1652 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1654 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1659 /* Check Sub-version number */
1660 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1661 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1663 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1664 "(corrupted header)" );
1669 /* Check the language hasn't changed */
1670 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1671 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1672 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1674 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1675 "(language changed)" );
1680 /* Check header marker */
1681 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1682 if( i_read != sizeof(i_marker) ||
1683 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1685 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1686 "(corrupted header)" );
1691 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1692 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1694 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1695 malloc( i_cache * sizeof(void *) );
1697 #define LOAD_IMMEDIATE(a) \
1698 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1699 #define LOAD_STRING(a) \
1702 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1703 || ( i_size > 16384 ) ) \
1706 char *psz = malloc( i_size ); \
1707 if( fread( psz, i_size, 1, file ) != 1 ) { \
1711 if( psz[i_size-1] ) { \
1719 for( i = 0; i < i_cache; i++ )
1724 pp_cache[i] = malloc( sizeof(module_cache_t) );
1725 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1727 /* Load common info */
1728 LOAD_STRING( pp_cache[i]->psz_file );
1729 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1730 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1731 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1733 if( pp_cache[i]->b_junk ) continue;
1735 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1737 /* Load additional infos */
1738 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1739 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1740 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1741 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1742 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1744 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1746 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1747 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1748 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1749 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1750 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1751 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1754 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1757 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1759 LOAD_IMMEDIATE( i_submodules );
1761 while( i_submodules-- )
1763 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1764 vlc_object_attach( p_module, pp_cache[i]->p_module );
1765 p_module->b_submodule = VLC_TRUE;
1767 LOAD_STRING( p_module->psz_object_name );
1768 LOAD_STRING( p_module->psz_shortname );
1769 LOAD_STRING( p_module->psz_longname );
1770 LOAD_STRING( p_module->psz_program );
1771 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1773 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1775 LOAD_STRING( p_module->psz_capability );
1776 LOAD_IMMEDIATE( p_module->i_score );
1777 LOAD_IMMEDIATE( p_module->i_cpu );
1778 LOAD_IMMEDIATE( p_module->b_unloadable );
1779 LOAD_IMMEDIATE( p_module->b_reentrant );
1788 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1791 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1798 int CacheLoadConfig( module_t *p_module, FILE *file )
1803 /* Calculate the structure length */
1804 LOAD_IMMEDIATE( p_module->i_config_items );
1805 LOAD_IMMEDIATE( p_module->i_bool_items );
1807 LOAD_IMMEDIATE( i_lines );
1809 /* Allocate memory */
1812 p_module->p_config =
1813 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1814 if( p_module->p_config == NULL )
1816 p_module->confsize = 0;
1817 msg_Err( p_module, "config error: can't duplicate p_config" );
1821 p_module->confsize = i_lines;
1823 /* Do the duplication job */
1824 for (size_t i = 0; i < i_lines; i++ )
1826 LOAD_IMMEDIATE( p_module->p_config[i] );
1828 LOAD_STRING( p_module->p_config[i].psz_type );
1829 LOAD_STRING( p_module->p_config[i].psz_name );
1830 LOAD_STRING( p_module->p_config[i].psz_text );
1831 LOAD_STRING( p_module->p_config[i].psz_longtext );
1832 LOAD_STRING( p_module->p_config[i].psz_current );
1834 if (IsConfigStringType (p_module->p_config[i].i_type))
1836 LOAD_STRING (p_module->p_config[i].orig.psz);
1837 p_module->p_config[i].value.psz =
1838 (p_module->p_config[i].orig.psz != NULL)
1839 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1840 p_module->p_config[i].saved.psz = NULL;
1844 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1845 sizeof (p_module->p_config[i].value));
1846 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1847 sizeof (p_module->p_config[i].saved));
1850 p_module->p_config[i].b_dirty = VLC_FALSE;
1852 p_module->p_config[i].p_lock = &p_module->object_lock;
1854 if( p_module->p_config[i].i_list )
1856 if( p_module->p_config[i].ppsz_list )
1859 p_module->p_config[i].ppsz_list =
1860 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1861 if( p_module->p_config[i].ppsz_list )
1863 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1864 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1865 p_module->p_config[i].ppsz_list[j] = NULL;
1868 if( p_module->p_config[i].ppsz_list_text )
1871 p_module->p_config[i].ppsz_list_text =
1872 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1873 if( p_module->p_config[i].ppsz_list_text )
1875 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1876 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1877 p_module->p_config[i].ppsz_list_text[j] = NULL;
1880 if( p_module->p_config[i].pi_list )
1882 p_module->p_config[i].pi_list =
1883 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1884 if( p_module->p_config[i].pi_list )
1886 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1887 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1892 if( p_module->p_config[i].i_action )
1894 p_module->p_config[i].ppf_action =
1895 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1896 p_module->p_config[i].ppsz_action_text =
1897 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1899 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1901 p_module->p_config[i].ppf_action[j] = 0;
1902 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1906 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1913 return VLC_EGENERIC;
1916 /*****************************************************************************
1917 * SavePluginsCache: saves the plugins cache to a file
1918 *****************************************************************************/
1919 static void CacheSave( vlc_object_t *p_this )
1921 static char const psz_tag[] =
1922 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1923 "# This file is a cache directory tag created by VLC.\r\n"
1924 "# For information about cache directory tags, see:\r\n"
1925 "# http://www.brynosaurus.com/cachedir/\r\n";
1927 char *psz_filename, *psz_homedir;
1930 module_cache_t **pp_cache;
1931 int32_t i_file_size = 0;
1933 psz_homedir = p_this->p_libvlc->psz_homedir;
1936 msg_Err( p_this, "psz_homedir is null" );
1940 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1941 strlen(psz_homedir) + strlen(CacheName()) );
1945 msg_Err( p_this, "out of memory" );
1949 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1951 config_CreateDir( p_this, psz_filename );
1953 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1955 config_CreateDir( p_this, psz_filename );
1957 strcat( psz_filename, "/CACHEDIR.TAG" );
1959 file = utf8_fopen( psz_filename, "wb" );
1962 fwrite( psz_tag, 1, strlen(psz_tag), file );
1966 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1967 PLUGINSCACHE_DIR, CacheName() );
1969 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1971 file = utf8_fopen( psz_filename, "wb" );
1974 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1976 free( psz_filename );
1979 free( psz_filename );
1981 /* Empty space for file size */
1982 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1984 /* Contains version number */
1985 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1987 /* Sub-version number (to avoid breakage in the dev version when cache
1988 * structure changes) */
1989 i_file_size = CACHE_SUBVERSION_NUM;
1990 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1993 fprintf( file, "%5.5s", _("C") );
1996 i_file_size = ftell( file );
1997 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1999 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2000 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2002 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2004 #define SAVE_IMMEDIATE(a) \
2005 fwrite( &a, sizeof(char), sizeof(a), file )
2006 #define SAVE_STRING(a) \
2007 { i_size = a ? strlen( a ) + 1 : 0; \
2008 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2009 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2012 for( i = 0; i < i_cache; i++ )
2015 uint32_t i_submodule;
2017 /* Save common info */
2018 SAVE_STRING( pp_cache[i]->psz_file );
2019 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2020 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2021 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2023 if( pp_cache[i]->b_junk ) continue;
2025 /* Save additional infos */
2026 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2027 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2028 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2029 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2030 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2032 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2034 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2035 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2036 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2037 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2038 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2039 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2042 CacheSaveConfig( pp_cache[i]->p_module, file );
2044 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2046 i_submodule = pp_cache[i]->p_module->i_children;
2047 SAVE_IMMEDIATE( i_submodule );
2048 for( i_submodule = 0;
2049 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2052 module_t *p_module =
2053 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2055 SAVE_STRING( p_module->psz_object_name );
2056 SAVE_STRING( p_module->psz_shortname );
2057 SAVE_STRING( p_module->psz_longname );
2058 SAVE_STRING( p_module->psz_program );
2059 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2061 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2063 SAVE_STRING( p_module->psz_capability );
2064 SAVE_IMMEDIATE( p_module->i_score );
2065 SAVE_IMMEDIATE( p_module->i_cpu );
2066 SAVE_IMMEDIATE( p_module->b_unloadable );
2067 SAVE_IMMEDIATE( p_module->b_reentrant );
2071 /* Fill-up file size */
2072 i_file_size = ftell( file );
2073 fseek( file, 0, SEEK_SET );
2074 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2081 void CacheSaveConfig( module_t *p_module, FILE *file )
2083 uint32_t i_lines = p_module->confsize;
2086 SAVE_IMMEDIATE( p_module->i_config_items );
2087 SAVE_IMMEDIATE( p_module->i_bool_items );
2088 SAVE_IMMEDIATE( i_lines );
2090 for (size_t i = 0; i < i_lines ; i++)
2092 SAVE_IMMEDIATE( p_module->p_config[i] );
2094 SAVE_STRING( p_module->p_config[i].psz_type );
2095 SAVE_STRING( p_module->p_config[i].psz_name );
2096 SAVE_STRING( p_module->p_config[i].psz_text );
2097 SAVE_STRING( p_module->p_config[i].psz_longtext );
2098 SAVE_STRING( p_module->p_config[i].psz_current );
2099 if (IsConfigStringType (p_module->p_config[i].i_type))
2100 SAVE_STRING( p_module->p_config[i].orig.psz );
2102 if( p_module->p_config[i].i_list )
2104 if( p_module->p_config[i].ppsz_list )
2106 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2107 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2110 if( p_module->p_config[i].ppsz_list_text )
2112 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2113 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2115 if( p_module->p_config[i].pi_list )
2117 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2118 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2122 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2123 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2125 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2129 /*****************************************************************************
2130 * CacheName: Return the cache file name for this platform.
2131 *****************************************************************************/
2132 static char *CacheName( void )
2134 static char psz_cachename[32];
2136 /* Code int size, pointer size and endianness in the filename */
2137 int32_t x = 0xbe00001e;
2138 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2139 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2140 return psz_cachename;
2143 /*****************************************************************************
2144 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2145 *****************************************************************************/
2146 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2147 module_t *p_module )
2151 p_cache->pf_activate = p_module->pf_activate;
2152 p_cache->pf_deactivate = p_module->pf_deactivate;
2153 #ifndef HAVE_SHARED_LIBVLC
2154 p_cache->p_symbols = p_module->p_symbols;
2156 p_cache->handle = p_module->handle;
2158 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2160 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2161 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2162 p_cchild->pf_activate = p_child->pf_activate;
2163 p_cchild->pf_deactivate = p_child->pf_deactivate;
2164 #ifndef HAVE_SHARED_LIBVLC
2165 p_cchild->p_symbols = p_child->p_symbols;
2169 p_cache->b_loaded = VLC_TRUE;
2170 p_module->b_loaded = VLC_FALSE;
2173 /*****************************************************************************
2174 * FindPluginCache: finds the cache entry corresponding to a file
2175 *****************************************************************************/
2176 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2177 int64_t i_time, int64_t i_size )
2179 module_cache_t **pp_cache;
2182 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2183 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2185 for( i = 0; i < i_cache; i++ )
2187 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2188 pp_cache[i]->i_time == i_time &&
2189 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2195 #endif /* HAVE_DYNAMIC_PLUGINS */