summaryrefslogtreecommitdiff
path: root/src/libtracker-sparql/tracker-remote-module.c
blob: bd6ed114662787a1509417f0c7ef1d263da9f51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Yuck */

#include "config.h"

#include <gio/gio.h>
#include <tracker-sparql.h>
#include <dlfcn.h>

#define LIBSOUP_2_SONAME "libsoup-2.4." G_MODULE_SUFFIX

static gboolean initialized = FALSE;

GType (* remote_endpoint_get_type) (void) = NULL;

TrackerEndpoint * (* remote_endpoint_new) (TrackerSparqlConnection  *sparql_connection,
                                           guint                     port,
                                           GTlsCertificate          *certificate,
                                           GCancellable             *cancellable,
                                           GError                  **error) = NULL;
TrackerSparqlConnection * (* remote_connection_new) (const gchar *url_base) = NULL;

static void
tracker_init_remote (void)
{
	const char *modules[3] = { 0 };
	gpointer handle = NULL;
	gint i = 0;

	if (initialized)
		return;

	g_assert (g_module_supported ());

#ifdef HAVE_RTLD_NOLOAD
	if ((handle = dlopen (LIBSOUP_2_SONAME, RTLD_NOW | RTLD_NOLOAD))) {
		/* Force load of soup2 module */
		modules[0] = "libtracker-remote-soup2." G_MODULE_SUFFIX;
	} else
#endif
	{
		modules[0] = "libtracker-remote-soup3." G_MODULE_SUFFIX;
		modules[1] = "libtracker-remote-soup2." G_MODULE_SUFFIX;
	}

	g_clear_pointer (&handle, dlclose);

	for (i = 0; modules[i]; i++) {
		GModule *remote_module;
		gchar *module_path;

		if (g_strcmp0 (g_get_current_dir (), BUILDROOT) == 0) {
			/* Detect in-build runtime of this code, this may happen
			 * building introspection information or running tests.
			 * We want the in-tree modules to be loaded then.
			 */
			module_path = g_strdup_printf (BUILD_LIBDIR "/%s", modules[i]);
		} else {
			module_path = g_strdup_printf (PRIVATE_LIBDIR "/%s", modules[i]);
		}

		remote_module = g_module_open (module_path,
		                               G_MODULE_BIND_LAZY |
		                               G_MODULE_BIND_LOCAL);
		g_free (module_path);

		if (!remote_module)
			continue;

		if (!g_module_symbol (remote_module, "tracker_endpoint_http_get_type", (gpointer *) &remote_endpoint_get_type) ||
		    !g_module_symbol (remote_module, "tracker_endpoint_http_new", (gpointer *) &remote_endpoint_new) ||
		    !g_module_symbol (remote_module, "tracker_remote_connection_new", (gpointer *) &remote_connection_new)) {
			g_clear_pointer (&remote_module, g_module_close);
			continue;
		}

		g_module_make_resident (remote_module);
		g_module_close (remote_module);
		initialized = TRUE;
		return;
	}

	g_assert_not_reached ();
}

GType
tracker_endpoint_http_get_type (void)
{
	tracker_init_remote ();

	return remote_endpoint_get_type ();
}

TrackerEndpointHttp *
tracker_endpoint_http_new (TrackerSparqlConnection  *sparql_connection,
                           guint                     port,
                           GTlsCertificate          *certificate,
                           GCancellable             *cancellable,
                           GError                  **error)
{
	tracker_init_remote ();

	return (TrackerEndpointHttp *) remote_endpoint_new (sparql_connection,
	                                                    port,
	                                                    certificate,
	                                                    cancellable,
	                                                    error);
}

TrackerSparqlConnection *
tracker_sparql_connection_remote_new (const gchar *url_base)
{
	tracker_init_remote ();

	return remote_connection_new (url_base);
}