modules.c 42.4 KB
Newer Older
Sam Hocevar's avatar
   
Sam Hocevar committed
1
/*****************************************************************************
2
 * modules.c : Builtin and plugin modules management functions
Sam Hocevar's avatar
   
Sam Hocevar committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
Sam Hocevar's avatar
   
Sam Hocevar committed
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
Sam Hocevar's avatar
   
Sam Hocevar committed
8
 *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
Sam Hocevar's avatar
   
Sam Hocevar committed
9
 *          Hans-Peter Jansen <hpj@urpla.net>
10
 *          Gildas Bazin <gbazin@videolan.org>
Sam Hocevar's avatar
   
Sam Hocevar committed
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
   
Sam Hocevar committed
25
26
 *****************************************************************************/

27
28
29
30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
33
#include "libvlc.h"
34

Sam Hocevar's avatar
   
Sam Hocevar committed
35
36
37
38
39
40
/* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
 * is set to 64. Don't try to be cleverer. */
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
41
42
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
   
Sam Hocevar committed
43
#include <string.h>                                              /* strdup() */
44
#include <assert.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
45

46
47
48
49
#ifdef HAVE_DIRENT_H
#   include <dirent.h>
#endif

50
51
52
53
54
55
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
56
57
58
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
59

60
61
62
#if !defined(HAVE_DYNAMIC_PLUGINS)
    /* no support for plugins */
#elif defined(HAVE_DL_DYLD)
Sam Hocevar's avatar
Sam Hocevar committed
63
64
65
66
67
68
69
70
#   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(HAVE_DL_WINDOWS)
Sam Hocevar's avatar
Sam Hocevar committed
71
#   include <windows.h>
Sam Hocevar's avatar
Sam Hocevar committed
72
73
#elif defined(HAVE_DL_DLOPEN)
#   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
Sam Hocevar's avatar
Sam Hocevar committed
74
75
#       include <dlfcn.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
76
#   if defined(HAVE_SYS_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
77
78
#       include <sys/dl.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
79
80
#elif defined(HAVE_DL_SHL_LOAD)
#   if defined(HAVE_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
81
82
#       include <dl.h>
#   endif
Sam Hocevar's avatar
   
Sam Hocevar committed
83
84
#endif

85
#include "config/configuration.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
86

zorglub's avatar
zorglub committed
87
#include "vlc_charset.h"
88
#include "vlc_arrays.h"
gbazin's avatar
   
gbazin committed
89

90
91
#include "modules/modules.h"
#include "modules/builtin.h"
zorglub's avatar
zorglub committed
92

93
94
module_bank_t *p_module_bank;

Sam Hocevar's avatar
   
Sam Hocevar committed
95
96
97
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
98
#ifdef HAVE_DYNAMIC_PLUGINS
99
100
101
102
static void AllocateAllPlugins  ( vlc_object_t * );
static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
static module_t * AllocatePlugin( vlc_object_t *, char * );
Sam Hocevar's avatar
   
Sam Hocevar committed
103
#endif
104
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
105
static void DeleteModule ( module_t *, bool );
Sam Hocevar's avatar
   
Sam Hocevar committed
106
#ifdef HAVE_DYNAMIC_PLUGINS
107
108
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
109
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
110

111
112
113
114
/**
 * Init bank
 *
 * Creates a module bank structure which will be filled later
gbazin's avatar
   
gbazin committed
115
 * on with all the modules found.
116
117
118
 * \param p_this vlc object structure
 * \return nothing
 */
119
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
120
{
121
    module_bank_t *p_bank = NULL;
122

123
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
124

125
    if( p_module_bank == NULL )
126
    {
127
        p_bank = calloc (1, sizeof(*p_bank));
128
129
130
131
        p_bank->i_usage = 1;
        p_bank->i_cache = p_bank->i_loaded_cache = 0;
        p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
        p_bank->b_cache = p_bank->b_cache_dirty =
132
        p_bank->b_cache_delete = false;
133
        p_bank->head = NULL;
134
135

        /* Everything worked, attach the object */
136
        p_module_bank = p_bank;
137
138
139
140
141
142
143

        /* Fills the module bank structure with the main module infos.
         * This is very useful as it will allow us to consider the main
         * library just as another module, and for instance the configuration
         * options of main will be available in the module bank structure just
         * as for every other module. */
        AllocateBuiltinModule( p_this, vlc_entry__main );
144
    }
145
    else
146
        p_module_bank->i_usage++;
147

148
    vlc_mutex_unlock( lock );
gbazin's avatar
   
gbazin committed
149
150
}

