gstplugin.c 57.5 KB
Newer Older
1 2 3 4 5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstplugin.c: Plugin subsystem for loading elements, types, and libs
Erik Walthinsen's avatar
Erik Walthinsen committed
6 7 8 9 10
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
11
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
12 13 14 15 16 17 18
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Erik Walthinsen's avatar
Erik Walthinsen committed
21
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
22

23 24
/**
 * SECTION:gstplugin
25
 * @title: GstPlugin
26 27 28 29
 * @short_description: Container for features loaded from a shared object module
 * @see_also: #GstPluginFeature, #GstElementFactory
 *
 * GStreamer is extensible, so #GstElement instances can be loaded at runtime.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
30 31
 * A plugin system can provide one or more of the basic
 * <application>GStreamer</application> #GstPluginFeature subclasses.
32
 *
33
 * A plugin should export a symbol <symbol>gst_plugin_desc</symbol> that is a
34
 * struct of type #GstPluginDesc.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
35 36
 * the plugin loader will check the version of the core library the plugin was
 * linked against and will create a new #GstPlugin. It will then call the
37 38
 * #GstPluginInitFunc function that was provided in the
 * <symbol>gst_plugin_desc</symbol>.
39
 *
40
 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistry), you
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
41
 * can add any object that subclasses #GstPluginFeature.
42
 *
43
 * Usually plugins are always automatically loaded so you don't need to call
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44 45 46 47
 * gst_plugin_load() explicitly to bring it into memory. There are options to
 * statically link plugins to an app or even use GStreamer without a plugin
 * repository in which case gst_plugin_load() can be needed to bring the plugin
 * into memory.
48
 */
Erik Walthinsen's avatar
Erik Walthinsen committed
49

50 51 52
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
53 54 55

#include "gst_private.h"

56
#include <glib/gstdio.h>
Erik Walthinsen's avatar
Erik Walthinsen committed
57
#include <sys/types.h>
58
#ifdef HAVE_DIRENT_H
Erik Walthinsen's avatar
Erik Walthinsen committed
59
#include <dirent.h>
60 61
#endif
#ifdef HAVE_UNISTD_H
Erik Walthinsen's avatar
Erik Walthinsen committed
62
#include <unistd.h>
63
#endif
64
#include <signal.h>
65
#include <errno.h>
66
#include <string.h>
Erik Walthinsen's avatar
Erik Walthinsen committed
67

68
#include "glib-compat-private.h"
69

70
#include <gst/gst.h>
71

72 73
#define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING

74 75
static guint _num_static_plugins;       /* 0    */
static GstPluginDesc *_static_plugins;  /* NULL */
76
static gboolean _gst_plugin_inited;
77
static gchar **_plugin_loading_whitelist;       /* NULL */
78

79 80
/* static variables for segfault handling of plugin loading */
static char *_gst_plugin_fault_handler_filename = NULL;
81

82
/* list of valid licenses.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83
 * One of these must be specified or the plugin won't be loaded
84
 * Please file a bug to request any additional license be added.
85 86 87 88
 *
 * GPL: http://www.gnu.org/copyleft/gpl.html
 * LGPL: http://www.gnu.org/copyleft/lesser.html
 * QPL: http://www.trolltech.com/licenses/qpl.html
89
 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
90 91
 * MIT/X11: http://www.opensource.org/licenses/mit-license.php
 * 3-clause BSD: http://www.opensource.org/licenses/bsd-license.php
92
 */
93 94 95 96 97 98 99 100 101 102 103
static const gchar valid_licenses[] = "LGPL\000"        /* GNU Lesser General Public License */
    "GPL\000"                   /* GNU General Public License */
    "QPL\000"                   /* Trolltech Qt Public License */
    "GPL/QPL\000"               /* Combi-license of GPL + QPL */
    "MPL\000"                   /* MPL 1.1 license */
    "BSD\000"                   /* 3-clause BSD license */
    "MIT/X11\000"               /* MIT/X11 license */
    "Proprietary\000"           /* Proprietary license */
    GST_LICENSE_UNKNOWN;        /* some other license */

static const guint8 valid_licenses_idx[] = { 0, 5, 9, 13, 21, 25, 29, 37, 49 };
104

105
static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
106
    const GstPluginDesc * desc, gpointer user_data);
107 108
static void gst_plugin_desc_copy (GstPluginDesc * dest,
    const GstPluginDesc * src);
109

110
static void gst_plugin_ext_dep_free (GstPluginDep * dep);
David Schleef's avatar
David Schleef committed
111

112
G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
David Schleef's avatar
David Schleef committed
113 114 115

static void
gst_plugin_init (GstPlugin * plugin)
David Schleef's avatar
David Schleef committed
116
{
117 118
  plugin->priv =
      G_TYPE_INSTANCE_GET_PRIVATE (plugin, GST_TYPE_PLUGIN, GstPluginPrivate);
David Schleef's avatar
David Schleef committed
119 120
}

