1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
31 #include "gdbusaddress.h"
32 #include "gdbusutils.h"
33 #include "gdbusconnection.h"
34 #include "gdbusserver.h"
35 #include "gioenumtypes.h"
36 #include "gdbusprivate.h"
37 #include "gdbusauthobserver.h"
38 #include "ginitable.h"
39 #include "gsocketservice.h"
40 #include "gthreadedsocketservice.h"
41 #include "gresolver.h"
42 #include "glib/gstdio.h"
43 #include "ginetaddress.h"
44 #include "ginetsocketaddress.h"
45 #include "ginputstream.h"
46 #include "giostream.h"
47 #include "gmarshal-internal.h"
56 #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://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/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.
325 * See #GDBusConnection:guid for more details.
329 g_object_class_install_property (gobject_class,
331 g_param_spec_string ("guid",
333 P_("The guid of the server"),
337 G_PARAM_CONSTRUCT_ONLY |
338 G_PARAM_STATIC_NAME |
339 G_PARAM_STATIC_BLURB |
340 G_PARAM_STATIC_NICK));
343 * GDBusServer:address:
345 * The D-Bus address to listen on.
349 g_object_class_install_property (gobject_class,
351 g_param_spec_string ("address",
353 P_("The address to listen on"),
357 G_PARAM_CONSTRUCT_ONLY |
358 G_PARAM_STATIC_NAME |
359 G_PARAM_STATIC_BLURB |
360 G_PARAM_STATIC_NICK));
363 * GDBusServer:client-address:
365 * The D-Bus address that clients can use.
369 g_object_class_install_property (gobject_class,
371 g_param_spec_string ("client-address",
372 P_("Client Address"),
373 P_("The address clients can use"),
376 G_PARAM_STATIC_NAME |
377 G_PARAM_STATIC_BLURB |
378 G_PARAM_STATIC_NICK));
381 * GDBusServer:active:
383 * Whether the server is currently active.
387 g_object_class_install_property (gobject_class,
389 g_param_spec_boolean ("active",
391 P_("Whether the server is currently active"),
394 G_PARAM_STATIC_NAME |
395 G_PARAM_STATIC_BLURB |
396 G_PARAM_STATIC_NICK));
399 * GDBusServer:authentication-observer:
401 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
405 g_object_class_install_property (gobject_class,
406 PROP_AUTHENTICATION_OBSERVER,
407 g_param_spec_object ("authentication-observer",
408 P_("Authentication Observer"),
409 P_("Object used to assist in the authentication process"),
410 G_TYPE_DBUS_AUTH_OBSERVER,
413 G_PARAM_CONSTRUCT_ONLY |
414 G_PARAM_STATIC_NAME |
415 G_PARAM_STATIC_BLURB |
416 G_PARAM_STATIC_NICK));
419 * GDBusServer::new-connection:
420 * @server: The #GDBusServer emitting the signal.
421 * @connection: A #GDBusConnection for the new connection.
423 * Emitted when a new authenticated connection has been made. Use
424 * g_dbus_connection_get_peer_credentials() to figure out what
425 * identity (if any), was authenticated.
427 * If you want to accept the connection, take a reference to the
428 * @connection object and return %TRUE. When you are done with the
429 * connection call g_dbus_connection_close() and give up your
430 * reference. Note that the other peer may disconnect at any time -
431 * a typical thing to do when accepting a connection is to listen to
432 * the #GDBusConnection::closed signal.
434 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
435 * then the signal is emitted in a new thread dedicated to the
436 * connection. Otherwise the signal is emitted in the
437 * [thread-default main context][g-main-context-push-thread-default]
438 * of the thread that @server was constructed in.
440 * You are guaranteed that signal handlers for this signal runs
441 * before incoming messages on @connection are processed. This means
442 * that it's suitable to call g_dbus_connection_register_object() or
443 * similar from the signal handler.
445 * Returns: %TRUE to claim @connection, %FALSE to let other handlers
450 _signals[NEW_CONNECTION_SIGNAL] = g_signal_new (I_("new-connection"),
453 G_STRUCT_OFFSET (GDBusServerClass, new_connection),
454 g_signal_accumulator_true_handled,
455 NULL, /* accu_data */
456 _g_cclosure_marshal_BOOLEAN__OBJECT,
459 G_TYPE_DBUS_CONNECTION);
460 g_signal_set_va_marshaller (_signals[NEW_CONNECTION_SIGNAL],
461 G_TYPE_FROM_CLASS (klass),
462 _g_cclosure_marshal_BOOLEAN__OBJECTv);
466 g_dbus_server_init (GDBusServer *server)
468 server->main_context_at_construction = g_main_context_ref_thread_default ();
472 on_run (GSocketService *service,
473 GSocketConnection *socket_connection,
474 GObject *source_object,
478 * g_dbus_server_new_sync:
479 * @address: A D-Bus address.
480 * @flags: Flags from the #GDBusServerFlags enumeration.
481 * @guid: A D-Bus GUID.
482 * @observer: (nullable): A #GDBusAuthObserver or %NULL.
483 * @cancellable: (nullable): A #GCancellable or %NULL.
484 * @error: Return location for server or %NULL.
486 * Creates a new D-Bus server that listens on the first address in
487 * @address that works.
489 * Once constructed, you can use g_dbus_server_get_client_address() to
490 * get a D-Bus address string that clients can use to connect.
492 * To have control over the available authentication mechanisms and
493 * the users that are authorized to connect, it is strongly recommended
494 * to provide a non-%NULL #GDBusAuthObserver.
496 * Connect to the #GDBusServer::new-connection signal to handle
497 * incoming connections.
499 * The returned #GDBusServer isn't active - you have to start it with
500 * g_dbus_server_start().
502 * #GDBusServer is used in this [example][gdbus-peer-to-peer].
504 * This is a synchronous failable constructor. There is currently no
505 * asynchronous version.
507 * Returns: A #GDBusServer or %NULL if @error is set. Free with
513 g_dbus_server_new_sync (const gchar *address,
514 GDBusServerFlags flags,
516 GDBusAuthObserver *observer,
517 GCancellable *cancellable,
522 g_return_val_if_fail (address != NULL, NULL);
523 g_return_val_if_fail (g_dbus_is_guid (guid), NULL);
524 g_return_val_if_fail ((flags & ~G_DBUS_SERVER_FLAGS_ALL) == 0, NULL);
525 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
527 server = g_initable_new (G_TYPE_DBUS_SERVER,
533 "authentication-observer", observer,
540 * g_dbus_server_get_client_address:
541 * @server: A #GDBusServer.
544 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
545 * string that can be used by clients to connect to @server.
547 * This is valid and non-empty if initializing the #GDBusServer succeeded.
549 * Returns: (not nullable): A D-Bus address string. Do not free, the string is owned
555 g_dbus_server_get_client_address (GDBusServer *server)
557 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
558 return server->client_address;
562 * g_dbus_server_get_guid:
563 * @server: A #GDBusServer.
565 * Gets the GUID for @server, as provided to g_dbus_server_new_sync().
567 * Returns: (not nullable): A D-Bus GUID. Do not free this string, it is owned by @server.
572 g_dbus_server_get_guid (GDBusServer *server)
574 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
579 * g_dbus_server_get_flags:
580 * @server: A #GDBusServer.
582 * Gets the flags for @server.
584 * Returns: A set of flags from the #GDBusServerFlags enumeration.
589 g_dbus_server_get_flags (GDBusServer *server)
591 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
592 return server->flags;
596 * g_dbus_server_is_active:
597 * @server: A #GDBusServer.
599 * Gets whether @server is active.
601 * Returns: %TRUE if server is active, %FALSE otherwise.
606 g_dbus_server_is_active (GDBusServer *server)
608 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
609 return server->active;
613 * g_dbus_server_start:
614 * @server: A #GDBusServer.
621 g_dbus_server_start (GDBusServer *server)
623 g_return_if_fail (G_IS_DBUS_SERVER (server));
626 /* Right now we don't have any transport not using the listener... */
627 g_assert (server->is_using_listener);
628 server->run_signal_handler_id = g_signal_connect_data (G_SOCKET_SERVICE (server->listener),
631 g_object_ref (server),
632 (GClosureNotify) g_object_unref,
634 g_socket_service_start (G_SOCKET_SERVICE (server->listener));
635 server->active = TRUE;
636 g_object_notify (G_OBJECT (server), "active");
640 * g_dbus_server_stop:
641 * @server: A #GDBusServer.
648 g_dbus_server_stop (GDBusServer *server)
650 g_return_if_fail (G_IS_DBUS_SERVER (server));
653 /* Right now we don't have any transport not using the listener... */
654 g_assert (server->is_using_listener);
655 g_assert (server->run_signal_handler_id > 0);
656 g_clear_signal_handler (&server->run_signal_handler_id, server->listener);
657 g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
658 server->active = FALSE;
659 g_object_notify (G_OBJECT (server), "active");
661 if (server->unix_socket_path)
663 if (g_unlink (server->unix_socket_path) != 0)
664 g_warning ("Failed to delete %s: %s", server->unix_socket_path, g_strerror (errno));
667 if (server->nonce_file)
669 if (g_unlink (server->nonce_file) != 0)
670 g_warning ("Failed to delete %s: %s", server->nonce_file, g_strerror (errno));
674 /* ---------------------------------------------------------------------------------------------------- */
680 ret = g_random_int_range (0, 60);
690 /* note that address_entry has already been validated => exactly one of path, dir, tmpdir, or abstract keys are set */
692 try_unix (GDBusServer *server,
693 const gchar *address_entry,
694 GHashTable *key_value_pairs,
701 const gchar *abstract;
702 GSocketAddress *address;
707 path = g_hash_table_lookup (key_value_pairs, "path");
708 dir = g_hash_table_lookup (key_value_pairs, "dir");
709 tmpdir = g_hash_table_lookup (key_value_pairs, "tmpdir");
710 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
714 address = g_unix_socket_address_new (path);
716 else if (dir != NULL || tmpdir != NULL)
723 s = g_string_new (tmpdir != NULL ? tmpdir : dir);
724 g_string_append (s, "/dbus-");
725 for (n = 0; n < 8; n++)
726 g_string_append_c (s, random_ascii ());
728 address = g_unix_socket_address_new (s->str);
729 g_string_free (s, TRUE);
732 if (!g_socket_listener_add_address (server->listener,
734 G_SOCKET_TYPE_STREAM,
735 G_SOCKET_PROTOCOL_DEFAULT,
736 NULL, /* source_object */
737 NULL, /* effective_address */
740 if (local_error->domain == G_IO_ERROR && local_error->code == G_IO_ERROR_ADDRESS_IN_USE)
742 g_error_free (local_error);
745 g_propagate_error (error, local_error);
751 else if (abstract != NULL)
753 if (!g_unix_socket_address_abstract_names_supported ())
755 g_set_error_literal (error,
757 G_IO_ERROR_NOT_SUPPORTED,
758 _("Abstract namespace not supported"));
761 address = g_unix_socket_address_new_with_type (abstract,
763 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
767 g_assert_not_reached ();
770 if (!g_socket_listener_add_address (server->listener,
772 G_SOCKET_TYPE_STREAM,
773 G_SOCKET_PROTOCOL_DEFAULT,
774 NULL, /* source_object */
775 NULL, /* effective_address */
785 /* Fill out client_address if the connection attempt worked */
788 const char *address_path;
791 server->is_using_listener = TRUE;
792 address_path = g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address));
793 escaped_path = g_dbus_address_escape_value (address_path);
795 switch (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (address)))
797 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
798 server->client_address = g_strdup_printf ("unix:abstract=%s", escaped_path);
801 case G_UNIX_SOCKET_ADDRESS_PATH:
802 server->client_address = g_strdup_printf ("unix:path=%s", escaped_path);
803 server->unix_socket_path = g_strdup (address_path);
807 g_assert_not_reached ();
811 g_free (escaped_path);
813 g_object_unref (address);
818 /* ---------------------------------------------------------------------------------------------------- */
820 /* note that address_entry has already been validated =>
821 * both host and port (guaranteed to be a number in [0, 65535]) are set (family is optional)
824 try_tcp (GDBusServer *server,
825 const gchar *address_entry,
826 GHashTable *key_value_pairs,
835 GList *resolved_addresses;
840 resolved_addresses = NULL;
842 host = g_hash_table_lookup (key_value_pairs, "host");
843 port = g_hash_table_lookup (key_value_pairs, "port");
844 /* family = g_hash_table_lookup (key_value_pairs, "family"); */
845 if (g_hash_table_lookup (key_value_pairs, "noncefile") != NULL)
847 g_set_error_literal (error,
849 G_IO_ERROR_INVALID_ARGUMENT,
850 _("Cannot specify nonce file when creating a server"));
858 port_num = strtol (port, NULL, 10);
860 resolver = g_resolver_get_default ();
861 resolved_addresses = g_resolver_lookup_by_name (resolver,
865 if (resolved_addresses == NULL)
868 /* TODO: handle family */
869 for (l = resolved_addresses; l != NULL; l = l->next)
871 GInetAddress *address = G_INET_ADDRESS (l->data);
872 GSocketAddress *socket_address;
873 GSocketAddress *effective_address;
875 socket_address = g_inet_socket_address_new (address, port_num);
876 if (!g_socket_listener_add_address (server->listener,
878 G_SOCKET_TYPE_STREAM,
879 G_SOCKET_PROTOCOL_TCP,
880 NULL, /* GObject *source_object */
884 g_object_unref (socket_address);
888 /* make sure we allocate the same port number for other listeners */
889 port_num = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (effective_address));
891 g_object_unref (effective_address);
892 g_object_unref (socket_address);
900 gsize bytes_remaining;
904 server->nonce = g_new0 (guchar, 16);
905 for (n = 0; n < 16; n++)
906 server->nonce[n] = g_random_int_range (0, 256);
907 fd = g_file_open_tmp ("gdbus-nonce-file-XXXXXX",
912 g_socket_listener_close (server->listener);
917 bytes_remaining = 16;
918 while (bytes_remaining > 0)
923 size = write (fd, server->nonce + bytes_written, bytes_remaining);
931 g_io_error_from_errno (errsv),
932 _("Error writing nonce file at “%s”: %s"),
937 bytes_written += size;
938 bytes_remaining -= size;
940 if (!g_close (fd, error))
942 host_escaped = g_dbus_address_escape_value (host);
943 file_escaped = g_dbus_address_escape_value (server->nonce_file);
944 server->client_address = g_strdup_printf ("nonce-tcp:host=%s,port=%d,noncefile=%s",
948 g_free (host_escaped);
949 g_free (file_escaped);
953 server->client_address = g_strdup_printf ("tcp:host=%s,port=%d", host, port_num);
955 server->is_using_listener = TRUE;
959 g_list_free_full (resolved_addresses, g_object_unref);
961 g_object_unref (resolver);
965 /* ---------------------------------------------------------------------------------------------------- */
970 GDBusConnection *connection;
974 emit_idle_data_free (EmitIdleData *data)
976 g_object_unref (data->server);
977 g_object_unref (data->connection);
982 emit_new_connection_in_idle (gpointer user_data)
984 EmitIdleData *data = user_data;
988 g_signal_emit (data->server,
989 _signals[NEW_CONNECTION_SIGNAL],
995 g_dbus_connection_start_message_processing (data->connection);
996 g_object_unref (data->connection);
1001 /* Called in new thread */
1003 on_run (GSocketService *service,
1004 GSocketConnection *socket_connection,
1005 GObject *source_object,
1008 GDBusServer *server = G_DBUS_SERVER (user_data);
1009 GDBusConnection *connection;
1010 GDBusConnectionFlags connection_flags;
1012 if (server->nonce != NULL)
1017 if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)),
1021 NULL, /* GCancellable */
1025 if (bytes_read != 16)
1028 if (memcmp (buf, server->nonce, 16) != 0)
1033 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
1034 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING;
1035 if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)
1036 connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
1037 if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
1038 connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER;
1040 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1043 server->authentication_observer,
1044 NULL, /* GCancellable */
1046 if (connection == NULL)
1049 if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD)
1054 g_signal_emit (server,
1055 _signals[NEW_CONNECTION_SIGNAL],
1060 g_dbus_connection_start_message_processing (connection);
1061 g_object_unref (connection);
1065 GSource *idle_source;
1068 data = g_new0 (EmitIdleData, 1);
1069 data->server = g_object_ref (server);
1070 data->connection = g_object_ref (connection);
1072 idle_source = g_idle_source_new ();
1073 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1074 g_source_set_callback (idle_source,
1075 emit_new_connection_in_idle,
1077 (GDestroyNotify) emit_idle_data_free);
1078 g_source_set_static_name (idle_source, "[gio] emit_new_connection_in_idle");
1079 g_source_attach (idle_source, server->main_context_at_construction);
1080 g_source_unref (idle_source);
1088 initable_init (GInitable *initable,
1089 GCancellable *cancellable,
1092 GDBusServer *server = G_DBUS_SERVER (initable);
1102 if (!g_dbus_is_guid (server->guid))
1104 g_set_error (&last_error,
1106 G_IO_ERROR_INVALID_ARGUMENT,
1107 _("The string “%s” is not a valid D-Bus GUID"),
1112 server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
1114 addr_array = g_strsplit (server->address, ";", 0);
1116 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
1118 const gchar *address_entry = addr_array[n];
1119 GHashTable *key_value_pairs;
1120 gchar *transport_name;
1124 if (g_dbus_is_supported_address (address_entry,
1126 _g_dbus_address_parse_entry (address_entry,
1135 else if (g_strcmp0 (transport_name, "unix") == 0)
1136 ret = try_unix (server, address_entry, key_value_pairs, &this_error);
1137 else if (g_strcmp0 (transport_name, "tcp") == 0)
1138 ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
1139 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
1140 ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error);
1142 g_set_error (&this_error,
1144 G_IO_ERROR_INVALID_ARGUMENT,
1145 _("Cannot listen on unsupported transport “%s”"),
1148 g_free (transport_name);
1149 if (key_value_pairs != NULL)
1150 g_hash_table_unref (key_value_pairs);
1154 g_assert (this_error == NULL);
1159 if (this_error != NULL)
1161 if (last_error != NULL)
1162 g_error_free (last_error);
1163 last_error = this_error;
1169 g_strfreev (addr_array);
1173 g_clear_error (&last_error);
1177 g_assert (last_error != NULL);
1178 g_propagate_error (error, last_error);
1185 initable_iface_init (GInitableIface *initable_iface)
1187 initable_iface->init = initable_init;
1190 /* ---------------------------------------------------------------------------------------------------- */