Sam Hocevar's avatar
   
Sam Hocevar committed
151

152
153
154
155
/**
 * End bank
 *
 * Unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
   
Sam Hocevar committed
156
 * bank in case of success.
157
158
159
 * \param p_this vlc object structure
 * \return nothing
 */
160
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
161
{
162
    module_bank_t *p_bank;
163

164
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
165
166
167
    p_bank = p_module_bank;
    assert (p_bank != NULL);
    if( --p_bank->i_usage > 0 )
168
    {
169
        vlc_mutex_unlock( lock );
170
171
        return;
    }
172
173
    /*FIXME: For thread safety, we need to:
    p_module_bank = NULL; - immediately, but that will crash the cache */
174
    vlc_mutex_unlock( lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
175

176
    /* Save the configuration */
177
178
    config_AutoSaveConfigFile( p_this );

179
#ifdef HAVE_DYNAMIC_PLUGINS
180
181
182
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
183
184
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
185
186
187
            DeleteModule(
                    p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
                    p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
188
189
190
191
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
            p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
        }
192
    }
193
    if( p_bank->pp_loaded_cache )
194
    {
195
        free( p_bank->pp_loaded_cache );
196
197
        p_bank->pp_loaded_cache = NULL;
    }
198
199
200
201
    while( p_bank->i_cache-- )
    {
        free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
        free( p_bank->pp_cache[p_bank->i_cache] );
202
        p_bank->pp_cache[p_bank->i_cache] = NULL;
203
    }
204
    if( p_bank->pp_cache )
205
    {
206
        free( p_bank->pp_cache );
207
208
        p_bank->pp_cache = NULL;
    }
209
210
#endif

211
212
    while( p_bank->head != NULL )
        DeleteModule( p_bank->head, true );
Sam Hocevar's avatar
   
Sam Hocevar committed
213

214
215
    p_module_bank = NULL; /* FIXME: do this inside the lock */
    free( p_bank );
Sam Hocevar's avatar
   
Sam Hocevar committed
216
217
}

218
219
220
221
222
223
224
/**
 * Load all modules which we built with.
 *
 * Fills the module bank structure with the builtin modules.
 * \param p_this vlc object structure
 * \return nothing
 */
225
void __module_LoadBuiltins( vlc_object_t * p_this )
226
{
227
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
228
    if( p_module_bank->b_builtins )
229
    {
230
        vlc_mutex_unlock( lock );
231
232
        return;
    }
233
    p_module_bank->b_builtins = true;
234
    vlc_mutex_unlock( lock );
235

236
237
238
239
    msg_Dbg( p_this, "checking builtin modules" );
    ALLOCATE_ALL_BUILTINS();
}

240
241
242
243
244
245
246
247
/**
 * Load all plugins
 *
 * Load all plugin modules we can find.
 * Fills the module bank structure with the plugin modules.
 * \param p_this vlc object structure
 * \return nothing
 */
248
void __module_LoadPlugins( vlc_object_t * p_this )
249
250
{
#ifdef HAVE_DYNAMIC_PLUGINS
251
    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
252
    if( p_module_bank->b_plugins )
253
    {
254
        vlc_mutex_unlock( lock );
255
256
        return;
    }
257
    p_module_bank->b_plugins = true;
258
    vlc_mutex_unlock( lock );
259

260
    msg_Dbg( p_this, "checking plugin modules" );
261
262

    if( config_GetInt( p_this, "plugins-cache" ) )
263
        p_module_bank->b_cache = true;
264

265
266
    if( p_module_bank->b_cache ||
        p_module_bank->b_cache_delete ) CacheLoad( p_this );
267

268
269
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
270
271
}

272
273
274
275
276
277
278
/**
 * Checks whether a module implements a capability.
 *
 * \param m the module
 * \param cap the capability to check
 * \return TRUE if the module have the capability
 */
279
bool module_provides( const module_t *m, const char *cap )
280
281
282
283
{
    return !strcmp( m->psz_capability, cap );
}

284
285
286
287
288
289
/**
 * Get the internal name of a module
 *
 * \param m the module
 * \return the module name
 */
290
const char *module_get_object( const module_t *m )
291
292
293
294
{
    return m->psz_object_name;
}

295
296
297
298
299
300
301
/**
 * Get the human-friendly name of a module.
 *
 * \param m the module
 * \param long_name TRUE to have the long name of the module
 * \return the short or long name of the module
 */