David Schleef's avatar
David Schleef committed
121
static void
122
gst_plugin_finalize (GObject * object)
123
{
124
  GstPlugin *plugin = GST_PLUGIN_CAST (object);
125

126
  GST_DEBUG ("finalizing plugin %" GST_PTR_FORMAT, plugin);
127 128 129

  /* FIXME: make registry add a weak ref instead */
#if 0
130
  GstRegistry *registry = gst_registry_get ();
131
  GList *g;
132 133 134 135 136
  for (g = registry->plugins; g; g = g->next) {
    if (g->data == (gpointer) plugin) {
      g_warning ("removing plugin that is still in registry");
    }
  }
137 138
#endif

139
  g_free (plugin->filename);
140
  g_free (plugin->basename);
141

142 143 144 145
  g_list_foreach (plugin->priv->deps, (GFunc) gst_plugin_ext_dep_free, NULL);
  g_list_free (plugin->priv->deps);
  plugin->priv->deps = NULL;

146 147 148 149
  if (plugin->priv->cache_data) {
    gst_structure_free (plugin->priv->cache_data);
  }

150
  G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
151 152
}

153 154 155
static void
gst_plugin_class_init (GstPluginClass * klass)
{
156
  G_OBJECT_CLASS (klass)->finalize = gst_plugin_finalize;
157 158

  g_type_class_add_private (klass, sizeof (GstPluginPrivate));
159
}
David Schleef's avatar
David Schleef committed
160

161
GQuark
162 163 164
gst_plugin_error_quark (void)
{
  static GQuark quark = 0;
165

166 167 168 169 170
  if (!quark)
    quark = g_quark_from_static_string ("gst_plugin_error");
  return quark;
}

171 172
/**
 * gst_plugin_register_static:
173 174 175 176 177 178 179 180
 * @major_version: the major version number of the GStreamer core that the
 *     plugin was compiled for, you can just use GST_VERSION_MAJOR here
 * @minor_version: the minor version number of the GStreamer core that the
 *     plugin was compiled for, you can just use GST_VERSION_MINOR here
 * @name: a unique name of the plugin (ideally prefixed with an application- or
 *     library-specific namespace prefix in order to avoid name conflicts in
 *     case a similar plugin with the same name ever gets added to GStreamer)
 * @description: description of the plugin
181
 * @init_func: (scope call): pointer to the init function of this plugin.
182 183 184 185 186 187
 * @version: version string of the plugin
 * @license: effective license of plugin. Must be one of the approved licenses
 *     (see #GstPluginDesc above) or the plugin will not be registered.
 * @source: source module plugin belongs to
 * @package: shipped package plugin belongs to
 * @origin: URL to provider of plugin
188 189 190 191 192 193 194 195
 *
 * Registers a static plugin, ie. a plugin which is private to an application
 * or library and contained within the application or library (as opposed to
 * being shipped as a separate module file).
 *
 * You must make sure that GStreamer has been initialised (with gst_init() or
 * via gst_init_get_option_group()) before calling this function.
 *
196
 * Returns: %TRUE if the plugin was registered correctly, otherwise %FALSE.
197 198
 */
gboolean
199
gst_plugin_register_static (gint major_version, gint minor_version,
Benjamin Otte's avatar
Benjamin Otte committed
200
    const gchar * name, const gchar * description, GstPluginInitFunc init_func,
201 202
    const gchar * version, const gchar * license, const gchar * source,
    const gchar * package, const gchar * origin)
203
{
204
  GstPluginDesc desc = { major_version, minor_version, name, description,
205
    init_func, version, license, source, package, origin, NULL,
206
  };
207 208 209
  GstPlugin *plugin;
  gboolean res = FALSE;

210 211 212 213 214 215 216 217
  g_return_val_if_fail (name != NULL, FALSE);
  g_return_val_if_fail (description != NULL, FALSE);
  g_return_val_if_fail (init_func != NULL, FALSE);
  g_return_val_if_fail (version != NULL, FALSE);
  g_return_val_if_fail (license != NULL, FALSE);
  g_return_val_if_fail (source != NULL, FALSE);
  g_return_val_if_fail (package != NULL, FALSE);
  g_return_val_if_fail (origin != NULL, FALSE);
218 219 220 221

  /* make sure gst_init() has been called */
  g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);

222
  GST_LOG ("attempting to load static plugin \"%s\" now...", name);
223
  plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
224 225
  if (gst_plugin_register_func (plugin, &desc, NULL) != NULL) {
    GST_INFO ("registered static plugin \"%s\"", name);
226
    res = gst_registry_add_plugin (gst_registry_get (), plugin);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    GST_INFO ("added static plugin \"%s\", result: %d", name, res);
  }
  return res;
}

/**
 * gst_plugin_register_static_full:
 * @major_version: the major version number of the GStreamer core that the
 *     plugin was compiled for, you can just use GST_VERSION_MAJOR here
 * @minor_version: the minor version number of the GStreamer core that the
 *     plugin was compiled for, you can just use GST_VERSION_MINOR here
 * @name: a unique name of the plugin (ideally prefixed with an application- or
 *     library-specific namespace prefix in order to avoid name conflicts in
 *     case a similar plugin with the same name ever gets added to GStreamer)
 * @description: description of the plugin
242 243
 * @init_full_func: (scope call): pointer to the init function with user data
 *     of this plugin.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
 * @version: version string of the plugin
 * @license: effective license of plugin. Must be one of the approved licenses
 *     (see #GstPluginDesc above) or the plugin will not be registered.
 * @source: source module plugin belongs to
 * @package: shipped package plugin belongs to
 * @origin: URL to provider of plugin
 * @user_data: gpointer to user data
 *
 * Registers a static plugin, ie. a plugin which is private to an application
 * or library and contained within the application or library (as opposed to
 * being shipped as a separate module file) with a #GstPluginInitFullFunc
 * which allows user data to be passed to the callback function (useful
 * for bindings).
 *
 * You must make sure that GStreamer has been initialised (with gst_init() or
 * via gst_init_get_option_group()) before calling this function.
 *
261
 * Returns: %TRUE if the plugin was registered correctly, otherwise %FALSE.
262 263 264
 */
