Add Friulian translation
[glib.git] / gio / gdbusserver.c
bloba50b958f2e11b0e4c5078616d280637ec8350e85
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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
27 #include "giotypes.h"
28 #include "gioerror.h"
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"
46 #ifdef G_OS_UNIX
47 #include <unistd.h>
48 #endif
49 #ifdef G_OS_WIN32
50 #include <io.h>
51 #endif
53 #ifdef G_OS_UNIX
54 #include "gunixsocketaddress.h"
55 #endif
57 #include "glibintl.h"
59 /**
60 * SECTION:gdbusserver
61 * @short_description: Helper for accepting connections
62 * @include: gio/gio.h
64 * #GDBusServer is a helper for listening to and accepting D-Bus
65 * connections. This can be used to create a new D-Bus server, allowing two
66 * peers to use the D-Bus protocol for their own specialized communication.
67 * A server instance provided in this way will not perform message routing or
68 * implement the org.freedesktop.DBus interface.
70 * To just export an object on a well-known name on a message bus, such as the
71 * session or system bus, you should instead use g_bus_own_name().
73 * An example of peer-to-peer communication with G-DBus can be found
74 * in [gdbus-example-peer.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-peer.c).
77 /**
78 * GDBusServer:
80 * The #GDBusServer structure contains only private data and
81 * should only be accessed using the provided API.
83 * Since: 2.26
85 struct _GDBusServer
87 /*< private >*/
88 GObject parent_instance;
90 GDBusServerFlags flags;
91 gchar *address;
92 gchar *guid;
94 guchar *nonce;
95 gchar *nonce_file;
97 gchar *client_address;
99 GSocketListener *listener;
100 gboolean is_using_listener;
101 gulong run_signal_handler_id;
103 /* The result of g_main_context_ref_thread_default() when the object
104 * was created (the GObject _init() function) - this is used for delivery
105 * of the :new-connection GObject signal.
107 GMainContext *main_context_at_construction;
109 gboolean active;
111 GDBusAuthObserver *authentication_observer;
114 typedef struct _GDBusServerClass GDBusServerClass;
117 * GDBusServerClass:
118 * @new_connection: Signal class handler for the #GDBusServer::new-connection signal.
120 * Class structure for #GDBusServer.
122 * Since: 2.26
124 struct _GDBusServerClass
126 /*< private >*/
127 GObjectClass parent_class;
129 /*< public >*/
130 /* Signals */
131 gboolean (*new_connection) (GDBusServer *server,
132 GDBusConnection *connection);
135 enum
137 PROP_0,
138 PROP_ADDRESS,
139 PROP_CLIENT_ADDRESS,
140 PROP_FLAGS,
141 PROP_GUID,
142 PROP_ACTIVE,
143 PROP_AUTHENTICATION_OBSERVER,
146 enum
148 NEW_CONNECTION_SIGNAL,
149 LAST_SIGNAL,
152 static guint _signals[LAST_SIGNAL] = {0};
154 static void initable_iface_init (GInitableIface *initable_iface);
156 G_DEFINE_TYPE_WITH_CODE (GDBusServer, g_dbus_server, G_TYPE_OBJECT,
157 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
160 static void
161 g_dbus_server_finalize (GObject *object)
163 GDBusServer *server = G_DBUS_SERVER (object);
165 if (server->authentication_observer != NULL)
166 g_object_unref (server->authentication_observer);
168 if (server->run_signal_handler_id > 0)
169 g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
171 if (server->listener != NULL)
172 g_object_unref (server->listener);
174 g_free (server->address);
175 g_free (server->guid);
176 g_free (server->client_address);
177 if (server->nonce != NULL)
179 memset (server->nonce, '\0', 16);
180 g_free (server->nonce);
182 /* we could unlink the nonce file but I don't
183 * think it's really worth the effort/risk
185 g_free (server->nonce_file);
187 g_main_context_unref (server->main_context_at_construction);
189 G_OBJECT_CLASS (g_dbus_server_parent_class)->finalize (object);
192 static void
193 g_dbus_server_get_property (GObject *object,
194 guint prop_id,
195 GValue *value,
196 GParamSpec *pspec)
198 GDBusServer *server = G_DBUS_SERVER (object);
200 switch (prop_id)
202 case PROP_FLAGS:
203 g_value_set_flags (value, server->flags);
204 break;
206 case PROP_GUID:
207 g_value_set_string (value, server->guid);
208 break;
210 case PROP_ADDRESS:
211 g_value_set_string (value, server->address);
212 break;
214 case PROP_CLIENT_ADDRESS:
215 g_value_set_string (value, server->client_address);
216 break;
218 case PROP_ACTIVE:
219 g_value_set_boolean (value, server->active);
220 break;
222 case PROP_AUTHENTICATION_OBSERVER:
223 g_value_set_object (value, server->authentication_observer);
224 break;
226 default:
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 break;
232 static void
233 g_dbus_server_set_property (GObject *object,
234 guint prop_id,
235 const GValue *value,
236 GParamSpec *pspec)
238 GDBusServer *server = G_DBUS_SERVER (object);
240 switch (prop_id)
242 case PROP_FLAGS:
243 server->flags = g_value_get_flags (value);
244 break;
246 case PROP_GUID:
247 server->guid = g_value_dup_string (value);
248 break;
250 case PROP_ADDRESS:
251 server->address = g_value_dup_string (value);
252 break;
254 case PROP_AUTHENTICATION_OBSERVER:
255 server->authentication_observer = g_value_dup_object (value);
256 break;
258 default:
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 break;
264 static void
265 g_dbus_server_class_init (GDBusServerClass *klass)
267 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
269 gobject_class->finalize = g_dbus_server_finalize;
270 gobject_class->set_property = g_dbus_server_set_property;
271 gobject_class->get_property = g_dbus_server_get_property;
274 * GDBusServer:flags:
276 * Flags from the #GDBusServerFlags enumeration.
278 * Since: 2.26
280 g_object_class_install_property (gobject_class,
281 PROP_FLAGS,
282 g_param_spec_flags ("flags",
283 P_("Flags"),
284 P_("Flags for the server"),
285 G_TYPE_DBUS_SERVER_FLAGS,
286 G_DBUS_SERVER_FLAGS_NONE,
287 G_PARAM_READABLE |
288 G_PARAM_WRITABLE |
289 G_PARAM_CONSTRUCT_ONLY |
290 G_PARAM_STATIC_NAME |
291 G_PARAM_STATIC_BLURB |
292 G_PARAM_STATIC_NICK));
295 * GDBusServer:guid:
297 * The guid of the server.
299 * Since: 2.26
301 g_object_class_install_property (gobject_class,
302 PROP_GUID,
303 g_param_spec_string ("guid",
304 P_("GUID"),
305 P_("The guid of the server"),
306 NULL,
307 G_PARAM_READABLE |
308 G_PARAM_WRITABLE |
309 G_PARAM_CONSTRUCT_ONLY |
310 G_PARAM_STATIC_NAME |
311 G_PARAM_STATIC_BLURB |
312 G_PARAM_STATIC_NICK));
315 * GDBusServer:address:
317 * The D-Bus address to listen on.
319 * Since: 2.26
321 g_object_class_install_property (gobject_class,
322 PROP_ADDRESS,
323 g_param_spec_string ("address",
324 P_("Address"),
325 P_("The address to listen on"),
326 NULL,
327 G_PARAM_READABLE |
328 G_PARAM_WRITABLE |
329 G_PARAM_CONSTRUCT_ONLY |
330 G_PARAM_STATIC_NAME |
331 G_PARAM_STATIC_BLURB |
332 G_PARAM_STATIC_NICK));
335 * GDBusServer:client-address:
337 * The D-Bus address that clients can use.
339 * Since: 2.26
341 g_object_class_install_property (gobject_class,
342 PROP_CLIENT_ADDRESS,
343 g_param_spec_string ("client-address",
344 P_("Client Address"),
345 P_("The address clients can use"),
346 NULL,
347 G_PARAM_READABLE |
348 G_PARAM_STATIC_NAME |
349 G_PARAM_STATIC_BLURB |
350 G_PARAM_STATIC_NICK));
353 * GDBusServer:active:
355 * Whether the server is currently active.
357 * Since: 2.26
359 g_object_class_install_property (gobject_class,
360 PROP_ACTIVE,
361 g_param_spec_boolean ("active",
362 P_("Active"),
363 P_("Whether the server is currently active"),
364 FALSE,
365 G_PARAM_READABLE |
366 G_PARAM_STATIC_NAME |
367 G_PARAM_STATIC_BLURB |
368 G_PARAM_STATIC_NICK));
371 * GDBusServer:authentication-observer:
373 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
375 * Since: 2.26
377 g_object_class_install_property (gobject_class,
378 PROP_AUTHENTICATION_OBSERVER,
379 g_param_spec_object ("authentication-observer",
380 P_("Authentication Observer"),
381 P_("Object used to assist in the authentication process"),
382 G_TYPE_DBUS_AUTH_OBSERVER,
383 G_PARAM_READABLE |
384 G_PARAM_WRITABLE |
385 G_PARAM_CONSTRUCT_ONLY |
386 G_PARAM_STATIC_NAME |
387 G_PARAM_STATIC_BLURB |
388 G_PARAM_STATIC_NICK));
391 * GDBusServer::new-connection:
392 * @server: The #GDBusServer emitting the signal.
393 * @connection: A #GDBusConnection for the new connection.
395 * Emitted when a new authenticated connection has been made. Use
396 * g_dbus_connection_get_peer_credentials() to figure out what
397 * identity (if any), was authenticated.
399 * If you want to accept the connection, take a reference to the
400 * @connection object and return %TRUE. When you are done with the
401 * connection call g_dbus_connection_close() and give up your
402 * reference. Note that the other peer may disconnect at any time -
403 * a typical thing to do when accepting a connection is to listen to
404 * the #GDBusConnection::closed signal.
406 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
407 * then the signal is emitted in a new thread dedicated to the
408 * connection. Otherwise the signal is emitted in the
409 * [thread-default main context][g-main-context-push-thread-default]
410 * of the thread that @server was constructed in.
412 * You are guaranteed that signal handlers for this signal runs
413 * before incoming messages on @connection are processed. This means
414 * that it's suitable to call g_dbus_connection_register_object() or
415 * similar from the signal handler.
417 * Returns: %TRUE to claim @connection, %FALSE to let other handlers
418 * run.
420 * Since: 2.26
422 _signals[NEW_CONNECTION_SIGNAL] = g_signal_new (I_("new-connection"),
423 G_TYPE_DBUS_SERVER,
424 G_SIGNAL_RUN_LAST,
425 G_STRUCT_OFFSET (GDBusServerClass, new_connection),
426 g_signal_accumulator_true_handled,
427 NULL, /* accu_data */
428 NULL,
429 G_TYPE_BOOLEAN,
431 G_TYPE_DBUS_CONNECTION);
434 static void
435 g_dbus_server_init (GDBusServer *server)
437 server->main_context_at_construction = g_main_context_ref_thread_default ();
440 static gboolean
441 on_run (GSocketService *service,
442 GSocketConnection *socket_connection,
443 GObject *source_object,
444 gpointer user_data);
447 * g_dbus_server_new_sync:
448 * @address: A D-Bus address.
449 * @flags: Flags from the #GDBusServerFlags enumeration.
450 * @guid: A D-Bus GUID.
451 * @observer: (nullable): A #GDBusAuthObserver or %NULL.
452 * @cancellable: (nullable): A #GCancellable or %NULL.
453 * @error: Return location for server or %NULL.
455 * Creates a new D-Bus server that listens on the first address in
456 * @address that works.
458 * Once constructed, you can use g_dbus_server_get_client_address() to
459 * get a D-Bus address string that clients can use to connect.
461 * Connect to the #GDBusServer::new-connection signal to handle
462 * incoming connections.
464 * The returned #GDBusServer isn't active - you have to start it with
465 * g_dbus_server_start().
467 * #GDBusServer is used in this [example][gdbus-peer-to-peer].
469 * This is a synchronous failable constructor. See
470 * g_dbus_server_new() for the asynchronous version.
472 * Returns: A #GDBusServer or %NULL if @error is set. Free with
473 * g_object_unref().
475 * Since: 2.26
477 GDBusServer *
478 g_dbus_server_new_sync (const gchar *address,
479 GDBusServerFlags flags,
480 const gchar *guid,
481 GDBusAuthObserver *observer,
482 GCancellable *cancellable,
483 GError **error)
485 GDBusServer *server;
487 g_return_val_if_fail (address != NULL, NULL);
488 g_return_val_if_fail (g_dbus_is_guid (guid), NULL);
489 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
491 server = g_initable_new (G_TYPE_DBUS_SERVER,
492 cancellable,
493 error,
494 "address", address,
495 "flags", flags,
496 "guid", guid,
497 "authentication-observer", observer,
498 NULL);
500 return server;
504 * g_dbus_server_get_client_address:
505 * @server: A #GDBusServer.
507 * Gets a
508 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
509 * string that can be used by clients to connect to @server.
511 * Returns: A D-Bus address string. Do not free, the string is owned
512 * by @server.
514 * Since: 2.26
516 const gchar *
517 g_dbus_server_get_client_address (GDBusServer *server)
519 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
520 return server->client_address;
524 * g_dbus_server_get_guid:
525 * @server: A #GDBusServer.
527 * Gets the GUID for @server.
529 * Returns: A D-Bus GUID. Do not free this string, it is owned by @server.
531 * Since: 2.26
533 const gchar *
534 g_dbus_server_get_guid (GDBusServer *server)
536 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
537 return server->guid;
541 * g_dbus_server_get_flags:
542 * @server: A #GDBusServer.
544 * Gets the flags for @server.
546 * Returns: A set of flags from the #GDBusServerFlags enumeration.
548 * Since: 2.26
550 GDBusServerFlags
551 g_dbus_server_get_flags (GDBusServer *server)
553 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
554 return server->flags;
558 * g_dbus_server_is_active:
559 * @server: A #GDBusServer.
561 * Gets whether @server is active.
563 * Returns: %TRUE if server is active, %FALSE otherwise.
565 * Since: 2.26
567 gboolean
568 g_dbus_server_is_active (GDBusServer *server)
570 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
571 return server->active;
575 * g_dbus_server_start:
576 * @server: A #GDBusServer.
578 * Starts @server.
580 * Since: 2.26
582 void
583 g_dbus_server_start (GDBusServer *server)
585 g_return_if_fail (G_IS_DBUS_SERVER (server));
586 if (server->active)
587 return;
588 /* Right now we don't have any transport not using the listener... */
589 g_assert (server->is_using_listener);
590 g_socket_service_start (G_SOCKET_SERVICE (server->listener));
591 server->active = TRUE;
592 g_object_notify (G_OBJECT (server), "active");
596 * g_dbus_server_stop:
597 * @server: A #GDBusServer.
599 * Stops @server.
601 * Since: 2.26
603 void
604 g_dbus_server_stop (GDBusServer *server)
606 g_return_if_fail (G_IS_DBUS_SERVER (server));
607 if (!server->active)
608 return;
609 /* Right now we don't have any transport not using the listener... */
610 g_assert (server->is_using_listener);
611 g_assert (server->run_signal_handler_id > 0);
612 g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
613 server->run_signal_handler_id = 0;
614 g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
615 server->active = FALSE;
616 g_object_notify (G_OBJECT (server), "active");
619 /* ---------------------------------------------------------------------------------------------------- */
621 #ifdef G_OS_UNIX
623 static gint
624 random_ascii (void)
626 gint ret;
627 ret = g_random_int_range (0, 60);
628 if (ret < 25)
629 ret += 'A';
630 else if (ret < 50)
631 ret += 'a' - 25;
632 else
633 ret += '0' - 50;
634 return ret;
637 /* note that address_entry has already been validated => exactly one of path, tmpdir or abstract keys are set */
638 static gboolean
639 try_unix (GDBusServer *server,
640 const gchar *address_entry,
641 GHashTable *key_value_pairs,
642 GError **error)
644 gboolean ret;
645 const gchar *path;
646 const gchar *tmpdir;
647 const gchar *abstract;
648 GSocketAddress *address;
650 ret = FALSE;
651 address = NULL;
653 path = g_hash_table_lookup (key_value_pairs, "path");
654 tmpdir = g_hash_table_lookup (key_value_pairs, "tmpdir");
655 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
657 if (path != NULL)
659 address = g_unix_socket_address_new (path);
661 else if (tmpdir != NULL)
663 gint n;
664 GString *s;
665 GError *local_error;
667 retry:
668 s = g_string_new (tmpdir);
669 g_string_append (s, "/dbus-");
670 for (n = 0; n < 8; n++)
671 g_string_append_c (s, random_ascii ());
673 /* prefer abstract namespace if available */
674 if (g_unix_socket_address_abstract_names_supported ())
675 address = g_unix_socket_address_new_with_type (s->str,
677 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
678 else
679 address = g_unix_socket_address_new (s->str);
680 g_string_free (s, TRUE);
682 local_error = NULL;
683 if (!g_socket_listener_add_address (server->listener,
684 address,
685 G_SOCKET_TYPE_STREAM,
686 G_SOCKET_PROTOCOL_DEFAULT,
687 NULL, /* source_object */
688 NULL, /* effective_address */
689 &local_error))
691 if (local_error->domain == G_IO_ERROR && local_error->code == G_IO_ERROR_ADDRESS_IN_USE)
693 g_error_free (local_error);
694 goto retry;
696 g_propagate_error (error, local_error);
697 goto out;
699 ret = TRUE;
700 goto out;
702 else if (abstract != NULL)
704 if (!g_unix_socket_address_abstract_names_supported ())
706 g_set_error_literal (error,
707 G_IO_ERROR,
708 G_IO_ERROR_NOT_SUPPORTED,
709 _("Abstract name space not supported"));
710 goto out;
712 address = g_unix_socket_address_new_with_type (abstract,
714 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
716 else
718 g_assert_not_reached ();
721 if (!g_socket_listener_add_address (server->listener,
722 address,
723 G_SOCKET_TYPE_STREAM,
724 G_SOCKET_PROTOCOL_DEFAULT,
725 NULL, /* source_object */
726 NULL, /* effective_address */
727 error))
728 goto out;
730 ret = TRUE;
732 out:
734 if (address != NULL)
736 /* Fill out client_address if the connection attempt worked */
737 if (ret)
739 server->is_using_listener = TRUE;
741 switch (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (address)))
743 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
744 server->client_address = g_strdup_printf ("unix:abstract=%s",
745 g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address)));
746 break;
748 case G_UNIX_SOCKET_ADDRESS_PATH:
749 server->client_address = g_strdup_printf ("unix:path=%s",
750 g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address)));
751 break;
753 default:
754 g_assert_not_reached ();
755 break;
758 g_object_unref (address);
760 return ret;
762 #endif
764 /* ---------------------------------------------------------------------------------------------------- */
766 /* note that address_entry has already been validated =>
767 * both host and port (guaranteed to be a number in [0, 65535]) are set (family is optional)
769 static gboolean
770 try_tcp (GDBusServer *server,
771 const gchar *address_entry,
772 GHashTable *key_value_pairs,
773 gboolean do_nonce,
774 GError **error)
776 gboolean ret;
777 const gchar *host;
778 const gchar *port;
779 gint port_num;
780 GResolver *resolver;
781 GList *resolved_addresses;
782 GList *l;
784 ret = FALSE;
785 resolver = NULL;
786 resolved_addresses = NULL;
788 host = g_hash_table_lookup (key_value_pairs, "host");
789 port = g_hash_table_lookup (key_value_pairs, "port");
790 /* family = g_hash_table_lookup (key_value_pairs, "family"); */
791 if (g_hash_table_lookup (key_value_pairs, "noncefile") != NULL)
793 g_set_error_literal (error,
794 G_IO_ERROR,
795 G_IO_ERROR_INVALID_ARGUMENT,
796 _("Cannot specify nonce file when creating a server"));
797 goto out;
800 if (host == NULL)
801 host = "localhost";
802 if (port == NULL)
803 port = "0";
804 port_num = strtol (port, NULL, 10);
806 resolver = g_resolver_get_default ();
807 resolved_addresses = g_resolver_lookup_by_name (resolver,
808 host,
809 NULL,
810 error);
811 if (resolved_addresses == NULL)
812 goto out;
814 /* TODO: handle family */
815 for (l = resolved_addresses; l != NULL; l = l->next)
817 GInetAddress *address = G_INET_ADDRESS (l->data);
818 GSocketAddress *socket_address;
819 GSocketAddress *effective_address;
821 socket_address = g_inet_socket_address_new (address, port_num);
822 if (!g_socket_listener_add_address (server->listener,
823 socket_address,
824 G_SOCKET_TYPE_STREAM,
825 G_SOCKET_PROTOCOL_TCP,
826 NULL, /* GObject *source_object */
827 &effective_address,
828 error))
830 g_object_unref (socket_address);
831 goto out;
833 if (port_num == 0)
834 /* make sure we allocate the same port number for other listeners */
835 port_num = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (effective_address));
837 g_object_unref (effective_address);
838 g_object_unref (socket_address);
841 if (do_nonce)
843 gint fd;
844 guint n;
845 gsize bytes_written;
846 gsize bytes_remaining;
847 char *file_escaped;
849 server->nonce = g_new0 (guchar, 16);
850 for (n = 0; n < 16; n++)
851 server->nonce[n] = g_random_int_range (0, 256);
852 fd = g_file_open_tmp ("gdbus-nonce-file-XXXXXX",
853 &server->nonce_file,
854 error);
855 if (fd == -1)
857 g_socket_listener_close (server->listener);
858 goto out;
860 again:
861 bytes_written = 0;
862 bytes_remaining = 16;
863 while (bytes_remaining > 0)
865 gssize ret;
866 ret = write (fd, server->nonce + bytes_written, bytes_remaining);
867 if (ret == -1)
869 if (errno == EINTR)
870 goto again;
871 g_set_error (error,
872 G_IO_ERROR,
873 g_io_error_from_errno (errno),
874 _("Error writing nonce file at “%s”: %s"),
875 server->nonce_file,
876 strerror (errno));
877 goto out;
879 bytes_written += ret;
880 bytes_remaining -= ret;
882 if (!g_close (fd, error))
883 goto out;
884 file_escaped = g_uri_escape_string (server->nonce_file, "/\\", FALSE);
885 server->client_address = g_strdup_printf ("nonce-tcp:host=%s,port=%d,noncefile=%s",
886 host,
887 port_num,
888 file_escaped);
889 g_free (file_escaped);
891 else
893 server->client_address = g_strdup_printf ("tcp:host=%s,port=%d", host, port_num);
895 server->is_using_listener = TRUE;
896 ret = TRUE;
898 out:
899 g_list_free_full (resolved_addresses, g_object_unref);
900 if (resolver)
901 g_object_unref (resolver);
902 return ret;
905 /* ---------------------------------------------------------------------------------------------------- */
907 typedef struct
909 GDBusServer *server;
910 GDBusConnection *connection;
911 } EmitIdleData;
913 static void
914 emit_idle_data_free (EmitIdleData *data)
916 g_object_unref (data->server);
917 g_object_unref (data->connection);
918 g_free (data);
921 static gboolean
922 emit_new_connection_in_idle (gpointer user_data)
924 EmitIdleData *data = user_data;
925 gboolean claimed;
927 claimed = FALSE;
928 g_signal_emit (data->server,
929 _signals[NEW_CONNECTION_SIGNAL],
931 data->connection,
932 &claimed);
934 if (claimed)
935 g_dbus_connection_start_message_processing (data->connection);
936 g_object_unref (data->connection);
938 return FALSE;
941 /* Called in new thread */
942 static gboolean
943 on_run (GSocketService *service,
944 GSocketConnection *socket_connection,
945 GObject *source_object,
946 gpointer user_data)
948 GDBusServer *server = G_DBUS_SERVER (user_data);
949 GDBusConnection *connection;
950 GDBusConnectionFlags connection_flags;
952 if (server->nonce != NULL)
954 gchar buf[16];
955 gsize bytes_read;
957 if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)),
958 buf,
960 &bytes_read,
961 NULL, /* GCancellable */
962 NULL)) /* GError */
963 goto out;
965 if (bytes_read != 16)
966 goto out;
968 if (memcmp (buf, server->nonce, 16) != 0)
969 goto out;
972 connection_flags =
973 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
974 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING;
975 if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)
976 connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
978 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
979 server->guid,
980 connection_flags,
981 server->authentication_observer,
982 NULL, /* GCancellable */
983 NULL); /* GError */
984 if (connection == NULL)
985 goto out;
987 if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD)
989 gboolean claimed;
991 claimed = FALSE;
992 g_signal_emit (server,
993 _signals[NEW_CONNECTION_SIGNAL],
995 connection,
996 &claimed);
997 if (claimed)
998 g_dbus_connection_start_message_processing (connection);
999 g_object_unref (connection);
1001 else
1003 GSource *idle_source;
1004 EmitIdleData *data;
1006 data = g_new0 (EmitIdleData, 1);
1007 data->server = g_object_ref (server);
1008 data->connection = g_object_ref (connection);
1010 idle_source = g_idle_source_new ();
1011 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1012 g_source_set_callback (idle_source,
1013 emit_new_connection_in_idle,
1014 data,
1015 (GDestroyNotify) emit_idle_data_free);
1016 g_source_set_name (idle_source, "[gio] emit_new_connection_in_idle");
1017 g_source_attach (idle_source, server->main_context_at_construction);
1018 g_source_unref (idle_source);
1021 out:
1022 return TRUE;
1025 static gboolean
1026 initable_init (GInitable *initable,
1027 GCancellable *cancellable,
1028 GError **error)
1030 GDBusServer *server = G_DBUS_SERVER (initable);
1031 gboolean ret;
1032 guint n;
1033 gchar **addr_array;
1034 GError *last_error;
1036 ret = FALSE;
1037 addr_array = NULL;
1038 last_error = NULL;
1040 if (!g_dbus_is_guid (server->guid))
1042 g_set_error (&last_error,
1043 G_IO_ERROR,
1044 G_IO_ERROR_INVALID_ARGUMENT,
1045 _("The string “%s” is not a valid D-Bus GUID"),
1046 server->guid);
1047 goto out;
1050 server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
1052 addr_array = g_strsplit (server->address, ";", 0);
1053 last_error = NULL;
1054 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
1056 const gchar *address_entry = addr_array[n];
1057 GHashTable *key_value_pairs;
1058 gchar *transport_name;
1059 GError *this_error;
1061 this_error = NULL;
1062 if (g_dbus_is_supported_address (address_entry,
1063 &this_error) &&
1064 _g_dbus_address_parse_entry (address_entry,
1065 &transport_name,
1066 &key_value_pairs,
1067 &this_error))
1070 if (FALSE)
1073 #ifdef G_OS_UNIX
1074 else if (g_strcmp0 (transport_name, "unix") == 0)
1075 ret = try_unix (server, address_entry, key_value_pairs, &this_error);
1076 #endif
1077 else if (g_strcmp0 (transport_name, "tcp") == 0)
1078 ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
1079 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
1080 ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error);
1081 else
1082 g_set_error (&this_error,
1083 G_IO_ERROR,
1084 G_IO_ERROR_INVALID_ARGUMENT,
1085 _("Cannot listen on unsupported transport “%s”"),
1086 transport_name);
1088 g_free (transport_name);
1089 if (key_value_pairs != NULL)
1090 g_hash_table_unref (key_value_pairs);
1092 if (ret)
1094 g_assert (this_error == NULL);
1095 goto out;
1099 if (this_error != NULL)
1101 if (last_error != NULL)
1102 g_error_free (last_error);
1103 last_error = this_error;
1107 out:
1109 g_strfreev (addr_array);
1111 if (ret)
1113 if (last_error != NULL)
1114 g_error_free (last_error);
1116 /* Right now we don't have any transport not using the listener... */
1117 g_assert (server->is_using_listener);
1118 server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener),
1119 "run",
1120 G_CALLBACK (on_run),
1121 server);
1123 else
1125 g_assert (last_error != NULL);
1126 g_propagate_error (error, last_error);
1128 return ret;
1132 static void
1133 initable_iface_init (GInitableIface *initable_iface)
1135 initable_iface->init = initable_init;
1138 /* ---------------------------------------------------------------------------------------------------- */