302
const char *module_get_name( const module_t *m, bool long_name )
303
304
305
{
    if( long_name && ( m->psz_longname != NULL) )
        return m->psz_longname;
306

307
308
309
    return m->psz_shortname ?: m->psz_object_name;
}

310
311
312
313
314
315
/**
 * Get the help for a module
 *
 * \param m the module
 * \return the help
 */
316
const char *module_get_help( const module_t *m )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
317
318
319
320
{
    return m->psz_help;
}

321
322
323
324
325
326
327
328
329
330
331
module_t *module_hold (module_t *m)
{
    vlc_hold (&m->vlc_gc_data);
    return m;
}

void module_release (module_t *m)
{
    vlc_release (&m->vlc_gc_data);
}

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 * Frees the flat list of VLC modules.
 * @param list list obtained by module_list_get
 * @param length number of items on the list
 * @return nothing.
 */
void module_list_free (module_t **list)
{
    if (list == NULL)
        return;

    for (size_t i = 0; list[i] != NULL; i++)
         module_release (list[i]);
    free (list);
}

/**
 * Gets the flat list of VLC modules.
 * @param n [OUT] pointer to the number of modules or NULL
 * @return NULL-terminated table of module pointers
 *         (release with module_list_free()), or NULL in case of error.
 */
module_t **module_list_get (size_t *n)
{
    /* TODO: this whole module lookup is quite inefficient */
357
    /* Remove this and improve module_need */
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    module_t **tab = NULL;
    size_t i = 0;

    for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
    {
         module_t **nt;
         nt  = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
         if (nt == NULL)
         {
             module_list_free (tab);
             return NULL;
         }

         tab = nt;
         tab[i++] = module_hold (mod);
         for (module_t *subm = mod->submodule; subm; subm = subm->next)
             tab[i++] = module_hold (subm);
         tab[i] = NULL;
    }
    if (n != NULL)
        *n = i;
    return tab;
}

382
383
384
385
/**
 * module Need
 *
 * Return the best module function, given a capability list.
386
387
388
389
390
391
 *
 * If the p_this object doesn't have it's psz_object_name set, then
 * psz_object_name will be set to the module's name, unless the user
 * provided an alias using the "module name@alias" syntax in which case
 * psz_object_name will be set to the alias.
 *
392
393
394
395
396
397
 * \param p_this the vlc object
 * \param psz_capability list of capabilities needed
 * \param psz_name name of the module asked
 * \param b_strict TRUE yto use the strict mode
 * \return the module or NULL in case of a failure
 */
398
module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
399
                          const char *psz_name, bool b_strict )
Sam Hocevar's avatar
   
