gst_private.h: make sure gst_private.h is included before glib.h
[platform/upstream/gstreamer.git] / gst / gstpluginloader.c
1 /* GStreamer
2  * Copyright (C) 2008 Jan Schmidt <jan.schmidt@sun.com>
3  *
4  * gstpluginloader.c: GstPluginLoader helper for loading plugin files
5  * out of process.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <gst/gst_private.h>
28
29 #ifndef G_OS_WIN32
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #endif
36 #include <errno.h>
37
38 #include <gst/gstconfig.h>
39
40 #include <gst/gstpoll.h>
41 #include <gst/gstutils.h>
42
43 #include <gst/gstpluginloader.h>
44 #include <gst/gstregistrychunks.h>
45 #include <gst/gstregistrybinary.h>
46
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
50  */
51 static const guint32 loader_protocol_version = 3;
52
53 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
54
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);
59
60 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
61   plugin_loader_new, plugin_loader_free, plugin_loader_load
62 };
63
64 typedef struct _PendingPluginEntry
65 {
66   guint32 tag;
67   gchar *filename;
68   off_t file_size;
69   time_t file_mtime;
70 } PendingPluginEntry;
71
72 struct _GstPluginLoader
73 {
74   GstRegistry *registry;
75   GstPoll *fdset;
76
77   gboolean child_running;
78   GPid child_pid;
79   GstPollFD fd_w;
80   GstPollFD fd_r;
81
82   gboolean is_child;
83   gboolean got_plugin_details;
84
85   /* Transmit buffer */
86   guint8 *tx_buf;
87   guint tx_buf_size;
88   guint tx_buf_write;
89   guint tx_buf_read;
90
91   guint32 next_tag;
92
93   guint8 *rx_buf;
94   guint rx_buf_size;
95   gboolean rx_done;
96   gboolean rx_got_sync;
97
98   /* Head and tail of the pending plugins list. List of
99      PendingPluginEntry structs */
100   GList *pending_plugins;
101   GList *pending_plugins_tail;
102 };
103
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
109
110 #define BUF_INIT_SIZE 512
111 #define BUF_GROW_EXTRA 512
112 #define BUF_MAX_SIZE (32 * 1024 * 1024)
113
114 #define HEADER_SIZE 12
115 /* 4 magic hex bytes to mark each packet */
116 #define HEADER_MAGIC 0xbefec0ae
117 #define ALIGNMENT   (sizeof (void *))
118
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);
130
131 static GstPluginLoader *
132 plugin_loader_new (GstRegistry * registry)
133 {
134   GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
135
136   if (registry)
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);
141
142   l->tx_buf_size = BUF_INIT_SIZE;
143   l->tx_buf = g_malloc (BUF_INIT_SIZE);
144
145   l->next_tag = 0;
146
147   l->rx_buf_size = BUF_INIT_SIZE;
148   l->rx_buf = g_malloc (BUF_INIT_SIZE);
149
150   return l;
151 }
152
153 static gboolean
154 plugin_loader_free (GstPluginLoader * loader)
155 {
156   GList *cur;
157   gboolean got_plugin_details;
158
159   fsync (loader->fd_w.fd);
160
161   if (loader->child_running) {
162     put_packet (loader, PACKET_EXIT, 0, NULL, 0);
163
164     /* Swap packets with the child until it exits cleanly */
165     while (!loader->rx_done) {
166       if (exchange_packets (loader) || loader->rx_done)
167         continue;
168
169       if (!plugin_loader_replay_pending (loader))
170         break;
171       put_packet (loader, PACKET_EXIT, 0, NULL, 0);
172     }
173
174     plugin_loader_cleanup_child (loader);
175   } else {
176     close (loader->fd_w.fd);
177     close (loader->fd_r.fd);
178   }
179
180   gst_poll_free (loader->fdset);
181
182   g_free (loader->rx_buf);
183   g_free (loader->tx_buf);
184
185   if (loader->registry)
186     gst_object_unref (loader->registry);
187
188   got_plugin_details = loader->got_plugin_details;
189
190   /* Free any pending plugin entries */
191   cur = loader->pending_plugins;
192   while (cur) {
193     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
194     g_free (entry->filename);
195     g_free (entry);
196
197     cur = g_list_delete_link (cur, cur);
198   }
199
200   g_free (loader);
201
202   return got_plugin_details;
203 }
204
205 static gboolean
206 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
207     off_t file_size, time_t file_mtime)
208 {
209   gint len;
210   PendingPluginEntry *entry;
211
212   if (!gst_plugin_loader_spawn (loader))
213     return FALSE;
214
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);
218
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);
226
227   if (loader->pending_plugins == NULL)
228     loader->pending_plugins = loader->pending_plugins_tail;
229   else
230     loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
231
232   len = strlen (filename);
233   put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
234       (guint8 *) filename, len + 1);
235
236   if (!exchange_packets (loader)) {
237     if (!plugin_loader_replay_pending (loader))
238       return FALSE;
239   }
240
241   return TRUE;
242 }
243
244 static gboolean
245 plugin_loader_replay_pending (GstPluginLoader * l)
246 {
247   GList *cur, *next;
248
249 restart:
250   if (!gst_plugin_loader_spawn (l))
251     return FALSE;
252
253   /* Load each plugin one by one synchronously until we find the
254    * crashing one */
255   while ((cur = l->pending_plugins)) {
256     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
257
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",
261           entry->filename);
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))
269         return FALSE;
270       break;
271     }
272   }
273
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);
278
279     next = g_list_next (cur);
280
281     put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
282         (guint8 *) entry->filename, strlen (entry->filename) + 1);
283
284     /* This might invalidate cur, which is why we grabbed 'next' above */
285     if (!exchange_packets (l))
286       goto restart;
287   }
288
289   return TRUE;
290 }
291
292 static gboolean
293 plugin_loader_sync_with_child (GstPluginLoader * l)
294 {
295   put_packet (l, PACKET_SYNC, 0, NULL, 0);
296
297   l->rx_got_sync = FALSE;
298   while (!l->rx_got_sync) {
299     if (!exchange_packets (l))
300       return FALSE;
301   }
302   return TRUE;
303 }
304
305 static gboolean
306 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
307 {
308   gint len;
309
310   GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
311       entry->filename);
312
313   len = strlen (entry->filename);
314   put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
315       (guint8 *) entry->filename, len + 1);
316
317   return plugin_loader_sync_with_child (l);
318 }
319
320 static void
321 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
322     PendingPluginEntry * entry)
323 {
324   GstPlugin *plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
325
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;
330
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;
339
340   GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
341   gst_registry_add_plugin (l->registry, plugin);
342 }
343
344 static gboolean
345 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
346 {
347   char *argv[] = { location, "-l", NULL };
348
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,
353           NULL, NULL))
354     return FALSE;
355
356   gst_poll_add_fd (loader->fdset, &loader->fd_w);
357   gst_poll_add_fd (loader->fdset, &loader->fd_r);
358
359   gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
360
361   loader->tx_buf_write = loader->tx_buf_read = 0;
362
363   put_packet (loader, PACKET_VERSION, 0, NULL, 0);
364   if (!plugin_loader_sync_with_child (loader))
365     return FALSE;
366
367   loader->child_running = TRUE;
368
369   return TRUE;
370 }
371
372 static gboolean
373 gst_plugin_loader_spawn (GstPluginLoader * loader)
374 {
375   char *helper_bin;
376   gboolean res;
377
378   if (loader->child_running)
379     return TRUE;
380
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);
384   g_free (helper_bin);
385
386   if (!res) {
387     /* Try the GST_PLUGIN_SCANNER env var */
388     const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
389     if (env != NULL) {
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);
394       g_free (helper_bin);
395     } else {
396       GST_LOG ("Installed plugin scanner failed and GST_PLUGIN_SCANNER "
397           " env var not set. No gst-plugin-scanner available");
398     }
399   }
400
401   return loader->child_running;
402 }
403
404 static void
405 plugin_loader_cleanup_child (GstPluginLoader * l)
406 {
407   if (!l->child_running || l->is_child)
408     return;
409
410   gst_poll_remove_fd (l->fdset, &l->fd_w);
411   gst_poll_remove_fd (l->fdset, &l->fd_r);
412
413   close (l->fd_w.fd);
414   close (l->fd_r.fd);
415
416 #ifndef G_OS_WIN32
417   GST_LOG ("waiting for child process to exit");
418   waitpid (l->child_pid, NULL, 0);
419 #else
420   g_warning ("FIXME: Implement child process shutdown for Win32");
421 #endif
422   g_spawn_close_pid (l->child_pid);
423
424   l->child_running = FALSE;
425 }
426
427 gboolean
428 _gst_plugin_loader_client_run ()
429 {
430   GstPluginLoader *l;
431   int dup_fd;
432
433   l = plugin_loader_new (NULL);
434   if (l == NULL)
435     return FALSE;
436
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 */
440 #ifndef G_OS_WIN32
441   dup_fd = dup (0);             /* STDIN */
442   if (dup_fd == -1) {
443     GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
444     return FALSE;
445   }
446   l->fd_r.fd = dup_fd;
447   close (0);
448
449   dup_fd = dup (1);             /* STDOUT */
450   if (dup_fd == -1) {
451     GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
452     return FALSE;
453   }
454   l->fd_w.fd = dup_fd;
455   close (1);
456
457   /* Dup stderr down to stdout so things that plugins print are visible,
458    * but don't care if it fails */
459   dup2 (2, 1);
460 #else
461   /* FIXME: Use DuplicateHandle and friends on win32 */
462   l->fd_w.fd = 1;               /* STDOUT */
463   l->fd_r.fd = 0;               /* STDIN */
464 #endif
465
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);
469
470   l->is_child = TRUE;
471
472   GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
473
474   /* Loop, listening for incoming packets on the fd and writing responses */
475   while (!l->rx_done && exchange_packets (l));
476
477   plugin_loader_free (l);
478
479   return TRUE;
480 }
481
482 static void
483 put_packet (GstPluginLoader * l, guint type, guint32 tag,
484     const guint8 * payload, guint32 payload_len)
485 {
486   guint8 *out;
487   guint len = payload_len + HEADER_SIZE;
488
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);
494   }
495
496   out = l->tx_buf + l->tx_buf_write;
497
498   /* one byte packet type */
499   out[0] = 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);
504   /* payload */
505   memcpy (out + HEADER_SIZE, payload, payload_len);
506   /* Write magic into the header */
507   GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
508
509   l->tx_buf_write += len;
510   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
511 }
512
513 static void
514 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
515 {
516   guint padsize = 0;
517   guint len;
518   guint8 *out;
519
520   /* Might need to align the chunk */
521   if (chunk->align && ((*pos) % ALIGNMENT) != 0)
522     padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
523
524   len = padsize + chunk->size;
525
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);
533   }
534
535   out = l->tx_buf + l->tx_buf_write;
536   /* Clear the padding */
537   if (padsize)
538     memset (out, 0, padsize);
539   memcpy (out + padsize, chunk->data, chunk->size);
540
541   l->tx_buf_write += len;
542   *pos += len;
543
544   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
545 };
546
547 static gboolean
548 write_one (GstPluginLoader * l)
549 {
550   guint8 *out;
551   guint32 to_write, magic;
552   int res;
553
554   if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
555     return FALSE;
556
557   out = l->tx_buf + l->tx_buf_read;
558
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;
563   }
564
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;
570   }
571
572   l->tx_buf_read += to_write;
573
574   GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
575
576   do {
577     res = write (l->fd_w.fd, out, to_write);
578     if (G_UNLIKELY (res < 0)) {
579       if (errno == EAGAIN || errno == EINTR)
580         continue;
581       /* Failed to write -> child died */
582       goto fail_and_cleanup;
583     }
584     to_write -= res;
585     out += res;
586   } while (to_write > 0);
587
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;
591   }
592
593   return TRUE;
594
595 fail_and_cleanup:
596   plugin_loader_cleanup_child (l);
597   return FALSE;
598 }
599
600 static gboolean
601 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
602 {
603   GstPlugin *newplugin;
604   GList *chunks = NULL;
605
606   GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
607
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));
612   }
613 #endif
614
615   newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
616   if (newplugin) {
617     guint hdr_pos;
618     guint offset;
619
620     /* Now serialise the plugin details and send */
621     if (!_priv_gst_registry_chunks_save_plugin (&chunks,
622             gst_registry_get_default (), newplugin))
623       goto fail;
624
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);
630
631     if (chunks) {
632       GList *walk;
633       for (walk = chunks; walk; walk = g_list_next (walk)) {
634         GstRegistryChunk *cur = walk->data;
635         put_chunk (l, cur, &offset);
636
637         if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
638           g_free (cur->data);
639         g_free (cur);
640       }
641
642       g_list_free (chunks);
643
644       /* Store the size of the written payload */
645       GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
646     }
647 #if 0                           /* Test code - corrupt the tx buffer based on filename */
648     if (strstr (filename, "sink") != NULL) {
649       int fd, res;
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);
653       close (fd);
654     }
655 #endif
656
657     gst_object_unref (newplugin);
658   } else {
659     put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
660   }
661
662   return TRUE;
663 fail:
664   put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
665   if (chunks) {
666     GList *walk;
667     for (walk = chunks; walk; walk = g_list_next (walk)) {
668       GstRegistryChunk *cur = walk->data;
669
670       if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
671         g_free (cur->data);
672       g_free (cur);
673     }
674
675     g_list_free (chunks);
676   }
677
678   return FALSE;
679 }
680
681 static gboolean
682 check_protocol_version (GstPluginLoader * l, guint8 * payload,
683     guint payload_len)
684 {
685   guint32 got_version;
686   guint8 *binary_reg_ver;
687
688   if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
689     return FALSE;
690
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)
695     return FALSE;
696
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);
701     return FALSE;
702   }
703
704   return TRUE;
705 };
706
707 static gboolean
708 handle_rx_packet (GstPluginLoader * l,
709     guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
710 {
711   gboolean res = TRUE;
712
713   switch (pack_type) {
714     case PACKET_EXIT:
715       gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
716       if (l->is_child) {
717         /* Respond, then we keep looping until the parent closes the fd */
718         put_packet (l, PACKET_EXIT, 0, NULL, 0);
719       } else {
720         l->rx_done = TRUE;      /* All done reading from child */
721       }
722       return TRUE;
723     case PACKET_LOAD_PLUGIN:{
724       if (!l->is_child)
725         return TRUE;
726
727       /* Payload is the filename to load */
728       res = do_plugin_load (l, (gchar *) payload, tag);
729
730       break;
731     }
732     case PACKET_PLUGIN_DETAILS:{
733       gchar *tmp = (gchar *) payload;
734       PendingPluginEntry *entry = NULL;
735       GList *cur;
736
737       GST_DEBUG_OBJECT (l->registry,
738           "Received plugin details from child w/ tag %u. %d bytes info",
739           tag, payload_len);
740
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;
745       while (cur) {
746         PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
747
748         if (e->tag > tag)
749           break;
750
751         if (e->tag == tag) {
752           entry = e;
753           break;
754         } else {
755           cur = g_list_delete_link (cur, cur);
756           g_free (e->filename);
757           g_free (e);
758         }
759       }
760
761       l->pending_plugins = cur;
762       if (cur == NULL)
763         l->pending_plugins_tail = NULL;
764
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);
772           return FALSE;
773         }
774
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;
780
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;
787       }
788
789       if (entry != NULL) {
790         g_free (entry->filename);
791         g_free (entry);
792       }
793
794       /* Remove the plugin entry we just loaded */
795       cur = l->pending_plugins;
796       if (cur != NULL)
797         cur = g_list_delete_link (cur, cur);
798       l->pending_plugins = cur;
799       if (cur == NULL)
800         l->pending_plugins_tail = NULL;
801
802       break;
803     }
804     case PACKET_SYNC:
805       if (l->is_child) {
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");
809       } else
810         l->rx_got_sync = TRUE;
811       break;
812     case PACKET_VERSION:
813       if (l->is_child) {
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);
824       } else {
825         res = check_protocol_version (l, payload, payload_len);
826       }
827       break;
828     default:
829       return FALSE;             /* Invalid packet -> something is wrong */
830   }
831
832   return res;
833 }
834
835 static gboolean
836 read_one (GstPluginLoader * l)
837 {
838   guint64 magic;
839   guint32 to_read, packet_len, tag;
840   guint8 *in;
841   gint res;
842
843   to_read = HEADER_SIZE;
844   in = l->rx_buf;
845   do {
846     res = read (l->fd_r.fd, in, to_read);
847     if (G_UNLIKELY (res < 0)) {
848       if (errno == EAGAIN || errno == EINTR)
849         continue;
850       GST_LOG ("Failed reading packet header");
851       return FALSE;
852     }
853     to_read -= res;
854     in += res;
855   } while (to_read > 0);
856
857   magic = GST_READ_UINT32_BE (l->rx_buf + 8);
858   if (magic != HEADER_MAGIC) {
859     GST_WARNING
860         ("Invalid packet (bad magic number) received from plugin scanner subprocess");
861     return FALSE;
862   }
863
864   packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
865   if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
866     GST_WARNING
867         ("Received excessively large packet for plugin scanner subprocess");
868     return FALSE;
869   }
870   tag = GST_READ_UINT24_BE (l->rx_buf + 1);
871
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);
878     }
879
880     in = l->rx_buf + HEADER_SIZE;
881     to_read = packet_len;
882     do {
883       res = read (l->fd_r.fd, in, to_read);
884       if (G_UNLIKELY (res < 0)) {
885         if (errno == EAGAIN || errno == EINTR)
886           continue;
887         GST_ERROR ("Packet payload read failed");
888         return FALSE;
889       }
890       to_read -= res;
891       in += res;
892     } while (to_read > 0);
893   } else {
894     GST_LOG ("No payload to read for 0 length packet type %d tag %u",
895         l->rx_buf[0], tag);
896   }
897
898   return handle_rx_packet (l, l->rx_buf[0], tag,
899       l->rx_buf + HEADER_SIZE, packet_len);
900 }
901
902 static gboolean
903 exchange_packets (GstPluginLoader * l)
904 {
905   gint res;
906
907   /* Wait for activity on our FDs */
908   do {
909     do {
910       res = gst_poll_wait (l->fdset, GST_SECOND);
911     } while (res == -1 && (errno == EINTR || errno == EAGAIN));
912
913     if (res < 0)
914       return FALSE;
915
916     GST_LOG ("Poll res = %d. %d bytes pending for write", res,
917         l->tx_buf_write - l->tx_buf_read);
918
919     if (!l->rx_done) {
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;
924       }
925
926       if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
927         if (!read_one (l))
928           goto fail_and_cleanup;
929       }
930     }
931
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;
937       }
938       if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
939         if (!write_one (l))
940           goto fail_and_cleanup;
941       }
942     }
943   } while (l->tx_buf_read < l->tx_buf_write);
944
945   return TRUE;
946 fail_and_cleanup:
947   plugin_loader_cleanup_child (l);
948   return FALSE;
949 }