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"),
261 if (nonce_file == NULL)
265 G_IO_ERROR_INVALID_ARGUMENT,
266 _("Error in address `%s' - missing noncefile attribute"),
274 G_IO_ERROR_INVALID_ARGUMENT,
275 _("Error in address `%s' - missing host attribute"),
289 is_valid_tcp (const gchar *address_entry,
290 GHashTable *key_value_pairs,
308 keys = g_hash_table_get_keys (key_value_pairs);
309 for (l = keys; l != NULL; l = l->next)
311 const gchar *key = l->data;
312 if (g_strcmp0 (key, "host") == 0)
313 host = g_hash_table_lookup (key_value_pairs, key);
314 else if (g_strcmp0 (key, "port") == 0)
315 port = g_hash_table_lookup (key_value_pairs, key);
316 else if (g_strcmp0 (key, "family") == 0)
317 family = g_hash_table_lookup (key_value_pairs, key);
322 G_IO_ERROR_INVALID_ARGUMENT,
323 _("Unsupported key `%s' in address entry `%s'"),
332 port_num = strtol (port, &endp, 10);
333 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
337 G_IO_ERROR_INVALID_ARGUMENT,
338 _("Error in address `%s' - the port attribute is malformed"),
344 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
348 G_IO_ERROR_INVALID_ARGUMENT,
349 _("Error in address `%s' - the family attribute is malformed"),
358 G_IO_ERROR_INVALID_ARGUMENT,
359 _("Error in address `%s' - missing host attribute"),
373 * g_dbus_is_supported_address:
375 * @error: Return location for error or %NULL.
377 * Like g_dbus_is_address() but also checks if the library suppors the
378 * transports in @string and that key/value pairs for each transport
381 * Returns: %TRUE if @string is a valid D-Bus address that is
382 * supported by this library, %FALSE if @error is set.
387 g_dbus_is_supported_address (const gchar *string,
396 g_return_val_if_fail (string != NULL, FALSE);
397 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
399 a = g_strsplit (string, ";", 0);
400 for (n = 0; a[n] != NULL; n++)
402 gchar *transport_name;
403 GHashTable *key_value_pairs;
406 if (!_g_dbus_address_parse_entry (a[n],
413 if (g_strcmp0 (transport_name, "unix") == 0)
414 supported = is_valid_unix (a[n], key_value_pairs, error);
415 else if (g_strcmp0 (transport_name, "tcp") == 0)
416 supported = is_valid_tcp (a[n], key_value_pairs, error);
417 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
418 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
419 else if (g_strcmp0 (a[n], "autolaunch:") == 0)
422 g_free (transport_name);
423 g_hash_table_unref (key_value_pairs);
434 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
440 _g_dbus_address_parse_entry (const gchar *address_entry,
441 gchar **out_transport_name,
442 GHashTable **out_key_value_pairs,
446 GHashTable *key_value_pairs;
447 gchar *transport_name;
454 transport_name = NULL;
455 key_value_pairs = NULL;
457 s = strchr (address_entry, ':');
462 G_IO_ERROR_INVALID_ARGUMENT,
463 _("Address element `%s', does not contain a colon (:)"),
468 transport_name = g_strndup (address_entry, s - address_entry);
469 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
471 kv_pairs = g_strsplit (s + 1, ",", 0);
472 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
474 const gchar *kv_pair = kv_pairs[n];
478 s = strchr (kv_pair, '=');
483 G_IO_ERROR_INVALID_ARGUMENT,
484 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
491 key = g_uri_unescape_segment (kv_pair, s, NULL);
492 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
493 if (key == NULL || value == NULL)
497 G_IO_ERROR_INVALID_ARGUMENT,
498 _("Error unescaping key or value in Key/Value pair %d, `%s', in address element `%s'"),
506 g_hash_table_insert (key_value_pairs, key, value);
512 g_strfreev (kv_pairs);
515 if (out_transport_name != NULL)
516 *out_transport_name = transport_name;
518 g_free (transport_name);
519 if (out_key_value_pairs != NULL)
520 *out_key_value_pairs = key_value_pairs;
521 else if (key_value_pairs != NULL)
522 g_hash_table_unref (key_value_pairs);
526 g_free (transport_name);
527 if (key_value_pairs != NULL)
528 g_hash_table_unref (key_value_pairs);
533 /* ---------------------------------------------------------------------------------------------------- */
536 g_dbus_address_try_connect_one (const gchar *address_entry,
538 GCancellable *cancellable,
541 /* TODO: Declare an extension point called GDBusTransport (or similar)
542 * and move code below to extensions implementing said extension
543 * point. That way we can implement a D-Bus transport over X11 without
544 * making libgio link to libX11...
547 g_dbus_address_connect (const gchar *address_entry,
548 const gchar *transport_name,
549 GHashTable *key_value_pairs,
550 GCancellable *cancellable,
554 GSocketConnectable *connectable;
555 const gchar *nonce_file;
565 else if (g_strcmp0 (transport_name, "unix") == 0)
568 const gchar *abstract;
569 path = g_hash_table_lookup (key_value_pairs, "path");
570 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
571 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
575 G_IO_ERROR_INVALID_ARGUMENT,
576 _("Error in address `%s' - the unix transport requires exactly one of the "
577 "keys `path' or `abstract' to be set"),
580 else if (path != NULL)
582 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
584 else if (abstract != NULL)
586 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
588 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
592 g_assert_not_reached ();
596 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
604 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
606 host = g_hash_table_lookup (key_value_pairs, "host");
611 G_IO_ERROR_INVALID_ARGUMENT,
612 _("Error in address `%s' - the host attribute is missing or malformed"),
617 s = g_hash_table_lookup (key_value_pairs, "port");
620 port = strtol (s, &endp, 10);
621 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
625 G_IO_ERROR_INVALID_ARGUMENT,
626 _("Error in address `%s' - the port attribute is missing or malformed"),
634 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
635 if (nonce_file == NULL)
639 G_IO_ERROR_INVALID_ARGUMENT,
640 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
646 /* TODO: deal with family key/value-pair */
647 connectable = g_network_address_new (host, port);
649 else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
651 gchar *autolaunch_address;
652 autolaunch_address = get_session_address_platform_specific (error);
653 if (autolaunch_address != NULL)
655 ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
656 g_free (autolaunch_address);
661 g_prefix_error (error, _("Error auto-launching: "));
668 G_IO_ERROR_INVALID_ARGUMENT,
669 _("Unknown or unsupported transport `%s' for address `%s'"),
674 if (connectable != NULL)
676 GSocketClient *client;
677 GSocketConnection *connection;
679 g_assert (ret == NULL);
680 client = g_socket_client_new ();
681 connection = g_socket_client_connect (client,
685 g_object_unref (connectable);
686 g_object_unref (client);
687 if (connection == NULL)
690 ret = G_IO_STREAM (connection);
692 if (nonce_file != NULL)
694 gchar nonce_contents[16 + 1];
695 size_t num_bytes_read;
698 /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
699 f = fopen (nonce_file, "rb");
704 G_IO_ERROR_INVALID_ARGUMENT,
705 _("Error opening nonce file `%s': %s"),
708 g_object_unref (ret);
712 num_bytes_read = fread (nonce_contents,
716 if (num_bytes_read != 16)
718 if (num_bytes_read == 0)
722 G_IO_ERROR_INVALID_ARGUMENT,
723 _("Error reading from nonce file `%s': %s"),
731 G_IO_ERROR_INVALID_ARGUMENT,
732 _("Error reading from nonce file `%s', expected 16 bytes, got %d"),
734 (gint) num_bytes_read);
736 g_object_unref (ret);
743 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
750 g_prefix_error (error, _("Error writing contents of nonce file `%s' to stream:"), nonce_file);
751 g_object_unref (ret);
764 g_dbus_address_try_connect_one (const gchar *address_entry,
766 GCancellable *cancellable,
770 GHashTable *key_value_pairs;
771 gchar *transport_name;
775 transport_name = NULL;
776 key_value_pairs = NULL;
778 if (!_g_dbus_address_parse_entry (address_entry,
784 ret = g_dbus_address_connect (address_entry,
792 guid = g_hash_table_lookup (key_value_pairs, "guid");
793 if (guid != NULL && out_guid != NULL)
794 *out_guid = g_strdup (guid);
797 g_free (transport_name);
798 if (key_value_pairs != NULL)
799 g_hash_table_unref (key_value_pairs);
804 /* ---------------------------------------------------------------------------------------------------- */
813 get_stream_data_free (GetStreamData *data)
815 g_free (data->address);
816 if (data->stream != NULL)
817 g_object_unref (data->stream);
823 get_stream_thread_func (GSimpleAsyncResult *res,
825 GCancellable *cancellable)
830 data = g_simple_async_result_get_op_res_gpointer (res);
833 data->stream = g_dbus_address_get_stream_sync (data->address,
837 if (data->stream == NULL)
838 g_simple_async_result_take_error (res, error);
842 * g_dbus_address_get_stream:
843 * @address: A valid D-Bus address.
844 * @cancellable: A #GCancellable or %NULL.
845 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
846 * @user_data: Data to pass to @callback.
848 * Asynchronously connects to an endpoint specified by @address and
849 * sets up the connection so it is in a state to run the client-side
850 * of the D-Bus authentication conversation.
852 * When the operation is finished, @callback will be invoked. You can
853 * then call g_dbus_address_get_stream_finish() to get the result of
856 * This is an asynchronous failable function. See
857 * g_dbus_address_get_stream_sync() for the synchronous version.
862 g_dbus_address_get_stream (const gchar *address,
863 GCancellable *cancellable,
864 GAsyncReadyCallback callback,
867 GSimpleAsyncResult *res;
870 g_return_if_fail (address != NULL);
872 res = g_simple_async_result_new (NULL,
875 g_dbus_address_get_stream);
876 data = g_new0 (GetStreamData, 1);
877 data->address = g_strdup (address);
878 g_simple_async_result_set_op_res_gpointer (res,
880 (GDestroyNotify) get_stream_data_free);
881 g_simple_async_result_run_in_thread (res,
882 get_stream_thread_func,
885 g_object_unref (res);
889 * g_dbus_address_get_stream_finish:
890 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
891 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
892 * @error: Return location for error or %NULL.
894 * Finishes an operation started with g_dbus_address_get_stream().
896 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
901 g_dbus_address_get_stream_finish (GAsyncResult *res,
905 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
909 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
910 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
912 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
916 data = g_simple_async_result_get_op_res_gpointer (simple);
917 if (g_simple_async_result_propagate_error (simple, error))
920 ret = g_object_ref (data->stream);
921 if (out_guid != NULL)
922 *out_guid = g_strdup (data->guid);
929 * g_dbus_address_get_stream_sync:
930 * @address: A valid D-Bus address.
931 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
932 * @cancellable: A #GCancellable or %NULL.
933 * @error: Return location for error or %NULL.
935 * Synchronously connects to an endpoint specified by @address and
936 * sets up the connection so it is in a state to run the client-side
937 * of the D-Bus authentication conversation.
939 * This is a synchronous failable function. See
940 * g_dbus_address_get_stream() for the asynchronous version.
942 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
947 g_dbus_address_get_stream_sync (const gchar *address,
949 GCancellable *cancellable,
957 g_return_val_if_fail (address != NULL, NULL);
958 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
963 addr_array = g_strsplit (address, ";", 0);
964 if (addr_array[0] == NULL)
966 last_error = g_error_new_literal (G_IO_ERROR,
967 G_IO_ERROR_INVALID_ARGUMENT,
968 _("The given address is empty"));
972 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
974 const gchar *addr = addr_array[n];
978 ret = g_dbus_address_try_connect_one (addr,
988 g_assert (this_error != NULL);
989 if (last_error != NULL)
990 g_error_free (last_error);
991 last_error = this_error;
998 if (last_error != NULL)
999 g_error_free (last_error);
1003 g_assert (last_error != NULL);
1004 g_propagate_error (error, last_error);
1007 g_strfreev (addr_array);
1011 /* ---------------------------------------------------------------------------------------------------- */
1015 get_session_address_dbus_launch (GError **error)
1019 gchar *command_line;
1020 gchar *launch_stdout;
1021 gchar *launch_stderr;
1023 gchar *old_dbus_verbose;
1024 gboolean restore_dbus_verbose;
1028 command_line = NULL;
1029 launch_stdout = NULL;
1030 launch_stderr = NULL;
1031 restore_dbus_verbose = FALSE;
1032 old_dbus_verbose = NULL;
1034 machine_id = _g_dbus_get_machine_id (error);
1035 if (machine_id == NULL)
1037 g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
1041 /* We're using private libdbus facilities here. When everything
1042 * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
1043 * X11 property is correctly documented right now) we should
1044 * consider using the spec instead of dbus-launch.
1046 * --autolaunch=MACHINEID
1047 * This option implies that dbus-launch should scan for a previ‐
1048 * ously-started session and reuse the values found there. If no
1049 * session is found, it will start a new session. The --exit-with-
1050 * session option is implied if --autolaunch is given. This option
1051 * is for the exclusive use of libdbus, you do not want to use it
1052 * manually. It may change in the future.
1055 /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
1056 command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
1058 if (G_UNLIKELY (_g_dbus_debug_address ()))
1060 _g_dbus_debug_print_lock ();
1061 g_print ("GDBus-debug:Address: Running `%s' to get bus address (possibly autolaunching)\n", command_line);
1062 old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
1063 restore_dbus_verbose = TRUE;
1064 g_setenv ("DBUS_VERBOSE", "1", TRUE);
1065 _g_dbus_debug_print_unlock ();
1068 if (!g_spawn_command_line_sync (command_line,
1074 g_prefix_error (error, _("Error spawning command line `%s': "), command_line);
1078 if (!WIFEXITED (exit_status))
1080 gchar *escaped_stderr;
1081 escaped_stderr = g_strescape (launch_stderr, "");
1085 _("Abnormal program termination spawning command line `%s': %s"),
1088 g_free (escaped_stderr);
1092 if (WEXITSTATUS (exit_status) != 0)
1094 gchar *escaped_stderr;
1095 escaped_stderr = g_strescape (launch_stderr, "");
1099 _("Command line `%s' exited with non-zero exit status %d: %s"),
1101 WEXITSTATUS (exit_status),
1103 g_free (escaped_stderr);
1107 /* From the dbus-launch(1) man page:
1109 * --binary-syntax Write to stdout a nul-terminated bus address,
1110 * then the bus PID as a binary integer of size sizeof(pid_t),
1111 * then the bus X window ID as a binary integer of size
1112 * sizeof(long). Integers are in the machine's byte order, not
1113 * network byte order or any other canonical byte order.
1115 ret = g_strdup (launch_stdout);
1118 if (G_UNLIKELY (_g_dbus_debug_address ()))
1121 _g_dbus_debug_print_lock ();
1122 g_print ("GDBus-debug:Address: dbus-launch output:");
1123 if (launch_stdout != NULL)
1125 s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
1126 g_print ("\n%s", s);
1131 g_print (" (none)\n");
1133 g_print ("GDBus-debug:Address: dbus-launch stderr output:");
1134 if (launch_stderr != NULL)
1135 g_print ("\n%s", launch_stderr);
1137 g_print (" (none)\n");
1138 _g_dbus_debug_print_unlock ();
1141 g_free (machine_id);
1142 g_free (command_line);
1143 g_free (launch_stdout);
1144 g_free (launch_stderr);
1145 if (G_UNLIKELY (restore_dbus_verbose))
1147 if (old_dbus_verbose != NULL)
1148 g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
1150 g_unsetenv ("DBUS_VERBOSE");
1152 g_free (old_dbus_verbose);
1157 /* ---------------------------------------------------------------------------------------------------- */
1160 get_session_address_platform_specific (GError **error)
1164 /* need to handle OS X in a different way since `dbus-launch --autolaunch' probably won't work there */
1165 ret = get_session_address_dbus_launch (error);
1167 /* TODO: implement for UNIX, Win32 and OS X */
1172 _("Cannot determine session bus address (not implemented for this OS)"));
1177 /* ---------------------------------------------------------------------------------------------------- */
1180 * g_dbus_address_get_for_bus_sync:
1181 * @bus_type: A #GBusType.
1182 * @cancellable: A #GCancellable or %NULL.
1183 * @error: Return location for error or %NULL.
1185 * Synchronously looks up the D-Bus address for the well-known message
1186 * bus instance specified by @bus_type. This may involve using various
1187 * platform specific mechanisms.
1189 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
1194 g_dbus_address_get_for_bus_sync (GBusType bus_type,
1195 GCancellable *cancellable,
1199 const gchar *starter_bus;
1200 GError *local_error;
1202 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1207 if (G_UNLIKELY (_g_dbus_debug_address ()))
1210 _g_dbus_debug_print_lock ();
1211 g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `%s'\n",
1212 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1213 for (n = 0; n < 3; n++)
1219 case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
1220 case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
1221 case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
1222 default: g_assert_not_reached ();
1225 g_print ("GDBus-debug:Address: env var %s", k);
1227 g_print ("=`%s'\n", v);
1229 g_print (" is not set\n");
1231 _g_dbus_debug_print_unlock ();
1236 case G_BUS_TYPE_SYSTEM:
1237 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
1240 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
1244 case G_BUS_TYPE_SESSION:
1245 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
1248 ret = get_session_address_platform_specific (&local_error);
1252 case G_BUS_TYPE_STARTER:
1253 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
1254 if (g_strcmp0 (starter_bus, "session") == 0)
1256 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
1259 else if (g_strcmp0 (starter_bus, "system") == 0)
1261 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
1266 if (starter_bus != NULL)
1268 g_set_error (&local_error,
1271 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1272 " - unknown value `%s'"),
1277 g_set_error_literal (&local_error,
1280 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1281 "variable is not set"));
1287 g_set_error (&local_error,
1290 _("Unknown bus type %d"),
1296 if (G_UNLIKELY (_g_dbus_debug_address ()))
1298 _g_dbus_debug_print_lock ();
1301 g_print ("GDBus-debug:Address: Returning address `%s' for bus type `%s'\n",
1303 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type));
1307 g_print ("GDBus-debug:Address: Cannot look-up address bus type `%s': %s\n",
1308 _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type),
1309 local_error->message);
1311 _g_dbus_debug_print_unlock ();
1314 if (local_error != NULL)
1315 g_propagate_error (error, local_error);