#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
+#include <errno.h>
#include "gioerror.h"
#include "gdbusutils.h"
#ifdef G_OS_UNIX
#include <gio/gunixsocketaddress.h>
+#include <sys/wait.h>
#endif
#include "glibintl.h"
-#include "gioalias.h"
/**
* SECTION:gdbusaddress
* is explained in detail in the <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html#addresses">D-Bus specification</link>.
*/
+static gchar *get_session_address_platform_specific (GError **error);
+
/* ---------------------------------------------------------------------------------------------------- */
/**
g_return_val_if_fail (string != NULL, FALSE);
a = g_strsplit (string, ";", 0);
+ if (a[0] == NULL)
+ goto out;
+
for (n = 0; a[n] != NULL; n++)
{
if (!_g_dbus_address_parse_entry (a[n],
{
if (tmpdir != NULL || abstract != NULL)
goto meaningless;
- /* TODO: validate path */
}
else if (tmpdir != NULL)
{
if (path != NULL || abstract != NULL)
goto meaningless;
- /* TODO: validate tmpdir */
}
else if (abstract != NULL)
{
if (path != NULL || tmpdir != NULL)
goto meaningless;
- /* TODO: validate abstract */
}
else
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys"),
+ _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys)"),
address_entry);
goto out;
}
goto out;
}
+ if (host != NULL)
+ {
+ /* TODO: validate host */
+ }
+
+ nonce_file = nonce_file; /* To avoid -Wunused-but-set-variable */
+
ret= TRUE;
out:
goto out;
}
+ if (host != NULL)
+ {
+ /* TODO: validate host */
+ }
+
ret= TRUE;
out:
supported = is_valid_tcp (a[n], key_value_pairs, error);
else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
+ else if (g_strcmp0 (a[n], "autolaunch:") == 0)
+ supported = TRUE;
g_free (transport_name);
g_hash_table_unref (key_value_pairs);
goto out;
}
- /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
key = g_uri_unescape_segment (kv_pair, s, NULL);
value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
+ if (key == NULL || value == NULL)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("Error unescaping key or value in Key/Value pair %d, `%s', in address element `%s'"),
+ n,
+ kv_pair,
+ address_entry);
+ g_free (key);
+ g_free (value);
+ goto out;
+ }
g_hash_table_insert (key_value_pairs, key, value);
}
/* ---------------------------------------------------------------------------------------------------- */
+static GIOStream *
+g_dbus_address_try_connect_one (const gchar *address_entry,
+ gchar **out_guid,
+ GCancellable *cancellable,
+ GError **error);
+
/* TODO: Declare an extension point called GDBusTransport (or similar)
* and move code below to extensions implementing said extension
* point. That way we can implement a D-Bus transport over X11 without
{
const gchar *s;
const gchar *host;
- guint port;
+ glong port;
gchar *endp;
gboolean is_nonce;
}
}
- /* TODO: deal with family */
+ /* TODO: deal with family key/value-pair */
connectable = g_network_address_new (host, port);
}
+ else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
+ {
+ gchar *autolaunch_address;
+ autolaunch_address = get_session_address_platform_specific (error);
+ if (autolaunch_address != NULL)
+ {
+ ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
+ g_free (autolaunch_address);
+ goto out;
+ }
+ else
+ {
+ g_prefix_error (error, _("Error auto-launching: "));
+ }
+ }
else
{
g_set_error (error,
if (nonce_file != NULL)
{
- gchar *nonce_contents;
- gsize nonce_length;
+ gchar nonce_contents[16 + 1];
+ size_t num_bytes_read;
+ FILE *f;
- /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
- if (!g_file_get_contents (nonce_file,
- &nonce_contents,
- &nonce_length,
- error))
+ /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
+ f = fopen (nonce_file, "rb");
+ if (f == NULL)
{
- g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("Error opening nonce file `%s': %s"),
+ nonce_file,
+ g_strerror (errno));
g_object_unref (ret);
ret = NULL;
goto out;
}
-
- if (nonce_length != 16)
+ num_bytes_read = fread (nonce_contents,
+ sizeof (gchar),
+ 16 + 1,
+ f);
+ if (num_bytes_read != 16)
{
- g_set_error (error,
- G_IO_ERROR,
- G_IO_ERROR_INVALID_ARGUMENT,
- _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
- nonce_file,
- nonce_length);
- g_free (nonce_contents);
+ if (num_bytes_read == 0)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("Error reading from nonce file `%s': %s"),
+ nonce_file,
+ g_strerror (errno));
+ }
+ else
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("Error reading from nonce file `%s', expected 16 bytes, got %d"),
+ nonce_file,
+ (gint) num_bytes_read);
+ }
g_object_unref (ret);
ret = NULL;
+ fclose (f);
goto out;
}
+ fclose (f);
if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
nonce_contents,
- nonce_length,
+ 16,
NULL,
cancellable,
error))
{
- g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
+ g_prefix_error (error, _("Error writing contents of nonce file `%s' to stream:"), nonce_file);
g_object_unref (ret);
ret = NULL;
- g_free (nonce_contents);
goto out;
}
- g_free (nonce_contents);
}
}
if (ret == NULL)
goto out;
- /* TODO: validate that guid is of correct format */
guid = g_hash_table_lookup (key_value_pairs, "guid");
if (guid != NULL && out_guid != NULL)
*out_guid = g_strdup (guid);
cancellable,
&error);
if (data->stream == NULL)
- {
- g_simple_async_result_set_from_error (res, error);
- g_error_free (error);
- }
+ g_simple_async_result_take_error (res, error);
}
/**
callback,
user_data,
g_dbus_address_get_stream);
+ g_simple_async_result_set_check_cancellable (res, cancellable);
data = g_new0 (GetStreamData, 1);
data->address = g_strdup (address);
g_simple_async_result_set_op_res_gpointer (res,
*
* Finishes an operation started with g_dbus_address_get_stream().
*
- * Returns: A #GIOStream or %NULL if @error is set.
+ * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
*
* Since: 2.26
*/
* This is a synchronous failable function. See
* g_dbus_address_get_stream() for the asynchronous version.
*
- * Returns: A #GIOStream or %NULL if @error is set.
+ * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
*
* Since: 2.26
*/
last_error = NULL;
addr_array = g_strsplit (address, ";", 0);
- last_error = NULL;
+ if (addr_array != NULL && addr_array[0] == NULL)
+ {
+ last_error = g_error_new_literal (G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("The given address is empty"));
+ goto out;
+ }
+
for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
{
const gchar *addr = addr_array[n];
GError *this_error;
+
this_error = NULL;
ret = g_dbus_address_try_connect_one (addr,
out_guid,
/* ---------------------------------------------------------------------------------------------------- */
-/* TODO: implement for UNIX, Win32 and OS X */
+#ifdef G_OS_UNIX
+static gchar *
+get_session_address_dbus_launch (GError **error)
+{
+ gchar *ret;
+ gchar *machine_id;
+ gchar *command_line;
+ gchar *launch_stdout;
+ gchar *launch_stderr;
+ gint exit_status;
+ gchar *old_dbus_verbose;
+ gboolean restore_dbus_verbose;
+
+ ret = NULL;
+ machine_id = NULL;
+ command_line = NULL;
+ launch_stdout = NULL;
+ launch_stderr = NULL;
+ restore_dbus_verbose = FALSE;
+ old_dbus_verbose = NULL;
+
+ machine_id = _g_dbus_get_machine_id (error);
+ if (machine_id == NULL)
+ {
+ g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
+ goto out;
+ }
+
+ /* We're using private libdbus facilities here. When everything
+ * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
+ * X11 property is correctly documented right now) we should
+ * consider using the spec instead of dbus-launch.
+ *
+ * --autolaunch=MACHINEID
+ * This option implies that dbus-launch should scan for a previā
+ * ously-started session and reuse the values found there. If no
+ * session is found, it will start a new session. The --exit-with-
+ * session option is implied if --autolaunch is given. This option
+ * is for the exclusive use of libdbus, you do not want to use it
+ * manually. It may change in the future.
+ */
+
+ /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
+ command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
+
+ if (G_UNLIKELY (_g_dbus_debug_address ()))
+ {
+ _g_dbus_debug_print_lock ();
+ g_print ("GDBus-debug:Address: Running `%s' to get bus address (possibly autolaunching)\n", command_line);
+ old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
+ restore_dbus_verbose = TRUE;
+ g_setenv ("DBUS_VERBOSE", "1", TRUE);
+ _g_dbus_debug_print_unlock ();
+ }
+
+ if (!g_spawn_command_line_sync (command_line,
+ &launch_stdout,
+ &launch_stderr,
+ &exit_status,
+ error))
+ {
+ g_prefix_error (error, _("Error spawning command line `%s': "), command_line);
+ goto out;
+ }
+
+ if (!WIFEXITED (exit_status))
+ {
+ gchar *escaped_stderr;
+ escaped_stderr = g_strescape (launch_stderr, "");
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Abnormal program termination spawning command line `%s': %s"),
+ command_line,
+ escaped_stderr);
+ g_free (escaped_stderr);
+ goto out;
+ }
+
+ if (WEXITSTATUS (exit_status) != 0)
+ {
+ gchar *escaped_stderr;
+ escaped_stderr = g_strescape (launch_stderr, "");
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Command line `%s' exited with non-zero exit status %d: %s"),
+ command_line,
+ WEXITSTATUS (exit_status),
+ escaped_stderr);
+ g_free (escaped_stderr);
+ goto out;
+ }
+
+ /* From the dbus-launch(1) man page:
+ *
+ * --binary-syntax Write to stdout a nul-terminated bus address,
+ * then the bus PID as a binary integer of size sizeof(pid_t),
+ * then the bus X window ID as a binary integer of size
+ * sizeof(long). Integers are in the machine's byte order, not
+ * network byte order or any other canonical byte order.
+ */
+ ret = g_strdup (launch_stdout);
+
+ out:
+ if (G_UNLIKELY (_g_dbus_debug_address ()))
+ {
+ gchar *s;
+ _g_dbus_debug_print_lock ();
+ g_print ("GDBus-debug:Address: dbus-launch output:");
+ if (launch_stdout != NULL)
+ {
+ s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
+ g_print ("\n%s", s);
+ g_free (s);
+ }
+ else
+ {
+ g_print (" (none)\n");
+ }
+ g_print ("GDBus-debug:Address: dbus-launch stderr output:");
+ if (launch_stderr != NULL)
+ g_print ("\n%s", launch_stderr);
+ else
+ g_print (" (none)\n");
+ _g_dbus_debug_print_unlock ();
+ }
+
+ g_free (machine_id);
+ g_free (command_line);
+ g_free (launch_stdout);
+ g_free (launch_stderr);
+ if (G_UNLIKELY (restore_dbus_verbose))
+ {
+ if (old_dbus_verbose != NULL)
+ g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
+ else
+ g_unsetenv ("DBUS_VERBOSE");
+ }
+ g_free (old_dbus_verbose);
+ return ret;
+}
+#endif
+
+/* ---------------------------------------------------------------------------------------------------- */
+
static gchar *
-get_session_address_platform_specific (void)
+get_session_address_platform_specific (GError **error)
{
- return NULL;
+ gchar *ret;
+#ifdef G_OS_UNIX
+ /* need to handle OS X in a different way since `dbus-launch --autolaunch' probably won't work there */
+ ret = get_session_address_dbus_launch (error);
+#else
+ /* TODO: implement for UNIX, Win32 and OS X */
+ ret = NULL;
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Cannot determine session bus address (not implemented for this OS)"));
+#endif
+ return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
{
gchar *ret;
const gchar *starter_bus;
+ GError *local_error;
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
ret = NULL;
+ local_error = NULL;
+
+ if (G_UNLIKELY (_g_dbus_debug_address ()))
+ {
+ guint n;
+ _g_dbus_debug_print_lock ();
+ g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `%s'\n",
+ _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
+ for (n = 0; n < 3; n++)
+ {
+ const gchar *k;
+ const gchar *v;
+ switch (n)
+ {
+ case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
+ case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
+ case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
+ default: g_assert_not_reached ();
+ }
+ v = g_getenv (k);
+ g_print ("GDBus-debug:Address: env var %s", k);
+ if (v != NULL)
+ g_print ("=`%s'\n", v);
+ else
+ g_print (" is not set\n");
+ }
+ _g_dbus_debug_print_unlock ();
+ }
switch (bus_type)
{
ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
if (ret == NULL)
{
- ret = get_session_address_platform_specific ();
- if (ret == NULL)
- {
- g_set_error (error,
- G_IO_ERROR,
- G_IO_ERROR_FAILED,
- _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
- }
+ ret = get_session_address_platform_specific (&local_error);
}
break;
starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
if (g_strcmp0 (starter_bus, "session") == 0)
{
- ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
+ ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
goto out;
}
else if (g_strcmp0 (starter_bus, "system") == 0)
{
- ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
+ ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
goto out;
}
else
{
if (starter_bus != NULL)
{
- g_set_error (error,
+ g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
_("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
}
else
{
- g_set_error_literal (error,
+ g_set_error_literal (&local_error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
_("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
break;
default:
- g_set_error (error,
+ g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
_("Unknown bus type %d"),
}
out:
+ if (G_UNLIKELY (_g_dbus_debug_address ()))
+ {
+ _g_dbus_debug_print_lock ();
+ if (ret != NULL)
+ {
+ g_print ("GDBus-debug:Address: Returning address `%s' for bus type `%s'\n",
+ ret,
+ _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
+ }
+ else
+ {
+ g_print ("GDBus-debug:Address: Cannot look-up address bus type `%s': %s\n",
+ _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type),
+ local_error ? local_error->message : "");
+ }
+ _g_dbus_debug_print_unlock ();
+ }
+
+ if (local_error != NULL)
+ g_propagate_error (error, local_error);
+
return ret;
}
-
-#define __G_DBUS_ADDRESS_C__
-#include "gioaliasdef.c"