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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
29 #include "gdbusaddress.h"
30 #include "gdbusutils.h"
31 #include "gdbusconnection.h"
32 #include "gdbusserver.h"
33 #include "gioenumtypes.h"
34 #include "gdbusprivate.h"
35 #include "gdbusauthobserver.h"
36 #include "ginitable.h"
37 #include "gsocketservice.h"
38 #include "gthreadedsocketservice.h"
39 #include "gresolver.h"
40 #include "glib/gstdio.h"
41 #include "ginetaddress.h"
42 #include "ginetsocketaddress.h"
43 #include "ginputstream.h"
44 #include "giostream.h"
45 #include "gmarshal-internal.h"
55 #include "gunixsocketaddress.h"
60 #define G_DBUS_SERVER_FLAGS_ALL \
61 (G_DBUS_SERVER_FLAGS_RUN_IN_THREAD | \
62 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS | \
63 G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
67 * @short_description: Helper for accepting connections
70 * #GDBusServer is a helper for listening to and accepting D-Bus
71 * connections. This can be used to create a new D-Bus server, allowing two
72 * peers to use the D-Bus protocol for their own specialized communication.
73 * A server instance provided in this way will not perform message routing or
74 * implement the org.freedesktop.DBus interface.
76 * To just export an object on a well-known name on a message bus, such as the
77 * session or system bus, you should instead use g_bus_own_name().
79 * An example of peer-to-peer communication with GDBus can be found
80 * in [gdbus-example-peer.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-peer.c).
82 * Note that a minimal #GDBusServer will accept connections from any
83 * peer. In many use-cases it will be necessary to add a #GDBusAuthObserver
84 * that only accepts connections that have successfully authenticated
85 * as the same user that is running the #GDBusServer. Since GLib 2.68 this can
86 * be achieved more simply by passing the
87 * %G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flag to the server.
93 * The #GDBusServer structure contains only private data and
94 * should only be accessed using the provided API.
101 GObject parent_instance;
103 GDBusServerFlags flags;
110 gchar *client_address;
112 gchar *unix_socket_path;
113 GSocketListener *listener;
114 gboolean is_using_listener;
115 gulong run_signal_handler_id;
117 /* The result of g_main_context_ref_thread_default() when the object
118 * was created (the GObject _init() function) - this is used for delivery
119 * of the :new-connection GObject signal.
121 GMainContext *main_context_at_construction;
125 GDBusAuthObserver *authentication_observer;
128 typedef struct _GDBusServerClass GDBusServerClass;
132 * @new_connection: Signal class handler for the #GDBusServer::new-connection signal.
134 * Class structure for #GDBusServer.
138 struct _GDBusServerClass
141 GObjectClass parent_class;
145 gboolean (*new_connection) (GDBusServer *server,
146 GDBusConnection *connection);
157 PROP_AUTHENTICATION_OBSERVER,
162 NEW_CONNECTION_SIGNAL,
166 static guint _signals[LAST_SIGNAL] = {0};
168 static void initable_iface_init (GInitableIface *initable_iface);
170 G_DEFINE_TYPE_WITH_CODE (GDBusServer, g_dbus_server, G_TYPE_OBJECT,
171 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
174 g_dbus_server_dispose (GObject *object)
176 GDBusServer *server = G_DBUS_SERVER (object);
179 g_dbus_server_stop (server);
181 G_OBJECT_CLASS (g_dbus_server_parent_class)->dispose (object);
185 g_dbus_server_finalize (GObject *object)
187 GDBusServer *server = G_DBUS_SERVER (object);
189 g_assert (!server->active);
191 if (server->authentication_observer != NULL)
192 g_object_unref (server->authentication_observer);
194 if (server->run_signal_handler_id > 0)
195 g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
197 if (server->listener != NULL)
198 g_object_unref (server->listener);
200 g_free (server->address);
201 g_free (server->guid);
202 g_free (server->client_address);
203 if (server->nonce != NULL)
205 memset (server->nonce, '\0', 16);
206 g_free (server->nonce);
209 g_free (server->unix_socket_path);
210 g_free (server->nonce_file);
212 g_main_context_unref (server->main_context_at_construction);
214 G_OBJECT_CLASS (g_dbus_server_parent_class)->finalize (object);
218 g_dbus_server_get_property (GObject *object,
223 GDBusServer *server = G_DBUS_SERVER (object);
228 g_value_set_flags (value, server->flags);
232 g_value_set_string (value, server->guid);
236 g_value_set_string (value, server->address);
239 case PROP_CLIENT_ADDRESS:
240 g_value_set_string (value, server->client_address);
244 g_value_set_boolean (value, server->active);
247 case PROP_AUTHENTICATION_OBSERVER:
248 g_value_set_object (value, server->authentication_observer);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258 g_dbus_server_set_property (GObject *object,
263 GDBusServer *server = G_DBUS_SERVER (object);
268 server->flags = g_value_get_flags (value);
272 server->guid = g_value_dup_string (value);
276 server->address = g_value_dup_string (value);
279 case PROP_AUTHENTICATION_OBSERVER:
280 server->authentication_observer = g_value_dup_object (value);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 g_dbus_server_class_init (GDBusServerClass *klass)
292 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
294 gobject_class->dispose = g_dbus_server_dispose;
295 gobject_class->finalize = g_dbus_server_finalize;
296 gobject_class->set_property = g_dbus_server_set_property;
297 gobject_class->get_property = g_dbus_server_get_property;
302 * Flags from the #GDBusServerFlags enumeration.
306 g_object_class_install_property (gobject_class,
308 g_param_spec_flags ("flags",
310 P_("Flags for the server"),
311 G_TYPE_DBUS_SERVER_FLAGS,
312 G_DBUS_SERVER_FLAGS_NONE,
315 G_PARAM_CONSTRUCT_ONLY |
316 G_PARAM_STATIC_NAME |
317 G_PARAM_STATIC_BLURB |
318 G_PARAM_STATIC_NICK));
323 * The guid of the server.
327 g_object_class_install_property (gobject_class,
329 g_param_spec_string ("guid",
331 P_("The guid of the server"),
335 G_PARAM_CONSTRUCT_ONLY |
336 G_PARAM_STATIC_NAME |
337 G_PARAM_STATIC_BLURB |
338 G_PARAM_STATIC_NICK));
341 * GDBusServer:address:
343 * The D-Bus address to listen on.
347 g_object_class_install_property (gobject_class,
349 g_param_spec_string ("address",
351 P_("The address to listen on"),
355 G_PARAM_CONSTRUCT_ONLY |
356 G_PARAM_STATIC_NAME |
357 G_PARAM_STATIC_BLURB |
358 G_PARAM_STATIC_NICK));
361 * GDBusServer:client-address:
363 * The D-Bus address that clients can use.
367 g_object_class_install_property (gobject_class,
369 g_param_spec_string ("client-address",
370 P_("Client Address"),
371 P_("The address clients can use"),
374 G_PARAM_STATIC_NAME |
375 G_PARAM_STATIC_BLURB |
376 G_PARAM_STATIC_NICK));
379 * GDBusServer:active:
381 * Whether the server is currently active.
385 g_object_class_install_property (gobject_class,
387 g_param_spec_boolean ("active",
389 P_("Whether the server is currently active"),
392 G_PARAM_STATIC_NAME |
393 G_PARAM_STATIC_BLURB |
394 G_PARAM_STATIC_NICK));
397 * GDBusServer:authentication-observer:
399 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
403 g_object_class_install_property (gobject_class,
404 PROP_AUTHENTICATION_OBSERVER,
405 g_param_spec_object ("authentication-observer",
406 P_("Authentication Observer"),
407 P_("Object used to assist in the authentication process"),
408 G_TYPE_DBUS_AUTH_OBSERVER,
411 G_PARAM_CONSTRUCT_ONLY |
412 G_PARAM_STATIC_NAME |
413 G_PARAM_STATIC_BLURB |
414 G_PARAM_STATIC_NICK));
417 * GDBusServer::new-connection:
418 * @server: The #GDBusServer emitting the signal.
419 * @connection: A #GDBusConnection for the new connection.
421 * Emitted when a new authenticated connection has been made. Use
422 * g_dbus_connection_get_peer_credentials() to figure out what
423 * identity (if any), was authenticated.
425 * If you want to accept the connection, take a reference to the
426 * @connection object and return %TRUE. When you are done with the
427 * connection call g_dbus_connection_close() and give up your
428 * reference. Note that the other peer may disconnect at any time -
429 * a typical thing to do when accepting a connection is to listen to
430 * the #GDBusConnection::closed signal.
432 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
433 * then the signal is emitted in a new thread dedicated to the
434 * connection. Otherwise the signal is emitted in the
435 * [thread-default main context][g-main-context-push-thread-default]
436 * of the thread that @server was constructed in.
438 * You are guaranteed that signal handlers for this signal runs
439 * before incoming messages on @connection are processed. This means
440 * that it's suitable to call g_dbus_connection_register_object() or
441 * similar from the signal handler.
443 * Returns: %TRUE to claim @connection, %FALSE to let other handlers
448 _signals[NEW_CONNECTION_SIGNAL] = g_signal_new (I_("new-connection"),
451 G_STRUCT_OFFSET (GDBusServerClass, new_connection),
452 g_signal_accumulator_true_handled,
453 NULL, /* accu_data */
454 _g_cclosure_marshal_BOOLEAN__OBJECT,
457 G_TYPE_DBUS_CONNECTION);
458 g_signal_set_va_marshaller (_signals[NEW_CONNECTION_SIGNAL],
459 G_TYPE_FROM_CLASS (klass),
460 _g_cclosure_marshal_BOOLEAN__OBJECTv);
464 g_dbus_server_init (GDBusServer *server)
466 server->main_context_at_construction = g_main_context_ref_thread_default ();
470 on_run (GSocketService *service,
471 GSocketConnection *socket_connection,
472 GObject *source_object,
476 * g_dbus_server_new_sync:
477 * @address: A D-Bus address.
478 * @flags: Flags from the #GDBusServerFlags enumeration.
479 * @guid: A D-Bus GUID.
480 * @observer: (nullable): A #GDBusAuthObserver or %NULL.
481 * @cancellable: (nullable): A #GCancellable or %NULL.
482 * @error: Return location for server or %NULL.
484 * Creates a new D-Bus server that listens on the first address in
485 * @address that works.
487 * Once constructed, you can use g_dbus_server_get_client_address() to
488 * get a D-Bus address string that clients can use to connect.
490 * To have control over the available authentication mechanisms and
491 * the users that are authorized to connect, it is strongly recommended
492 * to provide a non-%NULL #GDBusAuthObserver.
494 * Connect to the #GDBusServer::new-connection signal to handle
495 * incoming connections.
497 * The returned #GDBusServer isn't active - you have to start it with
498 * g_dbus_server_start().
500 * #GDBusServer is used in this [example][gdbus-peer-to-peer].
502 * This is a synchronous failable constructor. There is currently no
503 * asynchronous version.
505 * Returns: A #GDBusServer or %NULL if @error is set. Free with
511 g_dbus_server_new_sync (const gchar *address,
512 GDBusServerFlags flags,
514 GDBusAuthObserver *observer,
515 GCancellable *cancellable,
520 g_return_val_if_fail (address != NULL, NULL);
521 g_return_val_if_fail (g_dbus_is_guid (guid), NULL);
522 g_return_val_if_fail ((flags & ~G_DBUS_SERVER_FLAGS_ALL) == 0, NULL);
523 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
525 server = g_initable_new (G_TYPE_DBUS_SERVER,
531 "authentication-observer", observer,
538 * g_dbus_server_get_client_address:
539 * @server: A #GDBusServer.
542 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
543 * string that can be used by clients to connect to @server.
545 * Returns: A D-Bus address string. Do not free, the string is owned
551 g_dbus_server_get_client_address (GDBusServer *server)
553 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
554 return server->client_address;
558 * g_dbus_server_get_guid:
559 * @server: A #GDBusServer.
561 * Gets the GUID for @server.
563 * Returns: A D-Bus GUID. Do not free this string, it is owned by @server.
568 g_dbus_server_get_guid (GDBusServer *server)
570 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
575 * g_dbus_server_get_flags:
576 * @server: A #GDBusServer.
578 * Gets the flags for @server.
580 * Returns: A set of flags from the #GDBusServerFlags enumeration.
585 g_dbus_server_get_flags (GDBusServer *server)
587 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
588 return server->flags;
592 * g_dbus_server_is_active:
593 * @server: A #GDBusServer.
595 * Gets whether @server is active.
597 * Returns: %TRUE if server is active, %FALSE otherwise.
602 g_dbus_server_is_active (GDBusServer *server)
604 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
605 return server->active;
609 * g_dbus_server_start:
610 * @server: A #GDBusServer.
617 g_dbus_server_start (GDBusServer *server)
619 g_return_if_fail (G_IS_DBUS_SERVER (server));
622 /* Right now we don't have any transport not using the listener... */
623 g_assert (server->is_using_listener);
624 server->run_signal_handler_id = g_signal_connect_data (G_SOCKET_SERVICE (server->listener),
627 g_object_ref (server),
628 (GClosureNotify) g_object_unref,
630 g_socket_service_start (G_SOCKET_SERVICE (server->listener));
631 server->active = TRUE;
632 g_object_notify (G_OBJECT (server), "active");
636 * g_dbus_server_stop:
637 * @server: A #GDBusServer.
644 g_dbus_server_stop (GDBusServer *server)
646 g_return_if_fail (G_IS_DBUS_SERVER (server));
649 /* Right now we don't have any transport not using the listener... */
650 g_assert (server->is_using_listener);
651 g_assert (server->run_signal_handler_id > 0);
652 g_clear_signal_handler (&server->run_signal_handler_id, server->listener);
653 g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
654 server->active = FALSE;
655 g_object_notify (G_OBJECT (server), "active");
657 if (server->unix_socket_path)
659 if (g_unlink (server->unix_socket_path) != 0)
660 g_warning ("Failed to delete %s: %s", server->unix_socket_path, g_strerror (errno));
663 if (server->nonce_file)
665 if (g_unlink (server->nonce_file) != 0)
666 g_warning ("Failed to delete %s: %s", server->nonce_file, g_strerror (errno));
670 /* ---------------------------------------------------------------------------------------------------- */
678 ret = g_random_int_range (0, 60);
688 /* note that address_entry has already been validated => exactly one of path, dir, tmpdir, or abstract keys are set */
690 try_unix (GDBusServer *server,
691 const gchar *address_entry,
692 GHashTable *key_value_pairs,
699 const gchar *abstract;
700 GSocketAddress *address;
705 path = g_hash_table_lookup (key_value_pairs, "path");
706 dir = g_hash_table_lookup (key_value_pairs, "dir");
707 tmpdir = g_hash_table_lookup (key_value_pairs, "tmpdir");
708 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
712 address = g_unix_socket_address_new (path);
714 else if (dir != NULL || tmpdir != NULL)
721 s = g_string_new (tmpdir != NULL ? tmpdir : dir);
722 g_string_append (s, "/dbus-");
723 for (n = 0; n < 8; n++)
724 g_string_append_c (s, random_ascii ());
726 /* prefer abstract namespace if available for tmpdir: addresses
727 * abstract namespace is disallowed for dir: addresses */
728 if (tmpdir != NULL && g_unix_socket_address_abstract_names_supported ())
729 address = g_unix_socket_address_new_with_type (s->str,
731 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
733 address = g_unix_socket_address_new (s->str);
734 g_string_free (s, TRUE);
737 if (!g_socket_listener_add_address (server->listener,
739 G_SOCKET_TYPE_STREAM,
740 G_SOCKET_PROTOCOL_DEFAULT,
741 NULL, /* source_object */
742 NULL, /* effective_address */
745 if (local_error->domain == G_IO_ERROR && local_error->code == G_IO_ERROR_ADDRESS_IN_USE)
747 g_error_free (local_error);
750 g_propagate_error (error, local_error);
756 else if (abstract != NULL)
758 if (!g_unix_socket_address_abstract_names_supported ())
760 g_set_error_literal (error,
762 G_IO_ERROR_NOT_SUPPORTED,
763 _("Abstract namespace not supported"));
766 address = g_unix_socket_address_new_with_type (abstract,
768 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
772 g_assert_not_reached ();
775 if (!g_socket_listener_add_address (server->listener,
777 G_SOCKET_TYPE_STREAM,
778 G_SOCKET_PROTOCOL_DEFAULT,
779 NULL, /* source_object */
780 NULL, /* effective_address */
790 /* Fill out client_address if the connection attempt worked */
793 const char *address_path;
796 server->is_using_listener = TRUE;
797 address_path = g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address));
798 escaped_path = g_dbus_address_escape_value (address_path);
800 switch (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (address)))
802 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
803 server->client_address = g_strdup_printf ("unix:abstract=%s", escaped_path);
806 case G_UNIX_SOCKET_ADDRESS_PATH:
807 server->client_address = g_strdup_printf ("unix:path=%s", escaped_path);
808 server->unix_socket_path = g_strdup (address_path);
812 g_assert_not_reached ();
816 g_free (escaped_path);
818 g_object_unref (address);
824 /* ---------------------------------------------------------------------------------------------------- */
826 /* note that address_entry has already been validated =>
827 * both host and port (guaranteed to be a number in [0, 65535]) are set (family is optional)
830 try_tcp (GDBusServer *server,
831 const gchar *address_entry,
832 GHashTable *key_value_pairs,
841 GList *resolved_addresses;
846 resolved_addresses = NULL;
848 host = g_hash_table_lookup (key_value_pairs, "host");
849 port = g_hash_table_lookup (key_value_pairs, "port");
850 /* family = g_hash_table_lookup (key_value_pairs, "family"); */
851 if (g_hash_table_lookup (key_value_pairs, "noncefile") != NULL)
853 g_set_error_literal (error,
855 G_IO_ERROR_INVALID_ARGUMENT,
856 _("Cannot specify nonce file when creating a server"));
864 port_num = strtol (port, NULL, 10);
866 resolver = g_resolver_get_default ();
867 resolved_addresses = g_resolver_lookup_by_name (resolver,
871 if (resolved_addresses == NULL)
874 /* TODO: handle family */
875 for (l = resolved_addresses; l != NULL; l = l->next)
877 GInetAddress *address = G_INET_ADDRESS (l->data);
878 GSocketAddress *socket_address;
879 GSocketAddress *effective_address;
881 socket_address = g_inet_socket_address_new (address, port_num);
882 if (!g_socket_listener_add_address (server->listener,
884 G_SOCKET_TYPE_STREAM,
885 G_SOCKET_PROTOCOL_TCP,
886 NULL, /* GObject *source_object */
890 g_object_unref (socket_address);
894 /* make sure we allocate the same port number for other listeners */
895 port_num = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (effective_address));
897 g_object_unref (effective_address);
898 g_object_unref (socket_address);
906 gsize bytes_remaining;
910 server->nonce = g_new0 (guchar, 16);
911 for (n = 0; n < 16; n++)
912 server->nonce[n] = g_random_int_range (0, 256);
913 fd = g_file_open_tmp ("gdbus-nonce-file-XXXXXX",
918 g_socket_listener_close (server->listener);
923 bytes_remaining = 16;
924 while (bytes_remaining > 0)
929 ret = write (fd, server->nonce + bytes_written, bytes_remaining);
937 g_io_error_from_errno (errsv),
938 _("Error writing nonce file at ā%sā: %s"),
943 bytes_written += ret;
944 bytes_remaining -= ret;
946 if (!g_close (fd, error))
948 host_escaped = g_dbus_address_escape_value (host);
949 file_escaped = g_dbus_address_escape_value (server->nonce_file);
950 server->client_address = g_strdup_printf ("nonce-tcp:host=%s,port=%d,noncefile=%s",
954 g_free (host_escaped);
955 g_free (file_escaped);
959 server->client_address = g_strdup_printf ("tcp:host=%s,port=%d", host, port_num);
961 server->is_using_listener = TRUE;
965 g_list_free_full (resolved_addresses, g_object_unref);
967 g_object_unref (resolver);
971 /* ---------------------------------------------------------------------------------------------------- */
976 GDBusConnection *connection;
980 emit_idle_data_free (EmitIdleData *data)
982 g_object_unref (data->server);
983 g_object_unref (data->connection);
988 emit_new_connection_in_idle (gpointer user_data)
990 EmitIdleData *data = user_data;
994 g_signal_emit (data->server,
995 _signals[NEW_CONNECTION_SIGNAL],
1001 g_dbus_connection_start_message_processing (data->connection);
1002 g_object_unref (data->connection);
1007 /* Called in new thread */
1009 on_run (GSocketService *service,
1010 GSocketConnection *socket_connection,
1011 GObject *source_object,
1014 GDBusServer *server = G_DBUS_SERVER (user_data);
1015 GDBusConnection *connection;
1016 GDBusConnectionFlags connection_flags;
1018 if (server->nonce != NULL)
1023 if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)),
1027 NULL, /* GCancellable */
1031 if (bytes_read != 16)
1034 if (memcmp (buf, server->nonce, 16) != 0)
1039 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
1040 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING;
1041 if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)
1042 connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
1043 if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
1044 connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER;
1046 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1049 server->authentication_observer,
1050 NULL, /* GCancellable */
1052 if (connection == NULL)
1055 if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD)
1060 g_signal_emit (server,
1061 _signals[NEW_CONNECTION_SIGNAL],
1066 g_dbus_connection_start_message_processing (connection);
1067 g_object_unref (connection);
1071 GSource *idle_source;
1074 data = g_new0 (EmitIdleData, 1);
1075 data->server = g_object_ref (server);
1076 data->connection = g_object_ref (connection);
1078 idle_source = g_idle_source_new ();
1079 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1080 g_source_set_callback (idle_source,
1081 emit_new_connection_in_idle,
1083 (GDestroyNotify) emit_idle_data_free);
1084 g_source_set_name (idle_source, "[gio] emit_new_connection_in_idle");
1085 g_source_attach (idle_source, server->main_context_at_construction);
1086 g_source_unref (idle_source);
1094 initable_init (GInitable *initable,
1095 GCancellable *cancellable,
1098 GDBusServer *server = G_DBUS_SERVER (initable);
1108 if (!g_dbus_is_guid (server->guid))
1110 g_set_error (&last_error,
1112 G_IO_ERROR_INVALID_ARGUMENT,
1113 _("The string ā%sā is not a valid D-Bus GUID"),
1118 server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
1120 addr_array = g_strsplit (server->address, ";", 0);
1122 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
1124 const gchar *address_entry = addr_array[n];
1125 GHashTable *key_value_pairs;
1126 gchar *transport_name;
1130 if (g_dbus_is_supported_address (address_entry,
1132 _g_dbus_address_parse_entry (address_entry,
1142 else if (g_strcmp0 (transport_name, "unix") == 0)
1143 ret = try_unix (server, address_entry, key_value_pairs, &this_error);
1145 else if (g_strcmp0 (transport_name, "tcp") == 0)
1146 ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
1147 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
1148 ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error);
1150 g_set_error (&this_error,
1152 G_IO_ERROR_INVALID_ARGUMENT,
1153 _("Cannot listen on unsupported transport ā%sā"),
1156 g_free (transport_name);
1157 if (key_value_pairs != NULL)
1158 g_hash_table_unref (key_value_pairs);
1162 g_assert (this_error == NULL);
1167 if (this_error != NULL)
1169 if (last_error != NULL)
1170 g_error_free (last_error);
1171 last_error = this_error;
1177 g_strfreev (addr_array);
1181 g_clear_error (&last_error);
1185 g_assert (last_error != NULL);
1186 g_propagate_error (error, last_error);
1193 initable_iface_init (GInitableIface *initable_iface)
1195 initable_iface->init = initable_init;
1198 /* ---------------------------------------------------------------------------------------------------- */