2 * Copyright (C) 2008 Jan Schmidt <jan.schmidt@sun.com>
4 * gstpluginloader.c: GstPluginLoader helper for loading plugin files
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.
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.
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
27 #include <gst/gst_private.h>
30 #include <sys/types.h>
36 #define WIN32_LEAN_AND_MEAN
38 #define fsync(fd) _commit(fd)
42 extern HMODULE _priv_gst_dll_handle;
45 #ifdef HAVE_SYS_UTSNAME_H
46 #include <sys/utsname.h>
51 #include <gst/gstconfig.h>
53 #include <gst/gstpoll.h>
54 #include <gst/gstutils.h>
56 #include <gst/gstpluginloader.h>
57 #include <gst/gstregistrychunks.h>
58 #include <gst/gstregistrybinary.h>
60 /* IMPORTANT: Bump the version number if the plugin loader packet protocol
61 * changes. Changes in the binary registry format itself are handled by
62 * bumping the GST_MAGIC_BINARY_VERSION_STR
64 static const guint32 loader_protocol_version = 3;
66 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
68 static GstPluginLoader *plugin_loader_new (GstRegistry * registry);
69 static gboolean plugin_loader_free (GstPluginLoader * loader);
70 static gboolean plugin_loader_load (GstPluginLoader * loader,
71 const gchar * filename, off_t file_size, time_t file_mtime);
73 /* functions used in GstRegistry scanning */
74 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
75 plugin_loader_new, plugin_loader_free, plugin_loader_load
78 typedef struct _PendingPluginEntry
87 struct _GstPluginLoader
89 GstRegistry *registry;
92 gboolean child_running;
98 gboolean got_plugin_details;
100 /* Transmit buffer */
106 /* next sequence number (for PendingPluginEntry) */
112 gboolean rx_got_sync;
114 /* Head and tail of the pending plugins list. List of
115 PendingPluginEntry structs */
116 GList *pending_plugins;
117 GList *pending_plugins_tail;
120 #define PACKET_EXIT 1
121 #define PACKET_LOAD_PLUGIN 2
122 #define PACKET_SYNC 3
123 #define PACKET_PLUGIN_DETAILS 4
124 #define PACKET_VERSION 5
126 #define BUF_INIT_SIZE 512
127 #define BUF_GROW_EXTRA 512
128 #define BUF_MAX_SIZE (32 * 1024 * 1024)
130 #define HEADER_SIZE 12
131 /* 4 magic hex bytes to mark each packet */
132 #define HEADER_MAGIC 0xbefec0ae
133 #define ALIGNMENT (sizeof (void *))
135 static gboolean gst_plugin_loader_spawn (GstPluginLoader * loader);
136 static void put_packet (GstPluginLoader * loader, guint type, guint32 tag,
137 const guint8 * payload, guint32 payload_len);
138 static gboolean exchange_packets (GstPluginLoader * l);
139 static gboolean plugin_loader_replay_pending (GstPluginLoader * l);
140 static gboolean plugin_loader_load_and_sync (GstPluginLoader * l,
141 PendingPluginEntry * entry);
142 static void plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
143 PendingPluginEntry * entry);
144 static void plugin_loader_cleanup_child (GstPluginLoader * loader);
145 static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
147 static GstPluginLoader *
148 plugin_loader_new (GstRegistry * registry)
150 GstPluginLoader *l = g_slice_new0 (GstPluginLoader);
153 l->registry = gst_object_ref (registry);
154 l->fdset = gst_poll_new (FALSE);
155 gst_poll_fd_init (&l->fd_w);
156 gst_poll_fd_init (&l->fd_r);
158 l->tx_buf_size = BUF_INIT_SIZE;
159 l->tx_buf = g_malloc (BUF_INIT_SIZE);
163 l->rx_buf_size = BUF_INIT_SIZE;
164 l->rx_buf = g_malloc (BUF_INIT_SIZE);
170 plugin_loader_free (GstPluginLoader * loader)
173 gboolean got_plugin_details;
177 fsync_ret = fsync (loader->fd_w.fd);
178 } while (fsync_ret < 0 && errno == EINTR);
180 if (loader->child_running) {
181 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
183 /* Swap packets with the child until it exits cleanly */
184 while (!loader->rx_done) {
185 if (exchange_packets (loader) || loader->rx_done)
188 if (!plugin_loader_replay_pending (loader))
190 put_packet (loader, PACKET_EXIT, 0, NULL, 0);
193 plugin_loader_cleanup_child (loader);
195 close (loader->fd_w.fd);
196 close (loader->fd_r.fd);
199 gst_poll_free (loader->fdset);
201 g_free (loader->rx_buf);
202 g_free (loader->tx_buf);
204 if (loader->registry)
205 gst_object_unref (loader->registry);
207 got_plugin_details = loader->got_plugin_details;
209 /* Free any pending plugin entries */
210 cur = loader->pending_plugins;
212 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
213 g_free (entry->filename);
214 g_slice_free (PendingPluginEntry, entry);
216 cur = g_list_delete_link (cur, cur);
219 g_slice_free (GstPluginLoader, loader);
221 return got_plugin_details;
225 plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
226 off_t file_size, time_t file_mtime)
229 PendingPluginEntry *entry;
231 if (!gst_plugin_loader_spawn (loader))
234 /* Send a packet to the child requesting that it load the given file */
235 GST_LOG_OBJECT (loader->registry,
236 "Sending file %s to child. tag %u", filename, loader->next_tag);
238 entry = g_slice_new (PendingPluginEntry);
239 entry->tag = loader->next_tag++;
240 entry->filename = g_strdup (filename);
241 entry->file_size = file_size;
242 entry->file_mtime = file_mtime;
243 loader->pending_plugins_tail =
244 g_list_append (loader->pending_plugins_tail, entry);
246 if (loader->pending_plugins == NULL)
247 loader->pending_plugins = loader->pending_plugins_tail;
249 loader->pending_plugins_tail = g_list_next (loader->pending_plugins_tail);
251 len = strlen (filename);
252 put_packet (loader, PACKET_LOAD_PLUGIN, entry->tag,
253 (guint8 *) filename, len + 1);
255 if (!exchange_packets (loader)) {
256 if (!plugin_loader_replay_pending (loader))
264 plugin_loader_replay_pending (GstPluginLoader * l)
269 if (!gst_plugin_loader_spawn (l))
272 /* Load each plugin one by one synchronously until we find the
274 while ((cur = l->pending_plugins)) {
275 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
277 if (!plugin_loader_load_and_sync (l, entry)) {
278 /* Create dummy plugin entry to block re-scanning this file */
279 GST_ERROR ("Plugin file %s failed to load. Blacklisting",
281 plugin_loader_create_blacklist_plugin (l, entry);
282 l->got_plugin_details = TRUE;
283 /* Now remove this crashy plugin from the head of the list */
284 l->pending_plugins = g_list_delete_link (cur, cur);
285 g_free (entry->filename);
286 g_slice_free (PendingPluginEntry, entry);
287 if (l->pending_plugins == NULL)
288 l->pending_plugins_tail = NULL;
289 if (!gst_plugin_loader_spawn (l))
295 /* We exited after finding the crashing one. If there's any more pending,
296 * dispatch them post-haste, but don't wait */
297 for (cur = l->pending_plugins; cur != NULL; cur = next) {
298 PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
300 next = g_list_next (cur);
302 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
303 (guint8 *) entry->filename, strlen (entry->filename) + 1);
305 /* This might invalidate cur, which is why we grabbed 'next' above */
306 if (!exchange_packets (l))
314 plugin_loader_sync_with_child (GstPluginLoader * l)
316 put_packet (l, PACKET_SYNC, 0, NULL, 0);
318 l->rx_got_sync = FALSE;
319 while (!l->rx_got_sync) {
320 if (!exchange_packets (l))
327 plugin_loader_load_and_sync (GstPluginLoader * l, PendingPluginEntry * entry)
331 GST_DEBUG_OBJECT (l->registry, "Synchronously loading plugin file %s",
334 len = strlen (entry->filename);
335 put_packet (l, PACKET_LOAD_PLUGIN, entry->tag,
336 (guint8 *) entry->filename, len + 1);
338 return plugin_loader_sync_with_child (l);
342 plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
343 PendingPluginEntry * entry)
345 GstPlugin *plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
347 plugin->filename = g_strdup (entry->filename);
348 plugin->file_mtime = entry->file_mtime;
349 plugin->file_size = entry->file_size;
350 GST_OBJECT_FLAG_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED);
352 plugin->basename = g_path_get_basename (plugin->filename);
353 plugin->desc.name = g_intern_string (plugin->basename);
354 plugin->desc.description = "Plugin for blacklisted file";
355 plugin->desc.version = "0.0.0";
356 plugin->desc.license = "BLACKLIST";
357 plugin->desc.source = plugin->desc.license;
358 plugin->desc.package = plugin->desc.license;
359 plugin->desc.origin = plugin->desc.license;
361 GST_DEBUG ("Adding blacklist plugin '%s'", plugin->desc.name);
362 gst_registry_add_plugin (l->registry, plugin);
366 #if defined(__x86_64__)
367 #define USR_BIN_ARCH_SWITCH "-x86_64"
368 #elif defined(__i386__)
369 #define USR_BIN_ARCH_SWITCH "-i386"
370 #elif defined(__ppc__)
371 #define USR_BIN_ARCH_SWITCH "-ppc"
372 #elif defined(__ppc64__)
373 #define USR_BIN_ARCH_SWITCH "-ppc64"
377 #define YES_MULTIARCH 1
378 #define NO_MULTIARCH 2
380 #if defined (__APPLE__) && defined (USR_BIN_ARCH_SWITCH)
382 gst_plugin_loader_use_usr_bin_arch (void)
384 static gsize multiarch = 0;
386 if (g_once_init_enter (&multiarch)) {
387 gsize res = NO_MULTIARCH;
389 #ifdef HAVE_SYS_UTSNAME_H
391 struct utsname uname_data;
393 if (uname (&uname_data) == 0) {
394 /* Check for OS X >= 10.5 (darwin kernel 9.0) */
395 GST_LOG ("%s %s", uname_data.sysname, uname_data.release);
396 if (g_ascii_strcasecmp (uname_data.sysname, "Darwin") == 0 &&
397 g_strtod (uname_data.release, NULL) >= 9.0) {
404 GST_INFO ("multiarch: %s", (res == YES_MULTIARCH) ? "yes" : "no");
405 g_once_init_leave (&multiarch, res);
407 return (multiarch == YES_MULTIARCH);
409 #endif /* __APPLE__ && USR_BIN_ARCH_SWITCH */
412 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
414 char *argv[6] = { NULL, };
416 GError *error = NULL;
418 #if defined (__APPLE__) && defined (USR_BIN_ARCH_SWITCH)
419 if (gst_plugin_loader_use_usr_bin_arch ()) {
420 argv[c++] = (char *) "/usr/bin/arch";
421 argv[c++] = (char *) USR_BIN_ARCH_SWITCH;
424 argv[c++] = location;
425 argv[c++] = (char *) "-l";
426 argv[c++] = _gst_executable_path;
430 GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s with arch %s",
433 GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
436 g_spawn_async_with_pipes (NULL, argv, NULL,
437 G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
438 NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
442 GST_ERROR ("Spawning gst-plugin-scanner helper failed: %s", error->message);
443 g_clear_error (&error);
447 gst_poll_add_fd (loader->fdset, &loader->fd_w);
448 gst_poll_add_fd (loader->fdset, &loader->fd_r);
450 gst_poll_fd_ctl_read (loader->fdset, &loader->fd_r, TRUE);
452 loader->tx_buf_write = loader->tx_buf_read = 0;
454 put_packet (loader, PACKET_VERSION, 0, NULL, 0);
455 if (!plugin_loader_sync_with_child (loader))
458 loader->child_running = TRUE;
464 count_directories (const char *filepath)
470 g_return_val_if_fail (!g_path_is_absolute (filepath), 0);
472 tmp = g_strdup (filepath);
475 #if defined(G_OS_WIN32)
476 /* ignore UNC share paths entirely */
477 if (len >= 3 && G_IS_DIR_SEPARATOR (tmp[0]) && G_IS_DIR_SEPARATOR (tmp[1])
478 && !G_IS_DIR_SEPARATOR (tmp[2])) {
479 GST_WARNING ("found a UNC share path, ignoring");
484 /* remove trailing slashes if they exist */
486 #if defined(G_OS_WIN32)
487 /* don't remove the trailing slash for C:\.
488 * UNC paths are at least \\s\s */
491 /* don't remove the trailing slash for / */
494 && G_IS_DIR_SEPARATOR (tmp[len - 1])) {
500 char *dirname, *basename;
503 if (g_strcmp0 (tmp, ".") == 0)
505 if (g_strcmp0 (tmp, "/") == 0)
507 #if defined(G_OS_WIN32)
508 /* g_path_get_dirname() may return something of the form 'C:.', where C is
510 if (len == 3 && g_ascii_isalpha (tmp[0]) && tmp[1] == ':' && tmp[2] == '.')
514 basename = g_path_get_basename (tmp);
515 dirname = g_path_get_dirname (tmp);
517 if (g_strcmp0 (basename, "..") == 0) {
519 } else if (g_strcmp0 (basename, ".") == 0) {
525 g_clear_pointer (&basename, g_free);
526 g_clear_pointer (&tmp, g_free);
530 g_clear_pointer (&tmp, g_free);
533 g_critical ("path counting resulted in a negative directory count!");
541 gst_plugin_loader_spawn (GstPluginLoader * loader)
545 gboolean res = FALSE;
547 if (loader->child_running)
550 /* Find the gst-plugin-scanner */
551 env = g_getenv ("GST_PLUGIN_SCANNER_1_0");
553 env = g_getenv ("GST_PLUGIN_SCANNER");
555 if (env != NULL && *env != '\0') {
556 /* use the env-var if it is set */
557 GST_LOG ("Trying GST_PLUGIN_SCANNER env var: %s", env);
558 helper_bin = g_strdup (env);
559 res = gst_plugin_loader_try_helper (loader, helper_bin);
562 char *relocated_libgstreamer;
564 /* use the installed version */
565 GST_LOG ("Trying installed plugin scanner");
568 #define EXESUFFIX ".exe"
573 #define MAX_PATH_DEPTH 64
575 relocated_libgstreamer = priv_gst_get_relocated_libgstreamer ();
576 if (relocated_libgstreamer) {
577 int plugin_subdir_depth = count_directories (GST_PLUGIN_SUBDIR);
579 GST_DEBUG ("found libgstreamer-" GST_API_VERSION " library "
580 "at %s", relocated_libgstreamer);
582 if (plugin_subdir_depth < MAX_PATH_DEPTH) {
583 const char *filenamev[MAX_PATH_DEPTH + 5];
586 filenamev[i++] = relocated_libgstreamer;
587 for (j = 0; j < plugin_subdir_depth; j++)
588 filenamev[i++] = "..";
589 filenamev[i++] = GST_PLUGIN_SCANNER_SUBDIR;
590 filenamev[i++] = "gstreamer-" GST_API_VERSION;
591 filenamev[i++] = "gst-plugin-scanner" EXESUFFIX;
592 filenamev[i++] = NULL;
593 g_assert (i <= MAX_PATH_DEPTH + 5);
595 GST_DEBUG ("constructing path to system plugin scanner using "
596 "plugin dir: \'%s\', plugin scanner dir: \'%s\'",
597 GST_PLUGIN_SUBDIR, GST_PLUGIN_SCANNER_SUBDIR);
599 helper_bin = g_build_filenamev ((char **) filenamev);
601 GST_WARNING ("GST_PLUGIN_SUBDIR: \'%s\' has too many path segments",
603 helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
606 helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
609 GST_DEBUG ("using system plugin scanner at %s", helper_bin);
611 res = gst_plugin_loader_try_helper (loader, helper_bin);
613 g_free (relocated_libgstreamer);
617 GST_INFO ("No gst-plugin-scanner available, or not working");
620 return loader->child_running;
624 plugin_loader_cleanup_child (GstPluginLoader * l)
626 if (!l->child_running || l->is_child)
629 gst_poll_remove_fd (l->fdset, &l->fd_w);
630 gst_poll_remove_fd (l->fdset, &l->fd_r);
636 GST_LOG ("waiting for child process to exit");
637 waitpid (l->child_pid, NULL, 0);
639 g_warning ("FIXME: Implement child process shutdown for Win32");
641 g_spawn_close_pid (l->child_pid);
643 l->child_running = FALSE;
647 _gst_plugin_loader_client_run (void)
652 l = plugin_loader_new (NULL);
656 /* On entry, the inward pipe is STDIN, and outward is STDOUT.
657 * Dup those somewhere better so that plugins printing things
658 * won't interfere with anything */
663 dup_fd = dup (0); /* STDIN */
665 GST_ERROR ("Failed to start. Could not dup STDIN, errno %d", errno);
672 dup_fd = dup (1); /* STDOUT */
674 GST_ERROR ("Failed to start. Could not dup STDOUT, errno %d", errno);
681 /* Dup stderr down to stdout so things that plugins print are visible,
682 * but don't care if it fails */
686 /* FIXME: Use DuplicateHandle and friends on win32 */
687 l->fd_w.fd = 1; /* STDOUT */
688 l->fd_r.fd = 0; /* STDIN */
691 gst_poll_add_fd (l->fdset, &l->fd_w);
692 gst_poll_add_fd (l->fdset, &l->fd_r);
693 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, TRUE);
697 GST_DEBUG ("Plugin scanner child running. Waiting for instructions");
699 /* Loop, listening for incoming packets on the fd and writing responses */
700 while (!l->rx_done && exchange_packets (l));
706 plugin_loader_free (l);
712 put_packet (GstPluginLoader * l, guint type, guint32 tag,
713 const guint8 * payload, guint32 payload_len)
716 guint len = payload_len + HEADER_SIZE;
718 if (l->tx_buf_write + len >= l->tx_buf_size) {
719 GST_LOG ("Expanding tx buf from %d to %d for packet of size %d",
720 l->tx_buf_size, l->tx_buf_write + len + BUF_GROW_EXTRA, len);
721 l->tx_buf_size = l->tx_buf_write + len + BUF_GROW_EXTRA;
722 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
725 out = l->tx_buf + l->tx_buf_write;
727 /* one byte packet type */
729 /* 3 byte packet tag number */
730 GST_WRITE_UINT24_BE (out + 1, tag);
731 /* 4 bytes packet length */
732 GST_WRITE_UINT32_BE (out + 4, payload_len);
734 if (payload && payload_len)
735 memcpy (out + HEADER_SIZE, payload, payload_len);
736 /* Write magic into the header */
737 GST_WRITE_UINT32_BE (out + 8, HEADER_MAGIC);
739 l->tx_buf_write += len;
740 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
744 put_chunk (GstPluginLoader * l, GstRegistryChunk * chunk, guint * pos)
750 /* Might need to align the chunk */
751 if (chunk->align && ((*pos) % ALIGNMENT) != 0)
752 padsize = ALIGNMENT - ((*pos) % ALIGNMENT);
754 len = padsize + chunk->size;
756 if (G_UNLIKELY (l->tx_buf_write + len >= l->tx_buf_size)) {
757 guint new_size = MAX (l->tx_buf_write + len,
758 l->tx_buf_size + l->tx_buf_size / 4) + BUF_GROW_EXTRA;
759 GST_LOG ("Expanding tx buf from %d to %d for chunk of size %d",
760 l->tx_buf_size, new_size, chunk->size);
761 l->tx_buf_size = new_size;
762 l->tx_buf = g_realloc (l->tx_buf, l->tx_buf_size);
765 out = l->tx_buf + l->tx_buf_write;
766 /* Clear the padding */
768 memset (out, 0, padsize);
769 memcpy (out + padsize, chunk->data, chunk->size);
771 l->tx_buf_write += len;
774 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, TRUE);
778 write_one (GstPluginLoader * l)
781 guint32 to_write, magic;
784 if (l->tx_buf_read + HEADER_SIZE > l->tx_buf_write)
787 out = l->tx_buf + l->tx_buf_read;
789 magic = GST_READ_UINT32_BE (out + 8);
790 if (magic != HEADER_MAGIC) {
791 GST_ERROR ("Packet magic number is missing. Memory corruption detected");
792 goto fail_and_cleanup;
795 to_write = GST_READ_UINT32_BE (out + 4) + HEADER_SIZE;
796 /* Check that the magic is intact, and the size is sensible */
797 if (to_write > l->tx_buf_size) {
798 GST_ERROR ("Indicated packet size is too large. Corruption detected");
799 goto fail_and_cleanup;
802 l->tx_buf_read += to_write;
804 GST_LOG ("Writing packet of size %d bytes to fd %d", to_write, l->fd_w.fd);
807 res = write (l->fd_w.fd, out, to_write);
808 if (G_UNLIKELY (res < 0)) {
809 if (errno == EAGAIN || errno == EINTR)
811 /* Failed to write -> child died */
812 goto fail_and_cleanup;
813 } else if (G_UNLIKELY (res == 0)) {
814 /* FD closed -> child died */
815 goto fail_and_cleanup;
819 } while (to_write > 0);
821 if (l->tx_buf_read == l->tx_buf_write) {
822 gst_poll_fd_ctl_write (l->fdset, &l->fd_w, FALSE);
823 l->tx_buf_read = l->tx_buf_write = 0;
829 plugin_loader_cleanup_child (l);
834 do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
836 GstPlugin *newplugin;
837 GList *chunks = NULL;
839 GST_DEBUG ("Plugin scanner loading file %s. tag %u", filename, tag);
841 #if 0 /* Test code - crash based on filename */
842 if (strstr (filename, "coreelements") == NULL) {
843 g_printerr ("Crashing on file %s\n", filename);
844 g_printerr ("%d", *(gint *) (NULL));
848 newplugin = gst_plugin_load_file ((gchar *) filename, NULL);
853 /* Now serialise the plugin details and send */
854 if (!_priv_gst_registry_chunks_save_plugin (&chunks,
855 gst_registry_get (), newplugin))
858 /* Store where the header is, write an empty one, then write
859 * all the payload chunks, then fix up the header size */
860 hdr_pos = l->tx_buf_write;
861 offset = HEADER_SIZE;
862 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
866 for (walk = chunks; walk; walk = g_list_next (walk)) {
867 GstRegistryChunk *cur = walk->data;
868 put_chunk (l, cur, &offset);
870 _priv_gst_registry_chunk_free (cur);
873 g_list_free (chunks);
875 /* Store the size of the written payload */
876 GST_WRITE_UINT32_BE (l->tx_buf + hdr_pos + 4, offset - HEADER_SIZE);
878 #if 0 /* Test code - corrupt the tx buffer based on filename */
879 if (strstr (filename, "sink") != NULL) {
881 g_printerr ("Corrupting tx buf on file %s\n", filename);
882 fd = open ("/dev/urandom", O_RDONLY);
883 res = read (fd, l->tx_buf, l->tx_buf_size);
888 gst_object_unref (newplugin);
890 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
895 put_packet (l, PACKET_PLUGIN_DETAILS, tag, NULL, 0);
898 for (walk = chunks; walk; walk = g_list_next (walk)) {
899 GstRegistryChunk *cur = walk->data;
901 _priv_gst_registry_chunk_free (cur);
904 g_list_free (chunks);
911 check_protocol_version (GstPluginLoader * l, guint8 * payload,
915 guint8 *binary_reg_ver;
917 if (payload_len < sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN)
920 got_version = GST_READ_UINT32_BE (payload);
921 GST_LOG ("Got VERSION %u from child. Ours is %u", got_version,
922 loader_protocol_version);
923 if (got_version != loader_protocol_version)
926 binary_reg_ver = payload + sizeof (guint32);
927 if (strcmp ((gchar *) binary_reg_ver, GST_MAGIC_BINARY_VERSION_STR)) {
928 GST_LOG ("Binary chunk format of child is different. Ours: %s, child %s\n",
929 GST_MAGIC_BINARY_VERSION_STR, binary_reg_ver);
937 handle_rx_packet (GstPluginLoader * l,
938 guint pack_type, guint32 tag, guint8 * payload, guint payload_len)
944 gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
947 put_packet (l, PACKET_EXIT, 0, NULL, 0);
951 case PACKET_LOAD_PLUGIN:{
955 /* Payload is the filename to load */
956 res = do_plugin_load (l, (gchar *) payload, tag);
960 case PACKET_PLUGIN_DETAILS:{
961 gchar *tmp = (gchar *) payload;
962 PendingPluginEntry *entry = NULL;
965 GST_DEBUG_OBJECT (l->registry,
966 "Received plugin details from child w/ tag %u. %d bytes info",
969 /* Assume that tagged details come back in the order
970 * we requested, and delete anything before (but not
971 * including) this one */
972 cur = l->pending_plugins;
974 PendingPluginEntry *e = (PendingPluginEntry *) (cur->data);
983 cur = g_list_delete_link (cur, cur);
984 g_free (e->filename);
985 g_slice_free (PendingPluginEntry, e);
989 l->pending_plugins = cur;
991 l->pending_plugins_tail = NULL;
993 if (payload_len > 0) {
994 GstPlugin *newplugin = NULL;
995 if (!_priv_gst_registry_chunks_load_plugin (l->registry, &tmp,
996 tmp + payload_len, &newplugin)) {
997 /* Got garbage from the child, so fail and trigger replay of plugins */
998 GST_ERROR_OBJECT (l->registry,
999 "Problems loading plugin details with tag %u from scanner", tag);
1003 GST_OBJECT_FLAG_UNSET (newplugin, GST_PLUGIN_FLAG_CACHED);
1004 GST_LOG_OBJECT (l->registry,
1005 "marking plugin %p as registered as %s", newplugin,
1006 newplugin->filename);
1007 newplugin->registered = TRUE;
1009 /* We got a set of plugin details - remember it for later */
1010 l->got_plugin_details = TRUE;
1011 } else if (entry != NULL) {
1012 /* Create a blacklist entry for this file to prevent scanning every time */
1013 plugin_loader_create_blacklist_plugin (l, entry);
1014 l->got_plugin_details = TRUE;
1017 if (entry != NULL) {
1018 g_free (entry->filename);
1019 g_slice_free (PendingPluginEntry, entry);
1022 /* Remove the plugin entry we just loaded */
1023 cur = l->pending_plugins;
1025 cur = g_list_delete_link (cur, cur);
1026 l->pending_plugins = cur;
1028 l->pending_plugins_tail = NULL;
1034 /* Respond with our reply - also a sync */
1035 put_packet (l, PACKET_SYNC, tag, NULL, 0);
1036 GST_LOG ("Got SYNC in child - replying");
1038 l->rx_got_sync = TRUE;
1040 case PACKET_VERSION:
1042 /* Respond with our reply - a version packet, with the version */
1043 const gint version_len =
1044 sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN;
1045 guint8 version_info[sizeof (guint32) + GST_MAGIC_BINARY_VERSION_LEN];
1046 memset (version_info, 0, version_len);
1047 GST_WRITE_UINT32_BE (version_info, loader_protocol_version);
1048 memcpy (version_info + sizeof (guint32), GST_MAGIC_BINARY_VERSION_STR,
1049 strlen (GST_MAGIC_BINARY_VERSION_STR));
1050 put_packet (l, PACKET_VERSION, tag, version_info, version_len);
1051 GST_LOG ("Got VERSION in child - replying %u", loader_protocol_version);
1053 res = check_protocol_version (l, payload, payload_len);
1057 return FALSE; /* Invalid packet -> something is wrong */
1064 read_one (GstPluginLoader * l)
1067 guint32 to_read, packet_len, tag;
1071 to_read = HEADER_SIZE;
1074 res = read (l->fd_r.fd, in, to_read);
1075 if (G_UNLIKELY (res < 0)) {
1076 if (errno == EAGAIN || errno == EINTR)
1078 GST_LOG ("Failed reading packet header");
1080 } else if (G_UNLIKELY (res == 0)) {
1081 GST_LOG ("Failed reading packet header: Unexpected EOF");
1086 } while (to_read > 0);
1088 magic = GST_READ_UINT32_BE (l->rx_buf + 8);
1089 if (magic != HEADER_MAGIC) {
1091 ("Invalid packet (bad magic number) received from plugin scanner subprocess");
1095 packet_len = GST_READ_UINT32_BE (l->rx_buf + 4);
1096 if (packet_len + HEADER_SIZE > BUF_MAX_SIZE) {
1098 ("Received excessively large packet for plugin scanner subprocess");
1101 tag = GST_READ_UINT24_BE (l->rx_buf + 1);
1103 if (packet_len > 0) {
1104 if (packet_len + HEADER_SIZE >= l->rx_buf_size) {
1105 GST_LOG ("Expanding rx buf from %d to %d",
1106 l->rx_buf_size, packet_len + HEADER_SIZE + BUF_GROW_EXTRA);
1107 l->rx_buf_size = packet_len + HEADER_SIZE + BUF_GROW_EXTRA;
1108 l->rx_buf = g_realloc (l->rx_buf, l->rx_buf_size);
1111 in = l->rx_buf + HEADER_SIZE;
1112 to_read = packet_len;
1114 res = read (l->fd_r.fd, in, to_read);
1115 if (G_UNLIKELY (res < 0)) {
1116 if (errno == EAGAIN || errno == EINTR)
1118 GST_ERROR ("Packet payload read failed");
1120 } else if (G_UNLIKELY (res == 0)) {
1121 GST_ERROR ("Packet payload read failed: Unexpected EOF");
1126 } while (to_read > 0);
1128 GST_LOG ("No payload to read for 0 length packet type %d tag %u",
1132 return handle_rx_packet (l, l->rx_buf[0], tag,
1133 l->rx_buf + HEADER_SIZE, packet_len);
1137 exchange_packets (GstPluginLoader * l)
1141 /* Wait for activity on our FDs */
1144 res = gst_poll_wait (l->fdset, GST_SECOND);
1145 } while (res == -1 && (errno == EINTR || errno == EAGAIN));
1150 GST_LOG ("Poll res = %d. %d bytes pending for write", res,
1151 l->tx_buf_write - l->tx_buf_read);
1154 if (gst_poll_fd_has_error (l->fdset, &l->fd_r)) {
1155 GST_LOG ("read fd %d errored", l->fd_r.fd);
1156 goto fail_and_cleanup;
1159 if (gst_poll_fd_can_read (l->fdset, &l->fd_r)) {
1161 goto fail_and_cleanup;
1162 } else if (gst_poll_fd_has_closed (l->fdset, &l->fd_r)) {
1163 GST_LOG ("read fd %d closed", l->fd_r.fd);
1164 goto fail_and_cleanup;
1168 if (l->tx_buf_read < l->tx_buf_write) {
1169 if (gst_poll_fd_has_error (l->fdset, &l->fd_w)) {
1170 GST_ERROR ("write fd %d errored", l->fd_w.fd);
1171 goto fail_and_cleanup;
1173 if (gst_poll_fd_can_write (l->fdset, &l->fd_w)) {
1175 goto fail_and_cleanup;
1176 } else if (gst_poll_fd_has_closed (l->fdset, &l->fd_w)) {
1177 GST_LOG ("write fd %d closed", l->fd_w.fd);
1178 goto fail_and_cleanup;
1181 } while (l->tx_buf_read < l->tx_buf_write);
1185 plugin_loader_cleanup_child (l);