8 #include <gio/gunixsocketaddress.h>
9 #include "gdbusdaemon.h"
12 #include "gdbus-daemon-generated.h"
14 #define DBUS_SERVICE_NAME "org.freedesktop.DBus"
17 #define DBUS_NAME_FLAG_ALLOW_REPLACEMENT 0x1 /**< Allow another service to become the primary owner if requested */
18 #define DBUS_NAME_FLAG_REPLACE_EXISTING 0x2 /**< Request to replace the current primary owner */
19 #define DBUS_NAME_FLAG_DO_NOT_QUEUE 0x4 /**< If we can not become the primary owner do not place us in the queue */
21 /* Replies to request for a name */
22 #define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER 1 /**< Service has become the primary owner of the requested name */
23 #define DBUS_REQUEST_NAME_REPLY_IN_QUEUE 2 /**< Service could not become the primary owner and has been placed in the queue */
24 #define DBUS_REQUEST_NAME_REPLY_EXISTS 3 /**< Service is already in the queue */
25 #define DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER 4 /**< Service is already the primary owner */
27 /* Replies to releasing a name */
28 #define DBUS_RELEASE_NAME_REPLY_RELEASED 1 /**< Service was released from the given name */
29 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT 2 /**< The given name does not exist on the bus */
30 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER 3 /**< Service is not an owner of the given name */
32 /* Replies to service starts */
33 #define DBUS_START_REPLY_SUCCESS 1 /**< Service was auto started */
34 #define DBUS_START_REPLY_ALREADY_RUNNING 2 /**< Service was already running */
36 #define IDLE_TIMEOUT_MSEC 3000
40 _GFreedesktopDBusSkeleton parent_instance;
49 guint32 next_major_id;
50 guint32 next_minor_id;
53 struct _GDBusDaemonClass
55 _GFreedesktopDBusSkeletonClass parent_class;
69 static guint g_dbus_daemon_signals[NR_SIGNALS];
72 static void initable_iface_init (GInitableIface *initable_iface);
73 static void g_dbus_daemon_iface_init (_GFreedesktopDBusIface *iface);
75 #define g_dbus_daemon_get_type _g_dbus_daemon_get_type
76 G_DEFINE_TYPE_WITH_CODE (GDBusDaemon, g_dbus_daemon, _G_TYPE_FREEDESKTOP_DBUS_SKELETON,
77 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
78 G_IMPLEMENT_INTERFACE (_G_TYPE_FREEDESKTOP_DBUS, g_dbus_daemon_iface_init))
83 GDBusConnection *connection;
104 MATCH_ELEMENT_SENDER,
105 MATCH_ELEMENT_INTERFACE,
106 MATCH_ELEMENT_MEMBER,
108 MATCH_ELEMENT_PATH_NAMESPACE,
109 MATCH_ELEMENT_DESTINATION,
110 MATCH_ELEMENT_ARG0NAMESPACE,
111 MATCH_ELEMENT_EAVESDROP,
113 MATCH_ELEMENT_ARGNPATH,
124 GDBusMessageType type;
126 MatchElement *elements;
129 static GDBusMessage *filter_function (GDBusConnection *connection,
130 GDBusMessage *message,
133 static void connection_closed (GDBusConnection *connection,
134 gboolean remote_peer_vanished,
139 name_owner_new (Client *client, guint32 flags)
143 owner = g_new0 (NameOwner, 1);
144 owner->client = client;
145 owner->flags = flags;
150 name_owner_free (NameOwner *owner)
156 name_new (GDBusDaemon *daemon, const char *str)
160 name = g_new0 (Name, 1);
162 name->daemon = daemon;
163 name->name = g_strdup (str);
165 g_hash_table_insert (daemon->names, name->name, name);
171 name_ref (Name *name)
173 g_assert (name->refcount > 0);
179 name_unref (Name *name)
181 g_assert (name->refcount > 0);
182 if (--name->refcount == 0)
184 g_hash_table_remove (name->daemon->names, name->name);
191 name_ensure (GDBusDaemon *daemon, const char *str)
195 name = g_hash_table_lookup (daemon->names, str);
198 return name_ref (name);
199 return name_new (daemon, str);
203 name_lookup (GDBusDaemon *daemon, const char *str)
205 return g_hash_table_lookup (daemon->names, str);
209 is_key (const char *key_start, const char *key_end, const char *value)
211 gsize len = strlen (value);
213 g_assert (key_end >= key_start);
214 if (len != (gsize) (key_end - key_start))
217 return strncmp (key_start, value, len) == 0;
221 parse_key (MatchElement *element, const char *key_start, const char *key_end)
225 if (is_key (key_start, key_end, "type"))
227 element->type = MATCH_ELEMENT_TYPE;
229 else if (is_key (key_start, key_end, "sender"))
231 element->type = MATCH_ELEMENT_SENDER;
233 else if (is_key (key_start, key_end, "interface"))
235 element->type = MATCH_ELEMENT_INTERFACE;
237 else if (is_key (key_start, key_end, "member"))
239 element->type = MATCH_ELEMENT_MEMBER;
241 else if (is_key (key_start, key_end, "path"))
243 element->type = MATCH_ELEMENT_PATH;
245 else if (is_key (key_start, key_end, "path_namespace"))
247 element->type = MATCH_ELEMENT_PATH_NAMESPACE;
249 else if (is_key (key_start, key_end, "destination"))
251 element->type = MATCH_ELEMENT_DESTINATION;
253 else if (is_key (key_start, key_end, "arg0namespace"))
255 element->type = MATCH_ELEMENT_ARG0NAMESPACE;
257 else if (is_key (key_start, key_end, "eavesdrop"))
259 element->type = MATCH_ELEMENT_EAVESDROP;
261 else if (key_end - key_start > 3 && is_key (key_start, key_start + 3, "arg"))
263 const char *digits = key_start + 3;
264 const char *end_digits = digits;
266 while (end_digits < key_end && g_ascii_isdigit (*end_digits))
269 if (end_digits == key_end) /* argN */
271 element->type = MATCH_ELEMENT_ARGN;
272 element->arg = atoi (digits);
274 else if (is_key (end_digits, key_end, "path")) /* argNpath */
276 element->type = MATCH_ELEMENT_ARGNPATH;
277 element->arg = atoi (digits);
289 parse_value (MatchElement *element, const char *s)
294 value = g_string_new ("");
317 g_string_append_c (value, *s);
321 else if (quote_char == '\\')
323 /* \ only counts as an escape if escaping a quote mark */
325 g_string_append_c (value, '\\');
327 g_string_append_c (value, *s);
330 else /* quote_char == ' */
335 g_string_append_c (value, *s);
341 if (quote_char == '\\')
342 g_string_append_c (value, '\\');
343 else if (quote_char == '\'')
345 g_string_free (value, TRUE);
349 element->value = g_string_free (value, FALSE);
354 match_new (const char *str)
359 const char *key_start;
361 MatchElement element;
363 GDBusMessageType type;
367 type = G_DBUS_MESSAGE_TYPE_INVALID;
368 elements = g_array_new (TRUE, TRUE, sizeof (MatchElement));
374 memset (&element, 0, sizeof (element));
376 /* Skip initial whitespace */
377 while (*p && g_ascii_isspace (*p))
382 /* Read non-whitespace non-equals chars */
383 while (*p && *p != '=' && !g_ascii_isspace (*p))
388 /* Skip any whitespace after key */
389 while (*p && g_ascii_isspace (*p))
392 if (key_start == key_end)
393 continue; /* Allow trailing whitespace */
400 if (!parse_key (&element, key_start, key_end))
403 p = parse_value (&element, p);
407 if (element.type == MATCH_ELEMENT_EAVESDROP)
409 if (strcmp (element.value, "true") == 0)
411 else if (strcmp (element.value, "false") == 0)
415 g_free (element.value);
418 g_free (element.value);
420 else if (element.type == MATCH_ELEMENT_TYPE)
422 if (strcmp (element.value, "signal") == 0)
423 type = G_DBUS_MESSAGE_TYPE_SIGNAL;
424 else if (strcmp (element.value, "method_call") == 0)
425 type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
426 else if (strcmp (element.value, "method_return") == 0)
427 type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
428 else if (strcmp (element.value, "error") == 0)
429 type = G_DBUS_MESSAGE_TYPE_ERROR;
432 g_free (element.value);
435 g_free (element.value);
438 g_array_append_val (elements, element);
441 match = g_new0 (Match, 1);
442 match->n_elements = elements->len;
443 match->elements = (MatchElement *)g_array_free (elements, FALSE);
444 match->eavesdrop = eavesdrop;
450 for (i = 0; i < elements->len; i++)
451 g_free (g_array_index (elements, MatchElement, i).value);
452 g_array_free (elements, TRUE);
457 match_free (Match *match)
460 for (i = 0; i < match->n_elements; i++)
461 g_free (match->elements[i].value);
462 g_free (match->elements);
467 match_equal (Match *a, Match *b)
471 if (a->eavesdrop != b->eavesdrop)
473 if (a->type != b->type)
475 if (a->n_elements != b->n_elements)
477 for (i = 0; i < a->n_elements; i++)
479 if (a->elements[i].type != b->elements[i].type ||
480 a->elements[i].arg != b->elements[i].arg ||
481 strcmp (a->elements[i].value, b->elements[i].value) != 0)
488 message_get_argN (GDBusMessage *message, int n, gboolean allow_path)
495 body = g_dbus_message_get_body (message);
497 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE))
500 item = g_variant_get_child_value (body, n);
501 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING) ||
502 (allow_path && g_variant_is_of_type (item, G_VARIANT_TYPE_OBJECT_PATH)))
503 ret = g_variant_get_string (item, NULL);
504 g_variant_unref (item);
513 CHECK_TYPE_PATH_PREFIX,
514 CHECK_TYPE_PATH_RELATED,
515 CHECK_TYPE_NAMESPACE_PREFIX
519 match_matches (GDBusDaemon *daemon,
520 Match *match, GDBusMessage *message,
521 gboolean has_destination)
523 MatchElement *element;
529 if (has_destination && !match->eavesdrop)
532 if (match->type != G_DBUS_MESSAGE_TYPE_INVALID &&
533 g_dbus_message_get_message_type (message) != match->type)
536 for (i = 0; i < match->n_elements; i++)
538 element = &match->elements[i];
539 check_type = CHECK_TYPE_STRING;
540 switch (element->type)
542 case MATCH_ELEMENT_SENDER:
543 check_type = CHECK_TYPE_NAME;
544 value = g_dbus_message_get_sender (message);
546 value = DBUS_SERVICE_NAME;
548 case MATCH_ELEMENT_DESTINATION:
549 check_type = CHECK_TYPE_NAME;
550 value = g_dbus_message_get_destination (message);
552 case MATCH_ELEMENT_INTERFACE:
553 value = g_dbus_message_get_interface (message);
555 case MATCH_ELEMENT_MEMBER:
556 value = g_dbus_message_get_member (message);
558 case MATCH_ELEMENT_PATH:
559 value = g_dbus_message_get_path (message);
561 case MATCH_ELEMENT_PATH_NAMESPACE:
562 check_type = CHECK_TYPE_PATH_PREFIX;
563 value = g_dbus_message_get_path (message);
565 case MATCH_ELEMENT_ARG0NAMESPACE:
566 check_type = CHECK_TYPE_NAMESPACE_PREFIX;
567 value = message_get_argN (message, 0, FALSE);
569 case MATCH_ELEMENT_ARGN:
570 value = message_get_argN (message, element->arg, FALSE);
572 case MATCH_ELEMENT_ARGNPATH:
573 check_type = CHECK_TYPE_PATH_RELATED;
574 value = message_get_argN (message, element->arg, TRUE);
577 case MATCH_ELEMENT_TYPE:
578 case MATCH_ELEMENT_EAVESDROP:
579 g_assert_not_reached ();
587 case CHECK_TYPE_STRING:
588 if (strcmp (element->value, value) != 0)
591 case CHECK_TYPE_NAME:
592 name = name_lookup (daemon, element->value);
593 if (name != NULL && name->owner != NULL)
595 if (strcmp (name->owner->client->id, value) != 0)
598 else if (strcmp (element->value, value) != 0)
601 case CHECK_TYPE_PATH_PREFIX:
602 len = strlen (element->value);
604 /* Make sure to handle the case of element->value == '/'. */
608 /* Fail if there's no prefix match, or if the prefix match doesn't
609 * finish at the end of or at a separator in the @value. */
610 if (!g_str_has_prefix (value, element->value))
612 if (value[len] != 0 && value[len] != '/')
616 case CHECK_TYPE_PATH_RELATED:
617 len = strlen (element->value);
618 len2 = strlen (value);
620 if (!(strcmp (value, element->value) == 0 ||
621 (len2 > 0 && value[len2-1] == '/' && g_str_has_prefix (element->value, value)) ||
622 (len > 0 && element->value[len-1] == '/' && g_str_has_prefix (value, element->value))))
625 case CHECK_TYPE_NAMESPACE_PREFIX:
626 len = strlen (element->value);
627 if (!(g_str_has_prefix (value, element->value) &&
628 (value[len] == 0 || value[len] == '.')))
632 g_assert_not_reached ();
640 broadcast_message (GDBusDaemon *daemon,
641 GDBusMessage *message,
642 gboolean has_destination,
643 gboolean preserve_serial,
646 GList *clients, *l, *ll;
649 clients = g_hash_table_get_values (daemon->clients);
650 for (l = clients; l != NULL; l = l->next)
652 Client *client = l->data;
654 if (client == not_to)
657 for (ll = client->matches; ll != NULL; ll = ll->next)
659 Match *match = ll->data;
661 if (match_matches (daemon, match, message, has_destination))
667 copy = g_dbus_message_copy (message, NULL);
670 g_dbus_connection_send_message (client->connection, copy,
671 preserve_serial?G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL:0, NULL, NULL);
672 g_object_unref (copy);
677 g_list_free (clients);
681 send_name_owner_changed (GDBusDaemon *daemon,
683 const char *old_owner,
684 const char *new_owner)
686 GDBusMessage *signal_message;
688 signal_message = g_dbus_message_new_signal ("/org/freedesktop/DBus",
689 "org.freedesktop.DBus",
691 g_dbus_message_set_body (signal_message,
692 g_variant_new ("(sss)",
694 old_owner ? old_owner : "",
695 new_owner ? new_owner : ""));
697 broadcast_message (daemon, signal_message, FALSE, FALSE, NULL);
698 g_object_unref (signal_message);
703 name_unqueue_owner (Name *name, Client *client)
707 for (l = name->queue; l != NULL; l = l->next)
709 NameOwner *other = l->data;
711 if (other->client == client)
713 name->queue = g_list_delete_link (name->queue, l);
715 name_owner_free (other);
724 name_replace_owner (Name *name, NameOwner *owner)
726 GDBusDaemon *daemon = name->daemon;
727 NameOwner *old_owner;
728 char *old_name = NULL, *new_name = NULL;
729 Client *new_client = NULL;
732 new_client = owner->client;
736 old_owner = name->owner;
739 Client *old_client = old_owner->client;
741 g_assert (old_owner->client != new_client);
743 g_dbus_connection_emit_signal (old_client->connection,
744 NULL, "/org/freedesktop/DBus",
745 "org.freedesktop.DBus", "NameLost",
746 g_variant_new ("(s)",
749 old_name = g_strdup (old_client->id);
750 if (old_owner->flags & DBUS_NAME_FLAG_DO_NOT_QUEUE)
753 name_owner_free (old_owner);
756 name->queue = g_list_prepend (name->queue, old_owner);
762 name_unqueue_owner (name, owner->client);
764 new_name = new_client->id;
766 g_dbus_connection_emit_signal (new_client->connection,
767 NULL, "/org/freedesktop/DBus",
768 "org.freedesktop.DBus", "NameAcquired",
769 g_variant_new ("(s)",
773 send_name_owner_changed (daemon, name->name, old_name, new_name);
781 name_release_owner (Name *name)
783 NameOwner *next_owner = NULL;
787 /* Will someone else take over? */
790 next_owner = name->queue->data;
792 name->queue = g_list_delete_link (name->queue, name->queue);
795 name->owner->flags |= DBUS_NAME_FLAG_DO_NOT_QUEUE;
796 name_replace_owner (name, next_owner);
802 name_queue_owner (Name *name, NameOwner *owner)
806 for (l = name->queue; l != NULL; l = l->next)
808 NameOwner *other = l->data;
810 if (other->client == owner->client)
812 other->flags = owner->flags;
813 name_owner_free (owner);
818 name->queue = g_list_append (name->queue, owner);
823 client_new (GDBusDaemon *daemon, GDBusConnection *connection)
826 GError *error = NULL;
828 client = g_new0 (Client, 1);
829 client->daemon = daemon;
830 client->id = g_strdup_printf (":%d.%d", daemon->next_major_id, daemon->next_minor_id);
831 client->connection = g_object_ref (connection);
833 if (daemon->next_minor_id == G_MAXUINT32)
835 daemon->next_minor_id = 0;
836 daemon->next_major_id++;
839 daemon->next_minor_id++;
841 g_object_set_data (G_OBJECT (connection), "client", client);
842 g_hash_table_insert (daemon->clients, client->id, client);
844 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (daemon), connection,
845 "/org/freedesktop/DBus", &error);
846 g_assert_no_error (error);
848 g_signal_connect (connection, "closed", G_CALLBACK (connection_closed), client);
849 g_dbus_connection_add_filter (connection,
853 send_name_owner_changed (daemon, client->id, NULL, client->id);
859 client_free (Client *client)
861 GDBusDaemon *daemon = client->daemon;
864 g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (daemon),
867 g_hash_table_remove (daemon->clients, client->id);
869 names = g_hash_table_get_values (daemon->names);
870 for (l = names; l != NULL; l = l->next)
872 Name *name = l->data;
876 if (name->owner && name->owner->client == client)
877 name_release_owner (name);
879 name_unqueue_owner (name, client);
885 send_name_owner_changed (daemon, client->id, client->id, NULL);
887 g_object_unref (client->connection);
889 for (l = client->matches; l != NULL; l = l->next)
890 match_free (l->data);
891 g_list_free (client->matches);
898 idle_timeout_cb (gpointer user_data)
900 GDBusDaemon *daemon = user_data;
904 g_signal_emit (daemon,
905 g_dbus_daemon_signals[SIGNAL_IDLE_TIMEOUT],
908 return G_SOURCE_REMOVE;
912 connection_closed (GDBusConnection *connection,
913 gboolean remote_peer_vanished,
917 GDBusDaemon *daemon = client->daemon;
919 client_free (client);
921 if (g_hash_table_size (daemon->clients) == 0)
922 daemon->timeout = g_timeout_add (IDLE_TIMEOUT_MSEC,
928 handle_add_match (_GFreedesktopDBus *object,
929 GDBusMethodInvocation *invocation,
930 const gchar *arg_rule)
932 Client *client = g_object_get_data (G_OBJECT (g_dbus_method_invocation_get_connection (invocation)), "client");
935 match = match_new (arg_rule);
938 g_dbus_method_invocation_return_error (invocation,
939 G_DBUS_ERROR, G_DBUS_ERROR_MATCH_RULE_INVALID,
940 "Invalid rule: %s", arg_rule);
943 client->matches = g_list_prepend (client->matches, match);
944 _g_freedesktop_dbus_complete_add_match (object, invocation);
950 handle_get_connection_selinux_security_context (_GFreedesktopDBus *object,
951 GDBusMethodInvocation *invocation,
952 const gchar *arg_name)
954 g_dbus_method_invocation_return_error (invocation,
955 G_DBUS_ERROR, G_DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
956 "selinux context not supported");
957 _g_freedesktop_dbus_complete_get_connection_selinux_security_context (object, invocation, "");
962 handle_get_connection_unix_process_id (_GFreedesktopDBus *object,
963 GDBusMethodInvocation *invocation,
964 const gchar *arg_name)
966 g_dbus_method_invocation_return_error (invocation,
967 G_DBUS_ERROR, G_DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
968 "connection pid not supported");
973 handle_get_connection_unix_user (_GFreedesktopDBus *object,
974 GDBusMethodInvocation *invocation,
975 const gchar *arg_name)
977 g_dbus_method_invocation_return_error (invocation,
978 G_DBUS_ERROR, G_DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
979 "connection user not supported");
984 handle_get_id (_GFreedesktopDBus *object,
985 GDBusMethodInvocation *invocation)
987 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
988 _g_freedesktop_dbus_complete_get_id (object, invocation,
994 handle_get_name_owner (_GFreedesktopDBus *object,
995 GDBusMethodInvocation *invocation,
996 const gchar *arg_name)
998 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1001 if (strcmp (arg_name, DBUS_SERVICE_NAME) == 0)
1003 _g_freedesktop_dbus_complete_get_name_owner (object, invocation, DBUS_SERVICE_NAME);
1007 if (arg_name[0] == ':')
1009 if (g_hash_table_lookup (daemon->clients, arg_name) == NULL)
1010 g_dbus_method_invocation_return_error (invocation,
1011 G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER,
1012 "Could not get owner of name '%s': no such name", arg_name);
1014 _g_freedesktop_dbus_complete_get_name_owner (object, invocation, arg_name);
1018 name = name_lookup (daemon, arg_name);
1019 if (name == NULL || name->owner == NULL)
1021 g_dbus_method_invocation_return_error (invocation,
1022 G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER,
1023 "Could not get owner of name '%s': no such name", arg_name);
1027 _g_freedesktop_dbus_complete_get_name_owner (object, invocation, name->owner->client->id);
1032 handle_hello (_GFreedesktopDBus *object,
1033 GDBusMethodInvocation *invocation)
1035 Client *client = g_object_get_data (G_OBJECT (g_dbus_method_invocation_get_connection (invocation)), "client");
1036 _g_freedesktop_dbus_complete_hello (object, invocation, client->id);
1038 g_dbus_connection_emit_signal (client->connection,
1039 NULL, "/org/freedesktop/DBus",
1040 "org.freedesktop.DBus", "NameAcquired",
1041 g_variant_new ("(s)",
1048 handle_list_activatable_names (_GFreedesktopDBus *object,
1049 GDBusMethodInvocation *invocation)
1051 const char *names[] = { NULL };
1053 _g_freedesktop_dbus_complete_list_activatable_names (object,
1060 handle_list_names (_GFreedesktopDBus *object,
1061 GDBusMethodInvocation *invocation)
1063 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1065 GList *clients, *names, *l;
1067 array = g_ptr_array_new ();
1069 clients = g_hash_table_get_values (daemon->clients);
1070 for (l = clients; l != NULL; l = l->next)
1072 Client *client = l->data;
1074 g_ptr_array_add (array, client->id);
1077 g_list_free (clients);
1079 names = g_hash_table_get_values (daemon->names);
1080 for (l = names; l != NULL; l = l->next)
1082 Name *name = l->data;
1084 g_ptr_array_add (array, name->name);
1087 g_list_free (names);
1089 g_ptr_array_add (array, NULL);
1091 _g_freedesktop_dbus_complete_list_names (object,
1093 (const gchar * const*)array->pdata);
1094 g_ptr_array_free (array, TRUE);
1099 handle_list_queued_owners (_GFreedesktopDBus *object,
1100 GDBusMethodInvocation *invocation,
1101 const gchar *arg_name)
1103 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1108 array = g_ptr_array_new ();
1110 name = name_lookup (daemon, arg_name);
1111 if (name && name->owner)
1113 for (l = name->queue; l != NULL; l = l->next)
1115 Client *client = l->data;
1117 g_ptr_array_add (array, client->id);
1121 g_ptr_array_add (array, NULL);
1123 _g_freedesktop_dbus_complete_list_queued_owners (object,
1125 (const gchar * const*)array->pdata);
1126 g_ptr_array_free (array, TRUE);
1131 handle_name_has_owner (_GFreedesktopDBus *object,
1132 GDBusMethodInvocation *invocation,
1133 const gchar *arg_name)
1135 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1139 name = name_lookup (daemon, arg_name);
1140 client = g_hash_table_lookup (daemon->clients, arg_name);
1142 _g_freedesktop_dbus_complete_name_has_owner (object, invocation,
1143 name != NULL || client != NULL);
1148 handle_release_name (_GFreedesktopDBus *object,
1149 GDBusMethodInvocation *invocation,
1150 const gchar *arg_name)
1152 Client *client = g_object_get_data (G_OBJECT (g_dbus_method_invocation_get_connection (invocation)), "client");
1153 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1157 if (!g_dbus_is_name (arg_name))
1159 g_dbus_method_invocation_return_error (invocation,
1160 G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
1161 "Given bus name \"%s\" is not valid", arg_name);
1165 if (*arg_name == ':')
1167 g_dbus_method_invocation_return_error (invocation,
1168 G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
1169 "Cannot release a service starting with ':' such as \"%s\"", arg_name);
1173 if (strcmp (arg_name, DBUS_SERVICE_NAME) == 0)
1175 g_dbus_method_invocation_return_error (invocation,
1176 G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
1177 "Cannot release a service named " DBUS_SERVICE_NAME ", because that is owned by the bus");
1181 name = name_lookup (daemon, arg_name);
1184 result = DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
1185 else if (name->owner && name->owner->client == client)
1187 name_release_owner (name);
1188 result = DBUS_RELEASE_NAME_REPLY_RELEASED;
1190 else if (name_unqueue_owner (name, client))
1191 result = DBUS_RELEASE_NAME_REPLY_RELEASED;
1193 result = DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
1195 _g_freedesktop_dbus_complete_release_name (object, invocation, result);
1200 handle_reload_config (_GFreedesktopDBus *object,
1201 GDBusMethodInvocation *invocation)
1203 _g_freedesktop_dbus_complete_reload_config (object, invocation);
1208 handle_update_activation_environment (_GFreedesktopDBus *object,
1209 GDBusMethodInvocation *invocation,
1210 GVariant *arg_environment)
1212 g_dbus_method_invocation_return_error (invocation,
1213 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
1214 "UpdateActivationEnvironment not implemented");
1219 handle_remove_match (_GFreedesktopDBus *object,
1220 GDBusMethodInvocation *invocation,
1221 const gchar *arg_rule)
1223 Client *client = g_object_get_data (G_OBJECT (g_dbus_method_invocation_get_connection (invocation)), "client");
1224 Match *match, *other_match;
1227 match = match_new (arg_rule);
1230 g_dbus_method_invocation_return_error (invocation,
1231 G_DBUS_ERROR, G_DBUS_ERROR_MATCH_RULE_INVALID,
1232 "Invalid rule: %s", arg_rule);
1235 for (l = client->matches; l != NULL; l = l->next)
1237 other_match = l->data;
1238 if (match_equal (match, other_match))
1240 match_free (other_match);
1241 client->matches = g_list_delete_link (client->matches, l);
1247 g_dbus_method_invocation_return_error (invocation,
1248 G_DBUS_ERROR, G_DBUS_ERROR_MATCH_RULE_NOT_FOUND,
1249 "The given match rule wasn't found and can't be removed");
1251 _g_freedesktop_dbus_complete_remove_match (object, invocation);
1260 handle_request_name (_GFreedesktopDBus *object,
1261 GDBusMethodInvocation *invocation,
1262 const gchar *arg_name,
1265 Client *client = g_object_get_data (G_OBJECT (g_dbus_method_invocation_get_connection (invocation)), "client");
1266 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1271 if (!g_dbus_is_name (arg_name))
1273 g_dbus_method_invocation_return_error (invocation,
1274 G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
1275 "Requested bus name \"%s\" is not valid", arg_name);
1279 if (*arg_name == ':')
1281 g_dbus_method_invocation_return_error (invocation,
1282 G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
1283 "Cannot acquire a service starting with ':' such as \"%s\"", arg_name);
1287 if (strcmp (arg_name, DBUS_SERVICE_NAME) == 0)
1289 g_dbus_method_invocation_return_error (invocation,
1290 G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
1291 "Cannot acquire a service named " DBUS_SERVICE_NAME ", because that is reserved");
1295 name = name_ensure (daemon, arg_name);
1296 if (name->owner == NULL)
1298 owner = name_owner_new (client, flags);
1299 name_replace_owner (name, owner);
1301 result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
1303 else if (name->owner && name->owner->client == client)
1305 name->owner->flags = flags;
1306 result = DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
1308 else if ((flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
1309 (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) ||
1310 !(name->owner->flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)))
1312 /* Unqueue if queued */
1313 name_unqueue_owner (name, client);
1314 result = DBUS_REQUEST_NAME_REPLY_EXISTS;
1316 else if (!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
1317 (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) ||
1318 !(name->owner->flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)))
1320 /* Queue the connection */
1321 owner = name_owner_new (client, flags);
1322 name_queue_owner (name, owner);
1323 result = DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
1327 /* Replace the current owner */
1329 owner = name_owner_new (client, flags);
1330 name_replace_owner (name, owner);
1332 result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
1337 _g_freedesktop_dbus_complete_request_name (object, invocation, result);
1342 handle_start_service_by_name (_GFreedesktopDBus *object,
1343 GDBusMethodInvocation *invocation,
1344 const gchar *arg_name,
1347 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1350 name = name_lookup (daemon, arg_name);
1352 _g_freedesktop_dbus_complete_start_service_by_name (object, invocation,
1353 DBUS_START_REPLY_ALREADY_RUNNING);
1355 g_dbus_method_invocation_return_error (invocation,
1356 G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN,
1357 "No support for activation for name: %s", arg_name);
1364 return_error (Client *client, GDBusMessage *message,
1367 const gchar *format,
1370 GDBusMessage *reply;
1372 char *error_message;
1374 gchar *dbus_error_name;
1376 va_start (var_args, format);
1377 error_message = g_strdup_vprintf (format, var_args);
1380 error = g_error_new_literal (domain, code, "");
1381 dbus_error_name = g_dbus_error_encode_gerror (error);
1383 reply = g_dbus_message_new_method_error_literal (message,
1387 g_error_free (error);
1388 g_free (dbus_error_name);
1389 g_free (error_message);
1391 if (!g_dbus_connection_send_message (client->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL))
1392 g_warning ("Error sending reply");
1393 g_object_unref (reply);
1396 static GDBusMessage *
1397 route_message (Client *source_client, GDBusMessage *message)
1400 Client *dest_client;
1401 GDBusDaemon *daemon;
1403 daemon = source_client->daemon;
1406 dest = g_dbus_message_get_destination (message);
1407 if (dest != NULL && strcmp (dest, DBUS_SERVICE_NAME) != 0)
1409 dest_client = g_hash_table_lookup (daemon->clients, dest);
1411 if (dest_client == NULL)
1414 name = name_lookup (daemon, dest);
1415 if (name && name->owner)
1416 dest_client = name->owner->client;
1419 if (dest_client == NULL)
1421 if (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL)
1422 return_error (source_client, message,
1423 G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN,
1424 "The name %s is unknown", dest);
1428 GError *error = NULL;
1430 if (!g_dbus_connection_send_message (dest_client->connection, message, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, NULL, &error))
1432 g_warning ("Error forwarding message: %s", error->message);
1433 g_error_free (error);
1438 broadcast_message (daemon, message, dest_client != NULL, TRUE, dest_client);
1440 /* Swallow messages not for the bus */
1441 if (dest == NULL || strcmp (dest, DBUS_SERVICE_NAME) != 0)
1443 g_object_unref (message);
1450 static GDBusMessage *
1451 copy_if_locked (GDBusMessage *message)
1453 if (g_dbus_message_get_locked (message))
1455 GDBusMessage *copy = g_dbus_message_copy (message, NULL);
1456 g_object_unref (message);
1462 static GDBusMessage *
1463 filter_function (GDBusConnection *connection,
1464 GDBusMessage *message,
1468 Client *client = user_data;
1472 const char *types[] = {"invalid", "method_call", "method_return", "error", "signal" };
1473 g_printerr ("%s%s %s %d(%d) sender: %s destination: %s %s %s.%s\n",
1475 incoming? "->" : "<-",
1476 types[g_dbus_message_get_message_type (message)],
1477 g_dbus_message_get_serial (message),
1478 g_dbus_message_get_reply_serial (message),
1479 g_dbus_message_get_sender (message),
1480 g_dbus_message_get_destination (message),
1481 g_dbus_message_get_path (message),
1482 g_dbus_message_get_interface (message),
1483 g_dbus_message_get_member (message));
1488 /* Ensure its not locked so we can set the sender */
1489 message = copy_if_locked (message);
1490 if (message == NULL)
1492 g_warning ("Failed to copy incoming message");
1495 g_dbus_message_set_sender (message, client->id);
1497 return route_message (client, message);
1501 if (g_dbus_message_get_sender (message) == NULL ||
1502 g_dbus_message_get_destination (message) == NULL)
1504 message = copy_if_locked (message);
1505 if (message == NULL)
1507 g_warning ("Failed to copy outgoing message");
1512 if (g_dbus_message_get_sender (message) == NULL)
1513 g_dbus_message_set_sender (message, DBUS_SERVICE_NAME);
1514 if (g_dbus_message_get_destination (message) == NULL)
1515 g_dbus_message_set_destination (message, client->id);
1522 on_new_connection (GDBusServer *server,
1523 GDBusConnection *connection,
1526 GDBusDaemon *daemon = user_data;
1528 g_dbus_connection_set_exit_on_close (connection, FALSE);
1530 if (daemon->timeout)
1532 g_source_remove (daemon->timeout);
1533 daemon->timeout = 0;
1536 client_new (daemon, connection);
1542 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1544 GCredentials *credentials,
1547 gboolean authorized = FALSE;
1549 if (credentials != NULL)
1551 GCredentials *own_credentials;
1553 own_credentials = g_credentials_new ();
1554 authorized = g_credentials_is_same_user (credentials, own_credentials, NULL);
1555 g_object_unref (own_credentials);
1560 /* We allow ANONYMOUS authentication on Windows for now, in
1561 * combination with the nonce-tcp transport. */
1570 g_dbus_daemon_finalize (GObject *object)
1572 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1575 if (daemon->timeout)
1576 g_source_remove (daemon->timeout);
1578 clients = g_hash_table_get_values (daemon->clients);
1579 for (l = clients; l != NULL; l = l->next)
1580 client_free (l->data);
1581 g_list_free (clients);
1583 g_assert (g_hash_table_size (daemon->clients) == 0);
1584 g_assert (g_hash_table_size (daemon->names) == 0);
1586 g_hash_table_destroy (daemon->clients);
1587 g_hash_table_destroy (daemon->names);
1589 g_object_unref (daemon->server);
1593 g_rmdir (daemon->tmpdir);
1594 g_free (daemon->tmpdir);
1597 g_free (daemon->guid);
1598 g_free (daemon->address);
1600 G_OBJECT_CLASS (g_dbus_daemon_parent_class)->finalize (object);
1604 g_dbus_daemon_init (GDBusDaemon *daemon)
1606 daemon->next_major_id = 1;
1607 daemon->clients = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
1608 daemon->names = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
1609 daemon->guid = g_dbus_generate_guid ();
1613 initable_init (GInitable *initable,
1614 GCancellable *cancellable,
1617 GDBusDaemon *daemon = G_DBUS_DAEMON (initable);
1618 GDBusAuthObserver *observer;
1619 GDBusServerFlags flags;
1621 flags = G_DBUS_SERVER_FLAGS_NONE;
1622 if (daemon->address == NULL)
1625 if (g_unix_socket_address_abstract_names_supported ())
1626 daemon->address = g_strdup ("unix:tmpdir=/tmp/gdbus-daemon");
1629 daemon->tmpdir = g_dir_make_tmp ("gdbus-daemon-XXXXXX", NULL);
1630 daemon->address = g_strdup_printf ("unix:tmpdir=%s", daemon->tmpdir);
1633 daemon->address = g_strdup ("nonce-tcp:");
1634 flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
1638 observer = g_dbus_auth_observer_new ();
1639 daemon->server = g_dbus_server_new_sync (daemon->address,
1645 if (daemon->server == NULL)
1647 g_object_unref (observer);
1652 g_dbus_server_start (daemon->server);
1654 g_signal_connect (daemon->server, "new-connection",
1655 G_CALLBACK (on_new_connection),
1657 g_signal_connect (observer,
1658 "authorize-authenticated-peer",
1659 G_CALLBACK (on_authorize_authenticated_peer),
1662 g_object_unref (observer);
1668 g_dbus_daemon_set_property (GObject *object,
1670 const GValue *value,
1673 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1678 g_free (daemon->address);
1679 daemon->address = g_value_dup_string (value);
1683 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1688 g_dbus_daemon_get_property (GObject *object,
1693 GDBusDaemon *daemon = G_DBUS_DAEMON (object);
1698 g_value_set_string (value, daemon->address);
1702 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1707 g_dbus_daemon_class_init (GDBusDaemonClass *klass)
1709 GObjectClass *gobject_class;
1711 gobject_class = G_OBJECT_CLASS (klass);
1712 gobject_class->finalize = g_dbus_daemon_finalize;
1713 gobject_class->set_property = g_dbus_daemon_set_property;
1714 gobject_class->get_property = g_dbus_daemon_get_property;
1716 g_dbus_daemon_signals[SIGNAL_IDLE_TIMEOUT] =
1717 g_signal_new (I_("idle-timeout"),
1725 g_object_class_install_property (gobject_class,
1727 g_param_spec_string ("address",
1729 "The address the bus should use",
1732 G_PARAM_CONSTRUCT_ONLY |
1733 G_PARAM_STATIC_STRINGS));
1737 g_dbus_daemon_iface_init (_GFreedesktopDBusIface *iface)
1739 iface->handle_add_match = handle_add_match;
1740 iface->handle_get_connection_selinux_security_context = handle_get_connection_selinux_security_context;
1741 iface->handle_get_connection_unix_process_id = handle_get_connection_unix_process_id;
1742 iface->handle_get_connection_unix_user = handle_get_connection_unix_user;
1743 iface->handle_get_id = handle_get_id;
1744 iface->handle_get_name_owner = handle_get_name_owner;
1745 iface->handle_hello = handle_hello;
1746 iface->handle_list_activatable_names = handle_list_activatable_names;
1747 iface->handle_list_names = handle_list_names;
1748 iface->handle_list_queued_owners = handle_list_queued_owners;
1749 iface->handle_name_has_owner = handle_name_has_owner;
1750 iface->handle_release_name = handle_release_name;
1751 iface->handle_reload_config = handle_reload_config;
1752 iface->handle_update_activation_environment = handle_update_activation_environment;
1753 iface->handle_remove_match = handle_remove_match;
1754 iface->handle_request_name = handle_request_name;
1755 iface->handle_start_service_by_name = handle_start_service_by_name;
1759 initable_iface_init (GInitableIface *initable_iface)
1761 initable_iface->init = initable_init;
1765 _g_dbus_daemon_new (const char *address,
1766 GCancellable *cancellable,
1769 return g_initable_new (G_TYPE_DBUS_DAEMON,
1777 _g_dbus_daemon_get_address (GDBusDaemon *daemon)
1779 return g_dbus_server_get_client_address (daemon->server);