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