gboolean
gst_plugin_register_static_full (gint major_version, gint minor_version,
Benjamin Otte's avatar
Benjamin Otte committed
265
    const gchar * name, const gchar * description,
266 267 268 269 270 271
    GstPluginInitFullFunc init_full_func, const gchar * version,
    const gchar * license, const gchar * source, const gchar * package,
    const gchar * origin, gpointer user_data)
{
  GstPluginDesc desc = { major_version, minor_version, name, description,
    (GstPluginInitFunc) init_full_func, version, license, source, package,
272
    origin, NULL,
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  };
  GstPlugin *plugin;
  gboolean res = FALSE;

  g_return_val_if_fail (name != NULL, FALSE);
  g_return_val_if_fail (description != NULL, FALSE);
  g_return_val_if_fail (init_full_func != NULL, FALSE);
  g_return_val_if_fail (version != NULL, FALSE);
  g_return_val_if_fail (license != NULL, FALSE);
  g_return_val_if_fail (source != NULL, FALSE);
  g_return_val_if_fail (package != NULL, FALSE);
  g_return_val_if_fail (origin != NULL, FALSE);

  /* make sure gst_init() has been called */
  g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);

  GST_LOG ("attempting to load static plugin \"%s\" now...", name);
290
  plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
291
  if (gst_plugin_register_func (plugin, &desc, user_data) != NULL) {
292
    GST_INFO ("registered static plugin \"%s\"", name);
293
    res = gst_registry_add_plugin (gst_registry_get (), plugin);
294
    GST_INFO ("added static plugin \"%s\", result: %d", name, res);
295
  }
296
  return res;
297 298 299
}

void
300
_priv_gst_plugin_initialize (void)
301
{
302
  const gchar *whitelist;
303 304
  guint i;

305
  _gst_plugin_inited = TRUE;
306

307 308 309 310 311 312 313 314 315
  whitelist = g_getenv ("GST_PLUGIN_LOADING_WHITELIST");
  if (whitelist != NULL && *whitelist != '\0') {
    _plugin_loading_whitelist = g_strsplit (whitelist,
        G_SEARCHPATH_SEPARATOR_S, -1);
    for (i = 0; _plugin_loading_whitelist[i] != NULL; ++i) {
      GST_INFO ("plugins whitelist entry: %s", _plugin_loading_whitelist[i]);
    }
  }

316
  /* now register all static plugins */
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  GST_INFO ("registering %u static plugins", _num_static_plugins);
  for (i = 0; i < _num_static_plugins; ++i) {
    gst_plugin_register_static (_static_plugins[i].major_version,
        _static_plugins[i].minor_version, _static_plugins[i].name,
        _static_plugins[i].description, _static_plugins[i].plugin_init,
        _static_plugins[i].version, _static_plugins[i].license,
        _static_plugins[i].source, _static_plugins[i].package,
        _static_plugins[i].origin);
  }

  if (_static_plugins) {
    free (_static_plugins);
    _static_plugins = NULL;
    _num_static_plugins = 0;
  }
332 333
}

334 335 336 337 338 339 340 341 342 343 344 345 346
/* Whitelist entry format:
 *
 *   plugin1,plugin2@pathprefix or
 *   plugin1,plugin2@* or just
 *   plugin1,plugin2 or
 *   source-package@pathprefix or
 *   source-package@* or just
 *   source-package
 *
 * ie. the bit before the path will be checked against both the plugin
 * name and the plugin's source package name, to keep the format simple.
 */
static gboolean
347
gst_plugin_desc_matches_whitelist_entry (const GstPluginDesc * desc,
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    const gchar * filename, const gchar * pattern)
{
  const gchar *sep;
  gboolean ret = FALSE;
  gchar *name;

  GST_LOG ("Whitelist pattern '%s', plugin: %s of %s@%s", pattern, desc->name,
      desc->source, GST_STR_NULL (filename));

  /* do we have a path prefix? */
  sep = strchr (pattern, '@');
  if (sep != NULL && strcmp (sep, "@*") != 0 && strcmp (sep, "@") != 0) {
    /* paths are not canonicalised or treated with realpath() here. This
     * should be good enough for our use case, since we just use the paths
     * autotools uses, and those will be constructed from the same prefix. */
    if (filename != NULL && !g_str_has_prefix (filename, sep + 1))
      return FALSE;

    GST_LOG ("%s matches path prefix %s", GST_STR_NULL (filename), sep + 1);
  }

  if (sep != NULL) {
    name = g_strndup (pattern, (gsize) (sep - pattern));
  } else {
    name = g_strdup (pattern);
  }

  g_strstrip (name);
  if (!g_ascii_isalnum (*name)) {
    GST_WARNING ("Invalid whitelist pattern: %s", pattern);
    goto done;
  }

  /* now check plugin names / source package name */
  if (strchr (name, ',') == NULL) {
    /* only a single name: either a plugin name or the source package name */
    ret = (strcmp (desc->source, name) == 0 || strcmp (desc->name, name) == 0);
  } else {
    gchar **n, **names;

    /* multiple names: assume these are plugin names */
    names = g_strsplit (name, ",", -1);
    for (n = names; n != NULL && *n != NULL; ++n) {
      g_strstrip (*n);
      if (strcmp (desc->name, *n) == 0) {
        ret = TRUE;
        break;
      }
    }
    g_strfreev (names);
  }

  GST_LOG ("plugin / source package name match: %d", ret);

done:

  g_free (name);
  return ret;
}

