pluginloader: disable external plugin loader on Windows until it is ported properly
[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 #else
36 #define fsync(fd) _commit(fd)
37 #include <io.h>
38 #endif
39
40 #include <errno.h>
41
42 #include <gst/gstconfig.h>
43
44 #include <gst/gstpoll.h>
45 #include <gst/gstutils.h>
46
47 #include <gst/gstpluginloader.h>
48 #include <gst/gstregistrychunks.h>
49 #include <gst/gstregistrybinary.h>
50
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
54  */
55 static const guint32 loader_protocol_version = 3;
56
57 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
58
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);
63
64 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
65   plugin_loader_new, plugin_loader_free, plugin_loader_load
66 };
67
68 typedef struct _PendingPluginEntry
69 {
70   guint32 tag;
71   gchar *filename;
72   off_t file_size;
73   time_t file_mtime;
74 } PendingPluginEntry;
75
76 struct _GstPluginLoader
77 {
78   GstRegistry *registry;
79   GstPoll *fdset;
80
81   gboolean child_running;
82   GPid child_pid;
83   GstPollFD fd_w;
84   GstPollFD fd_r;
85
86   gboolean is_child;
87   gboolean got_plugin_details;
88
89   /* Transmit buffer */
90   guint8 *tx_buf;
91   guint tx_buf_size;
92   guint tx_buf_write;
93   guint tx_buf_read;
94
95   guint32 next_tag;
96
97   guint8 *rx_buf;
98   guint rx_buf_size;
99   gboolean rx_done;
100   gboolean rx_got_sync;
101
102   /* Head and tail of the pending plugins list. List of
103      PendingPluginEntry structs */
104   GList *pending_plugins;
105   GList *pending_plugins_tail;
106 };
107
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
113
114 #define BUF_INIT_SIZE 512
115 #define BUF_GROW_EXTRA 512
116 #define BUF_MAX_SIZE (32 * 1024 * 1024)
117
118 #define HEADER_SIZE 12
119 /* 4 magic hex bytes to mark each packet */
120 #define HEADER_MAGIC 0xbefec0ae
121 #define ALIGNMENT   (sizeof (void *))
122
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);
134
135 static GstPluginLoader *
136 plugin_loader_new (GstRegistry * registry)
137 {
138   GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
139
140   if (registry)
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);
145
146   l->tx_buf_size = BUF_INIT_SIZE;
147   l->tx_buf = g_malloc (BUF_INIT_SIZE);
148
149   l->next_tag = 0;
150
151   l->rx_buf_size = BUF_INIT_SIZE;
152   l->rx_buf = g_malloc (BUF_INIT_SIZE);
153
154   return l;
155 }
156
157 static gboolean
158 plugin_loader_free (GstPluginLoader * loader)
159 {
160   GList *cur;
161   gboolean got_plugin_details;
162
163   fsync (loader->fd_w.fd);
164
165   if (loader->child_running) {
166     put_packet (loader, PACKET_EXIT, 0, NULL, 0);
167
168     /* Swap packets with the child until it exits cleanly */
169     while (!loader->rx_done) {
170       if (exchange_packets (loader) || loader->rx_done)
171         continue;
172
173       if (!plugin_loader_replay_pending (loader))
174         break;
175       put_packet (loader, PACKET_EXIT, 0, NULL, 0);
176     }
177
178     plugin_loader_cleanup_child (loader);
179   } else {
180     close (loader->fd_w.fd);
181     close (loader->fd_r.fd);
182   }
183
184   gst_poll_free (loader->fdset);
185
186   g_free (loader->rx_buf);
187   g_free (loader->tx_buf);
188
189   if (loader->registry)
190     gst_object_unref (loader->registry);
191
192   got_plugin_details = loader->got_plugin_details;
193
194   /* Free any pending plugin entries */
195   cur = loader->pending_plugins;
196   while (cur) {
197     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
198     g_free (entry->filename);
199     g_free (entry);
200
201     cur = g_list_delete_link (cur, cur);
202   }
203
204   g_free (loader);
205
206   return got_plugin_details;
207 }
208
209 static gboolean
210 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
211     off_t file_size, time_t file_mtime)
212 {
213   gint len;
214   PendingPluginEntry *entry;
215
216   if (!gst_plugin_loader_spawn (loader))
217     return FALSE;
218
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);
222
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);
230
231   if (loader->pending_plugins == NULL)
232     loader->pending_plugins = loader->pending_plugins_tail;
233   else
234     loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
235
236   len = strlen (filename);
237   put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
238       (guint8 *) filename, len + 1);
239
240   if (!exchange_packets (loader)) {
241     if (!plugin_loader_replay_pending (loader))
242       return FALSE;
243   }
244
245   return TRUE;
246 }
247
248 static gboolean
249 plugin_loader_replay_pending (GstPluginLoader * l)
250 {
251   GList *cur, *next;
252
253 restart:
254   if (!gst_plugin_loader_spawn (l))
255     return FALSE;
256
257   /* Load each plugin one by one synchronously until we find the
258    * crashing one */
259   while ((cur = l->pending_plugins)) {
260     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
261
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",
265           entry->filename);
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))
273         return FALSE;
274       break;
275     }
276   }
277
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);
282
283     next = g_list_next (cur);
284
285     put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
286         (guint8 *) entry->filename, strlen (entry->filename) + 1);
287
288     /* This might invalidate cur, which is why we grabbed 'next' above */
289     if (!exchange_packets (l))
290       goto restart;
291   }
292
293   return TRUE;
294 }
295
296 static gboolean
297 plugin_loader_sync_with_child (GstPluginLoader * l)
298 {
299   put_packet (l, PACKET_SYNC, 0, NULL, 0);
300
301   l->rx_got_sync = FALSE;
302   while (!l->rx_got_sync) {
303     if (!exchange_packets (l))
304       return FALSE;
305   }
306   return TRUE;
307 }
308
309 static gboolean
310 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
311 {
312   gint len;
313
314   GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
315       entry->filename);
316
317   len = strlen (entry->filename);
318   put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
319       (guint8 *) entry->filename, len + 1);
320
321   return plugin_loader_sync_with_child (l);
322 }
323
324 static void
325 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
326     PendingPluginEntry * entry)
327 {
328   GstPlugin *plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
329
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;
334
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;
343
344   GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
345   gst_registry_add_plugin (l->registry, plugin);
346 }
347
348 static gboolean
349 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
350 {
351   char *argv[] = { location, "-l", NULL };
352
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,
357           NULL, NULL))
358     return FALSE;
359
360   gst_poll_add_fd (loader->fdset, &loader->fd_w);
361   gst_poll_add_fd (loader->fdset, &loader->fd_r);
362
363   gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
364
365   loader->tx_buf_write = loader->tx_buf_read = 0;
366
367   put_packet (loader, PACKET_VERSION, 0, NULL, 0);
368   if (!plugin_loader_sync_with_child (loader))
369     return FALSE;
370
371   loader->child_running = TRUE;
372
373   return TRUE;
374 }
375
376 static gboolean
377 gst_plugin_loader_spawn (GstPluginLoader * loader)
378 {
379   char *helper_bin;
380   gboolean res;
381
382   if (loader->child_running)
383     return TRUE;
384
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);
388   g_free (helper_bin);
389
390   if (!res) {
391     /* Try the GST_PLUGIN_SCANNER env var */
392     const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
393     if (env != NULL) {
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);
398       g_free (helper_bin);
399     } else {
400       GST_LOG ("Installed plugin scanner failed and GST_PLUGIN_SCANNER "
401           " env var not set. No gst-plugin-scanner available");
402     }
403   }
404
405   return loader->child_running;
406 }
407
408 static void
409 plugin_loader_cleanup_child (GstPluginLoader * l)
410 {
411   if (!l->child_running || l->is_child)
412     return;
413
414   gst_poll_remove_fd (l->fdset, &l->fd_w);
415   gst_poll_remove_fd (l->fdset, &l->fd_r);
416
417   close (l->fd_w.fd);
418   close (l->fd_r.fd);
419
420 #ifndef G_OS_WIN32
421   GST_LOG ("waiting for child process to exit");
422   waitpid (l->child_pid, NULL, 0);
423 #else
424   g_warning ("FIXME: Implement child process shutdown for Win32");
425 #endif
426   g_spawn_close_pid (l->child_pid);
427
428   l->child_running = FALSE;
429 }
430
431 gboolean
432 _gst_plugin_loader_client_run ()
433 {
434   GstPluginLoader *l;
435   int dup_fd = 0;
436
437   l = plugin_loader_new (NULL);
438   if (l == NULL)
439     return FALSE;
440
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 */
444 #ifndef G_OS_WIN32
445   dup_fd = dup (0);             /* STDIN */
446   if (dup_fd == -1) {
447     GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
448     return FALSE;
449   }
450   l->fd_r.fd = dup_fd;
451   close (0);
452
453   dup_fd = dup (1);             /* STDOUT */
454   if (dup_fd == -1) {
455     GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
456     return FALSE;
457   }
458   l->fd_w.fd = dup_fd;
459   close (1);
460
461   /* Dup stderr down to stdout so things that plugins print are visible,
462    * but don't care if it fails */
463   dup2 (2, 1);
464 #else
465   /* FIXME: Use DuplicateHandle and friends on win32 */
466   l->fd_w.fd = 1;               /* STDOUT */
467   l->fd_r.fd = 0;               /* STDIN */
468 #endif
469
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);
473
474   l->is_child = TRUE;
475
476   GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
477
478   /* Loop, listening for incoming packets on the fd and writing responses */
479   while (!l->rx_done && exchange_packets (l));
480
481   plugin_loader_free (l);
482
483   return TRUE;
484 }
485
486 static void
487 put_packet (GstPluginLoader * l, guint type, guint32 tag,
488     const guint8 * payload, guint32 payload_len)
489 {
490   guint8 *out;
491   guint len = payload_len + HEADER_SIZE;
492
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);
498   }
499
500   out = l->tx_buf + l->tx_buf_write;
501
502   /* one byte packet type */
503   out[0] = 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);
508   /* payload */
509   memcpy (out + HEADER_SIZE, payload, payload_len);
510   /* Write magic into the header */
511   GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
512
513   l->tx_buf_write += len;
514   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
515 }
516
517 static void
518 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
519 {
520   guint padsize = 0;
521   guint len;
522   guint8 *out;
523
524   /* Might need to align the chunk */
525   if (chunk->align && ((*pos) % ALIGNMENT) != 0)
526     padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
527
528   len = padsize + chunk->size;
529
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);
537   }
538
539   out = l->tx_buf + l->tx_buf_write;
540   /* Clear the padding */
541   if (padsize)
542     memset (out, 0, padsize);
543   memcpy (out + padsize, chunk->data, chunk->size);
544
545   l->tx_buf_write += len;
546   *pos += len;
547
548   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
549 };
550
551 static gboolean
552 write_one (GstPluginLoader * l)
553 {
554   guint8 *out;
555   guint32 to_write, magic;
556   int res;
557
558   if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
559     return FALSE;
560
561   out = l->tx_buf + l->tx_buf_read;
562
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;
567   }
568
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;
574   }
575
576   l->tx_buf_read += to_write;
577
578   GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
579
580   do {
581     res = write (l->fd_w.fd, out, to_write);
582     if (G_UNLIKELY (res < 0)) {
583       if (errno == EAGAIN || errno == EINTR)
584         continue;
585       /* Failed to write -> child died */
586       goto fail_and_cleanup;
587     }
588     to_write -= res;
589     out += res;
590   } while (to_write > 0);
591
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;
595   }
596
597   return TRUE;
598
599 fail_and_cleanup:
600   plugin_loader_cleanup_child (l);
601   return FALSE;
602 }
603
604 static gboolean
605 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
606 {
607   GstPlugin *newplugin;
608   GList *chunks = NULL;
609
610   GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
611
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));
616   }
617 #endif
618
619   newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
620   if (newplugin) {
621     guint hdr_pos;
622     guint offset;
623
624     /* Now serialise the plugin details and send */
625     if (!_priv_gst_registry_chunks_save_plugin (&chunks,
626             gst_registry_get_default (), newplugin))
627       goto fail;
628
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);
634
635     if (chunks) {
636       GList *walk;
637       for (walk = chunks; walk; walk = g_list_next (walk)) {
638         GstRegistryChunk *cur = walk->data;
639         put_chunk (l, cur, &offset);
640
641         if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
642           g_free (cur->data);
643         g_free (cur);
644       }
645
646       g_list_free (chunks);
647
648       /* Store the size of the written payload */
649       GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
650     }
651 #if 0                           /* Test code - corrupt the tx buffer based on filename */
652     if (strstr (filename, "sink") != NULL) {
653       int fd, res;
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);
657       close (fd);
658     }
659 #endif
660
661     gst_object_unref (newplugin);
662   } else {
663     put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
664   }
665
666   return TRUE;
667 fail:
668   put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
669   if (chunks) {
670     GList *walk;
671     for (walk = chunks; walk; walk = g_list_next (walk)) {
672       GstRegistryChunk *cur = walk->data;
673
674       if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
675         g_free (cur->data);
676       g_free (cur);
677     }
678
679     g_list_free (chunks);
680   }
681
682   return FALSE;
683 }
684
685 static gboolean
686 check_protocol_version (GstPluginLoader * l, guint8 * payload,
687     guint payload_len)
688 {
689   guint32 got_version;
690   guint8 *binary_reg_ver;
691
692   if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
693     return FALSE;
694
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)
699     return FALSE;
700
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);
705     return FALSE;
706   }
707
708   return TRUE;
709 };
710
711 static gboolean
712 handle_rx_packet (GstPluginLoader * l,
713     guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
714 {
715   gboolean res = TRUE;
716
717   switch (pack_type) {
718     case PACKET_EXIT:
719       gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
720       if (l->is_child) {
721         /* Respond, then we keep looping until the parent closes the fd */
722         put_packet (l, PACKET_EXIT, 0, NULL, 0);
723       } else {
724         l->rx_done = TRUE;      /* All done reading from child */
725       }
726       return TRUE;
727     case PACKET_LOAD_PLUGIN:{
728       if (!l->is_child)
729         return TRUE;
730
731       /* Payload is the filename to load */
732       res = do_plugin_load (l, (gchar *) payload, tag);
733
734       break;
735     }
736     case PACKET_PLUGIN_DETAILS:{
737       gchar *tmp = (gchar *) payload;
738       PendingPluginEntry *entry = NULL;
739       GList *cur;
740
741       GST_DEBUG_OBJECT (l->registry,
742           "Received plugin details from child w/ tag %u. %d bytes info",
743           tag, payload_len);
744
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;
749       while (cur) {
750         PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
751
752         if (e->tag > tag)
753           break;
754
755         if (e->tag == tag) {
756           entry = e;
757           break;
758         } else {
759           cur = g_list_delete_link (cur, cur);
760           g_free (e->filename);
761           g_free (e);
762         }
763       }
764
765       l->pending_plugins = cur;
766       if (cur == NULL)
767         l->pending_plugins_tail = NULL;
768
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);
776           return FALSE;
777         }
778
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;
784
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;
791       }
792
793       if (entry != NULL) {
794         g_free (entry->filename);
795         g_free (entry);
796       }
797
798       /* Remove the plugin entry we just loaded */
799       cur = l->pending_plugins;
800       if (cur != NULL)
801         cur = g_list_delete_link (cur, cur);
802       l->pending_plugins = cur;
803       if (cur == NULL)
804         l->pending_plugins_tail = NULL;
805
806       break;
807     }
808     case PACKET_SYNC:
809       if (l->is_child) {
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");
813       } else
814         l->rx_got_sync = TRUE;
815       break;
816     case PACKET_VERSION:
817       if (l->is_child) {
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);
828       } else {
829         res = check_protocol_version (l, payload, payload_len);
830       }
831       break;
832     default:
833       return FALSE;             /* Invalid packet -> something is wrong */
834   }
835
836   return res;
837 }
838
839 static gboolean
840 read_one (GstPluginLoader * l)
841 {
842   guint64 magic;
843   guint32 to_read, packet_len, tag;
844   guint8 *in;
845   gint res;
846
847   to_read = HEADER_SIZE;
848   in = l->rx_buf;
849   do {
850     res = read (l->fd_r.fd, in, to_read);
851     if (G_UNLIKELY (res < 0)) {
852       if (errno == EAGAIN || errno == EINTR)
853         continue;
854       GST_LOG ("Failed reading packet header");
855       return FALSE;
856     }
857     to_read -= res;
858     in += res;
859   } while (to_read > 0);
860
861   magic = GST_READ_UINT32_BE (l->rx_buf + 8);
862   if (magic != HEADER_MAGIC) {
863     GST_WARNING
864         ("Invalid packet (bad magic number) received from plugin scanner subprocess");
865     return FALSE;
866   }
867
868   packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
869   if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
870     GST_WARNING
871         ("Received excessively large packet for plugin scanner subprocess");
872     return FALSE;
873   }
874   tag = GST_READ_UINT24_BE (l->rx_buf + 1);
875
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);
882     }
883
884     in = l->rx_buf + HEADER_SIZE;
885     to_read = packet_len;
886     do {
887       res = read (l->fd_r.fd, in, to_read);
888       if (G_UNLIKELY (res < 0)) {
889         if (errno == EAGAIN || errno == EINTR)
890           continue;
891         GST_ERROR ("Packet payload read failed");
892         return FALSE;
893       }
894       to_read -= res;
895       in += res;
896     } while (to_read > 0);
897   } else {
898     GST_LOG ("No payload to read for 0 length packet type %d tag %u",
899         l->rx_buf[0], tag);
900   }
901
902   return handle_rx_packet (l, l->rx_buf[0], tag,
903       l->rx_buf + HEADER_SIZE, packet_len);
904 }
905
906 static gboolean
907 exchange_packets (GstPluginLoader * l)
908 {
909   gint res;
910
911   /* Wait for activity on our FDs */
912   do {
913     do {
914       res = gst_poll_wait (l->fdset, GST_SECOND);
915     } while (res == -1 && (errno == EINTR || errno == EAGAIN));
916
917     if (res < 0)
918       return FALSE;
919
920     GST_LOG ("Poll res = %d. %d bytes pending for write", res,
921         l->tx_buf_write - l->tx_buf_read);
922
923     if (!l->rx_done) {
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;
928       }
929
930       if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
931         if (!read_one (l))
932           goto fail_and_cleanup;
933       }
934     }
935
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;
941       }
942       if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
943         if (!write_one (l))
944           goto fail_and_cleanup;
945       }
946     }
947   } while (l->tx_buf_read < l->tx_buf_write);
948
949   return TRUE;
950 fail_and_cleanup:
951   plugin_loader_cleanup_child (l);
952   return FALSE;
953 }