X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gst%2Fgstpluginloader.c;h=a933a4bc4efca1e47e1fd000ebdfa30b4072be22;hb=ce43de86902c4e9c8ed4e9682602664cb9bce2ee;hp=18547f884b19e4829f47d016ca418dfdd6c89a15;hpb=97d6c854ae606e64f25741cc8471a098831c2ab5;p=platform%2Fupstream%2Fgstreamer.git diff --git a/gst/gstpluginloader.c b/gst/gstpluginloader.c index 18547f8..a933a4b 100644 --- a/gst/gstpluginloader.c +++ b/gst/gstpluginloader.c @@ -16,22 +16,38 @@ * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#include + #ifndef G_OS_WIN32 #include #include +#include +#include #include +#else +#define WIN32_LEAN_AND_MEAN + +#define fsync(fd) _commit(fd) +#include + +#include +extern HMODULE _priv_gst_dll_handle; +#endif + +#ifdef HAVE_SYS_UTSNAME_H +#include #endif + #include -#include #include #include @@ -42,9 +58,10 @@ #include /* IMPORTANT: Bump the version number if the plugin loader packet protocol - * changes. Changes in the binary registry format are handled by bumps - * in the GST_MAGIC_BINARY_VERSION_STR as before */ -static const guint32 loader_protocol_version = 2; + * changes. Changes in the binary registry format itself are handled by + * bumping the GST_MAGIC_BINARY_VERSION_STR + */ +static const guint32 loader_protocol_version = 3; #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING @@ -53,12 +70,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; @@ -84,6 +103,7 @@ struct _GstPluginLoader guint tx_buf_write; guint tx_buf_read; + /* next sequence number (for PendingPluginEntry) */ guint32 next_tag; guint8 *rx_buf; @@ -105,7 +125,11 @@ struct _GstPluginLoader #define BUF_INIT_SIZE 512 #define BUF_GROW_EXTRA 512 -#define HEADER_SIZE 8 +#define BUF_MAX_SIZE (32 * 1024 * 1024) + +#define HEADER_SIZE 12 +/* 4 magic hex bytes to mark each packet */ +#define HEADER_MAGIC 0xbefec0ae #define ALIGNMENT (sizeof (void *)) static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader); @@ -123,7 +147,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); @@ -184,12 +208,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; } @@ -208,7 +232,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; @@ -248,12 +272,15 @@ restart: PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data); if (!plugin_loader_load_and_sync (l, entry)) { - GST_INFO ("AHA! %s crashes on loading", entry->filename); /* Create dummy plugin entry to block re-scanning this file */ + GST_ERROR ("Plugin file %s failed to load. Blacklisting", + entry->filename); plugin_loader_create_blacklist_plugin (l, entry); 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)) @@ -317,13 +344,13 @@ plugin_loader_create_blacklist_plugin (GstPluginLoader * l, plugin->filename = g_strdup (entry->filename); plugin->file_mtime = entry->file_mtime; plugin->file_size = entry->file_size; - plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED; + GST_OBJECT_FLAG_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED); 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; @@ -332,12 +359,76 @@ plugin_loader_create_blacklist_plugin (GstPluginLoader * l, gst_registry_add_plugin (l->registry, plugin); } +#ifdef __APPLE__ +#if defined(__x86_64__) +#define USR_BIN_ARCH_SWITCH "-x86_64" +#elif defined(__i386__) +#define USR_BIN_ARCH_SWITCH "-i386" +#elif defined(__ppc__) +#define USR_BIN_ARCH_SWITCH "-ppc" +#elif defined(__ppc64__) +#define USR_BIN_ARCH_SWITCH "-ppc64" +#endif +#endif + +#define YES_MULTIARCH 1 +#define NO_MULTIARCH 2 + +#if defined (__APPLE__) && defined (USR_BIN_ARCH_SWITCH) +static gboolean +gst_plugin_loader_use_usr_bin_arch (void) +{ + static volatile gsize multiarch = 0; + + if (g_once_init_enter (&multiarch)) { + gsize res = NO_MULTIARCH; + +#ifdef HAVE_SYS_UTSNAME_H + { + struct utsname uname_data; + + if (uname (&uname_data) == 0) { + /* Check for OS X >= 10.5 (darwin kernel 9.0) */ + GST_LOG ("%s %s", uname_data.sysname, uname_data.release); + if (g_ascii_strcasecmp (uname_data.sysname, "Darwin") == 0 && + g_strtod (uname_data.release, NULL) >= 9.0) { + res = YES_MULTIARCH; + } + } + } +#endif + + GST_INFO ("multiarch: %s", (res == YES_MULTIARCH) ? "yes" : "no"); + g_once_init_leave (&multiarch, res); + } + return (multiarch == YES_MULTIARCH); +} +#endif /* __APPLE__ && USR_BIN_ARCH_SWITCH */ + static gboolean gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location) { - char *argv[] = { location, "-l", NULL }; + char *argv[6] = { NULL, }; + int c = 0; + +#if defined (__APPLE__) && defined (USR_BIN_ARCH_SWITCH) + if (gst_plugin_loader_use_usr_bin_arch ()) { + argv[c++] = (char *) "/usr/bin/arch"; + argv[c++] = (char *) USR_BIN_ARCH_SWITCH; + } +#endif + argv[c++] = location; + argv[c++] = (char *) "-l"; + argv[c++] = _gst_executable_path; + argv[c++] = NULL; + + if (c > 4) { + 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); + } - 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, @@ -363,29 +454,49 @@ 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_1_0"); + if (env == NULL) + 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"); + +#ifdef G_OS_WIN32 + { + gchar *basedir; + + basedir = + g_win32_get_package_installation_directory_of_module + (_priv_gst_dll_handle); + helper_bin = + g_build_filename (basedir, GST_PLUGIN_SCANNER_SUBDIR, + "gstreamer-" GST_API_VERSION, "gst-plugin-scanner.exe", NULL); + g_free (basedir); + } +#else + helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED); +#endif + 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"); } } @@ -416,18 +527,51 @@ plugin_loader_cleanup_child (GstPluginLoader * l) } gboolean -_gst_plugin_loader_client_run () +_gst_plugin_loader_client_run (void) { + gboolean res = TRUE; GstPluginLoader *l; l = plugin_loader_new (NULL); if (l == NULL) return FALSE; - l->fd_w.fd = 1; /* STDOUT */ - gst_poll_add_fd (l->fdset, &l->fd_w); + /* On entry, the inward pipe is STDIN, and outward is STDOUT. + * Dup those somewhere better so that plugins printing things + * won't interfere with anything */ +#ifndef G_OS_WIN32 + { + int dup_fd; + + dup_fd = dup (0); /* STDIN */ + if (dup_fd == -1) { + GST_ERROR ("Failed to start. Could not dup STDIN, errno %d", errno); + res = FALSE; + goto beach; + } + l->fd_r.fd = dup_fd; + close (0); + + dup_fd = dup (1); /* STDOUT */ + if (dup_fd == -1) { + GST_ERROR ("Failed to start. Could not dup STDOUT, errno %d", errno); + res = FALSE; + goto beach; + } + 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); + } +#else + /* FIXME: Use DuplicateHandle and friends on win32 */ + l->fd_w.fd = 1; /* STDOUT */ l->fd_r.fd = 0; /* STDIN */ +#endif + + gst_poll_add_fd (l->fdset, &l->fd_w); gst_poll_add_fd (l->fdset, &l->fd_r); gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE); @@ -438,9 +582,13 @@ _gst_plugin_loader_client_run () /* Loop, listening for incoming packets on the fd and writing responses */ while (!l->rx_done && exchange_packets (l)); +#ifndef G_OS_WIN32 +beach: +#endif + plugin_loader_free (l); - return TRUE; + return res; } static void @@ -451,16 +599,25 @@ 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); } out = l->tx_buf + l->tx_buf_write; + /* one byte packet type */ out[0] = type; + /* 3 byte packet tag number */ GST_WRITE_UINT24_BE (out + 1, tag); + /* 4 bytes packet length */ GST_WRITE_UINT32_BE (out + 4, payload_len); - memcpy (out + HEADER_SIZE, payload, payload_len); + /* payload */ + if (payload && payload_len) + memcpy (out + HEADER_SIZE, payload, payload_len); + /* Write magic into the header */ + GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC); l->tx_buf_write += len; gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE); @@ -479,12 +636,19 @@ 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); } out = l->tx_buf + l->tx_buf_write; + /* Clear the padding */ + if (padsize) + memset (out, 0, padsize); memcpy (out + padsize, chunk->data, chunk->size); l->tx_buf_write += len; @@ -497,36 +661,53 @@ static gboolean write_one (GstPluginLoader * l) { guint8 *out; - guint32 to_write; + guint32 to_write, magic; int res; if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write) return FALSE; out = l->tx_buf + l->tx_buf_read; + + magic = GST_READ_UINT32_BE (out + 8); + if (magic != HEADER_MAGIC) { + GST_ERROR ("Packet magic number is missing. Memory corruption detected"); + goto fail_and_cleanup; + } + to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE; + /* Check that the magic is intact, and the size is sensible */ + if (to_write > l->tx_buf_size) { + GST_ERROR ("Indicated packet size is too large. Corruption detected"); + goto fail_and_cleanup; + } + l->tx_buf_read += to_write; GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd); 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 */ - plugin_loader_cleanup_child (l); - return FALSE; - } + 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); l->tx_buf_read = l->tx_buf_write = 0; } + return TRUE; + +fail_and_cleanup: + plugin_loader_cleanup_child (l); + return FALSE; } static gboolean @@ -535,16 +716,15 @@ 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 an env var */ +#if 0 /* Test code - crash based on filename */ if (strstr (filename, "coreelements") == NULL) { g_printerr ("Crashing on file %s\n", filename); g_printerr ("%d", *(gint *) (NULL)); } #endif - newplugin = gst_plugin_load_file ((gchar *) filename, NULL); if (newplugin) { guint hdr_pos; @@ -552,7 +732,7 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag) /* Now serialise the plugin details and send */ if (!_priv_gst_registry_chunks_save_plugin (&chunks, - gst_registry_get_default (), newplugin)) + gst_registry_get (), newplugin)) goto fail; /* Store where the header is, write an empty one, then write @@ -567,9 +747,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); @@ -577,6 +755,15 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag) /* Store the size of the written payload */ GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE); } +#if 0 /* Test code - corrupt the tx buffer based on filename */ + if (strstr (filename, "sink") != NULL) { + int fd, res; + g_printerr ("Corrupting tx buf on file %s\n", filename); + fd = open ("/dev/urandom", O_RDONLY); + res = read (fd, l->tx_buf, l->tx_buf_size); + close (fd); + } +#endif gst_object_unref (newplugin); } else { @@ -591,9 +778,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); @@ -638,11 +823,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) @@ -663,7 +847,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); @@ -671,25 +856,31 @@ 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); - newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED; + 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; + } + + GST_OBJECT_FLAG_UNSET (newplugin, GST_PLUGIN_FLAG_CACHED); GST_LOG_OBJECT (l->registry, "marking plugin %p as registered as %s", newplugin, newplugin->filename); @@ -705,9 +896,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: @@ -744,6 +943,7 @@ handle_rx_packet (GstPluginLoader * l, static gboolean read_one (GstPluginLoader * l) { + guint64 magic; guint32 to_read, packet_len, tag; guint8 *in; gint res; @@ -752,41 +952,57 @@ 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"); + to_read -= res; + in += res; + } while (to_read > 0); + + magic = GST_READ_UINT32_BE (l->rx_buf + 8); + if (magic != HEADER_MAGIC) { + GST_WARNING + ("Invalid packet (bad magic number) received from plugin scanner subprocess"); return FALSE; } packet_len = GST_READ_UINT32_BE (l->rx_buf + 4); - - 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 + HEADER_SIZE > BUF_MAX_SIZE) { + GST_WARNING + ("Received excessively large packet for plugin scanner subprocess"); + return FALSE; } + tag = GST_READ_UINT24_BE (l->rx_buf + 1); - in = l->rx_buf + HEADER_SIZE; - to_read = packet_len; - do { - res = read (l->fd_r.fd, in, to_read); - if (res > 0) { - to_read -= res; - in += res; + 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); } - } while (to_read > 0 && res < 0 && (errno == EAGAIN || errno == EINTR)); - if (res < 0) { - GST_ERROR ("Packet payload read failed"); - return FALSE; + 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); + } 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); } @@ -799,7 +1015,7 @@ exchange_packets (GstPluginLoader * l) /* Wait for activity on our FDs */ do { do { - res = gst_poll_wait (l->fdset, GST_CLOCK_TIME_NONE); + res = gst_poll_wait (l->fdset, GST_SECOND); } while (res == -1 && (errno == EINTR || errno == EAGAIN)); if (res < 0) @@ -809,32 +1025,37 @@ exchange_packets (GstPluginLoader * l) l->tx_buf_write - l->tx_buf_read); if (!l->rx_done) { - 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; + if (gst_poll_fd_has_error (l->fdset, &l->fd_r)) { + GST_LOG ("read fd %d errored", l->fd_r.fd); + 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; + } else if (gst_poll_fd_has_closed (l->fdset, &l->fd_r)) { + GST_LOG ("read fd %d closed", l->fd_r.fd); + goto fail_and_cleanup; } } if (l->tx_buf_read < l->tx_buf_write) { - 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; + if (gst_poll_fd_has_error (l->fdset, &l->fd_w)) { + GST_ERROR ("write fd %d errored", l->fd_w.fd); + 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; + } else if (gst_poll_fd_has_closed (l->fdset, &l->fd_w)) { + GST_LOG ("write fd %d closed", l->fd_w.fd); + 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; }