2 * Copyright (C) 2008 Jan Schmidt <jan.schmidt@sun.com>
4 * gstpluginloader.c: GstPluginLoader helper for loading plugin files
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <gst/gst_private.h>
30 #include <sys/types.h>
38 #include <gst/gstconfig.h>
40 #include <gst/gstpoll.h>
41 #include <gst/gstutils.h>
43 #include <gst/gstpluginloader.h>
44 #include <gst/gstregistrychunks.h>
45 #include <gst/gstregistrybinary.h>
47 /* IMPORTANT: Bump the version number if the plugin loader packet protocol
48 * changes. Changes in the binary registry format itself are handled by
49 * bumping the GST_MAGIC_BINARY_VERSION_STR
51 static const guint32 loader_protocol_version = 3;
53 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
55 static GstPluginLoader *plugin_loader_new (GstRegistry * registry);
56 static gboolean plugin_loader_free (GstPluginLoader * loader);
57 static gboolean plugin_loader_load (GstPluginLoader * loader,
58 const gchar * filename, off_t file_size, time_t file_mtime);
60 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
61 plugin_loader_new, plugin_loader_free, plugin_loader_load
64 typedef struct _PendingPluginEntry
72 struct _GstPluginLoader
74 GstRegistry *registry;
77 gboolean child_running;
83 gboolean got_plugin_details;
98 /* Head and tail of the pending plugins list. List of
99 PendingPluginEntry structs */
100 GList *pending_plugins;
101 GList *pending_plugins_tail;
104 #define PACKET_EXIT 1
105 #define PACKET_LOAD_PLUGIN 2
106 #define PACKET_SYNC 3
107 #define PACKET_PLUGIN_DETAILS 4
108 #define PACKET_VERSION 5
110 #define BUF_INIT_SIZE 512
111 #define BUF_GROW_EXTRA 512
112 #define BUF_MAX_SIZE (32 * 1024 * 1024)
114 #define HEADER_SIZE 12
115 /* 4 magic hex bytes to mark each packet */
116 #define HEADER_MAGIC 0xbefec0ae
117 #define ALIGNMENT (sizeof (void *))
119 static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader);
120 static void put_packet (GstPluginLoader * loader, guint type, guint32 tag,
121 const guint8 * payload, guint32 payload_len);
122 static gboolean exchange_packets (GstPluginLoader * l);
123 static gboolean plugin_loader_replay_pending (GstPluginLoader * l);
124 static gboolean plugin_loader_load_and_sync (GstPluginLoader * l,
125 PendingPluginEntry * entry);
126 static void plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
127 PendingPluginEntry * entry);
128 static void plugin_loader_cleanup_child (GstPluginLoader * loader);
129 static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
131 static GstPluginLoader *
132 plugin_loader_new (GstRegistry * registry)
134 GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
137 l->registry = gst_object_ref (registry);
138 l->fdset = gst_poll_new (FALSE);
139 gst_poll_fd_init (&l->fd_w);
140 gst_poll_fd_init (&l->fd_r);
142 l->tx_buf_size = BUF_INIT_SIZE;
143 l->tx_buf = g_malloc (BUF_INIT_SIZE);
147 l->rx_buf_size = BUF_INIT_SIZE;
148 l->rx_buf = g_malloc (BUF_INIT_SIZE);
154 plugin_loader_free (GstPluginLoader * loader)
157 gboolean got_plugin_details;
159 fsync (loader->fd_w.fd);
161 if (loader->child_running) {
162 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
164 /* Swap packets with the child until it exits cleanly */
165 while (!loader->rx_done) {
166 if (exchange_packets (loader) || loader->rx_done)
169 if (!plugin_loader_replay_pending (loader))
171 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
174 plugin_loader_cleanup_child (loader);
176 close (loader->fd_w.fd);
177 close (loader->fd_r.fd);
180 gst_poll_free (loader->fdset);
182 g_free (loader->rx_buf);
183 g_free (loader->tx_buf);
185 if (loader->registry)
186 gst_object_unref (loader->registry);
188 got_plugin_details = loader->got_plugin_details;
190 /* Free any pending plugin entries */
191 cur = loader->pending_plugins;
193 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
194 g_free (entry->filename);
197 cur = g_list_delete_link (cur, cur);
202 return got_plugin_details;
206 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
207 off_t file_size, time_t file_mtime)
210 PendingPluginEntry *entry;
212 if (!gst_plugin_loader_spawn (loader))
215 /* Send a packet to the child requesting that it load the given file */
216 GST_LOG_OBJECT (loader->registry,
217 "Sending file %s to child. tag %u", filename, loader->next_tag);
219 entry = g_new (PendingPluginEntry, 1);
220 entry->tag = loader->next_tag++;
221 entry->filename = g_strdup (filename);
222 entry->file_size = file_size;
223 entry->file_mtime = file_mtime;
224 loader->pending_plugins_tail =
225 g_list_append (loader->pending_plugins_tail, entry);
227 if (loader->pending_plugins == NULL)
228 loader->pending_plugins = loader->pending_plugins_tail;
230 loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
232 len = strlen (filename);
233 put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
234 (guint8 *) filename, len + 1);
236 if (!exchange_packets (loader)) {
237 if (!plugin_loader_replay_pending (loader))
245 plugin_loader_replay_pending (GstPluginLoader * l)
250 if (!gst_plugin_loader_spawn (l))
253 /* Load each plugin one by one synchronously until we find the
255 while ((cur = l->pending_plugins)) {
256 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
258 if (!plugin_loader_load_and_sync (l, entry)) {
259 /* Create dummy plugin entry to block re-scanning this file */
260 GST_ERROR ("Plugin file %s failed to load. Blacklisting",
262 plugin_loader_create_blacklist_plugin (l, entry);
263 l->got_plugin_details = TRUE;
264 /* Now remove this crashy plugin from the head of the list */
265 l->pending_plugins = g_list_delete_link (cur, cur);
266 if (l->pending_plugins == NULL)
267 l->pending_plugins_tail = NULL;
268 if (!gst_plugin_loader_spawn (l))
274 /* We exited after finding the crashing one. If there's any more pending,
275 * dispatch them post-haste, but don't wait */
276 for (cur = l->pending_plugins; cur != NULL; cur = next) {
277 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
279 next = g_list_next (cur);
281 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
282 (guint8 *) entry->filename, strlen (entry->filename) + 1);
284 /* This might invalidate cur, which is why we grabbed 'next' above */
285 if (!exchange_packets (l))
293 plugin_loader_sync_with_child (GstPluginLoader * l)
295 put_packet (l, PACKET_SYNC, 0, NULL, 0);
297 l->rx_got_sync = FALSE;
298 while (!l->rx_got_sync) {
299 if (!exchange_packets (l))
306 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
310 GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
313 len = strlen (entry->filename);
314 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
315 (guint8 *) entry->filename, len + 1);
317 return plugin_loader_sync_with_child (l);
321 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
322 PendingPluginEntry * entry)
324 GstPlugin *plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
326 plugin->filename = g_strdup (entry->filename);
327 plugin->file_mtime = entry->file_mtime;
328 plugin->file_size = entry->file_size;
329 plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
331 plugin->basename = g_path_get_basename (plugin->filename);
332 plugin->desc.name = g_intern_string (plugin->basename);
333 plugin->desc.description = g_strdup_printf ("Plugin for blacklisted file");
334 plugin->desc.version = g_intern_string ("0.0.0");
335 plugin->desc.license = g_intern_string ("BLACKLIST");
336 plugin->desc.source = plugin->desc.license;
337 plugin->desc.package = plugin->desc.license;
338 plugin->desc.origin = plugin->desc.license;
340 GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
341 gst_registry_add_plugin (l->registry, plugin);
345 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
347 char *argv[] = { location, "-l", NULL };
349 GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
350 if (!g_spawn_async_with_pipes (NULL, argv, NULL,
351 G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
352 NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
356 gst_poll_add_fd (loader->fdset, &loader->fd_w);
357 gst_poll_add_fd (loader->fdset, &loader->fd_r);
359 gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
361 loader->tx_buf_write = loader->tx_buf_read = 0;
363 put_packet (loader, PACKET_VERSION, 0, NULL, 0);
364 if (!plugin_loader_sync_with_child (loader))
367 loader->child_running = TRUE;
373 gst_plugin_loader_spawn (GstPluginLoader * loader)
378 if (loader->child_running)
381 /* Find the gst-plugin-scanner, first try installed then by env-var */
382 helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
383 res = gst_plugin_loader_try_helper (loader, helper_bin);
387 /* Try the GST_PLUGIN_SCANNER env var */
388 const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
390 GST_LOG ("Installed plugin scanner failed. "
391 "Trying GST_PLUGIN_SCANNER env var: %s", env);
392 helper_bin = g_strdup (env);
393 res = gst_plugin_loader_try_helper (loader, helper_bin);
396 GST_LOG ("Installed plugin scanner failed and GST_PLUGIN_SCANNER "
397 " env var not set. No gst-plugin-scanner available");
401 return loader->child_running;
405 plugin_loader_cleanup_child (GstPluginLoader * l)
407 if (!l->child_running || l->is_child)
410 gst_poll_remove_fd (l->fdset, &l->fd_w);
411 gst_poll_remove_fd (l->fdset, &l->fd_r);
417 GST_LOG ("waiting for child process to exit");
418 waitpid (l->child_pid, NULL, 0);
420 g_warning ("FIXME: Implement child process shutdown for Win32");
422 g_spawn_close_pid (l->child_pid);
424 l->child_running = FALSE;
428 _gst_plugin_loader_client_run ()
433 l = plugin_loader_new (NULL);
437 /* On entry, the inward pipe is STDIN, and outward is STDOUT.
438 * Dup those somewhere better so that plugins printing things
439 * won't interfere with anything */
441 dup_fd = dup (0); /* STDIN */
443 GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
449 dup_fd = dup (1); /* STDOUT */
451 GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
457 /* Dup stderr down to stdout so things that plugins print are visible,
458 * but don't care if it fails */
461 /* FIXME: Use DuplicateHandle and friends on win32 */
462 l->fd_w.fd = 1; /* STDOUT */
463 l->fd_r.fd = 0; /* STDIN */
466 gst_poll_add_fd (l->fdset, &l->fd_w);
467 gst_poll_add_fd (l->fdset, &l->fd_r);
468 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
472 GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
474 /* Loop, listening for incoming packets on the fd and writing responses */
475 while (!l->rx_done && exchange_packets (l));
477 plugin_loader_free (l);
483 put_packet (GstPluginLoader * l, guint type, guint32 tag,
484 const guint8 * payload, guint32 payload_len)
487 guint len = payload_len + HEADER_SIZE;
489 if (l->tx_buf_write + len >= l->tx_buf_size) {
490 GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
491 l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
492 l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
493 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
496 out = l->tx_buf + l->tx_buf_write;
498 /* one byte packet type */
500 /* 3 byte packet tag number */
501 GST_WRITE_UINT24_BE (out + 1, tag);
502 /* 4 bytes packet length */
503 GST_WRITE_UINT32_BE (out + 4, payload_len);
505 memcpy (out + HEADER_SIZE, payload, payload_len);
506 /* Write magic into the header */
507 GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
509 l->tx_buf_write += len;
510 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
514 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
520 /* Might need to align the chunk */
521 if (chunk->align && ((*pos) % ALIGNMENT) != 0)
522 padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
524 len = padsize + chunk->size;
526 if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
527 guint new_size = MAX (l->tx_buf_write + len,
528 l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
529 GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
530 l->tx_buf_size, new_size, chunk->size);
531 l->tx_buf_size = new_size;
532 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
535 out = l->tx_buf + l->tx_buf_write;
536 /* Clear the padding */
538 memset (out, 0, padsize);
539 memcpy (out + padsize, chunk->data, chunk->size);
541 l->tx_buf_write += len;
544 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
548 write_one (GstPluginLoader * l)
551 guint32 to_write, magic;
554 if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
557 out = l->tx_buf + l->tx_buf_read;
559 magic = GST_READ_UINT32_BE (out + 8);
560 if (magic != HEADER_MAGIC) {
561 GST_ERROR ("Packet magic number is missing. Memory corruption detected");
562 goto fail_and_cleanup;
565 to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
566 /* Check that the magic is intact, and the size is sensible */
567 if (to_write > l->tx_buf_size) {
568 GST_ERROR ("Indicated packet size is too large. Corruption detected");
569 goto fail_and_cleanup;
572 l->tx_buf_read += to_write;
574 GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
577 res = write (l->fd_w.fd, out, to_write);
578 if (G_UNLIKELY (res < 0)) {
579 if (errno == EAGAIN || errno == EINTR)
581 /* Failed to write -> child died */
582 goto fail_and_cleanup;
586 } while (to_write > 0);
588 if (l->tx_buf_read == l->tx_buf_write) {
589 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
590 l->tx_buf_read = l->tx_buf_write = 0;
596 plugin_loader_cleanup_child (l);
601 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
603 GstPlugin *newplugin;
604 GList *chunks = NULL;
606 GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
608 #if 0 /* Test code - crash based on filename */
609 if (strstr (filename, "coreelements") == NULL) {
610 g_printerr ("Crashing on file %s\n", filename);
611 g_printerr ("%d", *(gint *) (NULL));
615 newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
620 /* Now serialise the plugin details and send */
621 if (!_priv_gst_registry_chunks_save_plugin (&chunks,
622 gst_registry_get_default (), newplugin))
625 /* Store where the header is, write an empty one, then write
626 * all the payload chunks, then fix up the header size */
627 hdr_pos = l->tx_buf_write;
628 offset = HEADER_SIZE;
629 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
633 for (walk = chunks; walk; walk = g_list_next (walk)) {
634 GstRegistryChunk *cur = walk->data;
635 put_chunk (l, cur, &offset);
637 if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
642 g_list_free (chunks);
644 /* Store the size of the written payload */
645 GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
647 #if 0 /* Test code - corrupt the tx buffer based on filename */
648 if (strstr (filename, "sink") != NULL) {
650 g_printerr ("Corrupting tx buf on file %s\n", filename);
651 fd = open ("/dev/urandom", O_RDONLY);
652 res = read (fd, l->tx_buf, l->tx_buf_size);
657 gst_object_unref (newplugin);
659 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
664 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
667 for (walk = chunks; walk; walk = g_list_next (walk)) {
668 GstRegistryChunk *cur = walk->data;
670 if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
675 g_list_free (chunks);
682 check_protocol_version (GstPluginLoader * l, guint8 * payload,
686 guint8 *binary_reg_ver;
688 if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
691 got_version = GST_READ_UINT32_BE (payload);
692 GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
693 loader_protocol_version);
694 if (got_version != loader_protocol_version)
697 binary_reg_ver = payload + sizeof (guint32);
698 if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
699 GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
700 GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
708 handle_rx_packet (GstPluginLoader * l,
709 guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
715 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
717 /* Respond, then we keep looping until the parent closes the fd */
718 put_packet (l, PACKET_EXIT, 0, NULL, 0);
720 l->rx_done = TRUE; /* All done reading from child */
723 case PACKET_LOAD_PLUGIN:{
727 /* Payload is the filename to load */
728 res = do_plugin_load (l, (gchar *) payload, tag);
732 case PACKET_PLUGIN_DETAILS:{
733 gchar *tmp = (gchar *) payload;
734 PendingPluginEntry *entry = NULL;
737 GST_DEBUG_OBJECT (l->registry,
738 "Received plugin details from child w/ tag %u. %d bytes info",
741 /* Assume that tagged details come back in the order
742 * we requested, and delete anything before (but not
743 * including) this one */
744 cur = l->pending_plugins;
746 PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
755 cur = g_list_delete_link (cur, cur);
756 g_free (e->filename);
761 l->pending_plugins = cur;
763 l->pending_plugins_tail = NULL;
765 if (payload_len > 0) {
766 GstPlugin *newplugin = NULL;
767 if (!_priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
768 tmp + payload_len, &newplugin)) {
769 /* Got garbage from the child, so fail and trigger replay of plugins */
770 GST_ERROR_OBJECT (l->registry,
771 "Problems loading plugin details with tag %u from scanner", tag);
775 newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
776 GST_LOG_OBJECT (l->registry,
777 "marking plugin %p as registered as %s", newplugin,
778 newplugin->filename);
779 newplugin->registered = TRUE;
781 /* We got a set of plugin details - remember it for later */
782 l->got_plugin_details = TRUE;
783 } else if (entry != NULL) {
784 /* Create a blacklist entry for this file to prevent scanning every time */
785 plugin_loader_create_blacklist_plugin (l, entry);
786 l->got_plugin_details = TRUE;
790 g_free (entry->filename);
794 /* Remove the plugin entry we just loaded */
795 cur = l->pending_plugins;
797 cur = g_list_delete_link (cur, cur);
798 l->pending_plugins = cur;
800 l->pending_plugins_tail = NULL;
806 /* Respond with our reply - also a sync */
807 put_packet (l, PACKET_SYNC, tag, NULL, 0);
808 GST_LOG ("Got SYNC in child - replying");
810 l->rx_got_sync = TRUE;
814 /* Respond with our reply - a version packet, with the version */
815 const gint version_len =
816 sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN;
817 guint8 version_info[sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN];
818 memset (version_info, 0, version_len);
819 GST_WRITE_UINT32_BE (version_info, loader_protocol_version);
820 memcpy (version_info + sizeof (guint32), GST_MAGIC_BINARY_VERSION_STR,
821 strlen (GST_MAGIC_BINARY_VERSION_STR));
822 put_packet (l, PACKET_VERSION, tag, version_info, version_len);
823 GST_LOG ("Got VERSION in child - replying %u", loader_protocol_version);
825 res = check_protocol_version (l, payload, payload_len);
829 return FALSE; /* Invalid packet -> something is wrong */
836 read_one (GstPluginLoader * l)
839 guint32 to_read, packet_len, tag;
843 to_read = HEADER_SIZE;
846 res = read (l->fd_r.fd, in, to_read);
847 if (G_UNLIKELY (res < 0)) {
848 if (errno == EAGAIN || errno == EINTR)
850 GST_LOG ("Failed reading packet header");
855 } while (to_read > 0);
857 magic = GST_READ_UINT32_BE (l->rx_buf + 8);
858 if (magic != HEADER_MAGIC) {
860 ("Invalid packet (bad magic number) received from plugin scanner subprocess");
864 packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
865 if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
867 ("Received excessively large packet for plugin scanner subprocess");
870 tag = GST_READ_UINT24_BE (l->rx_buf + 1);
872 if (packet_len > 0) {
873 if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
874 GST_LOG ("Expanding rx buf from %d to %d",
875 l->rx_buf_size, packet_len + HEADER_SIZE + BUF_GROW_EXTRA);
876 l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
877 l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
880 in = l->rx_buf + HEADER_SIZE;
881 to_read = packet_len;
883 res = read (l->fd_r.fd, in, to_read);
884 if (G_UNLIKELY (res < 0)) {
885 if (errno == EAGAIN || errno == EINTR)
887 GST_ERROR ("Packet payload read failed");
892 } while (to_read > 0);
894 GST_LOG ("No payload to read for 0 length packet type %d tag %u",
898 return handle_rx_packet (l, l->rx_buf[0], tag,
899 l->rx_buf + HEADER_SIZE, packet_len);
903 exchange_packets (GstPluginLoader * l)
907 /* Wait for activity on our FDs */
910 res = gst_poll_wait (l->fdset, GST_SECOND);
911 } while (res == -1 && (errno == EINTR || errno == EAGAIN));
916 GST_LOG ("Poll res = %d. %d bytes pending for write", res,
917 l->tx_buf_write - l->tx_buf_read);
920 if (gst_poll_fd_has_error (l->fdset, &l->fd_r) ||
921 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
922 GST_LOG ("read fd %d closed/errored", l->fd_r.fd);
923 goto fail_and_cleanup;
926 if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
928 goto fail_and_cleanup;
932 if (l->tx_buf_read < l->tx_buf_write) {
933 if (gst_poll_fd_has_error (l->fdset, &l->fd_w) ||
934 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
935 GST_ERROR ("write fd %d closed/errored", l->fd_w.fd);
936 goto fail_and_cleanup;
938 if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
940 goto fail_and_cleanup;
943 } while (l->tx_buf_read < l->tx_buf_write);
947 plugin_loader_cleanup_child (l);