gboolean
409
priv_gst_plugin_desc_is_whitelisted (const GstPluginDesc * desc,
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    const gchar * filename)
{
  gchar **entry;

  if (_plugin_loading_whitelist == NULL)
    return TRUE;

  for (entry = _plugin_loading_whitelist; *entry != NULL; ++entry) {
    if (gst_plugin_desc_matches_whitelist_entry (desc, filename, *entry)) {
      GST_LOG ("Plugin %s is in whitelist", filename);
      return TRUE;
    }
  }

  GST_LOG ("Plugin %s (package %s, file %s) not in whitelist", desc->name,
      desc->source, filename);
  return FALSE;
}

gboolean
priv_gst_plugin_loading_have_whitelist (void)
{
  return (_plugin_loading_whitelist != NULL);
}

435 436 437 438 439 440 441 442 443
guint32
priv_gst_plugin_loading_get_whitelist_hash (void)
{
  guint32 hash = 0;

  if (_plugin_loading_whitelist != NULL) {
    gchar **w;

    for (w = _plugin_loading_whitelist; *w != NULL; ++w)
444
      hash ^= g_str_hash (*w);
445 446 447 448 449
  }

  return hash;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
450
/* this function could be extended to check if the plugin license matches the
451 452 453 454
 * applications license (would require the app to register its license somehow).
 * We'll wait for someone who's interested in it to code it :)
 */
static gboolean
455
gst_plugin_check_license (const gchar * license)
456
{
457
  gint i;
458

459 460
  for (i = 0; i < G_N_ELEMENTS (valid_licenses_idx); ++i) {
    if (strcmp (license, valid_licenses + valid_licenses_idx[i]) == 0)
461 462 463 464 465
      return TRUE;
  }
  return FALSE;
}

466 467 468
static gboolean
gst_plugin_check_version (gint major, gint minor)
{
469 470
  /* return NULL if the major and minor version numbers are not compatible */
  /* with ours. */
Sebastian Dröge's avatar
Sebastian Dröge committed
471
  if (major != GST_VERSION_MAJOR || minor > GST_VERSION_MINOR)
472 473 474 475 476
    return FALSE;

  return TRUE;
}

477
static GstPlugin *
478 479
gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc,
    gpointer user_data)
480 481
{
  if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
482
    if (GST_CAT_DEFAULT)
483 484 485 486
      GST_WARNING ("plugin \"%s\" has incompatible version "
          "(plugin: %d.%d, gst: %d,%d), not loading",
          GST_STR_NULL (plugin->filename), desc->major_version,
          desc->minor_version, GST_VERSION_MAJOR, GST_VERSION_MINOR);
487
    return NULL;
488 489
  }

490 491
  if (!desc->license || !desc->description || !desc->source ||
      !desc->package || !desc->origin) {
492
    if (GST_CAT_DEFAULT)
493 494
      GST_WARNING ("plugin \"%s\" has missing detail in GstPluginDesc, not "
          "loading", GST_STR_NULL (plugin->filename));
495
    return NULL;
496
  }
497

498
  if (!gst_plugin_check_license (desc->license)) {
499
    if (GST_CAT_DEFAULT)
500
      GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
501
          GST_STR_NULL (plugin->filename), desc->license);
502
    return NULL;
503
  }
504

505 506 507
  if (GST_CAT_DEFAULT)
    GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));

508
  gst_plugin_desc_copy (&plugin->desc, desc);
509

510 511 512 513 514 515
  /* make resident so we're really sure it never gets unloaded again.
   * Theoretically this is not needed, but practically it doesn't hurt.
   * And we're rather safe than sorry. */
  if (plugin->module)
    g_module_make_resident (plugin->module);

516 517 518
  if (user_data) {
    if (!(((GstPluginInitFullFunc) (desc->plugin_init)) (plugin, user_data))) {
      if (GST_CAT_DEFAULT)
519 520
        GST_WARNING ("plugin \"%s\" failed to initialise",
            GST_STR_NULL (plugin->filename));
521 522 523 524 525
      return NULL;
    }
  } else {
    if (!((desc->plugin_init) (plugin))) {
      if (GST_CAT_DEFAULT)
526 527
        GST_WARNING ("plugin \"%s\" failed to initialise",
            GST_STR_NULL (plugin->filename));
528 529
      return NULL;
    }
530
  }
531 532 533

  if (GST_CAT_DEFAULT)
    GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
534

535 536 537
  return plugin;
}

538
#ifdef HAVE_SIGACTION
539
static struct sigaction oldaction;
540
static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
541

542
/*
543 544 545 546 547 548
 * _gst_plugin_fault_handler_restore:
 * segfault handler restorer
 */
