Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstpluginloader.c
index 36b796a..6ac9b60 100644 (file)
 #  include "config.h"
 #endif
 
+#include <gst/gst_private.h>
+
 #ifndef G_OS_WIN32
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#else
+#define fsync(fd) _commit(fd)
+#include <io.h>
 #endif
+
 #include <errno.h>
 
-#include <gst/gst_private.h>
 #include <gst/gstconfig.h>
 
 #include <gst/gstpoll.h>
@@ -56,12 +61,14 @@ static gboolean plugin_loader_free (GstPluginLoader * loader);
 static gboolean plugin_loader_load (GstPluginLoader * loader,
     const gchar * filename, off_t file_size, time_t file_mtime);
 
+/* functions used in GstRegistry scanning */
 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
   plugin_loader_new, plugin_loader_free, plugin_loader_load
 };
 
 typedef struct _PendingPluginEntry
 {
+  /* sequence number */
   guint32 tag;
   gchar *filename;
   off_t file_size;
@@ -87,6 +94,7 @@ struct _GstPluginLoader
   guint tx_buf_write;
   guint tx_buf_read;
 
+  /* next sequence number (for PendingPluginEntry) */
   guint32 next_tag;
 
   guint8 *rx_buf;
@@ -130,7 +138,7 @@ static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
 static GstPluginLoader *
 plugin_loader_new (GstRegistry * registry)
 {
-  GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
+  GstPluginLoader *l = g_slice_new0 (GstPluginLoader);
 
   if (registry)
     l->registry = gst_object_ref (registry);
@@ -191,12 +199,12 @@ plugin_loader_free (GstPluginLoader * loader)
   while (cur) {
     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
     g_free (entry->filename);
-    g_free (entry);
+    g_slice_free (PendingPluginEntry, entry);
 
     cur = g_list_delete_link (cur, cur);
   }
 
-  g_free (loader);
+  g_slice_free (GstPluginLoader, loader);
 
   return got_plugin_details;
 }
@@ -215,7 +223,7 @@ plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
   GST_LOG_OBJECT (loader->registry,
       "Sending file %s to child. tag %u", filename, loader->next_tag);
 
-  entry = g_new (PendingPluginEntry, 1);
+  entry = g_slice_new (PendingPluginEntry);
   entry->tag = loader->next_tag++;
   entry->filename = g_strdup (filename);
   entry->file_size = file_size;
@@ -262,6 +270,8 @@ restart:
       l->got_plugin_details = TRUE;
       /* Now remove this crashy plugin from the head of the list */
       l->pending_plugins = g_list_delete_link (cur, cur);
+      g_free (entry->filename);
+      g_slice_free (PendingPluginEntry, entry);
       if (l->pending_plugins == NULL)
         l->pending_plugins_tail = NULL;
       if (!gst_plugin_loader_spawn (l))
@@ -329,9 +339,9 @@ plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
 
   plugin->basename = g_path_get_basename (plugin->filename);
   plugin->desc.name = g_intern_string (plugin->basename);
-  plugin->desc.description = g_strdup_printf ("Plugin for blacklisted file");
-  plugin->desc.version = g_intern_string ("0.0.0");
-  plugin->desc.license = g_intern_string ("BLACKLIST");
+  plugin->desc.description = "Plugin for blacklisted file";
+  plugin->desc.version = "0.0.0";
+  plugin->desc.license = "BLACKLIST";
   plugin->desc.source = plugin->desc.license;
   plugin->desc.package = plugin->desc.license;
   plugin->desc.origin = plugin->desc.license;
@@ -343,9 +353,36 @@ plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
 static gboolean
 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
 {
-  char *argv[] = { location, "-l", NULL };
+#ifdef __APPLE__
+#if defined(__x86_64__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-x86_64",
+    location, (char *) "-l", NULL
+  };
+#elif defined(__i386__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-i386",
+    location, (char *) "-l", NULL
+  };
+#elif defined(__ppc__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-ppc",
+    location, (char *) "-l", NULL
+  };
+#elif defined(__ppc64__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-ppc64",
+    location, (char *) "-l", NULL
+  };
+#endif
+#else /* ! __APPLE__ */
+  char *argv[] = { location, (char *) "-l", NULL };
+#endif
+
+
+#ifdef __APPLE__
+  GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s with arch %s",
+      location, argv[1]);
+#else
+  GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
+#endif
 
