Move markup collect tests to the test framework
[glib.git] / gio / gdbusserver.c
blobf9e19b3054aebf1be497cd3b39d55211c78f0e78
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>
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef _WIN32
32 #include <io.h>
33 #endif
35 #include "giotypes.h"
36 #include "gioerror.h"
37 #include "gdbusaddress.h"
38 #include "gdbusutils.h"
39 #include "gdbusconnection.h"
40 #include "gdbusserver.h"
41 #include "gioenumtypes.h"
42 #include "gdbusprivate.h"
43 #include "gdbusauthobserver.h"
44 #include "gio-marshal.h"
45 #include "ginitable.h"
46 #include "gsocketservice.h"
47 #include "gthreadedsocketservice.h"
48 #include "gresolver.h"
49 #include "ginetaddress.h"
50 #include "ginetsocketaddress.h"
51 #include "ginputstream.h"
52 #include "giostream.h"
54 #ifdef G_OS_UNIX
55 #include "gunixsocketaddress.h"
56 #endif
58 #include "glibintl.h"
60 /**
61 * SECTION:gdbusserver
62 * @short_description: Helper for accepting connections
63 * @include: gio/gio.h
65 * #GDBusServer is a helper for listening to and accepting D-Bus
66 * connections.
68 * <example id="gdbus-peer-to-peer"><title>D-Bus peer-to-peer example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-peer.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
71 /**
72 * GDBusServer:
74 * The #GDBusServer structure contains only private data and
75 * should only be accessed using the provided API.
77 * Since: 2.26
79 struct _GDBusServer
81 /*< private >*/
82 GObject parent_instance;
84 GDBusServerFlags flags;
85 gchar *address;
86 gchar *guid;
88 guchar *nonce;
89 gchar *nonce_file;
91 gchar *client_address;
93 GSocketListener *listener;
94 gboolean is_using_listener;
95 gulong run_signal_handler_id;
97 /* The result of g_main_context_get_thread_default() when the object
98 * was created (the GObject _init() function) - this is used for delivery
99 * of the :new-connection GObject signal.
101 GMainContext *main_context_at_construction;
103 gboolean active;
105 GDBusAuthObserver *authentication_observer;
108 typedef struct _GDBusServerClass GDBusServerClass;
111 * GDBusServerClass:
112 * @new_connection: Signal class handler for the #GDBusServer::new-connection signal.
114 * Class structure for #GDBusServer.
116 * Since: 2.26
118 struct _GDBusServerClass
120 /*< private >*/
121 GObjectClass parent_class;
123 /*< public >*/
124 /* Signals */
125 void (*new_connection) (GDBusServer *server,
126 GDBusConnection *connection);
129 enum
131 PROP_0,
132 PROP_ADDRESS,
133 PROP_CLIENT_ADDRESS,
134 PROP_FLAGS,
135 PROP_GUID,
136 PROP_ACTIVE,
137 PROP_AUTHENTICATION_OBSERVER,
140 enum
142 NEW_CONNECTION_SIGNAL,
143 LAST_SIGNAL,
146 guint _signals[LAST_SIGNAL] = {0};
148 static void initable_iface_init (GInitableIface *initable_iface);
150 G_DEFINE_TYPE_WITH_CODE (GDBusServer, g_dbus_server, G_TYPE_OBJECT,
151 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
154 static void
155 g_dbus_server_finalize (GObject *object)
157 GDBusServer *server = G_DBUS_SERVER (object);
159 if (server->authentication_observer != NULL)
160 g_object_unref (server->authentication_observer);
162 if (server->run_signal_handler_id > 0)
163 g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
165 if (server->listener != NULL)
166 g_object_unref (server->listener);
168 g_free (server->address);
169 g_free (server->guid);
170 g_free (server->client_address);
171 if (server->nonce != NULL)
173 memset (server->nonce, '\0', 16);
174 g_free (server->nonce);
176 /* we could unlink the nonce file but I don't
177 * think it's really worth the effort/risk
179 g_free (server->nonce_file);
181 if (server->main_context_at_construction != NULL)
182 g_main_context_unref (server->main_context_at_construction);
184 G_OBJECT_CLASS (g_dbus_server_parent_class)->finalize (object);
187 static void
188 g_dbus_server_get_property (GObject *object,
189 guint prop_id,
190 GValue *value,
191 GParamSpec *pspec)
193 GDBusServer *server = G_DBUS_SERVER (object);
195 switch (prop_id)
197 case PROP_FLAGS:
198 g_value_set_flags (value, server->flags);
199 break;
201 case PROP_GUID:
202 g_value_set_string (value, server->guid);
203 break;
205 case PROP_ADDRESS:
206 g_value_set_string (value, server->address);
207 break;
209 case PROP_CLIENT_ADDRESS:
210 g_value_set_string (value, server->client_address);
211 break;
213 case PROP_ACTIVE:
214 g_value_set_boolean (value, server->active);
215 break;
217 case PROP_AUTHENTICATION_OBSERVER:
218 g_value_set_object (value, server->authentication_observer);
219 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 break;
227 static void
228 g_dbus_server_set_property (GObject *object,
229 guint prop_id,
230 const GValue *value,
231 GParamSpec *pspec)
233 GDBusServer *server = G_DBUS_SERVER (object);
235 switch (prop_id)
237 case PROP_FLAGS:
238 server->flags = g_value_get_flags (value);
239 break;
241 case PROP_GUID:
242 server->guid = g_value_dup_string (value);
243 break;
245 case PROP_ADDRESS:
246 server->address = g_value_dup_string (value);
247 break;
249 case PROP_AUTHENTICATION_OBSERVER:
250 server->authentication_observer = g_value_dup_object (value);
251 break;
253 default:
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 break;
259 static void
260 g_dbus_server_class_init (GDBusServerClass *klass)
262 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
264 gobject_class->finalize = g_dbus_server_finalize;
265 gobject_class->set_property = g_dbus_server_set_property;
266 gobject_class->get_property = g_dbus_server_get_property;
269 * GDBusServer:flags:
271 * Flags from the #GDBusServerFlags enumeration.
273 * Since: 2.26
275 g_object_class_install_property (gobject_class,
276 PROP_FLAGS,
277 g_param_spec_flags ("flags",
278 P_("Flags"),
279 P_("Flags for the server"),
280 G_TYPE_DBUS_SERVER_FLAGS,
281 G_DBUS_SERVER_FLAGS_NONE,
282 G_PARAM_READABLE |
283 G_PARAM_WRITABLE |
284 G_PARAM_CONSTRUCT_ONLY |
285 G_PARAM_STATIC_NAME |
286 G_PARAM_STATIC_BLURB |
287 G_PARAM_STATIC_NICK));
290 * GDBusServer:guid:
292 * The guid of the server.
294 * Since: 2.26
296 g_object_class_install_property (gobject_class,
297 PROP_GUID,
298 g_param_spec_string ("guid",
299 P_("GUID"),
300 P_("The guid of the server"),
301 NULL,
302 G_PARAM_READABLE |
303 G_PARAM_WRITABLE |
304 G_PARAM_CONSTRUCT_ONLY |
305 G_PARAM_STATIC_NAME |
306 G_PARAM_STATIC_BLURB |
307 G_PARAM_STATIC_NICK));
310 * GDBusServer:address:
312 * The D-Bus address to listen on.
314 * Since: 2.26
316 g_object_class_install_property (gobject_class,
317 PROP_ADDRESS,
318 g_param_spec_string ("address",
319 P_("Address"),
320 P_("The address to listen on"),
321 NULL,
322 G_PARAM_READABLE |
323 G_PARAM_WRITABLE |
324 G_PARAM_CONSTRUCT_ONLY |
325 G_PARAM_STATIC_NAME |
326 G_PARAM_STATIC_BLURB |
327 G_PARAM_STATIC_NICK));
330 * GDBusServer:client-address:
332 * The D-Bus address that clients can use.
334 * Since: 2.26
336 g_object_class_install_property (gobject_class,
337 PROP_CLIENT_ADDRESS,
338 g_param_spec_string ("client-address",
339 P_("Client Address"),
340 P_("The address clients can use"),
341 NULL,
342 G_PARAM_READABLE |
343 G_PARAM_STATIC_NAME |
344 G_PARAM_STATIC_BLURB |
345 G_PARAM_STATIC_NICK));
348 * GDBusServer:active:
350 * Whether the server is currently active.
352 * Since: 2.26
354 g_object_class_install_property (gobject_class,
355 PROP_ACTIVE,
356 g_param_spec_boolean ("active",
357 P_("Active"),
358 P_("Whether the server is currently active"),
359 FALSE,
360 G_PARAM_READABLE |
361 G_PARAM_STATIC_NAME |
362 G_PARAM_STATIC_BLURB |
363 G_PARAM_STATIC_NICK));
366 * GDBusServer:authentication-observer:
368 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
370 * Since: 2.26
372 g_object_class_install_property (gobject_class,
373 PROP_AUTHENTICATION_OBSERVER,
374 g_param_spec_object ("authentication-observer",
375 P_("Authentication Observer"),
376 P_("Object used to assist in the authentication process"),
377 G_TYPE_DBUS_AUTH_OBSERVER,
378 G_PARAM_READABLE |
379 G_PARAM_WRITABLE |
380 G_PARAM_CONSTRUCT_ONLY |
381 G_PARAM_STATIC_NAME |
382 G_PARAM_STATIC_BLURB |
383 G_PARAM_STATIC_NICK));
386 * GDBusServer::new-connection:
387 * @server: The #GDBusServer emitting the signal.
388 * @connection: A #GDBusConnection for the new connection.
390 * Emitted when a new authenticated connection has been made. Use
391 * g_dbus_connection_get_peer_credentials() to figure out what
392 * identity (if any), was authenticated.
394 * If you want to accept the connection, simply ref the @connection
395 * object. Then call g_dbus_connection_close() and unref it when you
396 * are done with it. A typical thing to do when accepting a
397 * connection is to listen to the #GDBusConnection::closed signal.
399 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
400 * then the signal is emitted in a new thread dedicated to the
401 * connection. Otherwise the signal is emitted in the <link
402 * linkend="g-main-context-push-thread-default">thread-default main
403 * loop</link> of the thread that @server was constructed in.
405 * You are guaranteed that signal handlers for this signal runs
406 * before incoming messages on @connection are processed. This means
407 * that it's suitable to call g_dbus_connection_register_object() or
408 * similar from the signal handler.
410 * Since: 2.26
412 _signals[NEW_CONNECTION_SIGNAL] = g_signal_new ("new-connection",
413 G_TYPE_DBUS_SERVER,
414 G_SIGNAL_RUN_LAST,
415 G_STRUCT_OFFSET (GDBusServerClass, new_connection),
416 NULL,
417 NULL,
418 g_cclosure_marshal_VOID__OBJECT,
419 G_TYPE_NONE,
421 G_TYPE_DBUS_CONNECTION);
424 static void
425 g_dbus_server_init (GDBusServer *server)
427 server->main_context_at_construction = g_main_context_get_thread_default ();
428 if (server->main_context_at_construction != NULL)
429 g_main_context_ref (server->main_context_at_construction);
432 static gboolean
433 on_run (GSocketService *service,
434 GSocketConnection *socket_connection,
435 GObject *source_object,
436 gpointer user_data);
439 * g_dbus_server_new_sync:
440 * @address: A D-Bus address.
441 * @flags: Flags from the #GDBusServerFlags enumeration.
442 * @guid: A D-Bus GUID.
443 * @observer: A #GDBusAuthObserver or %NULL.
444 * @cancellable: A #GCancellable or %NULL.
445 * @error: Return location for server or %NULL.
447 * Creates a new D-Bus server that listens on the first address in
448 * @address that works.
450 * Once constructed, you can use g_dbus_server_get_client_address() to
451 * get a D-Bus address string that clients can use to connect.
453 * Connect to the #GDBusServer::new-connection signal to handle
454 * incoming connections.
456 * The returned #GDBusServer isn't active - you have to start it with
457 * g_dbus_server_start().
459 * See <xref linkend="gdbus-peer-to-peer"/> for how #GDBusServer can
460 * be used.
462 * This is a synchronous failable constructor. See
463 * g_dbus_server_new() for the asynchronous version.
465 * Returns: A #GDBusServer or %NULL if @error is set. Free with
466 * g_object_unref().
468 * Since: 2.26
470 GDBusServer *
471 g_dbus_server_new_sync (const gchar *address,
472 GDBusServerFlags flags,
473 const gchar *guid,
474 GDBusAuthObserver *observer,
475 GCancellable *cancellable,
476 GError **error)
478 GDBusServer *server;
480 g_return_val_if_fail (address != NULL, NULL);
481 g_return_val_if_fail (g_dbus_is_guid (guid), NULL);
482 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
484 server = g_initable_new (G_TYPE_DBUS_SERVER,
485 cancellable,
486 error,
487 "address", address,
488 "flags", flags,
489 "guid", guid,
490 "authentication-observer", observer,
491 NULL);
492 if (server != NULL)
494 /* Right now we don't have any transport not using the listener... */
495 g_assert (server->is_using_listener);
496 server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener),
497 "run",
498 G_CALLBACK (on_run),
499 server);
502 return server;
506 * g_dbus_server_get_client_address:
507 * @server: A #GDBusServer.
509 * Gets a D-Bus address string that can be used by clients to connect
510 * to @server.
512 * Returns: A D-Bus address string. Do not free, the string is owned
513 * by @server.
515 * Since: 2.26
517 const gchar *
518 g_dbus_server_get_client_address (GDBusServer *server)
520 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
521 return server->client_address;
525 * g_dbus_server_get_guid:
526 * @server: A #GDBusServer.
528 * Gets the GUID for @server.
530 * Returns: A D-Bus GUID. Do not free this string, it is owned by @server.
532 * Since: 2.26
534 const gchar *
535 g_dbus_server_get_guid (GDBusServer *server)
537 g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
538 return server->guid;
542 * g_dbus_server_get_flags:
543 * @server: A #GDBusServer.
545 * Gets the flags for @server.
547 * Returns: A set of flags from the #GDBusServerFlags enumeration.
549 * Since: 2.26
551 GDBusServerFlags
552 g_dbus_server_get_flags (GDBusServer *server)
554 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
555 return server->flags;
559 * g_dbus_server_is_active:
560 * @server: A #GDBusServer.
562 * Gets whether @server is active.
564 * Returns: %TRUE if server is active, %FALSE otherwise.
566 * Since: 2.26
568 gboolean
569 g_dbus_server_is_active (GDBusServer *server)
571 g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
572 return server->active;
576 * g_dbus_server_start:
577 * @server: A #GDBusServer.
579 * Starts @server.
581 * Since: 2.26
583 void
584 g_dbus_server_start (GDBusServer *server)
586 g_return_if_fail (G_IS_DBUS_SERVER (server));
587 if (server->active)
588 return;
589 /* Right now we don't have any transport not using the listener... */
590 g_assert (server->is_using_listener);
591 g_socket_service_start (G_SOCKET_SERVICE (server->listener));
592 server->active = TRUE;
593 g_object_notify (G_OBJECT (server), "active");
597 * g_dbus_server_stop:
598 * @server: A #GDBusServer.
600 * Stops @server.
602 * Since: 2.26
604 void
605 g_dbus_server_stop (GDBusServer *server)
607 g_return_if_fail (G_IS_DBUS_SERVER (server));
608 if (!server->active)
609 return;
610 /* Right now we don't have any transport not using the listener... */
611 g_assert (server->is_using_listener);
612 g_assert (server->run_signal_handler_id > 0);
613 g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
614 server->run_signal_handler_id = 0;
615 g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
616 server->active = FALSE;
617 g_object_notify (G_OBJECT (server), "active");
620 /* ---------------------------------------------------------------------------------------------------- */
622 #ifdef G_OS_UNIX
624 static gint
625 random_ascii (void)
627 gint ret;
628 ret = g_random_int_range (0, 60);
629 if (ret < 25)
630 ret += 'A';
631 else if (ret < 50)
632 ret += 'a' - 25;
633 else
634 ret += '0' - 50;
635 return ret;
638 /* note that address_entry has already been validated => exactly one of path, tmpdir or abstract keys are set */
639 static gboolean
640 try_unix (GDBusServer *server,
641 const gchar *address_entry,
642 GHashTable *key_value_pairs,
643 GError **error)
645 gboolean ret;
646 const gchar *path;
647 const gchar *tmpdir;
648 const gchar *abstract;
649 GSocketAddress *address;
651 ret = FALSE;
652 address = NULL;
654 path = g_hash_table_lookup (key_value_pairs, "path");
655 tmpdir = g_hash_table_lookup (key_value_pairs, "tmpdir");
656 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
658 if (path != NULL)
660 address = g_unix_socket_address_new (path);
662 else if (tmpdir != NULL)
664 gint n;
665 GString *s;
666 GError *local_error;
668 retry:
669 s = g_string_new (tmpdir);
670 g_string_append (s, "/dbus-");
671 for (n = 0; n < 8; n++)
672 g_string_append_c (s, random_ascii ());
674 /* prefer abstract namespace if available */
675 if (g_unix_socket_address_abstract_names_supported ())
676 address = g_unix_socket_address_new_with_type (s->str,
678 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
679 else
680 address = g_unix_socket_address_new (s->str);
681 g_string_free (s, TRUE);
683 local_error = NULL;
684 if (!g_socket_listener_add_address (server->listener,
685 address,
686 G_SOCKET_TYPE_STREAM,
687 G_SOCKET_PROTOCOL_DEFAULT,
688 NULL, /* source_object */
689 NULL, /* effective_address */
690 &local_error))
692 if (local_error->domain == G_IO_ERROR && local_error->code == G_IO_ERROR_ADDRESS_IN_USE)
694 g_error_free (local_error);
695 goto retry;
697 g_propagate_error (error, local_error);
698 goto out;
700 ret = TRUE;
701 goto out;
703 else if (abstract != NULL)
705 if (!g_unix_socket_address_abstract_names_supported ())
707 g_set_error_literal (error,
708 G_IO_ERROR,
709 G_IO_ERROR_NOT_SUPPORTED,
710 _("Abstract name space not supported"));
711 goto out;
713 address = g_unix_socket_address_new_with_type (abstract,
715 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
717 else
719 g_assert_not_reached ();
722 if (!g_socket_listener_add_address (server->listener,
723 address,
724 G_SOCKET_TYPE_STREAM,
725 G_SOCKET_PROTOCOL_DEFAULT,
726 NULL, /* source_object */
727 NULL, /* effective_address */
728 error))
729 goto out;
731 ret = TRUE;
733 out:
735 if (address != NULL)
737 /* Fill out client_address if the connection attempt worked */
738 if (ret)
740 server->is_using_listener = TRUE;
742 switch (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (address)))
744 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
745 server->client_address = g_strdup_printf ("unix:abstract=%s",
746 g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address)));
747 break;
749 case G_UNIX_SOCKET_ADDRESS_PATH:
750 server->client_address = g_strdup_printf ("unix:path=%s",
751 g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address)));
752 break;
754 default:
755 g_assert_not_reached ();
756 break;
759 g_object_unref (address);
761 return ret;
763 #endif
765 /* ---------------------------------------------------------------------------------------------------- */
767 /* note that address_entry has already been validated =>
768 * both host and port (guranteed to be a number in [0, 65535]) are set (family is optional)
770 static gboolean
771 try_tcp (GDBusServer *server,
772 const gchar *address_entry,
773 GHashTable *key_value_pairs,
774 gboolean do_nonce,
775 GError **error)
777 gboolean ret;
778 const gchar *host;
779 const gchar *port;
780 const gchar *family;
781 gint port_num;
782 GSocketAddress *address;
783 GResolver *resolver;
784 GList *resolved_addresses;
785 GList *l;
787 ret = FALSE;
788 address = NULL;
789 resolver = NULL;
790 resolved_addresses = NULL;
792 host = g_hash_table_lookup (key_value_pairs, "host");
793 port = g_hash_table_lookup (key_value_pairs, "port");
794 family = g_hash_table_lookup (key_value_pairs, "family");
795 if (g_hash_table_lookup (key_value_pairs, "noncefile") != NULL)
797 g_set_error_literal (error,
798 G_IO_ERROR,
799 G_IO_ERROR_INVALID_ARGUMENT,
800 _("Cannot specify nonce file when creating a server"));
801 goto out;
804 if (host == NULL)
805 host = "localhost";
806 if (port == NULL)
807 port = "0";
808 port_num = strtol (port, NULL, 10);
810 resolver = g_resolver_get_default ();
811 resolved_addresses = g_resolver_lookup_by_name (resolver,
812 host,
813 NULL,
814 error);
815 if (resolved_addresses == NULL)
816 goto out;
818 /* TODO: handle family */
819 for (l = resolved_addresses; l != NULL; l = l->next)
821 GInetAddress *address = G_INET_ADDRESS (l->data);
822 GSocketAddress *socket_address;
823 GSocketAddress *effective_address;
825 socket_address = g_inet_socket_address_new (address, port_num);
826 if (!g_socket_listener_add_address (server->listener,
827 socket_address,
828 G_SOCKET_TYPE_STREAM,
829 G_SOCKET_PROTOCOL_TCP,
830 NULL, /* GObject *source_object */
831 &effective_address,
832 error))
834 g_object_unref (socket_address);
835 goto out;
837 if (port_num == 0)
838 /* make sure we allocate the same port number for other listeners */
839 port_num = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (effective_address));
841 g_object_unref (effective_address);
842 g_object_unref (socket_address);
845 if (do_nonce)
847 gint fd;
848 guint n;
849 gsize bytes_written;
850 gsize bytes_remaining;
852 server->nonce = g_new0 (guchar, 16);
853 for (n = 0; n < 16; n++)
854 server->nonce[n] = g_random_int_range (0, 256);
855 fd = g_file_open_tmp ("gdbus-nonce-file-XXXXXX",
856 &server->nonce_file,
857 error);
858 if (fd == -1)
860 g_socket_listener_close (server->listener);
861 goto out;
863 again:
864 bytes_written = 0;
865 bytes_remaining = 16;
866 while (bytes_remaining > 0)
868 gssize ret;
869 ret = write (fd, server->nonce + bytes_written, bytes_remaining);
870 if (ret == -1)
872 if (errno == EINTR)
873 goto again;
874 g_set_error (error,
875 G_IO_ERROR,
876 g_io_error_from_errno (errno),
877 _("Error writing nonce file at `%s': %s"),
878 server->nonce_file,
879 strerror (errno));
880 goto out;
882 bytes_written += ret;
883 bytes_remaining -= ret;
885 close (fd);
886 server->client_address = g_strdup_printf ("nonce-tcp:host=%s,port=%d,noncefile=%s",
887 host,
888 port_num,
889 server->nonce_file);
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_foreach (resolved_addresses, (GFunc) g_object_unref, NULL);
900 g_list_free (resolved_addresses);
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;
926 g_signal_emit (data->server,
927 _signals[NEW_CONNECTION_SIGNAL],
929 data->connection);
930 g_dbus_connection_start_message_processing (data->connection);
931 g_object_unref (data->connection);
933 return FALSE;
936 /* Called in new thread */
937 static gboolean
938 on_run (GSocketService *service,
939 GSocketConnection *socket_connection,
940 GObject *source_object,
941 gpointer user_data)
943 GDBusServer *server = G_DBUS_SERVER (user_data);
944 GDBusConnection *connection;
945 GDBusConnectionFlags connection_flags;
947 if (server->nonce != NULL)
949 gchar buf[16];
950 gsize bytes_read;
952 if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)),
953 buf,
955 &bytes_read,
956 NULL, /* GCancellable */
957 NULL)) /* GError */
958 goto out;
960 if (bytes_read != 16)
961 goto out;
963 if (memcmp (buf, server->nonce, 16) != 0)
964 goto out;
967 connection_flags =
968 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
969 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING;
970 if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)
971 connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
973 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
974 server->guid,
975 connection_flags,
976 server->authentication_observer,
977 NULL, /* GCancellable */
978 NULL); /* GError */
979 if (connection == NULL)
980 goto out;
982 if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD)
984 g_signal_emit (server,
985 _signals[NEW_CONNECTION_SIGNAL],
987 connection);
988 g_dbus_connection_start_message_processing (connection);
989 g_object_unref (connection);
991 else
993 GSource *idle_source;
994 EmitIdleData *data;
996 data = g_new0 (EmitIdleData, 1);
997 data->server = g_object_ref (server);
998 data->connection = g_object_ref (connection);
1000 idle_source = g_idle_source_new ();
1001 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1002 g_source_set_callback (idle_source,
1003 emit_new_connection_in_idle,
1004 data,
1005 (GDestroyNotify) emit_idle_data_free);
1006 g_source_attach (idle_source, server->main_context_at_construction);
1007 g_source_unref (idle_source);
1010 out:
1011 return TRUE;
1014 static gboolean
1015 initable_init (GInitable *initable,
1016 GCancellable *cancellable,
1017 GError **error)
1019 GDBusServer *server = G_DBUS_SERVER (initable);
1020 gboolean ret;
1021 guint n;
1022 gchar **addr_array;
1023 GError *last_error;
1025 ret = FALSE;
1026 last_error = NULL;
1028 if (!g_dbus_is_guid (server->guid))
1030 g_set_error (&last_error,
1031 G_IO_ERROR,
1032 G_IO_ERROR_INVALID_ARGUMENT,
1033 _("The string `%s' is not a valid D-Bus GUID"),
1034 server->guid);
1035 goto out;
1038 server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
1040 addr_array = g_strsplit (server->address, ";", 0);
1041 last_error = NULL;
1042 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
1044 const gchar *address_entry = addr_array[n];
1045 GHashTable *key_value_pairs;
1046 gchar *transport_name;
1047 GError *this_error;
1049 this_error = NULL;
1050 if (g_dbus_is_supported_address (address_entry,
1051 &this_error) &&
1052 _g_dbus_address_parse_entry (address_entry,
1053 &transport_name,
1054 &key_value_pairs,
1055 &this_error))
1058 if (FALSE)
1061 #ifdef G_OS_UNIX
1062 else if (g_strcmp0 (transport_name, "unix") == 0)
1063 ret = try_unix (server, address_entry, key_value_pairs, &this_error);
1064 #endif
1065 else if (g_strcmp0 (transport_name, "tcp") == 0)
1066 ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
1067 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
1068 ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error);
1069 else
1070 g_set_error (&this_error,
1071 G_IO_ERROR,
1072 G_IO_ERROR_INVALID_ARGUMENT,
1073 _("Cannot listen on unsupported transport `%s'"),
1074 transport_name);
1076 g_free (transport_name);
1077 if (key_value_pairs != NULL)
1078 g_hash_table_unref (key_value_pairs);
1080 if (ret)
1082 g_assert (this_error == NULL);
1083 goto out;
1087 if (this_error != NULL)
1089 if (last_error != NULL)
1090 g_error_free (last_error);
1091 last_error = this_error;
1095 if (!ret)
1096 goto out;
1098 out:
1099 if (ret)
1101 if (last_error != NULL)
1102 g_error_free (last_error);
1104 else
1106 g_assert (last_error != NULL);
1107 g_propagate_error (error, last_error);
1109 return ret;
1113 static void
1114 initable_iface_init (GInitableIface *initable_iface)
1116 initable_iface->init = initable_init;
1119 /* ---------------------------------------------------------------------------------------------------- */