static void
_gst_plugin_fault_handler_restore (void)
{
549
  if (!_gst_plugin_fault_handler_is_setup)
550 551
    return;

552
  _gst_plugin_fault_handler_is_setup = FALSE;
553

554
  sigaction (SIGSEGV, &oldaction, NULL);
555 556
}

557
/*
558 559 560 561 562 563 564 565 566
 * _gst_plugin_fault_handler_sighandler:
 * segfault handler implementation
 */
static void
_gst_plugin_fault_handler_sighandler (int signum)
{
  /* We need to restore the fault handler or we'll keep getting it */
  _gst_plugin_fault_handler_restore ();

567
  switch (signum) {
568
    case SIGSEGV:
569
      g_print ("\nERROR: ");
570 571 572 573
      g_print ("Caught a segmentation fault while loading plugin file:\n");
      g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
      g_print ("Please either:\n");
      g_print ("- remove it and restart.\n");
574 575
      g_print
          ("- run with --gst-disable-segtrap --gst-disable-registry-fork and debug.\n");
576 577 578 579 580 581 582 583
      exit (-1);
      break;
    default:
      g_print ("Caught unhandled signal on plugin loading\n");
      break;
  }
}

584
/*
585 586 587 588 589 590 591 592 593
 * _gst_plugin_fault_handler_setup:
 * sets up the segfault handler
 */
static void
_gst_plugin_fault_handler_setup (void)
{
  struct sigaction action;

  /* if asked to leave segfaults alone, just return */
594
  if (!gst_segtrap_is_enabled ())
595
    return;
596

597 598
  if (_gst_plugin_fault_handler_is_setup)
    return;
599

600 601
  _gst_plugin_fault_handler_is_setup = TRUE;

602 603 604
  memset (&action, 0, sizeof (action));
  action.sa_handler = _gst_plugin_fault_handler_sighandler;

605
  sigaction (SIGSEGV, &action, &oldaction);
606
}
607
#else /* !HAVE_SIGACTION */
608 609 610 611 612 613 614 615 616
static void
_gst_plugin_fault_handler_restore (void)
{
}

static void
_gst_plugin_fault_handler_setup (void)
{
}
617
#endif /* HAVE_SIGACTION */
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
/* g_time_val_from_iso8601() doesn't do quite what we want */
static gboolean
check_release_datetime (const gchar * date_time)
{
  guint64 val;

  /* we require YYYY-MM-DD or YYYY-MM-DDTHH:MMZ format */
  if (!g_ascii_isdigit (*date_time))
    return FALSE;

  val = g_ascii_strtoull (date_time, (gchar **) & date_time, 10);
  if (val < 2000 || val > 2100 || *date_time != '-')
    return FALSE;

  val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
  if (val == 0 || val > 12 || *date_time != '-')
    return FALSE;

  val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
  if (val == 0 || val > 32)
    return FALSE;

  /* end of string or date/time separator + HH:MMZ */
  if (*date_time == 'T' || *date_time == ' ') {
    val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
    if (val > 24 || *date_time != ':')
      return FALSE;

    val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
    if (val > 59 || *date_time != 'Z')
      return FALSE;

    ++date_time;
  }

  return (*date_time == '\0');
}

657
static GMutex gst_plugin_loading_mutex;
658

659
#define CHECK_PLUGIN_DESC_FIELD(desc,field,fn)                               \
660 661
  if (G_UNLIKELY ((desc)->field == NULL || *(desc)->field == '\0')) {        \
    g_warning ("Plugin description for '%s' has no valid %s field", fn, G_STRINGIFY (field)); \
662 663 664
    g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, \
        "Plugin %s has invalid plugin description field '%s'", \
        filename, G_STRINGIFY (field)); \
665
    goto return_error;                                                       \
666 667
  }

668
/**
David Schleef's avatar
David Schleef committed
669
 * gst_plugin_load_file:
670
 * @filename: (type filename): the plugin filename to load
671
 * @error: pointer to a %NULL-valued GError
672
 *
673
 * Loads the given plugin and refs it.  Caller needs to unref after use.
674
 *
675
 * Returns: (transfer full): a reference to the existing loaded GstPlugin, a
676
 * reference to the newly-loaded GstPlugin, or %NULL if an error occurred.
677
 */
David Schleef's avatar
David Schleef committed
678 679
GstPlugin *
gst_plugin_load_file (const gchar * filename, GError ** error)
680 681 682 683
{
  return _priv_gst_plugin_load_file_for_registry (filename, NULL, error);
}

684 685 686 687 688
static gchar *
extract_symname (const char *filename)
{
  gchar *bname, *name, *symname;
  const gchar *dot;
689
  gsize prefix_len, len;
690
  int i;
691 692

  bname = g_path_get_basename (filename);
693 694 695 696
  for (i = 0; bname[i]; ++i) {
    if (bname[i] == '-')
      bname[i] = '_';
  }
697 698

  if (g_str_has_prefix (bname, "libgst"))
699
    prefix_len = 6;
700
  else if (g_str_has_prefix (bname, "lib"))
701 702 703 704 705
    prefix_len = 3;
  else if (g_str_has_prefix (bname, "gst"))
    prefix_len = 3;
  else
    prefix_len = 0;             /* use whole name (minus suffix) as plugin name */
706

707
  dot = g_utf8_strchr (bname, -1, '.');
708 709 710
  if (dot)
    len = dot - bname - prefix_len;
  else
711
    len = strlen (bname + prefix_len);
712 713 714 715 716 717 718 719 720 721

  name = g_strndup (bname + prefix_len, len);
  g_free (bname);

  symname = g_strconcat ("gst_plugin_", name, "_get_desc", NULL);
  g_free (name);

  return symname;
}

