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 /* functions used in GstRegistry scanning */
65 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
66 plugin_loader_new, plugin_loader_free, plugin_loader_load
69 typedef struct _PendingPluginEntry
78 struct _GstPluginLoader
80 GstRegistry *registry;
83 gboolean child_running;
89 gboolean got_plugin_details;
97 /* next sequence number (for PendingPluginEntry) */
103 gboolean rx_got_sync;
105 /* Head and tail of the pending plugins list. List of
106 PendingPluginEntry structs */
107 GList *pending_plugins;
108 GList *pending_plugins_tail;
111 #define PACKET_EXIT 1
112 #define PACKET_LOAD_PLUGIN 2
113 #define PACKET_SYNC 3
114 #define PACKET_PLUGIN_DETAILS 4
115 #define PACKET_VERSION 5
117 #define BUF_INIT_SIZE 512
118 #define BUF_GROW_EXTRA 512
119 #define BUF_MAX_SIZE (32 * 1024 * 1024)
121 #define HEADER_SIZE 12
122 /* 4 magic hex bytes to mark each packet */
123 #define HEADER_MAGIC 0xbefec0ae
124 #define ALIGNMENT (sizeof (void *))
126 static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader);
127 static void put_packet (GstPluginLoader * loader, guint type, guint32 tag,
128 const guint8 * payload, guint32 payload_len);
129 static gboolean exchange_packets (GstPluginLoader * l);
130 static gboolean plugin_loader_replay_pending (GstPluginLoader * l);
131 static gboolean plugin_loader_load_and_sync (GstPluginLoader * l,
132 PendingPluginEntry * entry);
133 static void plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
134 PendingPluginEntry * entry);
135 static void plugin_loader_cleanup_child (GstPluginLoader * loader);
136 static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
138 static GstPluginLoader *
139 plugin_loader_new (GstRegistry * registry)
141 GstPluginLoader *l = g_slice_new0 (GstPluginLoader);
144 l->registry = gst_object_ref (registry);
145 l->fdset = gst_poll_new (FALSE);
146 gst_poll_fd_init (&l->fd_w);
147 gst_poll_fd_init (&l->fd_r);
149 l->tx_buf_size = BUF_INIT_SIZE;
150 l->tx_buf = g_malloc (BUF_INIT_SIZE);
154 l->rx_buf_size = BUF_INIT_SIZE;
155 l->rx_buf = g_malloc (BUF_INIT_SIZE);
161 plugin_loader_free (GstPluginLoader * loader)
164 gboolean got_plugin_details;
166 fsync (loader->fd_w.fd);
168 if (loader->child_running) {
169 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
171 /* Swap packets with the child until it exits cleanly */
172 while (!loader->rx_done) {
173 if (exchange_packets (loader) || loader->rx_done)
176 if (!plugin_loader_replay_pending (loader))
178 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
181 plugin_loader_cleanup_child (loader);
183 close (loader->fd_w.fd);
184 close (loader->fd_r.fd);
187 gst_poll_free (loader->fdset);
189 g_free (loader->rx_buf);
190 g_free (loader->tx_buf);
192 if (loader->registry)
193 gst_object_unref (loader->registry);
195 got_plugin_details = loader->got_plugin_details;
197 /* Free any pending plugin entries */
198 cur = loader->pending_plugins;
200 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
201 g_free (entry->filename);
202 g_slice_free (PendingPluginEntry, entry);
204 cur = g_list_delete_link (cur, cur);
207 g_slice_free (GstPluginLoader, loader);
209 return got_plugin_details;
213 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
214 off_t file_size, time_t file_mtime)
217 PendingPluginEntry *entry;
219 if (!gst_plugin_loader_spawn (loader))
222 /* Send a packet to the child requesting that it load the given file */
223 GST_LOG_OBJECT (loader->registry,
224 "Sending file %s to child. tag %u", filename, loader->next_tag);
226 entry = g_slice_new (PendingPluginEntry);
227 entry->tag = loader->next_tag++;
228 entry->filename = g_strdup (filename);
229 entry->file_size = file_size;
230 entry->file_mtime = file_mtime;
231 loader->pending_plugins_tail =
232 g_list_append (loader->pending_plugins_tail, entry);
234 if (loader->pending_plugins == NULL)
235 loader->pending_plugins = loader->pending_plugins_tail;
237 loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
239 len = strlen (filename);
240 put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
241 (guint8 *) filename, len + 1);
243 if (!exchange_packets (loader)) {
244 if (!plugin_loader_replay_pending (loader))
252 plugin_loader_replay_pending (GstPluginLoader * l)
257 if (!gst_plugin_loader_spawn (l))
260 /* Load each plugin one by one synchronously until we find the
262 while ((cur = l->pending_plugins)) {
263 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
265 if (!plugin_loader_load_and_sync (l, entry)) {
266 /* Create dummy plugin entry to block re-scanning this file */
267 GST_ERROR ("Plugin file %s failed to load. Blacklisting",
269 plugin_loader_create_blacklist_plugin (l, entry);
270 l->got_plugin_details = TRUE;
271 /* Now remove this crashy plugin from the head of the list */
272 l->pending_plugins = g_list_delete_link (cur, cur);
273 g_free (entry->filename);
274 g_slice_free (PendingPluginEntry, entry);
275 if (l->pending_plugins == NULL)
276 l->pending_plugins_tail = NULL;
277 if (!gst_plugin_loader_spawn (l))
283 /* We exited after finding the crashing one. If there's any more pending,
284 * dispatch them post-haste, but don't wait */
285 for (cur = l->pending_plugins; cur != NULL; cur = next) {
286 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
288 next = g_list_next (cur);
290 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
291 (guint8 *) entry->filename, strlen (entry->filename) + 1);
293 /* This might invalidate cur, which is why we grabbed 'next' above */
294 if (!exchange_packets (l))
302 plugin_loader_sync_with_child (GstPluginLoader * l)
304 put_packet (l, PACKET_SYNC, 0, NULL, 0);
306 l->rx_got_sync = FALSE;
307 while (!l->rx_got_sync) {
308 if (!exchange_packets (l))
315 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
319 GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
322 len = strlen (entry->filename);
323 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
324 (guint8 *) entry->filename, len + 1);
326 return plugin_loader_sync_with_child (l);
330 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
331 PendingPluginEntry * entry)
333 GstPlugin *plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
335 plugin->filename = g_strdup (entry->filename);
336 plugin->file_mtime = entry->file_mtime;
337 plugin->file_size = entry->file_size;
338 plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
340 plugin->basename = g_path_get_basename (plugin->filename);
341 plugin->desc.name = g_intern_string (plugin->basename);
342 plugin->desc.description = "Plugin for blacklisted file";
343 plugin->desc.version = "0.0.0";
344 plugin->desc.license = "BLACKLIST";
345 plugin->desc.source = plugin->desc.license;
346 plugin->desc.package = plugin->desc.license;
347 plugin->desc.origin = plugin->desc.license;
349 GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
350 gst_registry_add_plugin (l->registry, plugin);
354 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
356 char *argv[] = { location, (char *) "-l", NULL };
358 GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
359 if (!g_spawn_async_with_pipes (NULL, argv, NULL,
360 G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
361 NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
365 gst_poll_add_fd (loader->fdset, &loader->fd_w);
366 gst_poll_add_fd (loader->fdset, &loader->fd_r);
368 gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
370 loader->tx_buf_write = loader->tx_buf_read = 0;
372 put_packet (loader, PACKET_VERSION, 0, NULL, 0);
373 if (!plugin_loader_sync_with_child (loader))
376 loader->child_running = TRUE;
382 gst_plugin_loader_spawn (GstPluginLoader * loader)
386 gboolean res = FALSE;
388 if (loader->child_running)
391 /* Find the gst-plugin-scanner: first try the env-var if it is set,
392 * otherwise use the installed version */
393 env = g_getenv ("GST_PLUGIN_SCANNER");
395 if (env != NULL && *env != '\0') {
396 GST_LOG ("Trying GST_PLUGIN_SCANNER env var: %s", env);
397 helper_bin = g_strdup (env);
398 res = gst_plugin_loader_try_helper (loader, helper_bin);
403 GST_LOG ("Trying installed plugin scanner");
404 helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
405 res = gst_plugin_loader_try_helper (loader, helper_bin);
409 GST_INFO ("No gst-plugin-scanner available, or not working");
413 return loader->child_running;
417 plugin_loader_cleanup_child (GstPluginLoader * l)
419 if (!l->child_running || l->is_child)
422 gst_poll_remove_fd (l->fdset, &l->fd_w);
423 gst_poll_remove_fd (l->fdset, &l->fd_r);
429 GST_LOG ("waiting for child process to exit");
430 waitpid (l->child_pid, NULL, 0);
432 g_warning ("FIXME: Implement child process shutdown for Win32");
434 g_spawn_close_pid (l->child_pid);
436 l->child_running = FALSE;
440 _gst_plugin_loader_client_run (void)
444 l = plugin_loader_new (NULL);
448 /* On entry, the inward pipe is STDIN, and outward is STDOUT.
449 * Dup those somewhere better so that plugins printing things
450 * won't interfere with anything */
455 dup_fd = dup (0); /* STDIN */
457 GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
463 dup_fd = dup (1); /* STDOUT */
465 GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
471 /* Dup stderr down to stdout so things that plugins print are visible,
472 * but don't care if it fails */
476 /* FIXME: Use DuplicateHandle and friends on win32 */
477 l->fd_w.fd = 1; /* STDOUT */
478 l->fd_r.fd = 0; /* STDIN */
481 gst_poll_add_fd (l->fdset, &l->fd_w);
482 gst_poll_add_fd (l->fdset, &l->fd_r);
483 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
487 GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
489 /* Loop, listening for incoming packets on the fd and writing responses */
490 while (!l->rx_done && exchange_packets (l));
492 plugin_loader_free (l);
498 put_packet (GstPluginLoader * l, guint type, guint32 tag,
499 const guint8 * payload, guint32 payload_len)
502 guint len = payload_len + HEADER_SIZE;
504 if (l->tx_buf_write + len >= l->tx_buf_size) {
505 GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
506 l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
507 l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
508 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
511 out = l->tx_buf + l->tx_buf_write;
513 /* one byte packet type */
515 /* 3 byte packet tag number */
516 GST_WRITE_UINT24_BE (out + 1, tag);
517 /* 4 bytes packet length */
518 GST_WRITE_UINT32_BE (out + 4, payload_len);
520 memcpy (out + HEADER_SIZE, payload, payload_len);
521 /* Write magic into the header */
522 GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
524 l->tx_buf_write += len;
525 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
529 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
535 /* Might need to align the chunk */
536 if (chunk->align && ((*pos) % ALIGNMENT) != 0)
537 padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
539 len = padsize + chunk->size;
541 if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
542 guint new_size = MAX (l->tx_buf_write + len,
543 l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
544 GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
545 l->tx_buf_size, new_size, chunk->size);
546 l->tx_buf_size = new_size;
547 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
550 out = l->tx_buf + l->tx_buf_write;
551 /* Clear the padding */
553 memset (out, 0, padsize);
554 memcpy (out + padsize, chunk->data, chunk->size);
556 l->tx_buf_write += len;
559 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
563 write_one (GstPluginLoader * l)
566 guint32 to_write, magic;
569 if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
572 out = l->tx_buf + l->tx_buf_read;
574 magic = GST_READ_UINT32_BE (out + 8);
575 if (magic != HEADER_MAGIC) {
576 GST_ERROR ("Packet magic number is missing. Memory corruption detected");
577 goto fail_and_cleanup;
580 to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
581 /* Check that the magic is intact, and the size is sensible */
582 if (to_write > l->tx_buf_size) {
583 GST_ERROR ("Indicated packet size is too large. Corruption detected");
584 goto fail_and_cleanup;
587 l->tx_buf_read += to_write;
589 GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
592 res = write (l->fd_w.fd, out, to_write);
593 if (G_UNLIKELY (res < 0)) {
594 if (errno == EAGAIN || errno == EINTR)
596 /* Failed to write -> child died */
597 goto fail_and_cleanup;
601 } while (to_write > 0);
603 if (l->tx_buf_read == l->tx_buf_write) {
604 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
605 l->tx_buf_read = l->tx_buf_write = 0;
611 plugin_loader_cleanup_child (l);
616 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
618 GstPlugin *newplugin;
619 GList *chunks = NULL;
621 GST_DEBUG ("Plugin scanner loading file %s. tag %u", filename, tag);
623 #if 0 /* Test code - crash based on filename */
624 if (strstr (filename, "coreelements") == NULL) {
625 g_printerr ("Crashing on file %s\n", filename);
626 g_printerr ("%d", *(gint *) (NULL));
630 newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
635 /* Now serialise the plugin details and send */
636 if (!_priv_gst_registry_chunks_save_plugin (&chunks,
637 gst_registry_get_default (), newplugin))
640 /* Store where the header is, write an empty one, then write
641 * all the payload chunks, then fix up the header size */
642 hdr_pos = l->tx_buf_write;
643 offset = HEADER_SIZE;
644 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
648 for (walk = chunks; walk; walk = g_list_next (walk)) {
649 GstRegistryChunk *cur = walk->data;
650 put_chunk (l, cur, &offset);
652 _priv_gst_registry_chunk_free (cur);
655 g_list_free (chunks);
657 /* Store the size of the written payload */
658 GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
660 #if 0 /* Test code - corrupt the tx buffer based on filename */
661 if (strstr (filename, "sink") != NULL) {
663 g_printerr ("Corrupting tx buf on file %s\n", filename);
664 fd = open ("/dev/urandom", O_RDONLY);
665 res = read (fd, l->tx_buf, l->tx_buf_size);
670 gst_object_unref (newplugin);
672 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
677 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
680 for (walk = chunks; walk; walk = g_list_next (walk)) {
681 GstRegistryChunk *cur = walk->data;
683 _priv_gst_registry_chunk_free (cur);
686 g_list_free (chunks);
693 check_protocol_version (GstPluginLoader * l, guint8 * payload,
697 guint8 *binary_reg_ver;
699 if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
702 got_version = GST_READ_UINT32_BE (payload);
703 GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
704 loader_protocol_version);
705 if (got_version != loader_protocol_version)
708 binary_reg_ver = payload + sizeof (guint32);
709 if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
710 GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
711 GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
719 handle_rx_packet (GstPluginLoader * l,
720 guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
726 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
729 put_packet (l, PACKET_EXIT, 0, NULL, 0);
733 case PACKET_LOAD_PLUGIN:{
737 /* Payload is the filename to load */
738 res = do_plugin_load (l, (gchar *) payload, tag);
742 case PACKET_PLUGIN_DETAILS:{
743 gchar *tmp = (gchar *) payload;
744 PendingPluginEntry *entry = NULL;
747 GST_DEBUG_OBJECT (l->registry,
748 "Received plugin details from child w/ tag %u. %d bytes info",
751 /* Assume that tagged details come back in the order
752 * we requested, and delete anything before (but not
753 * including) this one */
754 cur = l->pending_plugins;
756 PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
765 cur = g_list_delete_link (cur, cur);
766 g_free (e->filename);
767 g_slice_free (PendingPluginEntry, e);
771 l->pending_plugins = cur;
773 l->pending_plugins_tail = NULL;
775 if (payload_len > 0) {
776 GstPlugin *newplugin = NULL;
777 if (!_priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
778 tmp + payload_len, &newplugin)) {
779 /* Got garbage from the child, so fail and trigger replay of plugins */
780 GST_ERROR_OBJECT (l->registry,
781 "Problems loading plugin details with tag %u from scanner", tag);
785 newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
786 GST_LOG_OBJECT (l->registry,
787 "marking plugin %p as registered as %s", newplugin,
788 newplugin->filename);
789 newplugin->registered = TRUE;
791 /* We got a set of plugin details - remember it for later */
792 l->got_plugin_details = TRUE;
793 } else if (entry != NULL) {
794 /* Create a blacklist entry for this file to prevent scanning every time */
795 plugin_loader_create_blacklist_plugin (l, entry);
796 l->got_plugin_details = TRUE;
800 g_free (entry->filename);
801 g_slice_free (PendingPluginEntry, entry);
804 /* Remove the plugin entry we just loaded */
805 cur = l->pending_plugins;
807 cur = g_list_delete_link (cur, cur);
808 l->pending_plugins = cur;
810 l->pending_plugins_tail = NULL;
816 /* Respond with our reply - also a sync */
817 put_packet (l, PACKET_SYNC, tag, NULL, 0);
818 GST_LOG ("Got SYNC in child - replying");
820 l->rx_got_sync = TRUE;
824 /* Respond with our reply - a version packet, with the version */
825 const gint version_len =
826 sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN;
827 guint8 version_info[sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN];
828 memset (version_info, 0, version_len);
829 GST_WRITE_UINT32_BE (version_info, loader_protocol_version);
830 memcpy (version_info + sizeof (guint32), GST_MAGIC_BINARY_VERSION_STR,
831 strlen (GST_MAGIC_BINARY_VERSION_STR));
832 put_packet (l, PACKET_VERSION, tag, version_info, version_len);
833 GST_LOG ("Got VERSION in child - replying %u", loader_protocol_version);
835 res = check_protocol_version (l, payload, payload_len);
839 return FALSE; /* Invalid packet -> something is wrong */
846 read_one (GstPluginLoader * l)
849 guint32 to_read, packet_len, tag;
853 to_read = HEADER_SIZE;
856 res = read (l->fd_r.fd, in, to_read);
857 if (G_UNLIKELY (res < 0)) {
858 if (errno == EAGAIN || errno == EINTR)
860 GST_LOG ("Failed reading packet header");
865 } while (to_read > 0);
867 magic = GST_READ_UINT32_BE (l->rx_buf + 8);
868 if (magic != HEADER_MAGIC) {
870 ("Invalid packet (bad magic number) received from plugin scanner subprocess");
874 packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
875 if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
877 ("Received excessively large packet for plugin scanner subprocess");
880 tag = GST_READ_UINT24_BE (l->rx_buf + 1);
882 if (packet_len > 0) {
883 if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
884 GST_LOG ("Expanding rx buf from %d to %d",
885 l->rx_buf_size, packet_len + HEADER_SIZE + BUF_GROW_EXTRA);
886 l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
887 l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
890 in = l->rx_buf + HEADER_SIZE;
891 to_read = packet_len;
893 res = read (l->fd_r.fd, in, to_read);
894 if (G_UNLIKELY (res < 0)) {
895 if (errno == EAGAIN || errno == EINTR)
897 GST_ERROR ("Packet payload read failed");
902 } while (to_read > 0);
904 GST_LOG ("No payload to read for 0 length packet type %d tag %u",
908 return handle_rx_packet (l, l->rx_buf[0], tag,
909 l->rx_buf + HEADER_SIZE, packet_len);
913 exchange_packets (GstPluginLoader * l)
917 /* Wait for activity on our FDs */
920 res = gst_poll_wait (l->fdset, GST_SECOND);
921 } while (res == -1 && (errno == EINTR || errno == EAGAIN));
926 GST_LOG ("Poll res = %d. %d bytes pending for write", res,
927 l->tx_buf_write - l->tx_buf_read);
930 if (gst_poll_fd_has_error (l->fdset, &l->fd_r) ||
931 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
932 GST_LOG ("read fd %d closed/errored", l->fd_r.fd);
933 goto fail_and_cleanup;
936 if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
938 goto fail_and_cleanup;
942 if (l->tx_buf_read < l->tx_buf_write) {
943 if (gst_poll_fd_has_error (l->fdset, &l->fd_w) ||
944 gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
945 GST_ERROR ("write fd %d closed/errored", l->fd_w.fd);
946 goto fail_and_cleanup;
948 if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
950 goto fail_and_cleanup;
953 } while (l->tx_buf_read < l->tx_buf_write);
957 plugin_loader_cleanup_child (l);