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>
30 #include "gdbusutils.h"
31 #include "gdbusaddress.h"
32 #include "gdbuserror.h"
33 #include "gioenumtypes.h"
34 #include "gnetworkaddress.h"
35 #include "gsocketclient.h"
36 #include "giostream.h"
37 #include "gasyncresult.h"
38 #include "gsimpleasyncresult.h"
39 #include "gdbusprivate.h"
42 #include <gio/gunixsocketaddress.h>
49 * SECTION:gdbusaddress
50 * @title: D-Bus Addresses
51 * @short_description: D-Bus connection endpoints
54 * Routines for working with D-Bus addresses. A D-Bus address is a string
55 * like "unix:tmpdir=/tmp/my-app-name". The exact format of addresses
56 * is explained in detail in the <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html#addresses">D-Bus specification</link>.
59 static gchar *get_session_address_platform_specific (GError **error);
61 /* ---------------------------------------------------------------------------------------------------- */
67 * Checks if @string is a D-Bus address.
69 * This doesn't check if @string is actually supported by #GDBusServer
70 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
73 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
78 g_dbus_is_address (const gchar *string)
86 g_return_val_if_fail (string != NULL, FALSE);
88 a = g_strsplit (string, ";", 0);
92 for (n = 0; a[n] != NULL; n++)
94 if (!_g_dbus_address_parse_entry (a[n],
109 is_valid_unix (const gchar *address_entry,
110 GHashTable *key_value_pairs,
118 const gchar *abstract;
126 keys = g_hash_table_get_keys (key_value_pairs);
127 for (l = keys; l != NULL; l = l->next)
129 const gchar *key = l->data;
130 if (g_strcmp0 (key, "path") == 0)
131 path = g_hash_table_lookup (key_value_pairs, key);
132 else if (g_strcmp0 (key, "tmpdir") == 0)
133 tmpdir = g_hash_table_lookup (key_value_pairs, key);
134 else if (g_strcmp0 (key, "abstract") == 0)
135 abstract = g_hash_table_lookup (key_value_pairs, key);
140 G_IO_ERROR_INVALID_ARGUMENT,
141 _("Unsupported key `%s' in address entry `%s'"),
150 if (tmpdir != NULL || abstract != NULL)
152 /* TODO: validate path */
154 else if (tmpdir != NULL)
156 if (path != NULL || abstract != NULL)
158 /* TODO: validate tmpdir */
160 else if (abstract != NULL)
162 if (path != NULL || tmpdir != NULL)
164 /* TODO: validate abstract */
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 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
465 key = g_uri_unescape_segment (kv_pair, s, NULL);
466 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
467 g_hash_table_insert (key_value_pairs, key, value);
473 g_strfreev (kv_pairs);
476 if (out_transport_name != NULL)
477 *out_transport_name = transport_name;
479 g_free (transport_name);
480 if (out_key_value_pairs != NULL)
481 *out_key_value_pairs = key_value_pairs;
482 else if (key_value_pairs != NULL)
483 g_hash_table_unref (key_value_pairs);
487 g_free (transport_name);
488 if (key_value_pairs != NULL)
489 g_hash_table_unref (key_value_pairs);
494 /* ---------------------------------------------------------------------------------------------------- */
497 g_dbus_address_try_connect_one (const gchar *address_entry,
499 GCancellable *cancellable,
502 /* TODO: Declare an extension point called GDBusTransport (or similar)
503 * and move code below to extensions implementing said extension
504 * point. That way we can implement a D-Bus transport over X11 without
505 * making libgio link to libX11...
508 g_dbus_address_connect (const gchar *address_entry,
509 const gchar *transport_name,
510 GHashTable *key_value_pairs,
511 GCancellable *cancellable,
515 GSocketConnectable *connectable;
516 const gchar *nonce_file;
526 else if (g_strcmp0 (transport_name, "unix") == 0)
529 const gchar *abstract;
530 path = g_hash_table_lookup (key_value_pairs, "path");
531 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
532 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
536 G_IO_ERROR_INVALID_ARGUMENT,
537 _("Error in address `%s' - the unix transport requires exactly one of the "
538 "keys `path' or `abstract' to be set"),
541 else if (path != NULL)
543 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
545 else if (abstract != NULL)
547 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
549 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
553 g_assert_not_reached ();
557 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
565 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
567 host = g_hash_table_lookup (key_value_pairs, "host");
572 G_IO_ERROR_INVALID_ARGUMENT,
573 _("Error in address `%s' - the host attribute is missing or malformed"),
578 s = g_hash_table_lookup (key_value_pairs, "port");
581 port = strtol (s, &endp, 10);
582 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
586 G_IO_ERROR_INVALID_ARGUMENT,
587 _("Error in address `%s' - the port attribute is missing or malformed"),
595 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
596 if (nonce_file == NULL)
600 G_IO_ERROR_INVALID_ARGUMENT,
601 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
607 /* TODO: deal with family */
608 connectable = g_network_address_new (host, port);
610 else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
612 gchar *autolaunch_address;
613 autolaunch_address = get_session_address_platform_specific (error);
614 if (autolaunch_address != NULL)
616 ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
617 g_free (autolaunch_address);
622 g_prefix_error (error, _("Error auto-launching: "));
629 G_IO_ERROR_INVALID_ARGUMENT,
630 _("Unknown or unsupported transport `%s' for address `%s'"),
635 if (connectable != NULL)
637 GSocketClient *client;
638 GSocketConnection *connection;
640 g_assert (ret == NULL);
641 client = g_socket_client_new ();
642 connection = g_socket_client_connect (client,
646 g_object_unref (connectable);
647 g_object_unref (client);
648 if (connection == NULL)
651 ret = G_IO_STREAM (connection);
653 if (nonce_file != NULL)
655 gchar *nonce_contents;
658 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
659 if (!g_file_get_contents (nonce_file,
664 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
665 g_object_unref (ret);
670 if (nonce_length != 16)
674 G_IO_ERROR_INVALID_ARGUMENT,
675 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
678 g_free (nonce_contents);
679 g_object_unref (ret);
684 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
691 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
692 g_object_unref (ret);
694 g_free (nonce_contents);
697 g_free (nonce_contents);
707 g_dbus_address_try_connect_one (const gchar *address_entry,
709 GCancellable *cancellable,
713 GHashTable *key_value_pairs;
714 gchar *transport_name;
718 transport_name = NULL;
719 key_value_pairs = NULL;
721 if (!_g_dbus_address_parse_entry (address_entry,
727 ret = g_dbus_address_connect (address_entry,
735 /* TODO: validate that guid is of correct format */
736 guid = g_hash_table_lookup (key_value_pairs, "guid");
737 if (guid != NULL && out_guid != NULL)
738 *out_guid = g_strdup (guid);
741 g_free (transport_name);
742 if (key_value_pairs != NULL)
743 g_hash_table_unref (key_value_pairs);
748 /* ---------------------------------------------------------------------------------------------------- */
757 get_stream_data_free (GetStreamData *data)
759 g_free (data->address);
760 if (data->stream != NULL)
761 g_object_unref (data->stream);
767 get_stream_thread_func (GSimpleAsyncResult *res,
769 GCancellable *cancellable)
774 data = g_simple_async_result_get_op_res_gpointer (res);
777 data->stream = g_dbus_address_get_stream_sync (data->address,
781 if (data->stream == NULL)
783 g_simple_async_result_set_from_error (res, error);
784 g_error_free (error);
789 * g_dbus_address_get_stream:
790 * @address: A valid D-Bus address.
791 * @cancellable: A #GCancellable or %NULL.
792 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
793 * @user_data: Data to pass to @callback.
795 * Asynchronously connects to an endpoint specified by @address and
796 * sets up the connection so it is in a state to run the client-side
797 * of the D-Bus authentication conversation.
799 * When the operation is finished, @callback will be invoked. You can
800 * then call g_dbus_address_get_stream_finish() to get the result of
803 * This is an asynchronous failable function. See
804 * g_dbus_address_get_stream_sync() for the synchronous version.
809 g_dbus_address_get_stream (const gchar *address,
810 GCancellable *cancellable,
811 GAsyncReadyCallback callback,
814 GSimpleAsyncResult *res;
817 g_return_if_fail (address != NULL);
819 res = g_simple_async_result_new (NULL,
822 g_dbus_address_get_stream);
823 data = g_new0 (GetStreamData, 1);
824 data->address = g_strdup (address);
825 g_simple_async_result_set_op_res_gpointer (res,
827 (GDestroyNotify) get_stream_data_free);
828 g_simple_async_result_run_in_thread (res,
829 get_stream_thread_func,
832 g_object_unref (res);
836 * g_dbus_address_get_stream_finish:
837 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
838 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
839 * @error: Return location for error or %NULL.
841 * Finishes an operation started with g_dbus_address_get_stream().
843 * Returns: A #GIOStream or %NULL if @error is set.
848 g_dbus_address_get_stream_finish (GAsyncResult *res,
852 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
856 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
857 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
859 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
863 data = g_simple_async_result_get_op_res_gpointer (simple);
864 if (g_simple_async_result_propagate_error (simple, error))
867 ret = g_object_ref (data->stream);
868 if (out_guid != NULL)
869 *out_guid = g_strdup (data->guid);
876 * g_dbus_address_get_stream_sync:
877 * @address: A valid D-Bus address.
878 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
879 * @cancellable: A #GCancellable or %NULL.
880 * @error: Return location for error or %NULL.
882 * Synchronously connects to an endpoint specified by @address and
883 * sets up the connection so it is in a state to run the client-side
884 * of the D-Bus authentication conversation.
886 * This is a synchronous failable function. See
887 * g_dbus_address_get_stream() for the asynchronous version.
889 * Returns: A #GIOStream or %NULL if @error is set.
894 g_dbus_address_get_stream_sync (const gchar *address,
896 GCancellable *cancellable,
904 g_return_val_if_fail (address != NULL, NULL);
905 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
910 addr_array = g_strsplit (address, ";", 0);
911 if (addr_array[0] == NULL)
913 last_error = g_error_new_literal (G_IO_ERROR,
914 G_IO_ERROR_INVALID_ARGUMENT,
915 _("The given address is empty"));
919 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
921 const gchar *addr = addr_array[n];
925 ret = g_dbus_address_try_connect_one (addr,
935 g_assert (this_error != NULL);
936 if (last_error != NULL)
937 g_error_free (last_error);
938 last_error = this_error;
945 if (last_error != NULL)
946 g_error_free (last_error);
950 g_assert (last_error != NULL);
951 g_propagate_error (error, last_error);
954 g_strfreev (addr_array);
958 /* ---------------------------------------------------------------------------------------------------- */
962 get_session_address_dbus_launch (GError **error)
967 gchar *launch_stdout;
968 gchar *launch_stderr;
970 gchar *old_dbus_verbose;
971 gboolean restore_dbus_verbose;
976 launch_stdout = NULL;
977 launch_stderr = NULL;
978 restore_dbus_verbose = FALSE;
979 old_dbus_verbose = NULL;
981 machine_id = _g_dbus_get_machine_id (error);
982 if (machine_id == NULL)
984 g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
988 /* We're using private libdbus facilities here. When everything
989 * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
990 * X11 property is correctly documented right now) we should
991 * consider using the spec instead of dbus-launch.
993 * --autolaunch=MACHINEID
994 * This option implies that dbus-launch should scan for a previ‐
995 * ously-started session and reuse the values found there. If no
996 * session is found, it will start a new session. The --exit-with-
997 * session option is implied if --autolaunch is given. This option
998 * is for the exclusive use of libdbus, you do not want to use it
999 * manually. It may change in the future.
1002 /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
1003 command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
1005 if (G_UNLIKELY (_g_dbus_debug_address ()))
1007 _g_dbus_debug_print_lock ();
1008 g_print ("GDBus-debug:Address: Running `%s' to get bus address (possibly autolaunching)\n", command_line);
1009 old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
1010 restore_dbus_verbose = TRUE;
1011 g_setenv ("DBUS_VERBOSE", "1", TRUE);
1012 _g_dbus_debug_print_unlock ();
1015 if (!g_spawn_command_line_sync (command_line,
1021 g_prefix_error (error, _("Error spawning command line `%s': "), command_line);
1025 if (!WIFEXITED (exit_status))
1027 gchar *escaped_stderr;
1028 escaped_stderr = g_strescape (launch_stderr, "");
1032 _("Abnormal program termination spawning command line `%s': %s"),
1035 g_free (escaped_stderr);
1039 if (WEXITSTATUS (exit_status) != 0)
1041 gchar *escaped_stderr;
1042 escaped_stderr = g_strescape (launch_stderr, "");
1046 _("Command line `%s' exited with non-zero exit status %d: %s"),
1048 WEXITSTATUS (exit_status),
1050 g_free (escaped_stderr);
1054 /* From the dbus-launch(1) man page:
1056 * --binary-syntax Write to stdout a nul-terminated bus address,
1057 * then the bus PID as a binary integer of size sizeof(pid_t),
1058 * then the bus X window ID as a binary integer of size
1059 * sizeof(long). Integers are in the machine's byte order, not
1060 * network byte order or any other canonical byte order.
1062 ret = g_strdup (launch_stdout);
1065 if (G_UNLIKELY (_g_dbus_debug_address ()))
1068 _g_dbus_debug_print_lock ();
1069 g_print ("GDBus-debug:Address: dbus-launch output:");
1070 if (launch_stdout != NULL)
1072 s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
1073 g_print ("\n%s", s);
1078 g_print (" (none)\n");
1080 g_print ("GDBus-debug:Address: dbus-launch stderr output:");
1081 if (launch_stderr != NULL)
1082 g_print ("\n%s", launch_stderr);
1084 g_print (" (none)\n");
1085 _g_dbus_debug_print_unlock ();
1088 g_free (machine_id);
1089 g_free (command_line);
1090 g_free (launch_stdout);
1091 g_free (launch_stderr);
1092 if (G_UNLIKELY (restore_dbus_verbose))
1094 if (old_dbus_verbose != NULL)
1095 g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
1097 g_unsetenv ("DBUS_VERBOSE");
1099 g_free (old_dbus_verbose);
1104 /* ---------------------------------------------------------------------------------------------------- */
1106 /* TODO: implement for UNIX, Win32 and OS X */
1108 get_session_address_platform_specific (GError **error)
1112 /* need to handle OS X in a different way since `dbus-launch --autolaunch' probably won't work there */
1113 ret = get_session_address_dbus_launch (error);
1119 _("Cannot determine session bus address (not implemented for this OS)"));
1124 /* ---------------------------------------------------------------------------------------------------- */
1127 * g_dbus_address_get_for_bus_sync:
1128 * @bus_type: A #GBusType.
1129 * @cancellable: A #GCancellable or %NULL.
1130 * @error: Return location for error or %NULL.
1132 * Synchronously looks up the D-Bus address for the well-known message
1133 * bus instance specified by @bus_type. This may involve using various
1134 * platform specific mechanisms.
1136 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
1141 g_dbus_address_get_for_bus_sync (GBusType bus_type,
1142 GCancellable *cancellable,
1146 const gchar *starter_bus;
1147 GError *local_error;
1149 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1154 if (G_UNLIKELY (_g_dbus_debug_address ()))
1157 _g_dbus_debug_print_lock ();
1158 g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `%s'\n",
1159 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1160 for (n = 0; n < 3; n++)
1166 case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
1167 case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
1168 case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
1169 default: g_assert_not_reached ();
1172 g_print ("GDBus-debug:Address: env var %s", k);
1174 g_print ("=`%s'\n", v);
1176 g_print (" is not set\n");
1178 _g_dbus_debug_print_unlock ();
1183 case G_BUS_TYPE_SYSTEM:
1184 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
1187 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
1191 case G_BUS_TYPE_SESSION:
1192 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
1195 ret = get_session_address_platform_specific (&local_error);
1199 case G_BUS_TYPE_STARTER:
1200 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
1201 if (g_strcmp0 (starter_bus, "session") == 0)
1203 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
1206 else if (g_strcmp0 (starter_bus, "system") == 0)
1208 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
1213 if (starter_bus != NULL)
1215 g_set_error (&local_error,
1218 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1219 " - unknown value `%s'"),
1224 g_set_error_literal (&local_error,
1227 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1228 "variable is not set"));
1234 g_set_error (&local_error,
1237 _("Unknown bus type %d"),
1243 if (G_UNLIKELY (_g_dbus_debug_address ()))
1245 _g_dbus_debug_print_lock ();
1248 g_print ("GDBus-debug:Address: Returning address `%s' for bus type `%s'\n",
1250 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1254 g_print ("GDBus-debug:Address: Cannot look-up address bus type `%s': %s\n",
1255 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type),
1256 local_error->message);
1258 _g_dbus_debug_print_unlock ();
1261 if (local_error != NULL)
1262 g_propagate_error (error, local_error);
1267 #define __G_DBUS_ADDRESS_C__
1268 #include "gioaliasdef.c"