722 723 724 725
/* Note: The return value is (transfer full) although we work with floating
 * references here. If a new plugin instance is created, it is always sinked
 * in the registry first and a new reference is returned
 */
726 727 728
GstPlugin *
_priv_gst_plugin_load_file_for_registry (const gchar * filename,
    GstRegistry * registry, GError ** error)
729
{
730
  const GstPluginDesc *desc;
David Schleef's avatar
David Schleef committed
731
  GstPlugin *plugin;
732
  gchar *symname;
David Schleef's avatar
David Schleef committed
733 734
  GModule *module;
  gboolean ret;
735
  gpointer ptr;
736
  GStatBuf file_status;
737
  gboolean new_plugin = TRUE;
738
  GModuleFlags flags;
739

David Schleef's avatar
David Schleef committed
740
  g_return_val_if_fail (filename != NULL, NULL);
741

742 743 744
  if (registry == NULL)
    registry = gst_registry_get ();

745
  g_mutex_lock (&gst_plugin_loading_mutex);
746 747

  plugin = gst_registry_lookup (registry, filename);
748 749
  if (plugin) {
    if (plugin->module) {
750
      /* already loaded */
751
      g_mutex_unlock (&gst_plugin_loading_mutex);
752 753
      return plugin;
    } else {
754 755
      /* load plugin and update fields */
      new_plugin = FALSE;
756
    }
757 758
  }

David Schleef's avatar
David Schleef committed
759 760
  GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
      filename);
761

762
  if (!g_module_supported ()) {
David Schleef's avatar
David Schleef committed
763
    GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
764 765 766
    g_set_error (error,
        GST_PLUGIN_ERROR,
        GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
767
    goto return_error;
768 769
  }

770
  if (g_stat (filename, &file_status)) {
David Schleef's avatar
David Schleef committed
771
    GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
772 773
    g_set_error (error,
        GST_PLUGIN_ERROR,
774 775
        GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
        g_strerror (errno));
776
    goto return_error;
777 778
  }

779 780 781 782 783 784 785
  flags = G_MODULE_BIND_LOCAL;
  /* libgstpython.so is the gst-python plugin loader. It needs to be loaded with
   * G_MODULE_BIND_LAZY.
   *
   * Ideally there should be a generic way for plugins to specify that they
   * need to be loaded with _LAZY.
   * */
786
  if (strstr (filename, "libgstpython"))
787
    flags |= G_MODULE_BIND_LAZY;
788

789
  module = g_module_open (filename, flags);
David Schleef's avatar
David Schleef committed
790
  if (module == NULL) {
791
    GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
David Schleef's avatar
David Schleef committed
792 793
        g_module_error ());
    g_set_error (error,
794 795
        GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
        g_module_error ());
796 797 798
    /* If we failed to open the shared object, then it's probably because a
     * plugin is linked against the wrong libraries. Print out an easy-to-see
     * message in this case. */
799
    g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
800
    goto return_error;
801
  }
802

803 804 805 806 807 808 809 810 811 812 813 814 815 816
  symname = extract_symname (filename);
  ret = g_module_symbol (module, symname, &ptr);

  if (ret) {
    GstPluginDesc *(*get_desc) (void) = ptr;
    ptr = get_desc ();
  } else {
    GST_DEBUG ("Could not find symbol '%s', falling back to gst_plugin_desc",
        symname);
    ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
  }

  g_free (symname);

David Schleef's avatar
David Schleef committed
817 818 819 820 821
  if (!ret) {
    GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
    g_set_error (error,
        GST_PLUGIN_ERROR,
        GST_PLUGIN_ERROR_MODULE,
822
        "File \"%s\" is not a GStreamer plugin", filename);
823
    g_module_close (module);
824
    goto return_error;
825
  }
826

827
  desc = (const GstPluginDesc *) ptr;
828 829 830 831 832 833 834 835 836 837 838 839

  if (priv_gst_plugin_loading_have_whitelist () &&
      !priv_gst_plugin_desc_is_whitelisted (desc, filename)) {
    GST_INFO ("Whitelist specified and plugin not in whitelist, not loading: "
        "name=%s, package=%s, file=%s", desc->name, desc->source, filename);
    g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
        "Not loading plugin file \"%s\", not in whitelist", filename);
    g_module_close (module);
    goto return_error;
  }

  if (new_plugin) {
840
    plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
841 842 843 844 845 846 847
    plugin->file_mtime = file_status.st_mtime;
    plugin->file_size = file_status.st_size;
    plugin->filename = g_strdup (filename);
    plugin->basename = g_path_get_basename (filename);
  }

  plugin->module = module;
848

