Merge branch 'work'
[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, (char *) "-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   const gchar *env;
380   char *helper_bin;
381   gboolean res = FALSE;
382
383   if (loader->child_running)
384     return TRUE;
385
386   /* Find the gst-plugin-scanner: first try the env-var if it is set,
387    * otherwise use the installed version */
388   env = g_getenv ("GST_PLUGIN_SCANNER");
389
390   if (env != NULL && *env != '\0') {
391     GST_LOG ("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   }
396
397   if (!res) {
398     GST_LOG ("Trying installed plugin scanner");
399     helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
400     res = gst_plugin_loader_try_helper (loader, helper_bin);
401     g_free (helper_bin);
402
403     if (!res) {
404       GST_INFO ("No gst-plugin-scanner available, or not working");
405     }
406   }
407
408   return loader->child_running;
409 }
410
411 static void
412 plugin_loader_cleanup_child (GstPluginLoader * l)
413 {
414   if (!l->child_running || l->is_child)
415     return;
416
417   gst_poll_remove_fd (l->fdset, &l->fd_w);
418   gst_poll_remove_fd (l->fdset, &l->fd_r);
419
420   close (l->fd_w.fd);
421   close (l->fd_r.fd);
422
423 #ifndef G_OS_WIN32
424   GST_LOG ("waiting for child process to exit");
425   waitpid (l->child_pid, NULL, 0);
426 #else
427   g_warning ("FIXME: Implement child process shutdown for Win32");
428 #endif
429   g_spawn_close_pid (l->child_pid);
430
431   l->child_running = FALSE;
432 }
433
434 gboolean
435 _gst_plugin_loader_client_run (void)
436 {
437   GstPluginLoader *l;
438
439   l = plugin_loader_new (NULL);
440   if (l == NULL)
441     return FALSE;
442
443   /* On entry, the inward pipe is STDIN, and outward is STDOUT.
444    * Dup those somewhere better so that plugins printing things
445    * won't interfere with anything */
446 #ifndef G_OS_WIN32
447   {
448     int dup_fd;
449
450     dup_fd = dup (0);           /* STDIN */
451     if (dup_fd == -1) {
452       GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
453       return FALSE;
454     }
455     l->fd_r.fd = dup_fd;
456     close (0);
457
458     dup_fd = dup (1);           /* STDOUT */
459     if (dup_fd == -1) {
460       GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
461       return FALSE;
462     }
463     l->fd_w.fd = dup_fd;
464     close (1);
465
466     /* Dup stderr down to stdout so things that plugins print are visible,
467      * but don't care if it fails */
468     dup2 (2, 1);
469   }
470 #else
471   /* FIXME: Use DuplicateHandle and friends on win32 */
472   l->fd_w.fd = 1;               /* STDOUT */
473   l->fd_r.fd = 0;               /* STDIN */
474 #endif
475
476   gst_poll_add_fd (l->fdset, &l->fd_w);
477   gst_poll_add_fd (l->fdset, &l->fd_r);
478   gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
479
480   l->is_child = TRUE;
481
482   GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
483
484   /* Loop, listening for incoming packets on the fd and writing responses */
485   while (!l->rx_done && exchange_packets (l));
486
487   plugin_loader_free (l);
488
489   return TRUE;
490 }
491
492 static void
493 put_packet (GstPluginLoader * l, guint type, guint32 tag,
494     const guint8 * payload, guint32 payload_len)
495 {
496   guint8 *out;
497   guint len = payload_len + HEADER_SIZE;
498
499   if (l->tx_buf_write + len >= l->tx_buf_size) {
500     GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
501         l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
502     l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
503     l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
504   }
505
506   out = l->tx_buf + l->tx_buf_write;
507
508   /* one byte packet type */
509   out[0] = type;
510   /* 3 byte packet tag number */
511   GST_WRITE_UINT24_BE (out + 1, tag);
512   /* 4 bytes packet length */
513   GST_WRITE_UINT32_BE (out + 4, payload_len);
514   /* payload */
515   memcpy (out + HEADER_SIZE, payload, payload_len);
516   /* Write magic into the header */
517   GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
518
519   l->tx_buf_write += len;
520   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
521 }
522
523 static void
524 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
525 {
526   guint padsize = 0;
527   guint len;
528   guint8 *out;
529
530   /* Might need to align the chunk */
531   if (chunk->align && ((*pos) % ALIGNMENT) != 0)
532     padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
533
534   len = padsize + chunk->size;
535
536   if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
537     guint new_size = MAX (l->tx_buf_write + len,
538         l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
539     GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
540         l->tx_buf_size, new_size, chunk->size);
541     l->tx_buf_size = new_size;
542     l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
543   }
544
545   out = l->tx_buf + l->tx_buf_write;
546   /* Clear the padding */
547   if (padsize)
548     memset (out, 0, padsize);
549   memcpy (out + padsize, chunk->data, chunk->size);
550
551   l->tx_buf_write += len;
552   *pos += len;
553
554   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
555 };
556
557 static gboolean
558 write_one (GstPluginLoader * l)
559 {
560   guint8 *out;
561   guint32 to_write, magic;
562   int res;
563
564   if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
565     return FALSE;
566
567   out = l->tx_buf + l->tx_buf_read;
568
569   magic = GST_READ_UINT32_BE (out + 8);
570   if (magic != HEADER_MAGIC) {
571     GST_ERROR ("Packet magic number is missing. Memory corruption detected");
572     goto fail_and_cleanup;
573   }
574
575   to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
576   /* Check that the magic is intact, and the size is sensible */
577   if (to_write > l->tx_buf_size) {
578     GST_ERROR ("Indicated packet size is too large. Corruption detected");
579     goto fail_and_cleanup;
580   }
581
582   l->tx_buf_read += to_write;
583
584   GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
585
586   do {
587     res = write (l->fd_w.fd, out, to_write);
588     if (G_UNLIKELY (res < 0)) {
589       if (errno == EAGAIN || errno == EINTR)
590         continue;
591       /* Failed to write -> child died */
592       goto fail_and_cleanup;
593     }
594     to_write -= res;
595     out += res;
596   } while (to_write > 0);
597
598   if (l->tx_buf_read == l->tx_buf_write) {
599     gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
600     l->tx_buf_read = l->tx_buf_write = 0;
601   }
602
603   return TRUE;
604
605 fail_and_cleanup:
606   plugin_loader_cleanup_child (l);
607   return FALSE;
608 }
609
610 static gboolean
611 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
612 {
613   GstPlugin *newplugin;
614   GList *chunks = NULL;
615
616   GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
617
618 #if 0                           /* Test code - crash based on filename */
619   if (strstr (filename, "coreelements") == NULL) {
620     g_printerr ("Crashing on file %s\n", filename);
621     g_printerr ("%d", *(gint *) (NULL));
622   }
623 #endif
624
625   newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
626   if (newplugin) {
627     guint hdr_pos;
628     guint offset;
629
630     /* Now serialise the plugin details and send */
631     if (!_priv_gst_registry_chunks_save_plugin (&chunks,
632             gst_registry_get_default (), newplugin))
633       goto fail;
634
635     /* Store where the header is, write an empty one, then write
636      * all the payload chunks, then fix up the header size */
637     hdr_pos = l->tx_buf_write;
638     offset = HEADER_SIZE;
639     put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
640
641     if (chunks) {
642       GList *walk;
643       for (walk = chunks; walk; walk = g_list_next (walk)) {
644         GstRegistryChunk *cur = walk->data;
645         put_chunk (l, cur, &offset);
646
647         if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
648           g_free (cur->data);
649         g_free (cur);
650       }
651
652       g_list_free (chunks);
653
654       /* Store the size of the written payload */
655       GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
656     }
657 #if 0                           /* Test code - corrupt the tx buffer based on filename */
658     if (strstr (filename, "sink") != NULL) {
659       int fd, res;
660       g_printerr ("Corrupting tx buf on file %s\n", filename);
661       fd = open ("/dev/urandom", O_RDONLY);
662       res = read (fd, l->tx_buf, l->tx_buf_size);
663       close (fd);
664     }
665 #endif
666
667     gst_object_unref (newplugin);
668   } else {
669     put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
670   }
671
672   return TRUE;
673 fail:
674   put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
675   if (chunks) {
676     GList *walk;
677     for (walk = chunks; walk; walk = g_list_next (walk)) {
678       GstRegistryChunk *cur = walk->data;
679
680       if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
681         g_free (cur->data);
682       g_free (cur);
683     }
684
685     g_list_free (chunks);
686   }
687
688   return FALSE;
689 }
690
691 static gboolean
692 check_protocol_version (GstPluginLoader * l, guint8 * payload,
693     guint payload_len)
694 {
695   guint32 got_version;
696   guint8 *binary_reg_ver;
697
698   if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
699     return FALSE;
700
701   got_version = GST_READ_UINT32_BE (payload);
702   GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
703       loader_protocol_version);
704   if (got_version != loader_protocol_version)
705     return FALSE;
706
707   binary_reg_ver = payload + sizeof (guint32);
708   if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
709     GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
710         GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
711     return FALSE;
712   }
713
714   return TRUE;
715 };
716
717 static gboolean
718 handle_rx_packet (GstPluginLoader * l,
719     guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
720 {
721   gboolean res = TRUE;
722
723   switch (pack_type) {
724     case PACKET_EXIT:
725       gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
726       if (l->is_child) {
727         /* Respond, then we keep looping until the parent closes the fd */
728         put_packet (l, PACKET_EXIT, 0, NULL, 0);
729       } else {
730         l->rx_done = TRUE;      /* All done reading from child */
731       }
732       return TRUE;
733     case PACKET_LOAD_PLUGIN:{
734       if (!l->is_child)
735         return TRUE;
736
737       /* Payload is the filename to load */
738       res = do_plugin_load (l, (gchar *) payload, tag);
739
740       break;
741     }
742     case PACKET_PLUGIN_DETAILS:{
743       gchar *tmp = (gchar *) payload;
744       PendingPluginEntry *entry = NULL;
745       GList *cur;
746
747       GST_DEBUG_OBJECT (l->registry,
748           "Received plugin details from child w/ tag %u. %d bytes info",
749           tag, payload_len);
750
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;
755       while (cur) {
756         PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
757
758         if (e->tag > tag)
759           break;
760
761         if (e->tag == tag) {
762           entry = e;
763           break;
764         } else {
765           cur = g_list_delete_link (cur, cur);
766           g_free (e->filename);
767           g_free (e);
768         }
769       }
770
771       l->pending_plugins = cur;
772       if (cur == NULL)
773         l->pending_plugins_tail = NULL;
774
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);
782           return FALSE;
783         }
784
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;
790
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;
797       }
798
799       if (entry != NULL) {
800         g_free (entry->filename);
801         g_free (entry);
802       }
803
804       /* Remove the plugin entry we just loaded */
805       cur = l->pending_plugins;
806       if (cur != NULL)
807         cur = g_list_delete_link (cur, cur);
808       l->pending_plugins = cur;
809       if (cur == NULL)
810         l->pending_plugins_tail = NULL;
811
812       break;
813     }
814     case PACKET_SYNC:
815       if (l->is_child) {
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");
819       } else
820         l->rx_got_sync = TRUE;
821       break;
822     case PACKET_VERSION:
823       if (l->is_child) {
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);
834       } else {
835         res = check_protocol_version (l, payload, payload_len);
836       }
837       break;
838     default:
839       return FALSE;             /* Invalid packet -> something is wrong */
840   }
841
842   return res;
843 }
844
845 static gboolean
846 read_one (GstPluginLoader * l)
847 {
848   guint64 magic;
849   guint32 to_read, packet_len, tag;
850   guint8 *in;
851   gint res;
852
853   to_read = HEADER_SIZE;
854   in = l->rx_buf;
855   do {
856     res = read (l->fd_r.fd, in, to_read);
857     if (G_UNLIKELY (res < 0)) {
858       if (errno == EAGAIN || errno == EINTR)
859         continue;
860       GST_LOG ("Failed reading packet header");
861       return FALSE;
862     }
863     to_read -= res;
864     in += res;
865   } while (to_read > 0);
866
867   magic = GST_READ_UINT32_BE (l->rx_buf + 8);
868   if (magic != HEADER_MAGIC) {
869     GST_WARNING
870         ("Invalid packet (bad magic number) received from plugin scanner subprocess");
871     return FALSE;
872   }
873
874   packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
875   if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
876     GST_WARNING
877         ("Received excessively large packet for plugin scanner subprocess");
878     return FALSE;
879   }
880   tag = GST_READ_UINT24_BE (l->rx_buf + 1);
881
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);
888     }
889
890     in = l->rx_buf + HEADER_SIZE;
891     to_read = packet_len;
892     do {
893       res = read (l->fd_r.fd, in, to_read);
894       if (G_UNLIKELY (res < 0)) {
895         if (errno == EAGAIN || errno == EINTR)
896           continue;
897         GST_ERROR ("Packet payload read failed");
898         return FALSE;
899       }
900       to_read -= res;
901       in += res;
902     } while (to_read > 0);
903   } else {
904     GST_LOG ("No payload to read for 0 length packet type %d tag %u",
905         l->rx_buf[0], tag);
906   }
907
908   return handle_rx_packet (l, l->rx_buf[0], tag,
909       l->rx_buf + HEADER_SIZE, packet_len);
910 }
911
912 static gboolean
913 exchange_packets (GstPluginLoader * l)
914 {
915   gint res;
916
917   /* Wait for activity on our FDs */
918   do {
919     do {
920       res = gst_poll_wait (l->fdset, GST_SECOND);
921     } while (res == -1 && (errno == EINTR || errno == EAGAIN));
922
923     if (res < 0)
924       return FALSE;
925
926     GST_LOG ("Poll res = %d. %d bytes pending for write", res,
927         l->tx_buf_write - l->tx_buf_read);
928
929     if (!l->rx_done) {
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;
934       }
935
936       if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
937         if (!read_one (l))
938           goto fail_and_cleanup;
939       }
940     }
941
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;
947       }
948       if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
949         if (!write_one (l))
950           goto fail_and_cleanup;
951       }
952     }
953   } while (l->tx_buf_read < l->tx_buf_write);
954
955   return TRUE;
956 fail_and_cleanup:
957   plugin_loader_cleanup_child (l);
958   return FALSE;
959 }