Sam Hocevar committed
400
{
401
    typedef struct module_list_t module_list_t;
402

403
    stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
404

405
    struct module_list_t
Sam Hocevar's avatar
   
Sam Hocevar committed
406
    {
407
        module_t *p_module;
408
        int i_score;
409
        bool b_force;
410
411
412
413
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
Sam Hocevar's avatar
   
Sam Hocevar committed
414

gbazin's avatar
   
gbazin committed
415
    int i_which_module, i_index = 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
416
417

    module_t *p_module;
418

419
    int   i_shortcuts = 0;
420
    char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
421
    bool b_force_backup = p_this->b_force;
Sam Hocevar's avatar
   
Sam Hocevar committed
422

423

424
425
426
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
dionoea's avatar
dionoea committed
427
        psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
428
429
    }

430
    /* Count how many different shortcuts were asked for */
431
    if( psz_name && *psz_name )
Sam Hocevar's avatar
   
Sam Hocevar committed
432
    {
gbazin's avatar
   
gbazin committed
433
        char *psz_parser, *psz_last_shortcut;
434

435
436
437
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
438
            free( psz_var );
439
440
441
            stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
            stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
            stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
442
443
444
            return NULL;
        }

445
        i_shortcuts++;
gbazin's avatar
   
gbazin committed
446
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
447
448

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
449
        {
450
            if( *psz_parser == ',' )
Sam Hocevar's avatar
   
Sam Hocevar committed
451
            {
452
453
                 *psz_parser = '\0';
                 i_shortcuts++;
gbazin's avatar
   
gbazin committed
454
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
455
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
456
        }
gbazin's avatar
   
gbazin committed
457
458
459
460

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
461
462
            if( !strcmp(psz_last_shortcut, "none") )
            {
463
                b_strict = true;
464
465
466
467
                i_shortcuts--;
            }
            else if( !strcmp(psz_last_shortcut, "any") )
            {
468
                b_strict = false;
469
470
                i_shortcuts--;
            }
gbazin's avatar
   
gbazin committed
471
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
472
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
473

Sam Hocevar's avatar
   
Sam Hocevar committed
474
    /* Sort the modules and test them */
475
476
477
    size_t count;
    module_t **p_all = module_list_get (&count);
    p_list = malloc( count * sizeof( module_list_t ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
478
    p_first = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
479
    unsigned i_cpu = vlc_CPU();
Sam Hocevar's avatar
   
Sam Hocevar committed
480

Sam Hocevar's avatar
   
Sam Hocevar committed
481
    /* Parse the module list for capabilities and probe each of them */
482
    for( i_which_module = 0; i_which_module < count; i_which_module++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
483
    {
484
        int i_shortcut_bonus = 0;
485
        p_module = p_all[i_which_module];
486

487
        /* Test that this module can do what we need */
488
        if( !module_provides( p_module, psz_capability ) )
489
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
490
        /* Test if we have the required CPU */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
491
        if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
   
Sam Hocevar committed
492
493
            continue;

494
        /* If we required a shortcut, check this plugin provides it. */
495
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
496
        {
497
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
   
Sam Hocevar committed
498

499
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
   
Sam Hocevar committed
500
            {
501
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
502
                {
503
504
505
506
507
                    char *c;
                    if( ( c = strchr( psz_name, '@' ) )
                        ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
                                        c-psz_name )
                        : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
gbazin's avatar
   
gbazin committed
508
509
                    {
                        /* Found it */
510
511
                        if( c && c[1] )
                            psz_alias = c+1;
gbazin's avatar
   
gbazin committed
512
                        i_shortcut_bonus = i_short * 10000;
513
                        goto found_shortcut;
gbazin's avatar
   
gbazin committed
514
                    }
515
516
                }

517
                /* Go to the next shortcut... This is so lame! */
518
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
519
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
520

521
522
523
            /* If we are in "strict" mode and we couldn't
             * find the module in the list of provided shortcuts,
             * then kick the bastard out of here!!! */
524
            if( b_strict )
Sam Hocevar's avatar
   
Sam Hocevar committed
525
                continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
526
        }
527
528
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
529
530
531
        {
            continue;
        }
532

533
534
found_shortcut:

Sam Hocevar's avatar
   
Sam Hocevar committed
535
536
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
537
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
538
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
   
Sam Hocevar committed
539

Sam Hocevar's avatar
   
Sam Hocevar committed
540
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
   
Sam Hocevar committed
541
542
        if( i_index == 0 )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
543
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
544
545
546
547
548
549
550
            p_first = p_list;
        }
        else
        {
            /* Ok, so at school you learned that quicksort is quick, and
             * bubble sort sucks raw eggs. But that's when dealing with
             * thousands of items. Here we have barely 50. */
551
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
   
Sam Hocevar committed
552

553
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
554
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
555
556
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
   
Sam Hocevar committed
557
558
            }
            else
559
            {
560
561
                while( p_newlist->p_next != NULL &&
                    p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
562
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
563
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
564
565
                }

Sam Hocevar's avatar
   
Sam Hocevar committed
566
567
568
                p_list[ i_index ].p_next = p_newlist->p_next;
                p_newlist->p_next = &p_list[ i_index ];
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
569
570
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
571
572
        i_index++;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
573

zorglub's avatar
zorglub committed
574
575
    msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
                                            i_index, i_index == 1 ? "" : "s" );
576

577
    /* Lock all candidate modules */
578
579
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
580
    {
581
        module_hold( p_tmp->p_module );
582
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
583
584
    }

585
    /* We can release the list, interesting modules were held */
586
    module_list_free (p_all);
Sam Hocevar's avatar
   
Sam Hocevar committed
587

Sam Hocevar's avatar
   
Sam Hocevar committed
588
    /* Parse the linked list and use the first successful module */
589
590
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
591
    {
592
593
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
594
595
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
596
            p_module = p_module->parent;
597

598
599
600
601
602
603
604
        if( !p_module->b_builtin && !p_module->b_loaded )
        {
            module_t *p_new_module =
                AllocatePlugin( p_this, p_module->psz_filename );
            if( p_new_module )
            {
                CacheMerge( p_this, p_module, p_new_module );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
605
                DeleteModule( p_new_module, false );
606
607
608
609
            }
        }
#endif

610
        p_this->b_force = p_tmp->b_force;
611
612
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
   
Sam Hocevar committed
613
614
        {
            break;
615
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
616

617
        module_release( p_tmp->p_module );
618
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
619
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
620

Sam Hocevar's avatar
   
Sam Hocevar committed
621
    /* Store the locked module value */
622
    if( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
623
    {
624
625
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
626
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
627
628
629
630
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
631

Sam Hocevar's avatar
   
Sam Hocevar committed
632
    /* Unlock the remaining modules */
633
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
634
    {
635
        module_release( p_tmp->p_module );
636
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
637
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
638

Sam Hocevar's avatar
   
Sam Hocevar committed
639
    free( p_list );
640
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
   
Sam Hocevar committed
641

Sam Hocevar's avatar
   
Sam Hocevar committed
642
    if( p_module != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
643
    {
644
        msg_Dbg( p_this, "using %s module \"%s\"",
645
                 psz_capability, p_module->psz_object_name );
646
647
648
    }
    else if( p_first == NULL )
    {
649
650
651
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
652
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
653
654
        }
        else
zorglub's avatar
zorglub committed
655
        {
656
657
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
658
659
660

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
661
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
662
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
663
664
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
665
666
        msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
Sam Hocevar's avatar
   
Sam Hocevar committed
667
    }
668
669
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
   
Sam Hocevar committed
670

671
672
    if( p_module && !p_this->psz_object_name )
    {
673
674
675
        /* This assumes that p_this is the object which will be using the
         * module. That's not always the case ... but it is in most cases.
         */
dionoea's avatar
dionoea committed
676
677
678
679
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
680
    }
681

682
683
    free( psz_shortcuts );
    free( psz_var );
684

685
686
687
688
    stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
    stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
    stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );

Sam Hocevar's avatar
   
Sam Hocevar committed
689
    /* Don't forget that the module is still locked */
690
    return p_module;
Sam Hocevar's avatar
   
Sam Hocevar committed
691
692
}

693
694
695
/**
 * Module unneed
 *
696
 * This function must be called by the thread that called module_need, to
697
 * decrease the reference count and allow for hiding of modules.
698
699
700
701
 * \param p_this vlc object structure
 * \param p_module the module structure
 * \return nothing
 */
702
void __module_unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
   
Sam Hocevar committed
703
{
704
705
706
707
708
709
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

710
    msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
Sam Hocevar's avatar
   
Sam Hocevar committed
711

712
    module_release( p_module );
Sam Hocevar's avatar
   
Sam Hocevar committed
713
714
}

715
716
717
718
719
720
721
/**
 * Get a pointer to a module_t given it's name.
 *
 * \param p_this vlc object structure
 * \param psz_name the name of the module
 * \return a pointer to the module or NULL in case of a failure
 */
722
module_t *__module_find( vlc_object_t *p_this, const char * psz_name )
723
{
724
725
726
727
728
729
730
    module_t **list, *module;

    list = module_list_get (NULL);
    if (!list)
        return NULL;

    for (size_t i = 0; (module = list[i]) != NULL; i++)
731
    {
732
733
        const char *psz_module_name = module->psz_object_name;

734
        if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
735
        {
736
737
            module_hold (module);
            break;
738
739
        }
    }
740
741
    module_list_free (list);
    return module;
742
743
}

744
745
746
747
748
749
750
/**
 * Tell if a module exists and release it in thic case
 *
 * \param p_this vlc object structure
 * \param psz_name th name of the module
 * \return TRUE if the module exists
 */
751
bool __module_exists( vlc_object_t *p_this, const char * psz_name )
752
{
753
    module_t *p_module = __module_find( p_this, psz_name );
754
    if( p_module )
755
756
        module_release (p_module);
    return true != NULL;
757
758
}

759
760
761
762
763
/**
 * GetModuleNamesForCapability
 *
 * Return a NULL terminated array with the names of the modules
 * that have a certain capability.
764
 * Free after uses both the string and the table.
765
766
767
768
769
770
 * \param p_this vlc object structure
 * \param psz_capability the capability asked
 * \param pppsz_longname an pointer to an array of string to contain
    the long names of the modules. If set to NULL the function don't use it.
 * \return the NULL terminated array
 */
771
char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
772
                                               const char *psz_capability,
773
                                               char ***pppsz_longname )
774
{
775
    size_t count = 0;
776
    char **psz_ret;
777

778
779
    module_t **list = module_list_get (NULL);

780
    /* Do it in two passes : count the number of modules before */
781
    for (size_t i = 0; list[i]; i++)
782
    {
783
        module_t *p_module = list[i];
784
        const char *psz_module_capability = p_module->psz_capability;
785

786
787
788
        if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
            count++;
    }
789

790
    psz_ret = malloc( sizeof(char*) * (count+1) );
791
792
    if( pppsz_longname )
        *pppsz_longname = malloc( sizeof(char*) * (count+1) );
793
794
795
796
797
    if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
    {
        free( psz_ret );
        free( *pppsz_longname );
        *pppsz_longname = NULL;
798
        module_list_free (list);
799
800
801
        return NULL;
    }

802
    for (size_t i = 0, j = 0; list[i]; i++)
803
    {
804
        module_t *p_module = list[i];
805
        const char *psz_module_capability = p_module->psz_capability;
806

807
        if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
808
809
810
811
812
813
814
815
816
        {
            int k = -1; /* hack to handle submodules properly */
            if( p_module->b_submodule )
            {
                while( p_module->pp_shortcuts[++k] != NULL );
                k--;
            }
            psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
                                     :p_module->psz_object_name );
817
            if( pppsz_longname )
818
                (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
819
820
            j++;
        }
821
822
823
    }
    psz_ret[count] = NULL;

824
    module_list_free (list);
825

826
827
828
    return psz_ret;
}

829
830
831
832
833
834
835
/**
 * Get the configuration of a module
 *
 * \param module the module
 * \param psize the size of the configuration returned
 * \return the configuration as an array
 */
836
module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
837
{
838
    unsigned i,j;
839
    unsigned size = module->confsize;
840
    module_config_t *config = malloc( size * sizeof( *config ) );
841

842
    assert( psize != NULL );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
843
    *psize = 0;
844

845
846
847
    if( !config )
        return NULL;

848
    for( i = 0, j = 0; i < size; i++ )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
849
    {
850
        const module_config_t *item = module->p_config + i;
851
        if( item->b_internal /* internal option */
852
         || item->b_unsaveable /* non-modifiable option */
853
         || item->b_removed /* removed option */ )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
854
855
            continue;

856
        memcpy( config + j, item, sizeof( *config ) );
857
        j++;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
858
    }
859
    *psize = j;
860
861
862
863

    return config;
}

864
865
866
867
868
869
/**
 * Release the configuration
 *
 * \param the configuration
 * \return nothing
 */
870
void module_config_free( module_config_t *config )
871
{
872
    free( config );
873
874
}

Sam Hocevar's avatar
   
Sam Hocevar committed
875
876
877
878
/*****************************************************************************
 * Following functions are local.
 *****************************************************************************/

879
880
881
882
883
884
885
 /*****************************************************************************
 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
 * return first path.
 *****************************************************************************/
static char * copy_next_paths_token( char * paths, char ** remaining_paths )
{
    char * path;
886
    int i, done;
887
888
889
890
891
    bool escaped = false;

    assert( paths );

    /* Alloc a buffer to store the path */
Rafaël Carré's avatar
Rafaël Carré committed
892
    path = malloc( strlen( paths ) + 1 );
893
894
895
    if( !path ) return NULL;

    /* Look for PATH_SEP_CHAR (a ':' or a ';') */
896
897
    for( i = 0, done = 0 ; paths[i]; i++ )
    {
898
        /* Take care of \\ and \: or \; escapement */
899
900
        if( escaped )
        {
901
            escaped = false;
902
            path[done++] = paths[i];
903
        }
904
905
906
907
#ifdef WIN32
        else if( paths[i] == '/' )
            escaped = true;
#else
908
909
        else if( paths[i] == '\\' )
            escaped = true;
910
#endif
911
912
913
        else if( paths[i] == PATH_SEP_CHAR )
            break;
        else
914
            path[done++] = paths[i];
915
    }
916
    path[done++] = 0;
917
918
919
920
921
922
923
924
925

    /* Return the remaining paths */
    if( remaining_paths ) {
        *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
    }

    return path;
}

Sam Hocevar's avatar
   
Sam Hocevar committed
926
927
928
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
929
#ifdef HAVE_DYNAMIC_PLUGINS
930
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
931
{
932
    const char *vlcpath = vlc_global()->psz_vlcpath;
933
934
935
    int count,i;
    char * path;
    vlc_array_t *arraypaths = vlc_array_new();
936
937
938

    /* Contruct the special search path for system that have a relocatable
     * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
939
940
941
942
943
944

    if( vlcpath && asprintf( &pat