registry: Also check the binary registry chunk version of the child.
[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 #ifndef G_OS_WIN32
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #endif
32 #include <errno.h>
33
34 #include <gst/gst_private.h>
35 #include <gst/gstconfig.h>
36
37 #include <gst/gstpoll.h>
38 #include <gst/gstutils.h>
39
40 #include <gst/gstpluginloader.h>
41 #include <gst/gstregistrychunks.h>
42 #include <gst/gstregistrybinary.h>
43
44 /* IMPORTANT: Bump the version number if the plugin loader packet protocol
45  * changes. Changes in the binary registry format are handled by bumps
46  * in the GST_MAGIC_BINARY_VERSION_STR as before */
47 static const guint32 loader_protocol_version = 2;
48
49 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
50
51 static GstPluginLoader *plugin_loader_new (GstRegistry * registry);
52 static gboolean plugin_loader_free (GstPluginLoader * loader);
53 static gboolean plugin_loader_load (GstPluginLoader * loader,
54     const gchar * filename, off_t file_size, time_t file_mtime);
55
56 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
57   plugin_loader_new, plugin_loader_free, plugin_loader_load
58 };
59
60 typedef struct _PendingPluginEntry
61 {
62   guint32 tag;
63   gchar *filename;
64   off_t file_size;
65   time_t file_mtime;
66 } PendingPluginEntry;
67
68 struct _GstPluginLoader
69 {
70   GstRegistry *registry;
71   GstPoll *fdset;
72
73   gboolean child_running;
74   GPid child_pid;
75   GstPollFD fd_w;
76   GstPollFD fd_r;
77
78   gboolean is_child;
79   gboolean got_plugin_details;
80
81   /* Transmit buffer */
82   guint8 *tx_buf;
83   guint tx_buf_size;
84   guint tx_buf_write;
85   guint tx_buf_read;
86
87   guint32 next_tag;
88
89   guint8 *rx_buf;
90   guint rx_buf_size;
91   gboolean rx_done;
92   gboolean rx_got_sync;
93
94   /* Head and tail of the pending plugins list. List of
95      PendingPluginEntry structs */
96   GList *pending_plugins;
97   GList *pending_plugins_tail;
98 };
99
100 #define PACKET_EXIT 1
101 #define PACKET_LOAD_PLUGIN 2
102 #define PACKET_SYNC 3
103 #define PACKET_PLUGIN_DETAILS 4
104 #define PACKET_VERSION 5
105
106 #define BUF_INIT_SIZE 512
107 #define BUF_GROW_EXTRA 512
108 #define HEADER_SIZE 8
109 #define ALIGNMENT   (sizeof (void *))
110
111 static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader);
112 static void put_packet (GstPluginLoader * loader, guint type, guint32 tag,
113     const guint8 * payload, guint32 payload_len);
114 static gboolean exchange_packets (GstPluginLoader * l);
115 static gboolean plugin_loader_replay_pending (GstPluginLoader * l);
116 static gboolean plugin_loader_load_and_sync (GstPluginLoader * l,
117     PendingPluginEntry * entry);
118 static void plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
119     PendingPluginEntry * entry);
120 static void plugin_loader_cleanup_child (GstPluginLoader * loader);
121 static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
122
123 static GstPluginLoader *
124 plugin_loader_new (GstRegistry * registry)
125 {
126   GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
127
128   if (registry)
129     l->registry = gst_object_ref (registry);
130   l->fdset = gst_poll_new (FALSE);
131   gst_poll_fd_init (&l->fd_w);
132   gst_poll_fd_init (&l->fd_r);
133
134   l->tx_buf_size = BUF_INIT_SIZE;
135   l->tx_buf = g_malloc (BUF_INIT_SIZE);
136
137   l->next_tag = 0;
138
139   l->rx_buf_size = BUF_INIT_SIZE;
140   l->rx_buf = g_malloc (BUF_INIT_SIZE);
141
142   return l;
143 }
144
145 static gboolean
146 plugin_loader_free (GstPluginLoader * loader)
147 {
148   GList *cur;
149   gboolean got_plugin_details;
150
151   fsync (loader->fd_w.fd);
152
153   if (loader->child_running) {
154     put_packet (loader, PACKET_EXIT, 0, NULL, 0);
155
156     /* Swap packets with the child until it exits cleanly */
157     while (!loader->rx_done) {
158       if (exchange_packets (loader) || loader->rx_done)
159         continue;
160
161       if (!plugin_loader_replay_pending (loader))
162         break;
163       put_packet (loader, PACKET_EXIT, 0, NULL, 0);
164     }
165
166     plugin_loader_cleanup_child (loader);
167   } else {
168     close (loader->fd_w.fd);
169     close (loader->fd_r.fd);
170   }
171
172   gst_poll_free (loader->fdset);
173
174   g_free (loader->rx_buf);
175   g_free (loader->tx_buf);
176
177   if (loader->registry)
178     gst_object_unref (loader->registry);
179
180   got_plugin_details = loader->got_plugin_details;
181
182   /* Free any pending plugin entries */
183   cur = loader->pending_plugins;
184   while (cur) {
185     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
186     g_free (entry->filename);
187     g_free (entry);
188
189     cur = g_list_delete_link (cur, cur);
190   }
191
192   g_free (loader);
193
194   return got_plugin_details;
195 }
196
197 static gboolean
198 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
199     off_t file_size, time_t file_mtime)
200 {
201   gint len;
202   PendingPluginEntry *entry;
203
204   if (!gst_plugin_loader_spawn (loader))
205     return FALSE;
206
207   /* Send a packet to the child requesting that it load the given file */
208   GST_LOG_OBJECT (loader->registry,
209       "Sending file %s to child. tag %u", filename, loader->next_tag);
210
211   entry = g_new (PendingPluginEntry, 1);
212   entry->tag = loader->next_tag++;
213   entry->filename = g_strdup (filename);
214   entry->file_size = file_size;
215   entry->file_mtime = file_mtime;
216   loader->pending_plugins_tail =
217       g_list_append (loader->pending_plugins_tail, entry);
218
219   if (loader->pending_plugins == NULL)
220     loader->pending_plugins = loader->pending_plugins_tail;
221   else
222     loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
223
224   len = strlen (filename);
225   put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
226       (guint8 *) filename, len + 1);
227
228   if (!exchange_packets (loader)) {
229     if (!plugin_loader_replay_pending (loader))
230       return FALSE;
231   }
232
233   return TRUE;
234 }
235
236 static gboolean
237 plugin_loader_replay_pending (GstPluginLoader * l)
238 {
239   GList *cur, *next;
240
241 restart:
242   if (!gst_plugin_loader_spawn (l))
243     return FALSE;
244
245   /* Load each plugin one by one synchronously until we find the
246    * crashing one */
247   while ((cur = l->pending_plugins)) {
248     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
249
250     if (!plugin_loader_load_and_sync (l, entry)) {
251       GST_INFO ("AHA! %s crashes on loading", entry->filename);
252       /* Create dummy plugin entry to block re-scanning this file */
253       plugin_loader_create_blacklist_plugin (l, entry);
254       l->got_plugin_details = TRUE;
255       /* Now remove this crashy plugin from the head of the list */
256       l->pending_plugins = g_list_delete_link (cur, cur);
257       if (l->pending_plugins == NULL)
258         l->pending_plugins_tail = NULL;
259       if (!gst_plugin_loader_spawn (l))
260         return FALSE;
261       break;
262     }
263   }
264
265   /* We exited after finding the crashing one. If there's any more pending,
266    * dispatch them post-haste, but don't wait */
267   for (cur = l->pending_plugins; cur != NULL; cur = next) {
268     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
269
270     next = g_list_next (cur);
271
272     put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
273         (guint8 *) entry->filename, strlen (entry->filename) + 1);
274
275     /* This might invalidate cur, which is why we grabbed 'next' above */
276     if (!exchange_packets (l))
277       goto restart;
278   }
279
280   return TRUE;
281 }
282
283 static gboolean
284 plugin_loader_sync_with_child (GstPluginLoader * l)
285 {
286   put_packet (l, PACKET_SYNC, 0, NULL, 0);
287
288   l->rx_got_sync = FALSE;
289   while (!l->rx_got_sync) {
290     if (!exchange_packets (l))
291       return FALSE;
292   }
293   return TRUE;
294 }
295
296 static gboolean
297 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
298 {
299   gint len;
300
301   GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
302       entry->filename);
303
304   len = strlen (entry->filename);
305   put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
306       (guint8 *) entry->filename, len + 1);
307
308   return plugin_loader_sync_with_child (l);
309 }
310
311 static void
312 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
313     PendingPluginEntry * entry)
314 {
315   GstPlugin *plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
316
317   plugin->filename = g_strdup (entry->filename);
318   plugin->file_mtime = entry->file_mtime;
319   plugin->file_size = entry->file_size;
320   plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
321
322   plugin->basename = g_path_get_basename (plugin->filename);
323   plugin->desc.name = g_intern_string (plugin->basename);
324   plugin->desc.description = g_strdup_printf ("Plugin for blacklisted file");
325   plugin->desc.version = g_intern_string ("0.0.0");
326   plugin->desc.license = g_intern_string ("BLACKLIST");
327   plugin->desc.source = plugin->desc.license;
328   plugin->desc.package = plugin->desc.license;
329   plugin->desc.origin = plugin->desc.license;
330
331   GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
332   gst_registry_add_plugin (l->registry, plugin);
333 }
334
335 static gboolean
336 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
337 {
338   char *argv[] = { location, "-l", NULL };
339
340   GST_LOG ("Trying to spawn plugin-scanner helper at %s", location);
341   if (!g_spawn_async_with_pipes (NULL, argv, NULL,
342           G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
343           NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
344           NULL, NULL))
345     return FALSE;
346
347   gst_poll_add_fd (loader->fdset, &loader->fd_w);
348   gst_poll_add_fd (loader->fdset, &loader->fd_r);
349
350   gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
351
352   loader->tx_buf_write = loader->tx_buf_read = 0;
353
354   put_packet (loader, PACKET_VERSION, 0, NULL, 0);
355   if (!plugin_loader_sync_with_child (loader))
356     return FALSE;
357
358   loader->child_running = TRUE;
359
360   return TRUE;
361 }
362
363 static gboolean
364 gst_plugin_loader_spawn (GstPluginLoader * loader)
365 {
366   char *helper_bin;
367   gboolean res;
368
369   if (loader->child_running)
370     return TRUE;
371
372   /* Find the plugin-scanner, first try installed then by env-var */
373   helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
374   res = gst_plugin_loader_try_helper (loader, helper_bin);
375   g_free (helper_bin);
376
377   if (!res) {
378     /* Try the GST_PLUGIN_SCANNER env var */
379     const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
380     if (env != NULL) {
381       GST_LOG ("Installed plugin scanner failed. "
382           "Trying GST_PLUGIN_SCANNER env var: %s", env);
383       helper_bin = g_strdup (env);
384       res = gst_plugin_loader_try_helper (loader, helper_bin);
385       g_free (helper_bin);
386     } else {
387       GST_LOG ("Installed plugin scanner failed and "
388           "GST_PLUGIN_SCANNER env var not set. No plugin-scanner available");
389     }
390   }
391
392   return loader->child_running;
393 }
394
395 static void
396 plugin_loader_cleanup_child (GstPluginLoader * l)
397 {
398   if (!l->child_running || l->is_child)
399     return;
400
401   gst_poll_remove_fd (l->fdset, &l->fd_w);
402   gst_poll_remove_fd (l->fdset, &l->fd_r);
403
404   close (l->fd_w.fd);
405   close (l->fd_r.fd);
406
407 #ifndef G_OS_WIN32
408   GST_LOG ("waiting for child process to exit");
409   waitpid (l->child_pid, NULL, 0);
410 #else
411   g_warning ("FIXME: Implement child process shutdown for Win32");
412 #endif
413   g_spawn_close_pid (l->child_pid);
414
415   l->child_running = FALSE;
416 }
417
418 gboolean
419 _gst_plugin_loader_client_run ()
420 {
421   GstPluginLoader *l;
422
423   l = plugin_loader_new (NULL);
424   if (l == NULL)
425     return FALSE;
426
427   l->fd_w.fd = 1;               /* STDOUT */
428   gst_poll_add_fd (l->fdset, &l->fd_w);
429
430   l->fd_r.fd = 0;               /* STDIN */
431   gst_poll_add_fd (l->fdset, &l->fd_r);
432   gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
433
434   l->is_child = TRUE;
435
436   GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
437
438   /* Loop, listening for incoming packets on the fd and writing responses */
439   while (!l->rx_done && exchange_packets (l));
440
441   plugin_loader_free (l);
442
443   return TRUE;
444 }
445
446 static void
447 put_packet (GstPluginLoader * l, guint type, guint32 tag,
448     const guint8 * payload, guint32 payload_len)
449 {
450   guint8 *out;
451   guint len = payload_len + HEADER_SIZE;
452
453   if (l->tx_buf_write + len >= l->tx_buf_size) {
454     l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
455     l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
456   }
457
458   out = l->tx_buf + l->tx_buf_write;
459
460   out[0] = type;
461   GST_WRITE_UINT24_BE (out + 1, tag);
462   GST_WRITE_UINT32_BE (out + 4, payload_len);
463   memcpy (out + HEADER_SIZE, payload, payload_len);
464
465   l->tx_buf_write += len;
466   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
467 }
468
469 static void
470 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
471 {
472   guint padsize = 0;
473   guint len;
474   guint8 *out;
475
476   /* Might need to align the chunk */
477   if (chunk->align && ((*pos) % ALIGNMENT) != 0)
478     padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
479
480   len = padsize + chunk->size;
481
482   if (l->tx_buf_write + len >= l->tx_buf_size) {
483     l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
484     l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
485   }
486
487   out = l->tx_buf + l->tx_buf_write;
488   memcpy (out + padsize, chunk->data, chunk->size);
489
490   l->tx_buf_write += len;
491   *pos += len;
492
493   gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
494 };
495
496 static gboolean
497 write_one (GstPluginLoader * l)
498 {
499   guint8 *out;
500   guint32 to_write;
501   int res;
502
503   if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
504     return FALSE;
505
506   out = l->tx_buf + l->tx_buf_read;
507   to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
508   l->tx_buf_read += to_write;
509
510   GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
511
512   do {
513     res = write (l->fd_w.fd, out, to_write);
514     if (res > 0) {
515       to_write -= res;
516       out += res;
517     }
518   } while (to_write > 0 && res < 0 && (errno == EAGAIN || errno == EINTR));
519   if (res < 0) {
520     /* Failed to write -> child died */
521     plugin_loader_cleanup_child (l);
522     return FALSE;
523   }
524
525   if (l->tx_buf_read == l->tx_buf_write) {
526     gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
527     l->tx_buf_read = l->tx_buf_write = 0;
528   }
529   return TRUE;
530 }
531
532 static gboolean
533 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
534 {
535   GstPlugin *newplugin;
536   GList *chunks = NULL;
537
538   GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
539
540 #if 0                           /* Test code - crash based on an env var */
541   if (strstr (filename, "coreelements") == NULL) {
542     g_printerr ("Crashing on file %s\n", filename);
543     g_printerr ("%d", *(gint *) (NULL));
544   }
545 #endif
546
547
548   newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
549   if (newplugin) {
550     guint hdr_pos;
551     guint offset;
552
553     /* Now serialise the plugin details and send */
554     if (!_priv_gst_registry_chunks_save_plugin (&chunks,
555             gst_registry_get_default (), newplugin))
556       goto fail;
557
558     /* Store where the header is, write an empty one, then write
559      * all the payload chunks, then fix up the header size */
560     hdr_pos = l->tx_buf_write;
561     offset = HEADER_SIZE;
562     put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
563
564     if (chunks) {
565       GList *walk;
566       for (walk = chunks; walk; walk = g_list_next (walk)) {
567         GstRegistryChunk *cur = walk->data;
568         put_chunk (l, cur, &offset);
569
570         if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
571           g_free (cur->data);
572         g_free (cur);
573       }
574
575       g_list_free (chunks);
576
577       /* Store the size of the written payload */
578       GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
579     }
580
581     gst_object_unref (newplugin);
582   } else {
583     put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
584   }
585
586   return TRUE;
587 fail:
588   put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
589   if (chunks) {
590     GList *walk;
591     for (walk = chunks; walk; walk = g_list_next (walk)) {
592       GstRegistryChunk *cur = walk->data;
593
594       if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
595         g_free (cur->data);
596       g_free (cur);
597     }
598
599     g_list_free (chunks);
600   }
601
602   return FALSE;
603 }
604
605 static gboolean
606 check_protocol_version (GstPluginLoader * l, guint8 * payload,
607     guint payload_len)
608 {
609   guint32 got_version;
610   guint8 *binary_reg_ver;
611
612   if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
613     return FALSE;
614
615   got_version = GST_READ_UINT32_BE (payload);
616   GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
617       loader_protocol_version);
618   if (got_version != loader_protocol_version)
619     return FALSE;
620
621   binary_reg_ver = payload + sizeof (guint32);
622   if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
623     GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
624         GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
625     return FALSE;
626   }
627
628   return TRUE;
629 };
630
631 static gboolean
632 handle_rx_packet (GstPluginLoader * l,
633     guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
634 {
635   gboolean res = TRUE;
636
637   switch (pack_type) {
638     case PACKET_EXIT:
639       gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
640       if (l->is_child) {
641         /* Respond, then we keep looping until the parent closes the fd */
642         put_packet (l, PACKET_EXIT, 0, NULL, 0);
643       } else {
644         l->rx_done = TRUE;      /* All done reading from child */
645       }
646       return TRUE;
647     case PACKET_LOAD_PLUGIN:{
648       if (!l->is_child)
649         return TRUE;
650
651       /* Payload is the filename to load */
652       res = do_plugin_load (l, (gchar *) payload, tag);
653
654       break;
655     }
656     case PACKET_PLUGIN_DETAILS:{
657       gchar *tmp = (gchar *) payload;
658       PendingPluginEntry *entry = NULL;
659       GList *cur;
660
661       GST_DEBUG_OBJECT (l->registry,
662           "Received plugin details from child w/ tag %u. %d bytes info",
663           tag, payload_len);
664
665       /* Assume that tagged details come back in the order
666        * we requested, and delete anything before this one */
667       cur = l->pending_plugins;
668       while (cur) {
669         PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
670
671         if (e->tag > tag)
672           break;
673
674         cur = g_list_delete_link (cur, cur);
675
676         if (e->tag == tag) {
677           entry = e;
678           break;
679         } else {
680           g_free (e->filename);
681           g_free (e);
682         }
683       }
684       l->pending_plugins = cur;
685       if (cur == NULL)
686         l->pending_plugins_tail = NULL;
687
688       if (payload_len > 0) {
689         GstPlugin *newplugin;
690         _priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
691             tmp + payload_len, &newplugin);
692         newplugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
693         GST_LOG_OBJECT (l->registry,
694             "marking plugin %p as registered as %s", newplugin,
695             newplugin->filename);
696         newplugin->registered = TRUE;
697
698         /* We got a set of plugin details - remember it for later */
699         l->got_plugin_details = TRUE;
700       } else if (entry != NULL) {
701         /* Create a blacklist entry for this file to prevent scanning every time */
702         plugin_loader_create_blacklist_plugin (l, entry);
703         l->got_plugin_details = TRUE;
704       }
705
706       if (entry != NULL) {
707         g_free (entry->filename);
708         g_free (entry);
709       }
710
711       break;
712     }
713     case PACKET_SYNC:
714       if (l->is_child) {
715         /* Respond with our reply - also a sync */
716         put_packet (l, PACKET_SYNC, tag, NULL, 0);
717         GST_LOG ("Got SYNC in child - replying");
718       } else
719         l->rx_got_sync = TRUE;
720       break;
721     case PACKET_VERSION:
722       if (l->is_child) {
723         /* Respond with our reply - a version packet, with the version */
724         const gint version_len =
725             sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN;
726         guint8 version_info[sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN];
727         memset (version_info, 0, version_len);
728         GST_WRITE_UINT32_BE (version_info, loader_protocol_version);
729         memcpy (version_info + sizeof (guint32), GST_MAGIC_BINARY_VERSION_STR,
730             strlen (GST_MAGIC_BINARY_VERSION_STR));
731         put_packet (l, PACKET_VERSION, tag, version_info, version_len);
732         GST_LOG ("Got VERSION in child - replying %u", loader_protocol_version);
733       } else {
734         res = check_protocol_version (l, payload, payload_len);
735       }
736       break;
737     default:
738       return FALSE;             /* Invalid packet -> something is wrong */
739   }
740
741   return res;
742 }
743
744 static gboolean
745 read_one (GstPluginLoader * l)
746 {
747   guint32 to_read, packet_len, tag;
748   guint8 *in;
749   gint res;
750
751   to_read = HEADER_SIZE;
752   in = l->rx_buf;
753   do {
754     res = read (l->fd_r.fd, in, to_read);
755     if (res > 0) {
756       to_read -= res;
757       in += res;
758     }
759   } while (to_read > 0 && res < 0 && (errno == EAGAIN || errno == EINTR));
760
761   if (res < 0) {
762     GST_LOG ("Failed reading packet header");
763     return FALSE;
764   }
765
766   packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
767
768   if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
769     l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
770     l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
771   }
772
773   in = l->rx_buf + HEADER_SIZE;
774   to_read = packet_len;
775   do {
776     res = read (l->fd_r.fd, in, to_read);
777     if (res > 0) {
778       to_read -= res;
779       in += res;
780     }
781   } while (to_read > 0 && res < 0 && (errno == EAGAIN || errno == EINTR));
782
783   if (res < 0) {
784     GST_ERROR ("Packet payload read failed");
785     return FALSE;
786   }
787
788   tag = GST_READ_UINT24_BE (l->rx_buf + 1);
789
790   return handle_rx_packet (l, l->rx_buf[0], tag,
791       l->rx_buf + HEADER_SIZE, packet_len);
792 }
793
794 static gboolean
795 exchange_packets (GstPluginLoader * l)
796 {
797   gint res;
798
799   /* Wait for activity on our FDs */
800   do {
801     do {
802       res = gst_poll_wait (l->fdset, GST_CLOCK_TIME_NONE);
803     } while (res == -1 && (errno == EINTR || errno == EAGAIN));
804
805     if (res < 0)
806       return FALSE;
807
808     GST_LOG ("Poll res = %d. %d bytes pending for write", res,
809         l->tx_buf_write - l->tx_buf_read);
810
811     if (!l->rx_done) {
812       if (gst_poll_fd_has_error (l->fdset, &l->fd_r) ||
813           gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
814         GST_LOG ("read fd %d closed/errored", l->fd_r.fd);
815         plugin_loader_cleanup_child (l);
816         return FALSE;
817       }
818
819       if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
820         if (!read_one (l))
821           return FALSE;
822       }
823     }
824
825     if (l->tx_buf_read < l->tx_buf_write) {
826       if (gst_poll_fd_has_error (l->fdset, &l->fd_w) ||
827           gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
828         GST_ERROR ("write fd %d closed/errored", l->fd_w.fd);
829         plugin_loader_cleanup_child (l);
830         return FALSE;
831       }
832       if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
833         if (!write_one (l))
834           return FALSE;
835       }
836     }
837   } while (l->tx_buf_read < l->tx_buf_write);
838
839   return TRUE;
840 }