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