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>
34 #include "gdbusutils.h"
35 #include "gdbusaddress.h"
36 #include "gdbuserror.h"
37 #include "gioenumtypes.h"
38 #include "gnetworkaddress.h"
39 #include "gsocketclient.h"
40 #include "giostream.h"
41 #include "gasyncresult.h"
42 #include "gsimpleasyncresult.h"
43 #include "gdbusprivate.h"
46 #include <gio/gunixsocketaddress.h>
52 * SECTION:gdbusaddress
53 * @title: D-Bus Addresses
54 * @short_description: D-Bus connection endpoints
57 * Routines for working with D-Bus addresses. A D-Bus address is a string
58 * like "unix:tmpdir=/tmp/my-app-name". The exact format of addresses
59 * is explained in detail in the <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html#addresses">D-Bus specification</link>.
62 static gchar *get_session_address_platform_specific (GError **error);
64 /* ---------------------------------------------------------------------------------------------------- */
70 * Checks if @string is a D-Bus address.
72 * This doesn't check if @string is actually supported by #GDBusServer
73 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
76 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
81 g_dbus_is_address (const gchar *string)
89 g_return_val_if_fail (string != NULL, FALSE);
91 a = g_strsplit (string, ";", 0);
95 for (n = 0; a[n] != NULL; n++)
97 if (!_g_dbus_address_parse_entry (a[n],
112 is_valid_unix (const gchar *address_entry,
113 GHashTable *key_value_pairs,
121 const gchar *abstract;
129 keys = g_hash_table_get_keys (key_value_pairs);
130 for (l = keys; l != NULL; l = l->next)
132 const gchar *key = l->data;
133 if (g_strcmp0 (key, "path") == 0)
134 path = g_hash_table_lookup (key_value_pairs, key);
135 else if (g_strcmp0 (key, "tmpdir") == 0)
136 tmpdir = g_hash_table_lookup (key_value_pairs, key);
137 else if (g_strcmp0 (key, "abstract") == 0)
138 abstract = g_hash_table_lookup (key_value_pairs, key);
143 G_IO_ERROR_INVALID_ARGUMENT,
144 _("Unsupported key `%s' in address entry `%s'"),
153 if (tmpdir != NULL || abstract != NULL)
156 else if (tmpdir != NULL)
158 if (path != NULL || abstract != NULL)
161 else if (abstract != NULL)
163 if (path != NULL || tmpdir != NULL)
170 G_IO_ERROR_INVALID_ARGUMENT,
171 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys)"),
183 G_IO_ERROR_INVALID_ARGUMENT,
184 _("Meaningless key/value pair combination in address entry `%s'"),
194 is_valid_nonce_tcp (const gchar *address_entry,
195 GHashTable *key_value_pairs,
204 const gchar *nonce_file;
215 keys = g_hash_table_get_keys (key_value_pairs);
216 for (l = keys; l != NULL; l = l->next)
218 const gchar *key = l->data;
219 if (g_strcmp0 (key, "host") == 0)
220 host = g_hash_table_lookup (key_value_pairs, key);
221 else if (g_strcmp0 (key, "port") == 0)
222 port = g_hash_table_lookup (key_value_pairs, key);
223 else if (g_strcmp0 (key, "family") == 0)
224 family = g_hash_table_lookup (key_value_pairs, key);
225 else if (g_strcmp0 (key, "noncefile") == 0)
226 nonce_file = g_hash_table_lookup (key_value_pairs, key);
231 G_IO_ERROR_INVALID_ARGUMENT,
232 _("Unsupported key `%s' in address entry `%s'"),
241 port_num = strtol (port, &endp, 10);
242 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
246 G_IO_ERROR_INVALID_ARGUMENT,
247 _("Error in address `%s' - the port attribute is malformed"),
253 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
257 G_IO_ERROR_INVALID_ARGUMENT,
258 _("Error in address `%s' - the family attribute is malformed"),
272 is_valid_tcp (const gchar *address_entry,
273 GHashTable *key_value_pairs,
291 keys = g_hash_table_get_keys (key_value_pairs);
292 for (l = keys; l != NULL; l = l->next)
294 const gchar *key = l->data;
295 if (g_strcmp0 (key, "host") == 0)
296 host = g_hash_table_lookup (key_value_pairs, key);
297 else if (g_strcmp0 (key, "port") == 0)
298 port = g_hash_table_lookup (key_value_pairs, key);
299 else if (g_strcmp0 (key, "family") == 0)
300 family = g_hash_table_lookup (key_value_pairs, key);
305 G_IO_ERROR_INVALID_ARGUMENT,
306 _("Unsupported key `%s' in address entry `%s'"),
315 port_num = strtol (port, &endp, 10);
316 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
320 G_IO_ERROR_INVALID_ARGUMENT,
321 _("Error in address `%s' - the port attribute is malformed"),
327 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
331 G_IO_ERROR_INVALID_ARGUMENT,
332 _("Error in address `%s' - the family attribute is malformed"),
346 * g_dbus_is_supported_address:
348 * @error: Return location for error or %NULL.
350 * Like g_dbus_is_address() but also checks if the library suppors the
351 * transports in @string and that key/value pairs for each transport
354 * Returns: %TRUE if @string is a valid D-Bus address that is
355 * supported by this library, %FALSE if @error is set.
360 g_dbus_is_supported_address (const gchar *string,
369 g_return_val_if_fail (string != NULL, FALSE);
370 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
372 a = g_strsplit (string, ";", 0);
373 for (n = 0; a[n] != NULL; n++)
375 gchar *transport_name;
376 GHashTable *key_value_pairs;
379 if (!_g_dbus_address_parse_entry (a[n],
386 if (g_strcmp0 (transport_name, "unix") == 0)
387 supported = is_valid_unix (a[n], key_value_pairs, error);
388 else if (g_strcmp0 (transport_name, "tcp") == 0)
389 supported = is_valid_tcp (a[n], key_value_pairs, error);
390 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
391 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
392 else if (g_strcmp0 (a[n], "autolaunch:") == 0)
395 g_free (transport_name);
396 g_hash_table_unref (key_value_pairs);
407 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
413 _g_dbus_address_parse_entry (const gchar *address_entry,
414 gchar **out_transport_name,
415 GHashTable **out_key_value_pairs,
419 GHashTable *key_value_pairs;
420 gchar *transport_name;
427 transport_name = NULL;
428 key_value_pairs = NULL;
430 s = strchr (address_entry, ':');
435 G_IO_ERROR_INVALID_ARGUMENT,
436 _("Address element `%s', does not contain a colon (:)"),
441 transport_name = g_strndup (address_entry, s - address_entry);
442 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
444 kv_pairs = g_strsplit (s + 1, ",", 0);
445 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
447 const gchar *kv_pair = kv_pairs[n];
451 s = strchr (kv_pair, '=');
456 G_IO_ERROR_INVALID_ARGUMENT,
457 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
464 key = g_uri_unescape_segment (kv_pair, s, NULL);
465 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
466 if (key == NULL || value == NULL)
470 G_IO_ERROR_INVALID_ARGUMENT,
471 _("Error unescaping key or value in Key/Value pair %d, `%s', in address element `%s'"),
479 g_hash_table_insert (key_value_pairs, key, value);
485 g_strfreev (kv_pairs);
488 if (out_transport_name != NULL)
489 *out_transport_name = transport_name;
491 g_free (transport_name);
492 if (out_key_value_pairs != NULL)
493 *out_key_value_pairs = key_value_pairs;
494 else if (key_value_pairs != NULL)
495 g_hash_table_unref (key_value_pairs);
499 g_free (transport_name);
500 if (key_value_pairs != NULL)
501 g_hash_table_unref (key_value_pairs);
506 /* ---------------------------------------------------------------------------------------------------- */
509 g_dbus_address_try_connect_one (const gchar *address_entry,
511 GCancellable *cancellable,
514 /* TODO: Declare an extension point called GDBusTransport (or similar)
515 * and move code below to extensions implementing said extension
516 * point. That way we can implement a D-Bus transport over X11 without
517 * making libgio link to libX11...
520 g_dbus_address_connect (const gchar *address_entry,
521 const gchar *transport_name,
522 GHashTable *key_value_pairs,
523 GCancellable *cancellable,
527 GSocketConnectable *connectable;
528 const gchar *nonce_file;
538 else if (g_strcmp0 (transport_name, "unix") == 0)
541 const gchar *abstract;
542 path = g_hash_table_lookup (key_value_pairs, "path");
543 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
544 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
548 G_IO_ERROR_INVALID_ARGUMENT,
549 _("Error in address `%s' - the unix transport requires exactly one of the "
550 "keys `path' or `abstract' to be set"),
553 else if (path != NULL)
555 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
557 else if (abstract != NULL)
559 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
561 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
565 g_assert_not_reached ();
569 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
577 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
579 host = g_hash_table_lookup (key_value_pairs, "host");
584 G_IO_ERROR_INVALID_ARGUMENT,
585 _("Error in address `%s' - the host attribute is missing or malformed"),
590 s = g_hash_table_lookup (key_value_pairs, "port");
593 port = strtol (s, &endp, 10);
594 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
598 G_IO_ERROR_INVALID_ARGUMENT,
599 _("Error in address `%s' - the port attribute is missing or malformed"),
607 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
608 if (nonce_file == NULL)
612 G_IO_ERROR_INVALID_ARGUMENT,
613 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
619 /* TODO: deal with family key/value-pair */
620 connectable = g_network_address_new (host, port);
622 else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
624 gchar *autolaunch_address;
625 autolaunch_address = get_session_address_platform_specific (error);
626 if (autolaunch_address != NULL)
628 ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
629 g_free (autolaunch_address);
634 g_prefix_error (error, _("Error auto-launching: "));
641 G_IO_ERROR_INVALID_ARGUMENT,
642 _("Unknown or unsupported transport `%s' for address `%s'"),
647 if (connectable != NULL)
649 GSocketClient *client;
650 GSocketConnection *connection;
652 g_assert (ret == NULL);
653 client = g_socket_client_new ();
654 connection = g_socket_client_connect (client,
658 g_object_unref (connectable);
659 g_object_unref (client);
660 if (connection == NULL)
663 ret = G_IO_STREAM (connection);
665 if (nonce_file != NULL)
667 gchar nonce_contents[16 + 1];
668 size_t num_bytes_read;
671 /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
672 f = fopen (nonce_file, "rb");
677 G_IO_ERROR_INVALID_ARGUMENT,
678 _("Error opening nonce file `%s': %s"),
681 g_object_unref (ret);
685 num_bytes_read = fread (nonce_contents,
689 if (num_bytes_read != 16)
691 if (num_bytes_read == 0)
695 G_IO_ERROR_INVALID_ARGUMENT,
696 _("Error reading from nonce file `%s': %s"),
704 G_IO_ERROR_INVALID_ARGUMENT,
705 _("Error reading from nonce file `%s', expected 16 bytes, got %d"),
707 (gint) num_bytes_read);
709 g_object_unref (ret);
716 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
723 g_prefix_error (error, _("Error writing contents of nonce file `%s' to stream:"), nonce_file);
724 g_object_unref (ret);
737 g_dbus_address_try_connect_one (const gchar *address_entry,
739 GCancellable *cancellable,
743 GHashTable *key_value_pairs;
744 gchar *transport_name;
748 transport_name = NULL;
749 key_value_pairs = NULL;
751 if (!_g_dbus_address_parse_entry (address_entry,
757 ret = g_dbus_address_connect (address_entry,
765 guid = g_hash_table_lookup (key_value_pairs, "guid");
766 if (guid != NULL && out_guid != NULL)
767 *out_guid = g_strdup (guid);
770 g_free (transport_name);
771 if (key_value_pairs != NULL)
772 g_hash_table_unref (key_value_pairs);
777 /* ---------------------------------------------------------------------------------------------------- */
786 get_stream_data_free (GetStreamData *data)
788 g_free (data->address);
789 if (data->stream != NULL)
790 g_object_unref (data->stream);
796 get_stream_thread_func (GSimpleAsyncResult *res,
798 GCancellable *cancellable)
803 data = g_simple_async_result_get_op_res_gpointer (res);
806 data->stream = g_dbus_address_get_stream_sync (data->address,
810 if (data->stream == NULL)
812 g_simple_async_result_set_from_error (res, error);
813 g_error_free (error);
818 * g_dbus_address_get_stream:
819 * @address: A valid D-Bus address.
820 * @cancellable: A #GCancellable or %NULL.
821 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
822 * @user_data: Data to pass to @callback.
824 * Asynchronously connects to an endpoint specified by @address and
825 * sets up the connection so it is in a state to run the client-side
826 * of the D-Bus authentication conversation.
828 * When the operation is finished, @callback will be invoked. You can
829 * then call g_dbus_address_get_stream_finish() to get the result of
832 * This is an asynchronous failable function. See
833 * g_dbus_address_get_stream_sync() for the synchronous version.
838 g_dbus_address_get_stream (const gchar *address,
839 GCancellable *cancellable,
840 GAsyncReadyCallback callback,
843 GSimpleAsyncResult *res;
846 g_return_if_fail (address != NULL);
848 res = g_simple_async_result_new (NULL,
851 g_dbus_address_get_stream);
852 data = g_new0 (GetStreamData, 1);
853 data->address = g_strdup (address);
854 g_simple_async_result_set_op_res_gpointer (res,
856 (GDestroyNotify) get_stream_data_free);
857 g_simple_async_result_run_in_thread (res,
858 get_stream_thread_func,
861 g_object_unref (res);
865 * g_dbus_address_get_stream_finish:
866 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
867 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
868 * @error: Return location for error or %NULL.
870 * Finishes an operation started with g_dbus_address_get_stream().
872 * Returns: A #GIOStream or %NULL if @error is set.
877 g_dbus_address_get_stream_finish (GAsyncResult *res,
881 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
885 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
886 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
888 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
892 data = g_simple_async_result_get_op_res_gpointer (simple);
893 if (g_simple_async_result_propagate_error (simple, error))
896 ret = g_object_ref (data->stream);
897 if (out_guid != NULL)
898 *out_guid = g_strdup (data->guid);
905 * g_dbus_address_get_stream_sync:
906 * @address: A valid D-Bus address.
907 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
908 * @cancellable: A #GCancellable or %NULL.
909 * @error: Return location for error or %NULL.
911 * Synchronously connects to an endpoint specified by @address and
912 * sets up the connection so it is in a state to run the client-side
913 * of the D-Bus authentication conversation.
915 * This is a synchronous failable function. See
916 * g_dbus_address_get_stream() for the asynchronous version.
918 * Returns: A #GIOStream or %NULL if @error is set.
923 g_dbus_address_get_stream_sync (const gchar *address,
925 GCancellable *cancellable,
933 g_return_val_if_fail (address != NULL, NULL);
934 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
939 addr_array = g_strsplit (address, ";", 0);
940 if (addr_array[0] == NULL)
942 last_error = g_error_new_literal (G_IO_ERROR,
943 G_IO_ERROR_INVALID_ARGUMENT,
944 _("The given address is empty"));
948 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
950 const gchar *addr = addr_array[n];
954 ret = g_dbus_address_try_connect_one (addr,
964 g_assert (this_error != NULL);
965 if (last_error != NULL)
966 g_error_free (last_error);
967 last_error = this_error;
974 if (last_error != NULL)
975 g_error_free (last_error);
979 g_assert (last_error != NULL);
980 g_propagate_error (error, last_error);
983 g_strfreev (addr_array);
987 /* ---------------------------------------------------------------------------------------------------- */
991 get_session_address_dbus_launch (GError **error)
996 gchar *launch_stdout;
997 gchar *launch_stderr;
999 gchar *old_dbus_verbose;
1000 gboolean restore_dbus_verbose;
1004 command_line = NULL;
1005 launch_stdout = NULL;
1006 launch_stderr = NULL;
1007 restore_dbus_verbose = FALSE;
1008 old_dbus_verbose = NULL;
1010 machine_id = _g_dbus_get_machine_id (error);
1011 if (machine_id == NULL)
1013 g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
1017 /* We're using private libdbus facilities here. When everything
1018 * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
1019 * X11 property is correctly documented right now) we should
1020 * consider using the spec instead of dbus-launch.
1022 * --autolaunch=MACHINEID
1023 * This option implies that dbus-launch should scan for a previ‐
1024 * ously-started session and reuse the values found there. If no
1025 * session is found, it will start a new session. The --exit-with-
1026 * session option is implied if --autolaunch is given. This option
1027 * is for the exclusive use of libdbus, you do not want to use it
1028 * manually. It may change in the future.
1031 /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
1032 command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
1034 if (G_UNLIKELY (_g_dbus_debug_address ()))
1036 _g_dbus_debug_print_lock ();
1037 g_print ("GDBus-debug:Address: Running `%s' to get bus address (possibly autolaunching)\n", command_line);
1038 old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
1039 restore_dbus_verbose = TRUE;
1040 g_setenv ("DBUS_VERBOSE", "1", TRUE);
1041 _g_dbus_debug_print_unlock ();
1044 if (!g_spawn_command_line_sync (command_line,
1050 g_prefix_error (error, _("Error spawning command line `%s': "), command_line);
1054 if (!WIFEXITED (exit_status))
1056 gchar *escaped_stderr;
1057 escaped_stderr = g_strescape (launch_stderr, "");
1061 _("Abnormal program termination spawning command line `%s': %s"),
1064 g_free (escaped_stderr);
1068 if (WEXITSTATUS (exit_status) != 0)
1070 gchar *escaped_stderr;
1071 escaped_stderr = g_strescape (launch_stderr, "");
1075 _("Command line `%s' exited with non-zero exit status %d: %s"),
1077 WEXITSTATUS (exit_status),
1079 g_free (escaped_stderr);
1083 /* From the dbus-launch(1) man page:
1085 * --binary-syntax Write to stdout a nul-terminated bus address,
1086 * then the bus PID as a binary integer of size sizeof(pid_t),
1087 * then the bus X window ID as a binary integer of size
1088 * sizeof(long). Integers are in the machine's byte order, not
1089 * network byte order or any other canonical byte order.
1091 ret = g_strdup (launch_stdout);
1094 if (G_UNLIKELY (_g_dbus_debug_address ()))
1097 _g_dbus_debug_print_lock ();
1098 g_print ("GDBus-debug:Address: dbus-launch output:");
1099 if (launch_stdout != NULL)
1101 s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
1102 g_print ("\n%s", s);
1107 g_print (" (none)\n");
1109 g_print ("GDBus-debug:Address: dbus-launch stderr output:");
1110 if (launch_stderr != NULL)
1111 g_print ("\n%s", launch_stderr);
1113 g_print (" (none)\n");
1114 _g_dbus_debug_print_unlock ();
1117 g_free (machine_id);
1118 g_free (command_line);
1119 g_free (launch_stdout);
1120 g_free (launch_stderr);
1121 if (G_UNLIKELY (restore_dbus_verbose))
1123 if (old_dbus_verbose != NULL)
1124 g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
1126 g_unsetenv ("DBUS_VERBOSE");
1128 g_free (old_dbus_verbose);
1133 /* ---------------------------------------------------------------------------------------------------- */
1136 get_session_address_platform_specific (GError **error)
1140 /* need to handle OS X in a different way since `dbus-launch --autolaunch' probably won't work there */
1141 ret = get_session_address_dbus_launch (error);
1143 /* TODO: implement for UNIX, Win32 and OS X */
1148 _("Cannot determine session bus address (not implemented for this OS)"));
1153 /* ---------------------------------------------------------------------------------------------------- */
1156 * g_dbus_address_get_for_bus_sync:
1157 * @bus_type: A #GBusType.
1158 * @cancellable: A #GCancellable or %NULL.
1159 * @error: Return location for error or %NULL.
1161 * Synchronously looks up the D-Bus address for the well-known message
1162 * bus instance specified by @bus_type. This may involve using various
1163 * platform specific mechanisms.
1165 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
1170 g_dbus_address_get_for_bus_sync (GBusType bus_type,
1171 GCancellable *cancellable,
1175 const gchar *starter_bus;
1176 GError *local_error;
1178 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1183 if (G_UNLIKELY (_g_dbus_debug_address ()))
1186 _g_dbus_debug_print_lock ();
1187 g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `%s'\n",
1188 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1189 for (n = 0; n < 3; n++)
1195 case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
1196 case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
1197 case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
1198 default: g_assert_not_reached ();
1201 g_print ("GDBus-debug:Address: env var %s", k);
1203 g_print ("=`%s'\n", v);
1205 g_print (" is not set\n");
1207 _g_dbus_debug_print_unlock ();
1212 case G_BUS_TYPE_SYSTEM:
1213 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
1216 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
1220 case G_BUS_TYPE_SESSION:
1221 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
1224 ret = get_session_address_platform_specific (&local_error);
1228 case G_BUS_TYPE_STARTER:
1229 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
1230 if (g_strcmp0 (starter_bus, "session") == 0)
1232 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
1235 else if (g_strcmp0 (starter_bus, "system") == 0)
1237 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
1242 if (starter_bus != NULL)
1244 g_set_error (&local_error,
1247 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1248 " - unknown value `%s'"),
1253 g_set_error_literal (&local_error,
1256 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1257 "variable is not set"));
1263 g_set_error (&local_error,
1266 _("Unknown bus type %d"),
1272 if (G_UNLIKELY (_g_dbus_debug_address ()))
1274 _g_dbus_debug_print_lock ();
1277 g_print ("GDBus-debug:Address: Returning address `%s' for bus type `%s'\n",
1279 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1283 g_print ("GDBus-debug:Address: Cannot look-up address bus type `%s': %s\n",
1284 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type),
1285 local_error->message);
1287 _g_dbus_debug_print_unlock ();
1290 if (local_error != NULL)
1291 g_propagate_error (error, local_error);