-  GST_LOG ("Trying to spawn plugin-scanner helper at %s", location);
   if (!g_spawn_async_with_pipes (NULL, argv, NULL,
           G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
           NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
@@ -371,29 +408,32 @@ gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
 static gboolean
 gst_plugin_loader_spawn (GstPluginLoader * loader)
 {
+  const gchar *env;
   char *helper_bin;
-  gboolean res;
+  gboolean res = FALSE;
 
   if (loader->child_running)
     return TRUE;
 
-  /* Find the plugin-scanner, first try installed then by env-var */
-  helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
-  res = gst_plugin_loader_try_helper (loader, helper_bin);
-  g_free (helper_bin);
+  /* Find the gst-plugin-scanner: first try the env-var if it is set,
+   * otherwise use the installed version */
+  env = g_getenv ("GST_PLUGIN_SCANNER");
+
+  if (env != NULL && *env != '\0') {
+    GST_LOG ("Trying GST_PLUGIN_SCANNER env var: %s", env);
+    helper_bin = g_strdup (env);
+    res = gst_plugin_loader_try_helper (loader, helper_bin);
+    g_free (helper_bin);
+  }
 
   if (!res) {
-    /* Try the GST_PLUGIN_SCANNER env var */
-    const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
-    if (env != NULL) {
-      GST_LOG ("Installed plugin scanner failed. "
-          "Trying GST_PLUGIN_SCANNER env var: %s", env);
-      helper_bin = g_strdup (env);
-      res = gst_plugin_loader_try_helper (loader, helper_bin);
-      g_free (helper_bin);
-    } else {
-      GST_LOG ("Installed plugin scanner failed and "
-          "GST_PLUGIN_SCANNER env var not set. No plugin-scanner available");
+    GST_LOG ("Trying installed plugin scanner");
+    helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
+    res = gst_plugin_loader_try_helper (loader, helper_bin);
+    g_free (helper_bin);
+
+    if (!res) {
+      GST_INFO ("No gst-plugin-scanner available, or not working");
     }
   }
 
@@ -424,10 +464,9 @@ plugin_loader_cleanup_child (GstPluginLoader * l)
 }
 
 gboolean
-_gst_plugin_loader_client_run ()
+_gst_plugin_loader_client_run (void)
 {
   GstPluginLoader *l;
-  int dup_fd;
 
   l = plugin_loader_new (NULL);
   if (l == NULL)
@@ -437,25 +476,29 @@ _gst_plugin_loader_client_run ()
    * Dup those somewhere better so that plugins printing things
    * won't interfere with anything */
 #ifndef G_OS_WIN32
-  dup_fd = dup (0);             /* STDIN */
-  if (dup_fd == -1) {
-    GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
-    return FALSE;
-  }
-  l->fd_r.fd = dup_fd;
-  close (0);
+  {
+    int dup_fd;
 
-  dup_fd = dup (1);             /* STDOUT */
-  if (dup_fd == -1) {
-    GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
-    return FALSE;
-  }
-  l->fd_w.fd = dup_fd;
-  close (1);
+    dup_fd = dup (0);           /* STDIN */
+    if (dup_fd == -1) {
+      GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
+      return FALSE;
+    }
+    l->fd_r.fd = dup_fd;
+    close (0);
+
+    dup_fd = dup (1);           /* STDOUT */
+    if (dup_fd == -1) {
+      GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
+      return FALSE;
+    }
+    l->fd_w.fd = dup_fd;
+    close (1);
 
-  /* Dup stderr down to stdout so things that plugins print are visible,
-   * but don't care if it fails */
-  dup2 (2, 1);
+    /* Dup stderr down to stdout so things that plugins print are visible,
+     * but don't care if it fails */
+    dup2 (2, 1);
+  }
 #else
   /* FIXME: Use DuplicateHandle and friends on win32 */
   l->fd_w.fd = 1;               /* STDOUT */
@@ -486,6 +529,8 @@ put_packet (GstPluginLoader * l, guint type, guint32 tag,
   guint len = payload_len + HEADER_SIZE;
 
   if (l->tx_buf_write + len >= l->tx_buf_size) {
+    GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
+        l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
     l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
     l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
   }
@@ -520,8 +565,12 @@ put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
 
   len = padsize + chunk->size;
 
-  if (l->tx_buf_write + len >= l->tx_buf_size) {
-    l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
+  if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
+    guint new_size = MAX (l->tx_buf_write + len,
+        l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
+    GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
+        l->tx_buf_size, new_size, chunk->size);
+    l->tx_buf_size = new_size;
     l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
   }
 
@@ -568,15 +617,15 @@ write_one (GstPluginLoader * l)
 
   do {
     res = write (l->fd_w.fd, out, to_write);
-    if (res > 0) {
-      to_write -= res;
-      out += res;
+    if (G_UNLIKELY (res < 0)) {
+      if (errno == EAGAIN || errno == EINTR)
+        continue;
+      /* Failed to write -> child died */
+      goto fail_and_cleanup;
     }
-  } while (to_write > 0 && res < 0 && (errno == EAGAIN || errno == EINTR));
-  if (res < 0) {
-    /* Failed to write -> child died */
-    goto fail_and_cleanup;
-  }
+    to_write -= res;
+    out += res;
+  } while (to_write > 0);
 
   if (l->tx_buf_read == l->tx_buf_write) {
     gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
@@ -596,7 +645,7 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
   GstPlugin *newplugin;
   GList *chunks = NULL;
 
-  GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
+  GST_DEBUG ("Plugin scanner loading file %s. tag %u", filename, tag);
 
 #if 0                           /* Test code - crash based on filename */
   if (strstr (filename, "coreelements") == NULL) {
@@ -605,7 +654,6 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
   }
 #endif
 
-
   newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
   if (newplugin) {
     guint hdr_pos;
@@ -628,9 +676,7 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
         GstRegistryChunk *cur = walk->data;
         put_chunk (l, cur, &offset);
 
-        if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
-          g_free (cur->data);
-        g_free (cur);
+        _priv_gst_registry_chunk_free (cur);
       }
 
       g_list_free (chunks);
@@ -661,9 +707,7 @@ fail:
     for (walk = chunks; walk; walk = g_list_next (walk)) {
       GstRegistryChunk *cur = walk->data;
 
-      if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
-        g_free (cur->data);
-      g_free (cur);
+      _priv_gst_registry_chunk_free (cur);
     }
 
     g_list_free (chunks);
@@ -708,11 +752,10 @@ handle_rx_packet (GstPluginLoader * l,
     case PACKET_EXIT:
       gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
       if (l->is_child) {
-        /* Respond, then we keep looping until the parent closes the fd */
+        /* Respond */
         put_packet (l, PACKET_EXIT, 0, NULL, 0);
-      } else {
-        l->rx_done = TRUE;      /* All done reading from child */
       }
+      l->rx_done = TRUE;
       return TRUE;
     case PACKET_LOAD_PLUGIN:{
       if (!l->is_child)
@@ -733,7 +776,8 @@ handle_rx_packet (GstPluginLoader * l,
           tag, payload_len);
 
       /* Assume that tagged details come back in the order
-       * we requested, and delete anything before this one */
+       * we requested, and delete anything before (but not
+       * including) this one */
       cur = l->pending_plugins;
       while (cur) {
         PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
@@ -741,24 +785,30 @@ handle_rx_packet (GstPluginLoader * l,
         if (e->tag > tag)
           break;
 
-        cur = g_list_delete_link (cur, cur);
-
         if (e->tag == tag) {
           entry = e;
           break;
         } else {
+          cur = g_list_delete_link (cur, cur);
           g_free (e->filename);
-          g_free (e);
+          g_slice_free (PendingPluginEntry, e);
         }
       }
+
       l->pending_plugins = cur;
       if (cur == NULL)
         l->pending_plugins_tail = NULL;
 
       if (payload_len > 0) {
-        GstPlugin *newplugin;
-        _priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
-            tmp + payload_len, &newplugin);
+        GstPlugin *newplugin = NULL;
+        if (!_priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
+                tmp + payload_len, &newplugin)) {
+          /* Got garbage from the child, so fail and trigger replay of plugins */
+          GST_ERROR_OBJECT (l->registry,
+              "Problems loading plugin details with tag %u from scanner", tag);
+          return FALSE;
+        }
+
         newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
         GST_LOG_OBJECT (l->registry,
             "marking plugin %p as registered as %s", newplugin,
@@ -775,9 +825,17 @@ handle_rx_packet (GstPluginLoader * l,
 
       if (entry != NULL) {
         g_free (entry->filename);
-        g_free (entry);
+        g_slice_free (PendingPluginEntry, entry);
       }
 
+      /* Remove the plugin entry we just loaded */
+      cur = l->pending_plugins;
+      if (cur != NULL)
+        cur = g_list_delete_link (cur, cur);
+      l->pending_plugins = cur;
+      if (cur == NULL)
+        l->pending_plugins_tail = NULL;
+
       break;
     }
     case PACKET_SYNC:
@@ -823,16 +881,15 @@ read_one (GstPluginLoader * l)
   in = l->rx_buf;
   do {
     res = read (l->fd_r.fd, in, to_read);
-    if (res > 0) {
-      to_read -= res;
-      in += res;
+    if (G_UNLIKELY (res < 0)) {
+      if (errno == EAGAIN || errno == EINTR)
+        continue;
+      GST_LOG ("Failed reading packet header");
+      return FALSE;
     }
-  } while (to_read > 0 && res < 0 && (errno == EAGAIN || errno == EINTR));
-
-  if (res < 0) {
-    GST_LOG ("Failed reading packet header");
-    return FALSE;
-  }
+    to_read -= res;
+    in += res;
+  } while (to_read > 0);
 
   magic = GST_READ_UINT32_BE (l->rx_buf + 8);
   if (magic != HEADER_MAGIC) {
@@ -847,29 +904,34 @@ read_one (GstPluginLoader * l)
         ("Received excessively large packet for plugin scanner subprocess");
     return FALSE;
   }
+  tag = GST_READ_UINT24_BE (l->rx_buf + 1);
 
-  if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
-    l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
-    l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
-  }
+  if (packet_len > 0) {
+    if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
+      GST_LOG ("Expanding rx buf from %d to %d",
+          l->rx_buf_size, packet_len + HEADER_SIZE + BUF_GROW_EXTRA);
+      l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
+      l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
+    }
 
-  in = l->rx_buf + HEADER_SIZE;
-  to_read = packet_len;
-  do {
-    res = read (l->fd_r.fd, in, to_read);
-    if (res > 0) {
+    in = l->rx_buf + HEADER_SIZE;
+    to_read = packet_len;
+    do {
+      res = read (l->fd_r.fd, in, to_read);
+      if (G_UNLIKELY (res < 0)) {
+        if (errno == EAGAIN || errno == EINTR)
+          continue;
+        GST_ERROR ("Packet payload read failed");
+        return FALSE;
+      }
       to_read -= res;
       in += res;
-    }
-  } while (to_read > 0 && res < 0 && (errno == EAGAIN || errno == EINTR));
-
-  if (res < 0) {
-    GST_ERROR ("Packet payload read failed");
-    return FALSE;
+    } while (to_read > 0);
+  } else {
+    GST_LOG ("No payload to read for 0 length packet type %d tag %u",
+        l->rx_buf[0], tag);
   }
 
-  tag = GST_READ_UINT24_BE (l->rx_buf + 1);
-
   return handle_rx_packet (l, l->rx_buf[0], tag,
       l->rx_buf + HEADER_SIZE, packet_len);
 }
@@ -895,13 +957,12 @@ exchange_packets (GstPluginLoader * l)
       if (gst_poll_fd_has_error (l->fdset, &l->fd_r) ||
           gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
         GST_LOG ("read fd %d closed/errored", l->fd_r.fd);
-        plugin_loader_cleanup_child (l);
-        return FALSE;
+        goto fail_and_cleanup;
       }
 
       if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
         if (!read_one (l))
-          return FALSE;
+          goto fail_and_cleanup;
       }
     }
 
@@ -909,15 +970,17 @@ exchange_packets (GstPluginLoader * l)
       if (gst_poll_fd_has_error (l->fdset, &l->fd_w) ||
           gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
         GST_ERROR ("write fd %d closed/errored", l->fd_w.fd);
-        plugin_loader_cleanup_child (l);
-        return FALSE;
+        goto fail_and_cleanup;
       }
       if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
         if (!write_one (l))
-          return FALSE;
+          goto fail_and_cleanup;
       }
     }
   } while (l->tx_buf_read < l->tx_buf_write);
 
   return TRUE;
+fail_and_cleanup:
+  plugin_loader_cleanup_child (l);
+  return FALSE;
 }