849
  if (new_plugin) {
850
    /* check plugin description: complain about bad values and fail */
851 852 853 854 855 856 857 858 859
    CHECK_PLUGIN_DESC_FIELD (desc, name, filename);
    CHECK_PLUGIN_DESC_FIELD (desc, description, filename);
    CHECK_PLUGIN_DESC_FIELD (desc, version, filename);
    CHECK_PLUGIN_DESC_FIELD (desc, license, filename);
    CHECK_PLUGIN_DESC_FIELD (desc, source, filename);
    CHECK_PLUGIN_DESC_FIELD (desc, package, filename);
    CHECK_PLUGIN_DESC_FIELD (desc, origin, filename);

    if (desc->name != NULL && desc->name[0] == '"') {
860
      g_warning ("Invalid plugin name '%s' - fix your GST_PLUGIN_DEFINE "
861
          "(remove quotes around plugin name)", desc->name);
862 863
    }

864 865 866 867 868 869 870 871
    if (desc->release_datetime != NULL &&
        !check_release_datetime (desc->release_datetime)) {
      g_warning ("GstPluginDesc for '%s' has invalid datetime '%s'",
          filename, desc->release_datetime);
      g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
          "Plugin %s has invalid plugin description field 'release_datetime'",
          filename);
      goto return_error;
872
    }
873
  }
874

875 876 877 878 879 880 881
  GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
      plugin, filename);

  /* this is where we load the actual .so, so let's trap SIGSEGV */
  _gst_plugin_fault_handler_setup ();
  _gst_plugin_fault_handler_filename = plugin->filename;

882 883 884
  GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
      plugin, filename);

885
  if (!gst_plugin_register_func (plugin, desc, NULL)) {
886 887 888 889 890 891 892
    /* remove signal handler */
    _gst_plugin_fault_handler_restore ();
    GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
    /* plugin == NULL */
    g_set_error (error,
        GST_PLUGIN_ERROR,
        GST_PLUGIN_ERROR_MODULE,
893 894
        "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
        filename);
895
    goto return_error;
Wim Taymans's avatar
Wim Taymans committed
896
  }
David Schleef's avatar
David Schleef committed
897 898 899 900 901 902

  /* remove signal handler */
  _gst_plugin_fault_handler_restore ();
  _gst_plugin_fault_handler_filename = NULL;
  GST_INFO ("plugin \"%s\" loaded", plugin->filename);

903 904
  if (new_plugin) {
    gst_object_ref (plugin);
905
    gst_registry_add_plugin (registry, plugin);
906
  }
David Schleef's avatar
David Schleef committed
907

908
  g_mutex_unlock (&gst_plugin_loading_mutex);
David Schleef's avatar
David Schleef committed
909
  return plugin;
910

911
return_error:
912 913 914
  {
    if (plugin)
      gst_object_unref (plugin);
915
    g_mutex_unlock (&gst_plugin_loading_mutex);
916 917
    return NULL;
  }
Erik Walthinsen's avatar
Erik Walthinsen committed
918 919
}

920
static void
921
gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
922 923 924
{
  dest->major_version = src->major_version;
  dest->minor_version = src->minor_version;
925
  dest->name = g_intern_string (src->name);
Benjamin Otte's avatar
Benjamin Otte committed
926
  dest->description = g_intern_string (src->description);
927
  dest->plugin_init = src->plugin_init;
928 929 930 931 932
  dest->version = g_intern_string (src->version);
  dest->license = g_intern_string (src->license);
  dest->source = g_intern_string (src->source);
  dest->package = g_intern_string (src->package);
  dest->origin = g_intern_string (src->origin);
933
  dest->release_datetime = g_intern_string (src->release_datetime);
934
}
935

936 937 938 939 940 941 942 943
/**
 * gst_plugin_get_name:
 * @plugin: plugin to get the name of
 *
 * Get the short name of the plugin
 *
 * Returns: the name of the plugin
 */
944 945
const gchar *
gst_plugin_get_name (GstPlugin * plugin)
946 947 948
{
  g_return_val_if_fail (plugin != NULL, NULL);

949
  return plugin->desc.name;
950 951 952
}

/**
953
 * gst_plugin_get_description:
954
 * @plugin: plugin to get long name of
955
 *
956 957 958
 * Get the long descriptive name of the plugin
 *
 * Returns: the long name of the plugin
959
 */
960
const gchar *
961
gst_plugin_get_description (GstPlugin * plugin)
962
{
963
  g_return_val_if_fail (plugin != NULL, NULL);
964

965
  return plugin->desc.description;
966 967
}

Erik Walthinsen's avatar
Erik Walthinsen committed
968
/**
969 970 971 972
 * gst_plugin_get_filename:
 * @plugin: plugin to get the filename of
 *
 * get the filename of the plugin
Erik Walthinsen's avatar
Erik Walthinsen committed
973
 *
974
 * Returns: (type filename): the filename of the plugin
Erik Walthinsen's avatar
Erik Walthinsen committed
975
 */
976
const gchar *
977
gst_plugin_get_filename (GstPlugin * plugin)
978
{
979
  g_return_val_if_fail (plugin != NULL, NULL);
980

981
  return plugin->filename;
Erik Walthinsen's avatar
Erik Walthinsen committed
982
}
983

984 985 986 987 988 989 990 991
/**
 * gst_plugin_get_version:
 * @plugin: plugin to get the version of
 *
 * get the version of the plugin
 *
 * Returns: the version of the plugin
 */
992
const gchar *
993 994 995 996 997 998 999
gst_plugin_get_version (GstPlugin * plugin)
{
  g_return_val_if_fail (plugin != NULL, NULL);

  return plugin->desc.version;
}

