1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
31 #include "gdbusutils.h"
32 #include "gdbusaddress.h"
33 #include "gdbuserror.h"
34 #include "gioenumtypes.h"
35 #include "gnetworkaddress.h"
36 #include "gsocketclient.h"
37 #include "giostream.h"
38 #include "gasyncresult.h"
39 #include "gsimpleasyncresult.h"
40 #include "gdbusprivate.h"
43 #include <gio/gunixsocketaddress.h>
50 * SECTION:gdbusaddress
51 * @title: D-Bus Addresses
52 * @short_description: D-Bus connection endpoints
55 * Routines for working with D-Bus addresses. A D-Bus address is a string
56 * like "unix:tmpdir=/tmp/my-app-name". The exact format of addresses
57 * is explained in detail in the <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html#addresses">D-Bus specification</link>.
60 static gchar *get_session_address_platform_specific (GError **error);
62 /* ---------------------------------------------------------------------------------------------------- */
68 * Checks if @string is a D-Bus address.
70 * This doesn't check if @string is actually supported by #GDBusServer
71 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
74 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
79 g_dbus_is_address (const gchar *string)
87 g_return_val_if_fail (string != NULL, FALSE);
89 a = g_strsplit (string, ";", 0);
93 for (n = 0; a[n] != NULL; n++)
95 if (!_g_dbus_address_parse_entry (a[n],
110 is_valid_unix (const gchar *address_entry,
111 GHashTable *key_value_pairs,
119 const gchar *abstract;
127 keys = g_hash_table_get_keys (key_value_pairs);
128 for (l = keys; l != NULL; l = l->next)
130 const gchar *key = l->data;
131 if (g_strcmp0 (key, "path") == 0)
132 path = g_hash_table_lookup (key_value_pairs, key);
133 else if (g_strcmp0 (key, "tmpdir") == 0)
134 tmpdir = g_hash_table_lookup (key_value_pairs, key);
135 else if (g_strcmp0 (key, "abstract") == 0)
136 abstract = g_hash_table_lookup (key_value_pairs, key);
141 G_IO_ERROR_INVALID_ARGUMENT,
142 _("Unsupported key `%s' in address entry `%s'"),
151 if (tmpdir != NULL || abstract != NULL)
154 else if (tmpdir != NULL)
156 if (path != NULL || abstract != NULL)
159 else if (abstract != NULL)
161 if (path != NULL || tmpdir != NULL)
168 G_IO_ERROR_INVALID_ARGUMENT,
169 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys)"),
181 G_IO_ERROR_INVALID_ARGUMENT,
182 _("Meaningless key/value pair combination in address entry `%s'"),
192 is_valid_nonce_tcp (const gchar *address_entry,
193 GHashTable *key_value_pairs,
202 const gchar *nonce_file;
213 keys = g_hash_table_get_keys (key_value_pairs);
214 for (l = keys; l != NULL; l = l->next)
216 const gchar *key = l->data;
217 if (g_strcmp0 (key, "host") == 0)
218 host = g_hash_table_lookup (key_value_pairs, key);
219 else if (g_strcmp0 (key, "port") == 0)
220 port = g_hash_table_lookup (key_value_pairs, key);
221 else if (g_strcmp0 (key, "family") == 0)
222 family = g_hash_table_lookup (key_value_pairs, key);
223 else if (g_strcmp0 (key, "noncefile") == 0)
224 nonce_file = g_hash_table_lookup (key_value_pairs, key);
229 G_IO_ERROR_INVALID_ARGUMENT,
230 _("Unsupported key `%s' in address entry `%s'"),
239 port_num = strtol (port, &endp, 10);
240 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
244 G_IO_ERROR_INVALID_ARGUMENT,
245 _("Error in address `%s' - the port attribute is malformed"),
251 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
255 G_IO_ERROR_INVALID_ARGUMENT,
256 _("Error in address `%s' - the family attribute is malformed"),
263 /* TODO: validate host */
266 nonce_file = nonce_file; /* To avoid -Wunused-but-set-variable */
277 is_valid_tcp (const gchar *address_entry,
278 GHashTable *key_value_pairs,
296 keys = g_hash_table_get_keys (key_value_pairs);
297 for (l = keys; l != NULL; l = l->next)
299 const gchar *key = l->data;
300 if (g_strcmp0 (key, "host") == 0)
301 host = g_hash_table_lookup (key_value_pairs, key);
302 else if (g_strcmp0 (key, "port") == 0)
303 port = g_hash_table_lookup (key_value_pairs, key);
304 else if (g_strcmp0 (key, "family") == 0)
305 family = g_hash_table_lookup (key_value_pairs, key);
310 G_IO_ERROR_INVALID_ARGUMENT,
311 _("Unsupported key `%s' in address entry `%s'"),
320 port_num = strtol (port, &endp, 10);
321 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
325 G_IO_ERROR_INVALID_ARGUMENT,
326 _("Error in address `%s' - the port attribute is malformed"),
332 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
336 G_IO_ERROR_INVALID_ARGUMENT,
337 _("Error in address `%s' - the family attribute is malformed"),
344 /* TODO: validate host */
356 * g_dbus_is_supported_address:
358 * @error: Return location for error or %NULL.
360 * Like g_dbus_is_address() but also checks if the library suppors the
361 * transports in @string and that key/value pairs for each transport
364 * Returns: %TRUE if @string is a valid D-Bus address that is
365 * supported by this library, %FALSE if @error is set.
370 g_dbus_is_supported_address (const gchar *string,
379 g_return_val_if_fail (string != NULL, FALSE);
380 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
382 a = g_strsplit (string, ";", 0);
383 for (n = 0; a[n] != NULL; n++)
385 gchar *transport_name;
386 GHashTable *key_value_pairs;
389 if (!_g_dbus_address_parse_entry (a[n],
396 if (g_strcmp0 (transport_name, "unix") == 0)
397 supported = is_valid_unix (a[n], key_value_pairs, error);
398 else if (g_strcmp0 (transport_name, "tcp") == 0)
399 supported = is_valid_tcp (a[n], key_value_pairs, error);
400 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
401 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
402 else if (g_strcmp0 (a[n], "autolaunch:") == 0)
405 g_free (transport_name);
406 g_hash_table_unref (key_value_pairs);
417 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
423 _g_dbus_address_parse_entry (const gchar *address_entry,
424 gchar **out_transport_name,
425 GHashTable **out_key_value_pairs,
429 GHashTable *key_value_pairs;
430 gchar *transport_name;
437 transport_name = NULL;
438 key_value_pairs = NULL;
440 s = strchr (address_entry, ':');
445 G_IO_ERROR_INVALID_ARGUMENT,
446 _("Address element `%s', does not contain a colon (:)"),
451 transport_name = g_strndup (address_entry, s - address_entry);
452 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
454 kv_pairs = g_strsplit (s + 1, ",", 0);
455 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
457 const gchar *kv_pair = kv_pairs[n];
461 s = strchr (kv_pair, '=');
466 G_IO_ERROR_INVALID_ARGUMENT,
467 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
474 key = g_uri_unescape_segment (kv_pair, s, NULL);
475 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
476 if (key == NULL || value == NULL)
480 G_IO_ERROR_INVALID_ARGUMENT,
481 _("Error unescaping key or value in Key/Value pair %d, `%s', in address element `%s'"),
489 g_hash_table_insert (key_value_pairs, key, value);
495 g_strfreev (kv_pairs);
498 if (out_transport_name != NULL)
499 *out_transport_name = transport_name;
501 g_free (transport_name);
502 if (out_key_value_pairs != NULL)
503 *out_key_value_pairs = key_value_pairs;
504 else if (key_value_pairs != NULL)
505 g_hash_table_unref (key_value_pairs);
509 g_free (transport_name);
510 if (key_value_pairs != NULL)
511 g_hash_table_unref (key_value_pairs);
516 /* ---------------------------------------------------------------------------------------------------- */
519 g_dbus_address_try_connect_one (const gchar *address_entry,
521 GCancellable *cancellable,
524 /* TODO: Declare an extension point called GDBusTransport (or similar)
525 * and move code below to extensions implementing said extension
526 * point. That way we can implement a D-Bus transport over X11 without
527 * making libgio link to libX11...
530 g_dbus_address_connect (const gchar *address_entry,
531 const gchar *transport_name,
532 GHashTable *key_value_pairs,
533 GCancellable *cancellable,
537 GSocketConnectable *connectable;
538 const gchar *nonce_file;
548 else if (g_strcmp0 (transport_name, "unix") == 0)
551 const gchar *abstract;
552 path = g_hash_table_lookup (key_value_pairs, "path");
553 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
554 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
558 G_IO_ERROR_INVALID_ARGUMENT,
559 _("Error in address `%s' - the unix transport requires exactly one of the "
560 "keys `path' or `abstract' to be set"),
563 else if (path != NULL)
565 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
567 else if (abstract != NULL)
569 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
571 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
575 g_assert_not_reached ();
579 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
587 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
589 host = g_hash_table_lookup (key_value_pairs, "host");
594 G_IO_ERROR_INVALID_ARGUMENT,
595 _("Error in address `%s' - the host attribute is missing or malformed"),
600 s = g_hash_table_lookup (key_value_pairs, "port");
603 port = strtol (s, &endp, 10);
604 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
608 G_IO_ERROR_INVALID_ARGUMENT,
609 _("Error in address `%s' - the port attribute is missing or malformed"),
617 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
618 if (nonce_file == NULL)
622 G_IO_ERROR_INVALID_ARGUMENT,
623 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
629 /* TODO: deal with family key/value-pair */
630 connectable = g_network_address_new (host, port);
632 else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
634 gchar *autolaunch_address;
635 autolaunch_address = get_session_address_platform_specific (error);
636 if (autolaunch_address != NULL)
638 ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
639 g_free (autolaunch_address);
644 g_prefix_error (error, _("Error auto-launching: "));
651 G_IO_ERROR_INVALID_ARGUMENT,
652 _("Unknown or unsupported transport `%s' for address `%s'"),
657 if (connectable != NULL)
659 GSocketClient *client;
660 GSocketConnection *connection;
662 g_assert (ret == NULL);
663 client = g_socket_client_new ();
664 connection = g_socket_client_connect (client,
668 g_object_unref (connectable);
669 g_object_unref (client);
670 if (connection == NULL)
673 ret = G_IO_STREAM (connection);
675 if (nonce_file != NULL)
677 gchar nonce_contents[16 + 1];
678 size_t num_bytes_read;
681 /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
682 f = fopen (nonce_file, "rb");
687 G_IO_ERROR_INVALID_ARGUMENT,
688 _("Error opening nonce file `%s': %s"),
691 g_object_unref (ret);
695 num_bytes_read = fread (nonce_contents,
699 if (num_bytes_read != 16)
701 if (num_bytes_read == 0)
705 G_IO_ERROR_INVALID_ARGUMENT,
706 _("Error reading from nonce file `%s': %s"),
714 G_IO_ERROR_INVALID_ARGUMENT,
715 _("Error reading from nonce file `%s', expected 16 bytes, got %d"),
717 (gint) num_bytes_read);
719 g_object_unref (ret);
726 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
733 g_prefix_error (error, _("Error writing contents of nonce file `%s' to stream:"), nonce_file);
734 g_object_unref (ret);
747 g_dbus_address_try_connect_one (const gchar *address_entry,
749 GCancellable *cancellable,
753 GHashTable *key_value_pairs;
754 gchar *transport_name;
758 transport_name = NULL;
759 key_value_pairs = NULL;
761 if (!_g_dbus_address_parse_entry (address_entry,
767 ret = g_dbus_address_connect (address_entry,
775 guid = g_hash_table_lookup (key_value_pairs, "guid");
776 if (guid != NULL && out_guid != NULL)
777 *out_guid = g_strdup (guid);
780 g_free (transport_name);
781 if (key_value_pairs != NULL)
782 g_hash_table_unref (key_value_pairs);
787 /* ---------------------------------------------------------------------------------------------------- */
796 get_stream_data_free (GetStreamData *data)
798 g_free (data->address);
799 if (data->stream != NULL)
800 g_object_unref (data->stream);
806 get_stream_thread_func (GSimpleAsyncResult *res,
808 GCancellable *cancellable)
813 data = g_simple_async_result_get_op_res_gpointer (res);
816 data->stream = g_dbus_address_get_stream_sync (data->address,
820 if (data->stream == NULL)
821 g_simple_async_result_take_error (res, error);
825 * g_dbus_address_get_stream:
826 * @address: A valid D-Bus address.
827 * @cancellable: A #GCancellable or %NULL.
828 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
829 * @user_data: Data to pass to @callback.
831 * Asynchronously connects to an endpoint specified by @address and
832 * sets up the connection so it is in a state to run the client-side
833 * of the D-Bus authentication conversation.
835 * When the operation is finished, @callback will be invoked. You can
836 * then call g_dbus_address_get_stream_finish() to get the result of
839 * This is an asynchronous failable function. See
840 * g_dbus_address_get_stream_sync() for the synchronous version.
845 g_dbus_address_get_stream (const gchar *address,
846 GCancellable *cancellable,
847 GAsyncReadyCallback callback,
850 GSimpleAsyncResult *res;
853 g_return_if_fail (address != NULL);
855 res = g_simple_async_result_new (NULL,
858 g_dbus_address_get_stream);
859 data = g_new0 (GetStreamData, 1);
860 data->address = g_strdup (address);
861 g_simple_async_result_set_op_res_gpointer (res,
863 (GDestroyNotify) get_stream_data_free);
864 g_simple_async_result_run_in_thread (res,
865 get_stream_thread_func,
868 g_object_unref (res);
872 * g_dbus_address_get_stream_finish:
873 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
874 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
875 * @error: Return location for error or %NULL.
877 * Finishes an operation started with g_dbus_address_get_stream().
879 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
884 g_dbus_address_get_stream_finish (GAsyncResult *res,
888 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
892 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
893 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
895 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
899 data = g_simple_async_result_get_op_res_gpointer (simple);
900 if (g_simple_async_result_propagate_error (simple, error))
903 ret = g_object_ref (data->stream);
904 if (out_guid != NULL)
905 *out_guid = g_strdup (data->guid);
912 * g_dbus_address_get_stream_sync:
913 * @address: A valid D-Bus address.
914 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
915 * @cancellable: A #GCancellable or %NULL.
916 * @error: Return location for error or %NULL.
918 * Synchronously connects to an endpoint specified by @address and
919 * sets up the connection so it is in a state to run the client-side
920 * of the D-Bus authentication conversation.
922 * This is a synchronous failable function. See
923 * g_dbus_address_get_stream() for the asynchronous version.
925 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
930 g_dbus_address_get_stream_sync (const gchar *address,
932 GCancellable *cancellable,
940 g_return_val_if_fail (address != NULL, NULL);
941 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
946 addr_array = g_strsplit (address, ";", 0);
947 if (addr_array != NULL && addr_array[0] == NULL)
949 last_error = g_error_new_literal (G_IO_ERROR,
950 G_IO_ERROR_INVALID_ARGUMENT,
951 _("The given address is empty"));
955 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
957 const gchar *addr = addr_array[n];
961 ret = g_dbus_address_try_connect_one (addr,
971 g_assert (this_error != NULL);
972 if (last_error != NULL)
973 g_error_free (last_error);
974 last_error = this_error;
981 if (last_error != NULL)
982 g_error_free (last_error);
986 g_assert (last_error != NULL);
987 g_propagate_error (error, last_error);
990 g_strfreev (addr_array);
994 /* ---------------------------------------------------------------------------------------------------- */
998 get_session_address_dbus_launch (GError **error)
1002 gchar *command_line;
1003 gchar *launch_stdout;
1004 gchar *launch_stderr;
1006 gchar *old_dbus_verbose;
1007 gboolean restore_dbus_verbose;
1011 command_line = NULL;
1012 launch_stdout = NULL;
1013 launch_stderr = NULL;
1014 restore_dbus_verbose = FALSE;
1015 old_dbus_verbose = NULL;
1017 machine_id = _g_dbus_get_machine_id (error);
1018 if (machine_id == NULL)
1020 g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
1024 /* We're using private libdbus facilities here. When everything
1025 * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
1026 * X11 property is correctly documented right now) we should
1027 * consider using the spec instead of dbus-launch.
1029 * --autolaunch=MACHINEID
1030 * This option implies that dbus-launch should scan for a previ‐
1031 * ously-started session and reuse the values found there. If no
1032 * session is found, it will start a new session. The --exit-with-
1033 * session option is implied if --autolaunch is given. This option
1034 * is for the exclusive use of libdbus, you do not want to use it
1035 * manually. It may change in the future.
1038 /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
1039 command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
1041 if (G_UNLIKELY (_g_dbus_debug_address ()))
1043 _g_dbus_debug_print_lock ();
1044 g_print ("GDBus-debug:Address: Running `%s' to get bus address (possibly autolaunching)\n", command_line);
1045 old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
1046 restore_dbus_verbose = TRUE;
1047 g_setenv ("DBUS_VERBOSE", "1", TRUE);
1048 _g_dbus_debug_print_unlock ();
1051 if (!g_spawn_command_line_sync (command_line,
1057 g_prefix_error (error, _("Error spawning command line `%s': "), command_line);
1061 if (!WIFEXITED (exit_status))
1063 gchar *escaped_stderr;
1064 escaped_stderr = g_strescape (launch_stderr, "");
1068 _("Abnormal program termination spawning command line `%s': %s"),
1071 g_free (escaped_stderr);
1075 if (WEXITSTATUS (exit_status) != 0)
1077 gchar *escaped_stderr;
1078 escaped_stderr = g_strescape (launch_stderr, "");
1082 _("Command line `%s' exited with non-zero exit status %d: %s"),
1084 WEXITSTATUS (exit_status),
1086 g_free (escaped_stderr);
1090 /* From the dbus-launch(1) man page:
1092 * --binary-syntax Write to stdout a nul-terminated bus address,
1093 * then the bus PID as a binary integer of size sizeof(pid_t),
1094 * then the bus X window ID as a binary integer of size
1095 * sizeof(long). Integers are in the machine's byte order, not
1096 * network byte order or any other canonical byte order.
1098 ret = g_strdup (launch_stdout);
1101 if (G_UNLIKELY (_g_dbus_debug_address ()))
1104 _g_dbus_debug_print_lock ();
1105 g_print ("GDBus-debug:Address: dbus-launch output:");
1106 if (launch_stdout != NULL)
1108 s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
1109 g_print ("\n%s", s);
1114 g_print (" (none)\n");
1116 g_print ("GDBus-debug:Address: dbus-launch stderr output:");
1117 if (launch_stderr != NULL)
1118 g_print ("\n%s", launch_stderr);
1120 g_print (" (none)\n");
1121 _g_dbus_debug_print_unlock ();
1124 g_free (machine_id);
1125 g_free (command_line);
1126 g_free (launch_stdout);
1127 g_free (launch_stderr);
1128 if (G_UNLIKELY (restore_dbus_verbose))
1130 if (old_dbus_verbose != NULL)
1131 g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
1133 g_unsetenv ("DBUS_VERBOSE");
1135 g_free (old_dbus_verbose);
1140 /* ---------------------------------------------------------------------------------------------------- */
1143 get_session_address_platform_specific (GError **error)
1147 /* need to handle OS X in a different way since `dbus-launch --autolaunch' probably won't work there */
1148 ret = get_session_address_dbus_launch (error);
1150 /* TODO: implement for UNIX, Win32 and OS X */
1155 _("Cannot determine session bus address (not implemented for this OS)"));
1160 /* ---------------------------------------------------------------------------------------------------- */
1163 * g_dbus_address_get_for_bus_sync:
1164 * @bus_type: A #GBusType.
1165 * @cancellable: A #GCancellable or %NULL.
1166 * @error: Return location for error or %NULL.
1168 * Synchronously looks up the D-Bus address for the well-known message
1169 * bus instance specified by @bus_type. This may involve using various
1170 * platform specific mechanisms.
1172 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
1177 g_dbus_address_get_for_bus_sync (GBusType bus_type,
1178 GCancellable *cancellable,
1182 const gchar *starter_bus;
1183 GError *local_error;
1185 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1190 if (G_UNLIKELY (_g_dbus_debug_address ()))
1193 _g_dbus_debug_print_lock ();
1194 g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `%s'\n",
1195 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1196 for (n = 0; n < 3; n++)
1202 case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
1203 case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
1204 case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
1205 default: g_assert_not_reached ();
1208 g_print ("GDBus-debug:Address: env var %s", k);
1210 g_print ("=`%s'\n", v);
1212 g_print (" is not set\n");
1214 _g_dbus_debug_print_unlock ();
1219 case G_BUS_TYPE_SYSTEM:
1220 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
1223 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
1227 case G_BUS_TYPE_SESSION:
1228 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
1231 ret = get_session_address_platform_specific (&local_error);
1235 case G_BUS_TYPE_STARTER:
1236 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
1237 if (g_strcmp0 (starter_bus, "session") == 0)
1239 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
1242 else if (g_strcmp0 (starter_bus, "system") == 0)
1244 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
1249 if (starter_bus != NULL)
1251 g_set_error (&local_error,
1254 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1255 " - unknown value `%s'"),
1260 g_set_error_literal (&local_error,
1263 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1264 "variable is not set"));
1270 g_set_error (&local_error,
1273 _("Unknown bus type %d"),
1279 if (G_UNLIKELY (_g_dbus_debug_address ()))
1281 _g_dbus_debug_print_lock ();
1284 g_print ("GDBus-debug:Address: Returning address `%s' for bus type `%s'\n",
1286 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1290 g_print ("GDBus-debug:Address: Cannot look-up address bus type `%s': %s\n",
1291 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type),
1292 local_error ? local_error->message : "");
1294 _g_dbus_debug_print_unlock ();
1297 if (local_error != NULL)
1298 g_propagate_error (error, local_error);