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>
36 #define fsync(fd) _commit(fd)
42 #include <gst/gstconfig.h>
44 #include <gst/gstpoll.h>
45 #include <gst/gstutils.h>
47 #include <gst/gstpluginloader.h>
48 #include <gst/gstregistrychunks.h>
49 #include <gst/gstregistrybinary.h>
51 /* IMPORTANT: Bump the version number if the plugin loader packet protocol
52 * changes. Changes in the binary registry format itself are handled by
53 * bumping the GST_MAGIC_BINARY_VERSION_STR
55 static const guint32 loader_protocol_version = 3;
57 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
59 static GstPluginLoader *plugin_loader_new (GstRegistry * registry);
60 static gboolean plugin_loader_free (GstPluginLoader * loader);
61 static gboolean plugin_loader_load (GstPluginLoader * loader,
62 const gchar * filename, off_t file_size, time_t file_mtime);
64 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
65 plugin_loader_new, plugin_loader_free, plugin_loader_load
68 typedef struct _PendingPluginEntry
76 struct _GstPluginLoader
78 GstRegistry *registry;
81 gboolean child_running;
87 gboolean got_plugin_details;
100 gboolean rx_got_sync;
102 /* Head and tail of the pending plugins list. List of
103 PendingPluginEntry structs */
104 GList *pending_plugins;
105 GList *pending_plugins_tail;
108 #define PACKET_EXIT 1
109 #define PACKET_LOAD_PLUGIN 2
110 #define PACKET_SYNC 3
111 #define PACKET_PLUGIN_DETAILS 4
112 #define PACKET_VERSION 5
114 #define BUF_INIT_SIZE 512
115 #define BUF_GROW_EXTRA 512
116 #define BUF_MAX_SIZE (32 * 1024 * 1024)
118 #define HEADER_SIZE 12
119 /* 4 magic hex bytes to mark each packet */
120 #define HEADER_MAGIC 0xbefec0ae
121 #define ALIGNMENT (sizeof (void *))
123 static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader);
124 static void put_packet (GstPluginLoader * loader, guint type, guint32 tag,
125 const guint8 * payload, guint32 payload_len);
126 static gboolean exchange_packets (GstPluginLoader * l);
127 static gboolean plugin_loader_replay_pending (GstPluginLoader * l);
128 static gboolean plugin_loader_load_and_sync (GstPluginLoader * l,
129 PendingPluginEntry * entry);
130 static void plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
131 PendingPluginEntry * entry);
132 static void plugin_loader_cleanup_child (GstPluginLoader * loader);
133 static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
135 static GstPluginLoader *
136 plugin_loader_new (GstRegistry * registry)
138 GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
141 l->registry = gst_object_ref (registry);
142 l->fdset = gst_poll_new (FALSE);
143 gst_poll_fd_init (&l->fd_w);
144 gst_poll_fd_init (&l->fd_r);
146 l->tx_buf_size = BUF_INIT_SIZE;
147 l->tx_buf = g_malloc (BUF_INIT_SIZE);
151 l->rx_buf_size = BUF_INIT_SIZE;
152 l->rx_buf = g_malloc (BUF_INIT_SIZE);
158 plugin_loader_free (GstPluginLoader * loader)
161 gboolean got_plugin_details;
163 fsync (loader->fd_w.fd);
165 if (loader->child_running) {
166 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
168 /* Swap packets with the child until it exits cleanly */
169 while (!loader->rx_done) {
170 if (exchange_packets (loader) || loader->rx_done)
173 if (!plugin_loader_replay_pending (loader))
175 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
178 plugin_loader_cleanup_child (loader);
180 close (loader->fd_w.fd);
181 close (loader->fd_r.fd);
184 gst_poll_free (loader->fdset);
186 g_free (loader->rx_buf);
187 g_free (loader->tx_buf);
189 if (loader->registry)
190 gst_object_unref (loader->registry);
192 got_plugin_details = loader->got_plugin_details;
194 /* Free any pending plugin entries */
195 cur = loader->pending_plugins;
197 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
198 g_free (entry->filename);
201 cur = g_list_delete_link (cur, cur);
206 return got_plugin_details;
210 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
211 off_t file_size, time_t file_mtime)
214 PendingPluginEntry *entry;
216 if (!gst_plugin_loader_spawn (loader))
219 /* Send a packet to the child requesting that it load the given file */
220 GST_LOG_OBJECT (loader->registry,
221 "Sending file %s to child. tag %u", filename, loader->next_tag);
223 entry = g_new (PendingPluginEntry, 1);
224 entry->tag = loader->next_tag++;
225 entry->filename = g_strdup (filename);
226 entry->file_size = file_size;
227 entry->file_mtime = file_mtime;
228 loader->pending_plugins_tail =
229 g_list_append (loader->pending_plugins_tail, entry);
231 if (loader->pending_plugins == NULL)
232 loader->pending_plugins = loader->pending_plugins_tail;
234 loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
236 len = strlen (filename);
237 put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
238 (guint8 *) filename, len + 1);
240 if (!exchange_packets (loader)) {
241 if (!plugin_loader_replay_pending (loader))
249 plugin_loader_replay_pending (GstPluginLoader * l)
254 if (!gst_plugin_loader_spawn (l))
257 /* Load each plugin one by one synchronously until we find the
259 while ((cur = l->pending_plugins)) {
260 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
262 if (!plugin_loader_load_and_sync (l, entry)) {
263 /* Create dummy plugin entry to block re-scanning this file */
264 GST_ERROR ("Plugin file %s failed to load. Blacklisting",
266 plugin_loader_create_blacklist_plugin (l, entry);
267 l->got_plugin_details = TRUE;
268 /* Now remove this crashy plugin from the head of the list */
269 l->pending_plugins = g_list_delete_link (cur, cur);
270 if (l->pending_plugins == NULL)
271 l->pending_plugins_tail = NULL;
272 if (!gst_plugin_loader_spawn (l))
278 /* We exited after finding the crashing one. If there's any more pending,
279 * dispatch them post-haste, but don't wait */
280 for (cur = l->pending_plugins; cur != NULL; cur = next) {
281 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
283 next = g_list_next (cur);
285 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
286 (guint8 *) entry->filename, strlen (entry->filename) + 1);
288 /* This might invalidate cur, which is why we grabbed 'next' above */
289 if (!exchange_packets (l))
297 plugin_loader_sync_with_child (GstPluginLoader * l)
299 put_packet (l, PACKET_SYNC, 0, NULL, 0);
301 l->rx_got_sync = FALSE;
302 while (!l->rx_got_sync) {
303 if (!exchange_packets (l))
310 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
314 GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
317 len = strlen (entry->filename);
318 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
319 (guint8 *) entry->filename, len + 1);
321 return plugin_loader_sync_with_child (l);
325 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
326 PendingPluginEntry * entry)
328 GstPlugin *plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
330 plugin->filename = g_strdup (entry->filename);
331 plugin->file_mtime = entry->file_mtime;
332 plugin->file_size = entry->file_size;
333 plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
335 plugin->basename = g_path_get_basename (plugin->filename);
336 plugin->desc.name = g_intern_string (plugin->basename);
337 plugin->desc.description = g_strdup_printf ("Plugin for blacklisted file");
338 plugin->desc.version = g_intern_string ("0.0.0");
339 plugin->desc.license = g_intern_string ("BLACKLIST");
340 plugin->desc.source = plugin->desc.license;
341 plugin->desc.package = plugin->desc.license;
342 plugin->desc.origin = plugin->desc.license;
344 GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
345 gst_registry_add_plugin (l->registry, plugin);
349 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
351 char *argv[] = { location, "-l", NULL };
353 GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
354 if (!g_spawn_async_with_pipes (NULL, argv, NULL,
355 G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
356 NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
360 gst_poll_add_fd (loader->fdset, &loader->fd_w);
361 gst_poll_add_fd (loader->fdset, &loader->fd_r);
363 gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
365 loader->tx_buf_write = loader->tx_buf_read = 0;
367 put_packet (loader, PACKET_VERSION, 0, NULL, 0);
368 if (!plugin_loader_sync_with_child (loader))
371 loader->child_running = TRUE;
377 gst_plugin_loader_spawn (GstPluginLoader * loader)
382 if (loader->child_running)
385 /* Find the gst-plugin-scanner, first try installed then by env-var */
386 helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
387 res = gst_plugin_loader_try_helper (loader, helper_bin);
391 /* Try the GST_PLUGIN_SCANNER env var */
392 const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
394 GST_LOG ("Installed plugin scanner failed. "
395 "Trying GST_PLUGIN_SCANNER env var: %s", env);
396 helper_bin = g_strdup (env);
397 res = gst_plugin_loader_try_helper (loader, helper_bin);
400 GST_LOG ("Installed plugin scanner failed and GST_PLUGIN_SCANNER "
401 " env var not set. No gst-plugin-scanner available");
405 return loader->child_running;
409 plugin_loader_cleanup_child (GstPluginLoader * l)
411 if (!l->child_running || l->is_child)
414 gst_poll_remove_fd (l->fdset, &l->fd_w);
415 gst_poll_remove_fd (l->fdset, &l->fd_r);
421 GST_LOG ("waiting for child process to exit");
422 waitpid (l->child_pid, NULL, 0);
424 g_warning ("FIXME: Implement child process shutdown for Win32");
426 g_spawn_close_pid (l->child_pid);
428 l->child_running = FALSE;
432 _gst_plugin_loader_client_run ()
437 l = plugin_loader_new (NULL);
441 /* On entry, the inward pipe is STDIN, and outward is STDOUT.
442 * Dup those somewhere better so that plugins printing things
443 * won't interfere with anything */
445 dup_fd = dup (0); /* STDIN */
447 GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
453 dup_fd = dup (1); /* STDOUT */
455 GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
461 /* Dup stderr down to stdout so things that plugins print are visible,
462 * but don't care if it fails */
465 /* FIXME: Use DuplicateHandle and friends on win32 */
466 l->fd_w.fd = 1; /* STDOUT */
467 l->fd_r.fd = 0; /* STDIN */
470 gst_poll_add_fd (l->fdset, &l->fd_w);
471 gst_poll_add_fd (l->fdset, &l->fd_r);
472 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
476 GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
478 /* Loop, listening for incoming packets on the fd and writing responses */
479 while (!l->rx_done && exchange_packets (l));
481 plugin_loader_free (l);
487 put_packet (GstPluginLoader * l, guint type, guint32 tag,
488 const guint8 * payload, guint32 payload_len)
491 guint len = payload_len + HEADER_SIZE;
493 if (l->tx_buf_write + len >= l->tx_buf_size) {
494 GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
495 l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
496 l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
497 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
500 out = l->tx_buf + l->tx_buf_write;
502 /* one byte packet type */
504 /* 3 byte packet tag number */
505 GST_WRITE_UINT24_BE (out + 1, tag);
506 /* 4 bytes packet length */
507 GST_WRITE_UINT32_BE (out + 4, payload_len);
509 memcpy (out + HEADER_SIZE, payload, payload_len);
510 /* Write magic into the header */
511 GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
513 l->tx_buf_write += len;
514 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
518 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
524 /* Might need to align the chunk */
525 if (chunk->align && ((*pos) % ALIGNMENT) != 0)
526 padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
528 len = padsize + chunk->size;
530 if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
531 guint new_size = MAX (l->tx_buf_write + len,
532 l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
533 GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
534 l->tx_buf_size, new_size, chunk->size);
535 l->tx_buf_size = new_size;
536 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
539 out = l->tx_buf + l->tx_buf_write;
540 /* Clear the padding */
542 memset (out, 0, padsize);
543 memcpy (out + padsize, chunk->data, chunk->size);
545 l->tx_buf_write += len;
548 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
552 write_one (GstPluginLoader * l)
555 guint32 to_write, magic;
558 if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
561 out = l->tx_buf + l->tx_buf_read;
563 magic = GST_READ_UINT32_BE (out + 8);
564 if (magic != HEADER_MAGIC) {
565 GST_ERROR ("Packet magic number is missing. Memory corruption detected");
566 goto fail_and_cleanup;
569 to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
570 /* Check that the magic is intact, and the size is sensible */
571 if (to_write > l->tx_buf_size) {
572 GST_ERROR ("Indicated packet size is too large. Corruption detected");
573 goto fail_and_cleanup;
576 l->tx_buf_read += to_write;
578 GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
581 res = write (l->fd_w.fd, out, to_write);
582 if (G_UNLIKELY (res < 0)) {
583 if (errno == EAGAIN || errno == EINTR)
585 /* Failed to write -> child died */
586 goto fail_and_cleanup;
590 } while (to_write > 0);
592 if (l->tx_buf_read == l->tx_buf_write) {
593 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
594 l->tx_buf_read = l->tx_buf_write = 0;
600 plugin_loader_cleanup_child (l);
605 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
607 GstPlugin *newplugin;
608 GList *chunks = NULL;
610 GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
612 #if 0 /* Test code - crash based on filename */
613 if (strstr (filename, "coreelements") == NULL) {
614 g_printerr ("Crashing on file %s\n", filename);
615 g_printerr ("%d", *(gint *) (NULL));
619 newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
624 /* Now serialise the plugin details and send */
625 if (!_priv_gst_registry_chunks_save_plugin (&chunks,
626 gst_registry_get_default (), newplugin))
629 /* Store where the header is, write an empty one, then write
630 * all the payload chunks, then fix up the header size */
631 hdr_pos = l->tx_buf_write;
632 offset = HEADER_SIZE;
633 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
637 for (walk = chunks; walk; walk = g_list_next (walk)) {
638 GstRegistryChunk *cur = walk->data;
639 put_chunk (l, cur, &offset);
641 if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
646 g_list_free (chunks);
648 /* Store the size of the written payload */
649 GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
651 #if 0 /* Test code - corrupt the tx buffer based on filename */
652 if (strstr (filename, "sink") != NULL) {
654 g_printerr ("Corrupting tx buf on file %s\n", filename);
655 fd = open ("/dev/urandom", O_RDONLY);
656 res = read (fd, l->tx_buf, l->tx_buf_size);
661 gst_object_unref (newplugin);
663 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
668 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
671 for (walk = chunks; walk; walk = g_list_next (walk)) {
672 GstRegistryChunk *cur = walk->data;
674 if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
679 g_list_free (chunks);
686 check_protocol_version (GstPluginLoader * l, guint8 * payload,
690 guint8 *binary_reg_ver;
692 if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
695 got_version = GST_READ_UINT32_BE (payload);
696 GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
697 loader_protocol_version);
698 if (got_version != loader_protocol_version)
701 binary_reg_ver = payload + sizeof (guint32);
702 if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
703 GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
704 GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
712 handle_rx_packet (GstPluginLoader * l,
713 guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
719 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
721 /* Respond, then we keep looping until the parent closes the fd */
722 put_packet (l, PACKET_EXIT, 0, NULL, 0);
724 l->rx_done = TRUE; /* All done reading from child */
727 case PACKET_LOAD_PLUGIN:{
731 /* Payload is the filename to load */
732 res = do_plugin_load (l, (gchar *) payload, tag);
736 case PACKET_PLUGIN_DETAILS:{
737 gchar *tmp = (gchar *) payload;
738 PendingPluginEntry *entry = NULL;
741 GST_DEBUG_OBJECT (l->registry,
742 "Received plugin details from child w/ tag %u. %d bytes info",
745 /* Assume that tagged details come back in the order
746 * we requested, and delete anything before (but not
747 * including) this one */
748 cur = l->pending_plugins;
750 PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
759 cur = g_list_delete_link (cur, cur);
760 g_free (e->filename);
765 l->pending_plugins = cur;
767 l->pending_plugins_tail = NULL;
769 if (payload_len > 0) {
770 GstPlugin *newplugin = NULL;
771 if (!_priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
772 tmp + payload_len, &newplugin)) {
773 /* Got garbage from the child, so fail and trigger replay of plugins */
774 GST_ERROR_OBJECT (l->registry,
775 "Problems loading plugin details with tag %u from scanner", tag);
779 newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
780 GST_LOG_OBJECT (l->registry,
781 "marking plugin %p as registered as %s", newplugin,
782 newplugin->filename);
783 newplugin->registered = TRUE;
785 /* We got a set of plugin details - remember it for later */
786 l->got_plugin_details = TRUE;
787 } else if (entry != NULL) {
788 /* Create a blacklist entry for this file to prevent scanning every time */
789 plugin_loader_create_blacklist_plugin (l, entry);
790 l->got_plugin_details = TRUE;
794 g_free (entry->filename);
798 /* Remove the plugin entry we just loaded */
799 cur = l->pending_plugins;
801 cur = g_list_delete_link (cur, cur);
802 l->pending_plugins = cur;
804 l->pending_plugins_tail = NULL;
810 /* Respond with our reply - also a sync */
811 put_packet (l, PACKET_SYNC, tag, NULL, 0);
812 GST_LOG ("Got SYNC in child - replying");
814 l->rx_got_sync = TRUE;
818 /* Respond with our reply - a version packet, with the version */
819 const gint version_len =
820 sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN;
821 guint8 version_info[sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN];
822 memset (version_info, 0, version_len);
823 GST_WRITE_UINT32_BE (version_info, loader_protocol_version);
824 memcpy (version_info + sizeof (guint32), GST_MAGIC_BINARY_VERSION_STR,
825 strlen (GST_MAGIC_BINARY_VERSION_STR));
826 put_packet (l, PACKET_VERSION, tag, version_info, version_len);
827 GST_LOG ("Got VERSION in child - replying %u", loader_protocol_version);
829 res = check_protocol_version (l, payload, payload_len);
833 return FALSE; /* Invalid packet -> something is wrong */
840 read_one (GstPluginLoader * l)
843 guint32 to_read, packet_len, tag;
847 to_read = HEADER_SIZE;
850 res = read (l->fd_r.fd, in, to_read);
851 if (G_UNLIKELY (res < 0)) {
852 if (errno == EAGAIN || errno == EINTR)
854 GST_LOG ("Failed reading packet header");
859 } while (to_read > 0);
861 magic = GST_READ_UINT32_BE (l->rx_buf + 8);
862 if (magic != HEADER_MAGIC) {
864 ("Invalid packet (bad magic number) received from plugin scanner subprocess");
868 packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
869 if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
871 ("Received excessively large packet for plugin scanner subprocess");
874 tag = GST_READ_UINT24_BE (l->rx_buf + 1);
876 if (packet_len > 0) {
877 if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
878 GST_LOG ("Expanding rx buf from %d to %d",
879 l->rx_buf_size, packet_len + HEADER_SIZE + BUF_GROW_EXTRA);
880 l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
881 l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
884 in = l->rx_buf + HEADER_SIZE;
885 to_read = packet_len;
887 res = read (l->fd_r.fd, in, to_read);
888 if (G_UNLIKELY (res < 0)) {
889 if (errno == EAGAIN || errno == EINTR)
891 GST_ERROR ("Packet payload read failed");
896 } while (to_read > 0);
898 GST_LOG ("No payload to read for 0 length packet type %d tag %u",
902 return handle_rx_packet (l, l->rx_buf[0], tag,
903 l->rx_buf + HEADER_SIZE, packet_len);
907 exchange_packets (GstPluginLoader * l)
911 /* Wait for activity on our FDs */
914 res = gst_poll_wait (l->fdset, GST_SECOND);
915 } while (res == -1 && (errno == EINTR || errno == EAGAIN));
920 GST_LOG ("Poll res = %d. %d bytes pending for write", res,
921 l->tx_buf_write - l->tx_buf_read);
924 if (gst_poll_fd_has_error (l->fdset, &l->fd_r) ||
925 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
926 GST_LOG ("read fd %d closed/errored", l->fd_r.fd);
927 goto fail_and_cleanup;
930 if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
932 goto fail_and_cleanup;
936 if (l->tx_buf_read < l->tx_buf_write) {
937 if (gst_poll_fd_has_error (l->fdset, &l->fd_w) ||
938 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
939 GST_ERROR ("write fd %d closed/errored", l->fd_w.fd);
940 goto fail_and_cleanup;
942 if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
944 goto fail_and_cleanup;
947 } while (l->tx_buf_read < l->tx_buf_write);
951 plugin_loader_cleanup_child (l);