1000 1001 1002 1003 1004 1005 1006 1007
/**
 * gst_plugin_get_license:
 * @plugin: plugin to get the license of
 *
 * get the license of the plugin
 *
 * Returns: the license of the plugin
 */
1008
const gchar *
1009
gst_plugin_get_license (GstPlugin * plugin)
1010 1011
{
  g_return_val_if_fail (plugin != NULL, NULL);
Erik Walthinsen's avatar
Erik Walthinsen committed
1012

1013 1014
  return plugin->desc.license;
}
1015

1016 1017 1018 1019 1020 1021 1022 1023
/**
 * gst_plugin_get_source:
 * @plugin: plugin to get the source of
 *
 * get the source module the plugin belongs to.
 *
 * Returns: the source of the plugin
 */
1024
const gchar *
1025 1026 1027 1028 1029 1030 1031
gst_plugin_get_source (GstPlugin * plugin)
{
  g_return_val_if_fail (plugin != NULL, NULL);

  return plugin->desc.source;
}

1032 1033 1034 1035 1036 1037 1038 1039
/**
 * gst_plugin_get_package:
 * @plugin: plugin to get the package of
 *
 * get the package the plugin belongs to.
 *
 * Returns: the package of the plugin
 */
1040
const gchar *
1041
gst_plugin_get_package (GstPlugin * plugin)
1042 1043
{
  g_return_val_if_fail (plugin != NULL, NULL);
1044

1045 1046
  return plugin->desc.package;
}
1047

1048
/**
1049 1050
 * gst_plugin_get_origin:
 * @plugin: plugin to get the origin of
1051
 *
1052
 * get the URL where the plugin comes from
1053
 *
1054
 * Returns: the origin of the plugin
1055
 */
1056
const gchar *
1057
gst_plugin_get_origin (GstPlugin * plugin)
1058 1059 1060
{
  g_return_val_if_fail (plugin != NULL, NULL);

1061
  return plugin->desc.origin;
1062
}
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
/**
 * gst_plugin_get_release_date_string:
 * @plugin: plugin to get the release date of
 *
 * Get the release date (and possibly time) in form of a string, if available.
 *
 * For normal GStreamer plugin releases this will usually just be a date in
 * the form of "YYYY-MM-DD", while pre-releases and builds from git may contain
 * a time component after the date as well, in which case the string will be
 * formatted like "YYYY-MM-DDTHH:MMZ" (e.g. "2012-04-30T09:30Z").
 *
 * There may be plugins that do not have a valid release date set on them.
 *
1077 1078
 * Returns: (nullable): the date string of the plugin, or %NULL if not
 * available.
1079 1080 1081 1082 1083 1084 1085 1086 1087
 */
const gchar *
gst_plugin_get_release_date_string (GstPlugin * plugin)
{
  g_return_val_if_fail (plugin != NULL, NULL);

  return plugin->desc.release_datetime;
}

1088 1089 1090 1091 1092 1093
/**
 * gst_plugin_is_loaded:
 * @plugin: plugin to query
 *
 * queries if the plugin is loaded into memory
 *
1094
 * Returns: %TRUE is loaded, %FALSE otherwise
1095 1096
 */
gboolean
1097
gst_plugin_is_loaded (GstPlugin * plugin)
1098 1099 1100
{
  g_return_val_if_fail (plugin != NULL, FALSE);

1101
  return (plugin->module != NULL || plugin->filename == NULL);
1102 1103
}

1104 1105 1106 1107 1108
/**
 * gst_plugin_get_cache_data:
 * @plugin: a plugin
 *
 * Gets the plugin specific data cache. If it is %NULL there is no cached data
Benjamin Otte's avatar
Benjamin Otte committed
1109
 * stored. This is the case when the registry is getting rebuilt.
1110
 *
1111 1112
 * Returns: (transfer none) (nullable): The cached data as a
 * #GstStructure or %NULL.
1113
 */
1114
const GstStructure *
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
gst_plugin_get_cache_data (GstPlugin * plugin)
{
  g_return_val_if_fail (GST_IS_PLUGIN (plugin), NULL);

  return plugin->priv->cache_data;
}

/**
 * gst_plugin_set_cache_data:
 * @plugin: a plugin
1125
 * @cache_data: (transfer full): a structure containing the data to cache
1126 1127 1128 1129
 *
 * Adds plugin specific data to cache. Passes the ownership of the structure to
 * the @plugin.
 *
Benjamin Otte's avatar
Benjamin Otte committed
1130
 * The cache is flushed every time the registry is rebuilt.
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
 */
void
gst_plugin_set_cache_data (GstPlugin * plugin, GstStructure * cache_data)
{
  g_return_if_fail (GST_IS_PLUGIN (plugin));
  g_return_if_fail (GST_IS_STRUCTURE (cache_data));

  if (plugin->priv->cache_data) {
    gst_structure_free (plugin->priv->cache_data);
  }
  plugin->priv->cache_data = cache_data;
}

1144
#if 0
Wim Taymans's avatar
Wim Taymans committed
1145 1146 1147 1148 1149 1150 1151 1152
/**
 * gst_plugin_feature_list:
 * @plugin: plugin to query
 * @filter: the filter to use
 * @first: only return first match
 * @user_data: user data passed to the filter function
 *
 * Runs a filter against all plugin features and returns a GList with