5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <dbus/dbus.h>
40 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
42 #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
43 #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
46 #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
47 #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
51 unsigned int refcount;
59 gboolean pending_prop;
61 struct generic_data *parent;
64 struct interface_data {
66 const GDBusMethodTable *methods;
67 const GDBusSignalTable *signals;
68 const GDBusPropertyTable *properties;
71 GDBusDestroyFunction destroy;
74 struct security_data {
75 GDBusPendingReply pending;
77 const GDBusMethodTable *method;
78 void *iface_user_data;
81 struct property_data {
83 GDBusPendingPropertySet id;
87 static int global_flags = 0;
88 static struct generic_data *root;
90 static gboolean process_changes(gpointer user_data);
91 static void process_properties_from_interface(struct generic_data *data,
92 struct interface_data *iface);
93 static void process_property_changes(struct generic_data *data);
95 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
96 const char *direction)
98 for (; args && args->name; args++) {
99 g_string_append_printf(gstr,
100 "<arg name=\"%s\" type=\"%s\"",
101 args->name, args->signature);
104 g_string_append_printf(gstr,
105 " direction=\"%s\"/>\n", direction);
107 g_string_append_printf(gstr, "/>\n");
112 #define G_DBUS_ANNOTATE(name_, value_) \
113 "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
114 "value=\"" value_ "\"/>"
116 #define G_DBUS_ANNOTATE_DEPRECATED \
117 G_DBUS_ANNOTATE("Deprecated", "true")
119 #define G_DBUS_ANNOTATE_NOREPLY \
120 G_DBUS_ANNOTATE("Method.NoReply", "true")
122 static gboolean check_experimental(int flags, int flag)
127 return !(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL);
130 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
132 const GDBusMethodTable *method;
133 const GDBusSignalTable *signal;
134 const GDBusPropertyTable *property;
136 for (method = iface->methods; method && method->name; method++) {
137 if (check_experimental(method->flags,
138 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
141 g_string_append_printf(gstr, "<method name=\"%s\">",
143 print_arguments(gstr, method->in_args, "in");
144 print_arguments(gstr, method->out_args, "out");
146 if (method->flags & G_DBUS_METHOD_FLAG_DEPRECATED)
147 g_string_append_printf(gstr,
148 G_DBUS_ANNOTATE_DEPRECATED);
150 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY)
151 g_string_append_printf(gstr, G_DBUS_ANNOTATE_NOREPLY);
153 g_string_append_printf(gstr, "</method>");
156 for (signal = iface->signals; signal && signal->name; signal++) {
157 if (check_experimental(signal->flags,
158 G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
161 g_string_append_printf(gstr, "<signal name=\"%s\">",
163 print_arguments(gstr, signal->args, NULL);
165 if (signal->flags & G_DBUS_SIGNAL_FLAG_DEPRECATED)
166 g_string_append_printf(gstr,
167 G_DBUS_ANNOTATE_DEPRECATED);
169 g_string_append_printf(gstr, "</signal>\n");
172 for (property = iface->properties; property && property->name;
174 if (check_experimental(property->flags,
175 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
178 g_string_append_printf(gstr, "<property name=\"%s\""
179 " type=\"%s\" access=\"%s%s\">",
180 property->name, property->type,
181 property->get ? "read" : "",
182 property->set ? "write" : "");
184 if (property->flags & G_DBUS_PROPERTY_FLAG_DEPRECATED)
185 g_string_append_printf(gstr,
186 G_DBUS_ANNOTATE_DEPRECATED);
188 g_string_append_printf(gstr, "</property>");
192 static void generate_introspection_xml(DBusConnection *conn,
193 struct generic_data *data, const char *path)
200 g_free(data->introspect);
202 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
204 g_string_append_printf(gstr, "<node>");
206 for (list = data->interfaces; list; list = list->next) {
207 struct interface_data *iface = list->data;
209 g_string_append_printf(gstr, "<interface name=\"%s\">",
212 generate_interface_xml(gstr, iface);
214 g_string_append_printf(gstr, "</interface>");
217 if (!dbus_connection_list_registered(conn, path, &children))
220 for (i = 0; children[i]; i++)
221 g_string_append_printf(gstr, "<node name=\"%s\"/>",
224 dbus_free_string_array(children);
227 g_string_append_printf(gstr, "</node>");
229 data->introspect = g_string_free(gstr, FALSE);
232 static DBusMessage *introspect(DBusConnection *connection,
233 DBusMessage *message, void *user_data)
235 struct generic_data *data = user_data;
238 if (data->introspect == NULL)
239 generate_introspection_xml(connection, data,
240 dbus_message_get_path(message));
242 reply = dbus_message_new_method_return(message);
246 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
252 static DBusHandlerResult process_message(DBusConnection *connection,
253 DBusMessage *message, const GDBusMethodTable *method,
254 void *iface_user_data)
258 reply = method->function(connection, message, iface_user_data);
260 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
262 dbus_message_unref(reply);
263 return DBUS_HANDLER_RESULT_HANDLED;
266 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
268 return DBUS_HANDLER_RESULT_HANDLED;
272 return DBUS_HANDLER_RESULT_NEED_MEMORY;
274 dbus_connection_send(connection, reply, NULL);
275 dbus_message_unref(reply);
277 return DBUS_HANDLER_RESULT_HANDLED;
280 static GDBusPendingReply next_pending = 1;
281 static GSList *pending_security = NULL;
283 static const GDBusSecurityTable *security_table = NULL;
285 void g_dbus_pending_success(DBusConnection *connection,
286 GDBusPendingReply pending)
290 for (list = pending_security; list; list = list->next) {
291 struct security_data *secdata = list->data;
293 if (secdata->pending != pending)
296 pending_security = g_slist_remove(pending_security, secdata);
298 process_message(connection, secdata->message,
299 secdata->method, secdata->iface_user_data);
301 dbus_message_unref(secdata->message);
307 void g_dbus_pending_error_valist(DBusConnection *connection,
308 GDBusPendingReply pending, const char *name,
309 const char *format, va_list args)
313 for (list = pending_security; list; list = list->next) {
314 struct security_data *secdata = list->data;
317 if (secdata->pending != pending)
320 pending_security = g_slist_remove(pending_security, secdata);
322 reply = g_dbus_create_error_valist(secdata->message,
325 dbus_connection_send(connection, reply, NULL);
326 dbus_message_unref(reply);
329 dbus_message_unref(secdata->message);
335 void g_dbus_pending_error(DBusConnection *connection,
336 GDBusPendingReply pending,
337 const char *name, const char *format, ...)
341 va_start(args, format);
343 g_dbus_pending_error_valist(connection, pending, name, format, args);
348 int polkit_check_authorization(DBusConnection *conn,
349 const char *action, gboolean interaction,
350 void (*function) (dbus_bool_t authorized,
352 void *user_data, int timeout);
354 struct builtin_security_data {
355 DBusConnection *conn;
356 GDBusPendingReply pending;
359 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
361 struct builtin_security_data *data = user_data;
363 if (authorized == TRUE)
364 g_dbus_pending_success(data->conn, data->pending);
366 g_dbus_pending_error(data->conn, data->pending,
367 DBUS_ERROR_AUTH_FAILED, NULL);
372 static void builtin_security_function(DBusConnection *conn,
374 gboolean interaction,
375 GDBusPendingReply pending)
377 struct builtin_security_data *data;
379 data = g_new0(struct builtin_security_data, 1);
381 data->pending = pending;
383 if (polkit_check_authorization(conn, action, interaction,
384 builtin_security_result, data, 30000) < 0)
385 g_dbus_pending_error(conn, pending, NULL, NULL);
388 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
389 const GDBusMethodTable *method, void *iface_user_data)
391 const GDBusSecurityTable *security;
393 for (security = security_table; security && security->privilege;
395 struct security_data *secdata;
396 gboolean interaction;
398 if (security->privilege != method->privilege)
401 secdata = g_new(struct security_data, 1);
402 secdata->pending = next_pending++;
403 secdata->message = dbus_message_ref(msg);
404 secdata->method = method;
405 secdata->iface_user_data = iface_user_data;
407 pending_security = g_slist_prepend(pending_security, secdata);
409 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
414 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
416 security->function(conn, security->action,
417 interaction, secdata->pending);
419 builtin_security_function(conn, security->action,
420 interaction, secdata->pending);
428 static GDBusPendingPropertySet next_pending_property = 1;
429 static GSList *pending_property_set;
431 static struct property_data *remove_pending_property_data(
432 GDBusPendingPropertySet id)
434 struct property_data *propdata;
437 for (l = pending_property_set; l != NULL; l = l->next) {
439 if (propdata->id != id)
448 pending_property_set = g_slist_delete_link(pending_property_set, l);
453 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
455 struct property_data *propdata;
457 propdata = remove_pending_property_data(id);
458 if (propdata == NULL)
461 g_dbus_send_reply(propdata->conn, propdata->message,
463 dbus_message_unref(propdata->message);
467 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
468 const char *name, const char *format,
471 struct property_data *propdata;
474 propdata = remove_pending_property_data(id);
475 if (propdata == NULL)
478 reply = g_dbus_create_error_valist(propdata->message, name, format,
481 dbus_connection_send(propdata->conn, reply, NULL);
482 dbus_message_unref(reply);
485 dbus_message_unref(propdata->message);
489 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
490 const char *format, ...)
494 va_start(args, format);
496 g_dbus_pending_property_error_valist(id, name, format, args);
501 static void reset_parent(gpointer data, gpointer user_data)
503 struct generic_data *child = data;
504 struct generic_data *parent = user_data;
506 child->parent = parent;
509 static void append_property(struct interface_data *iface,
510 const GDBusPropertyTable *p, DBusMessageIter *dict)
512 DBusMessageIter entry, value;
514 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
516 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
517 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
520 p->get(p, &value, iface->user_data);
522 dbus_message_iter_close_container(&entry, &value);
523 dbus_message_iter_close_container(dict, &entry);
526 static void append_properties(struct interface_data *data,
527 DBusMessageIter *iter)
529 DBusMessageIter dict;
530 const GDBusPropertyTable *p;
532 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
533 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
534 DBUS_TYPE_STRING_AS_STRING
535 DBUS_TYPE_VARIANT_AS_STRING
536 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
538 for (p = data->properties; p && p->name; p++) {
539 if (check_experimental(p->flags,
540 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
546 if (p->exists != NULL && !p->exists(p, data->user_data))
549 append_property(data, p, &dict);
552 dbus_message_iter_close_container(iter, &dict);
555 static void append_interface(gpointer data, gpointer user_data)
557 struct interface_data *iface = data;
558 DBusMessageIter *array = user_data;
559 DBusMessageIter entry;
561 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
563 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
564 append_properties(data, &entry);
565 dbus_message_iter_close_container(array, &entry);
568 static void emit_interfaces_added(struct generic_data *data)
571 DBusMessageIter iter, array;
573 if (root == NULL || data == root)
576 signal = dbus_message_new_signal(root->path,
577 DBUS_INTERFACE_OBJECT_MANAGER,
582 dbus_message_iter_init_append(signal, &iter);
583 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
586 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
587 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
588 DBUS_TYPE_STRING_AS_STRING
589 DBUS_TYPE_ARRAY_AS_STRING
590 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
591 DBUS_TYPE_STRING_AS_STRING
592 DBUS_TYPE_VARIANT_AS_STRING
593 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
594 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
596 g_slist_foreach(data->added, append_interface, &array);
597 g_slist_free(data->added);
600 dbus_message_iter_close_container(&iter, &array);
602 g_dbus_send_message(data->conn, signal);
605 static struct interface_data *find_interface(GSList *interfaces,
613 for (list = interfaces; list; list = list->next) {
614 struct interface_data *iface = list->data;
615 if (!strcmp(name, iface->name))
622 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
623 DBusMessage *message)
625 const char *sig = dbus_message_get_signature(message);
626 const char *p = NULL;
628 for (; args && args->signature && *sig; args++) {
631 for (; *sig && *p; sig++, p++) {
637 if (*sig || (p && *p) || (args && args->signature))
643 static gboolean remove_interface(struct generic_data *data, const char *name)
645 struct interface_data *iface;
647 iface = find_interface(data->interfaces, name);
651 process_properties_from_interface(data, iface);
653 data->interfaces = g_slist_remove(data->interfaces, iface);
655 if (iface->destroy) {
656 iface->destroy(iface->user_data);
657 iface->user_data = NULL;
661 * Interface being removed was just added, on the same mainloop
662 * iteration? Don't send any signal
664 if (g_slist_find(data->added, iface)) {
665 data->added = g_slist_remove(data->added, iface);
671 if (data->parent == NULL) {
677 data->removed = g_slist_prepend(data->removed, iface->name);
680 if (data->process_id > 0)
683 data->process_id = g_idle_add(process_changes, data);
688 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
689 const char *child_path)
691 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
692 char *parent_path, *slash;
694 parent_path = g_strdup(child_path);
695 slash = strrchr(parent_path, '/');
699 if (slash == parent_path && parent_path[1] != '\0')
700 parent_path[1] = '\0';
704 if (!strlen(parent_path))
707 if (dbus_connection_get_object_path_data(conn, parent_path,
708 (void *) &data) == FALSE) {
712 parent = invalidate_parent_data(conn, parent_path);
720 g_free(data->introspect);
721 data->introspect = NULL;
723 if (!dbus_connection_get_object_path_data(conn, child_path,
727 if (child == NULL || g_slist_find(data->objects, child) != NULL)
730 data->objects = g_slist_prepend(data->objects, child);
731 child->parent = data;
738 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
741 const GDBusPropertyTable *p;
743 for (p = properties; p && p->name; p++) {
744 if (strcmp(name, p->name) != 0)
747 if (check_experimental(p->flags,
748 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
757 static DBusMessage *properties_get(DBusConnection *connection,
758 DBusMessage *message, void *user_data)
760 struct generic_data *data = user_data;
761 struct interface_data *iface;
762 const GDBusPropertyTable *property;
763 const char *interface, *name;
764 DBusMessageIter iter, value;
767 if (!dbus_message_get_args(message, NULL,
768 DBUS_TYPE_STRING, &interface,
769 DBUS_TYPE_STRING, &name,
773 iface = find_interface(data->interfaces, interface);
775 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
776 "No such interface '%s'", interface);
778 property = find_property(iface->properties, name);
779 if (property == NULL)
780 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
781 "No such property '%s'", name);
783 if (property->exists != NULL &&
784 !property->exists(property, iface->user_data))
785 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
786 "No such property '%s'", name);
788 if (property->get == NULL)
789 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
790 "Property '%s' is not readable", name);
792 reply = dbus_message_new_method_return(message);
796 dbus_message_iter_init_append(reply, &iter);
797 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
798 property->type, &value);
800 if (!property->get(property, &value, iface->user_data)) {
801 dbus_message_unref(reply);
805 dbus_message_iter_close_container(&iter, &value);
810 static DBusMessage *properties_get_all(DBusConnection *connection,
811 DBusMessage *message, void *user_data)
813 struct generic_data *data = user_data;
814 struct interface_data *iface;
815 const char *interface;
816 DBusMessageIter iter;
819 if (!dbus_message_get_args(message, NULL,
820 DBUS_TYPE_STRING, &interface,
824 iface = find_interface(data->interfaces, interface);
826 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
827 "No such interface '%s'", interface);
829 reply = dbus_message_new_method_return(message);
833 dbus_message_iter_init_append(reply, &iter);
835 append_properties(iface, &iter);
840 static DBusMessage *properties_set(DBusConnection *connection,
841 DBusMessage *message, void *user_data)
843 struct generic_data *data = user_data;
844 DBusMessageIter iter, sub;
845 struct interface_data *iface;
846 const GDBusPropertyTable *property;
847 const char *name, *interface;
848 struct property_data *propdata;
849 gboolean valid_signature;
852 if (!dbus_message_iter_init(message, &iter))
853 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
854 "No arguments given");
856 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
857 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
858 "Invalid argument type: '%c'",
859 dbus_message_iter_get_arg_type(&iter));
861 dbus_message_iter_get_basic(&iter, &interface);
862 dbus_message_iter_next(&iter);
864 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
865 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
866 "Invalid argument type: '%c'",
867 dbus_message_iter_get_arg_type(&iter));
869 dbus_message_iter_get_basic(&iter, &name);
870 dbus_message_iter_next(&iter);
872 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
873 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
874 "Invalid argument type: '%c'",
875 dbus_message_iter_get_arg_type(&iter));
877 dbus_message_iter_recurse(&iter, &sub);
879 iface = find_interface(data->interfaces, interface);
881 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
882 "No such interface '%s'", interface);
884 property = find_property(iface->properties, name);
885 if (property == NULL)
886 return g_dbus_create_error(message,
887 DBUS_ERROR_UNKNOWN_PROPERTY,
888 "No such property '%s'", name);
890 if (property->set == NULL)
891 return g_dbus_create_error(message,
892 DBUS_ERROR_PROPERTY_READ_ONLY,
893 "Property '%s' is not writable", name);
895 if (property->exists != NULL &&
896 !property->exists(property, iface->user_data))
897 return g_dbus_create_error(message,
898 DBUS_ERROR_UNKNOWN_PROPERTY,
899 "No such property '%s'", name);
901 signature = dbus_message_iter_get_signature(&sub);
902 valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
903 dbus_free(signature);
904 if (!valid_signature)
905 return g_dbus_create_error(message,
906 DBUS_ERROR_INVALID_SIGNATURE,
907 "Invalid signature for '%s'", name);
909 propdata = g_new(struct property_data, 1);
910 propdata->id = next_pending_property++;
911 propdata->message = dbus_message_ref(message);
912 propdata->conn = connection;
913 pending_property_set = g_slist_prepend(pending_property_set, propdata);
915 property->set(property, &sub, propdata->id, iface->user_data);
920 static const GDBusMethodTable properties_methods[] = {
921 { GDBUS_METHOD("Get",
922 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
923 GDBUS_ARGS({ "value", "v" }),
925 { GDBUS_ASYNC_METHOD("Set",
926 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
930 { GDBUS_METHOD("GetAll",
931 GDBUS_ARGS({ "interface", "s" }),
932 GDBUS_ARGS({ "properties", "a{sv}" }),
933 properties_get_all) },
937 static const GDBusSignalTable properties_signals[] = {
938 { GDBUS_SIGNAL("PropertiesChanged",
939 GDBUS_ARGS({ "interface", "s" },
940 { "changed_properties", "a{sv}" },
941 { "invalidated_properties", "as"})) },
945 static void append_name(gpointer data, gpointer user_data)
948 DBusMessageIter *iter = user_data;
950 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
953 static void emit_interfaces_removed(struct generic_data *data)
956 DBusMessageIter iter, array;
958 if (root == NULL || data == root)
961 signal = dbus_message_new_signal(root->path,
962 DBUS_INTERFACE_OBJECT_MANAGER,
963 "InterfacesRemoved");
967 dbus_message_iter_init_append(signal, &iter);
968 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
970 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
971 DBUS_TYPE_STRING_AS_STRING, &array);
973 g_slist_foreach(data->removed, append_name, &array);
974 g_slist_free_full(data->removed, g_free);
975 data->removed = NULL;
977 dbus_message_iter_close_container(&iter, &array);
979 g_dbus_send_message(data->conn, signal);
982 static gboolean process_changes(gpointer user_data)
984 struct generic_data *data = user_data;
986 data->process_id = 0;
988 if (data->added != NULL)
989 emit_interfaces_added(data);
991 /* Flush pending properties */
992 if (data->pending_prop == TRUE)
993 process_property_changes(data);
995 if (data->removed != NULL)
996 emit_interfaces_removed(data);
1001 static void generic_unregister(DBusConnection *connection, void *user_data)
1003 struct generic_data *data = user_data;
1004 struct generic_data *parent = data->parent;
1007 parent->objects = g_slist_remove(parent->objects, data);
1009 if (data->process_id > 0) {
1010 g_source_remove(data->process_id);
1011 process_changes(data);
1014 g_slist_foreach(data->objects, reset_parent, data->parent);
1015 g_slist_free(data->objects);
1017 dbus_connection_unref(data->conn);
1018 g_free(data->introspect);
1023 static DBusHandlerResult generic_message(DBusConnection *connection,
1024 DBusMessage *message, void *user_data)
1026 struct generic_data *data = user_data;
1027 struct interface_data *iface;
1028 const GDBusMethodTable *method;
1029 const char *interface;
1031 interface = dbus_message_get_interface(message);
1033 iface = find_interface(data->interfaces, interface);
1035 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1037 for (method = iface->methods; method &&
1038 method->name && method->function; method++) {
1040 if (dbus_message_is_method_call(message, iface->name,
1041 method->name) == FALSE)
1044 if (check_experimental(method->flags,
1045 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1046 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1048 if (g_dbus_args_have_signature(method->in_args,
1052 if (check_privilege(connection, message, method,
1053 iface->user_data) == TRUE)
1054 return DBUS_HANDLER_RESULT_HANDLED;
1056 return process_message(connection, message, method,
1060 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1063 static DBusObjectPathVTable generic_table = {
1064 .unregister_function = generic_unregister,
1065 .message_function = generic_message,
1068 static const GDBusMethodTable introspect_methods[] = {
1069 { GDBUS_METHOD("Introspect", NULL,
1070 GDBUS_ARGS({ "xml", "s" }), introspect) },
1074 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1076 DBusMessageIter array;
1079 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1080 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1081 DBUS_TYPE_STRING_AS_STRING
1082 DBUS_TYPE_ARRAY_AS_STRING
1083 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1084 DBUS_TYPE_STRING_AS_STRING
1085 DBUS_TYPE_VARIANT_AS_STRING
1086 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1087 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1089 for (l = data->interfaces; l != NULL; l = l->next) {
1090 if (g_slist_find(data->added, l->data))
1093 append_interface(l->data, &array);
1096 dbus_message_iter_close_container(iter, &array);
1099 static void append_object(gpointer data, gpointer user_data)
1101 struct generic_data *child = data;
1102 DBusMessageIter *array = user_data;
1103 DBusMessageIter entry;
1105 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1107 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1109 append_interfaces(child, &entry);
1110 dbus_message_iter_close_container(array, &entry);
1112 g_slist_foreach(child->objects, append_object, user_data);
1115 static DBusMessage *get_objects(DBusConnection *connection,
1116 DBusMessage *message, void *user_data)
1118 struct generic_data *data = user_data;
1120 DBusMessageIter iter;
1121 DBusMessageIter array;
1123 reply = dbus_message_new_method_return(message);
1127 dbus_message_iter_init_append(reply, &iter);
1129 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1130 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1131 DBUS_TYPE_OBJECT_PATH_AS_STRING
1132 DBUS_TYPE_ARRAY_AS_STRING
1133 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1134 DBUS_TYPE_STRING_AS_STRING
1135 DBUS_TYPE_ARRAY_AS_STRING
1136 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1137 DBUS_TYPE_STRING_AS_STRING
1138 DBUS_TYPE_VARIANT_AS_STRING
1139 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1140 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1141 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1144 g_slist_foreach(data->objects, append_object, &array);
1146 dbus_message_iter_close_container(&iter, &array);
1151 static const GDBusMethodTable manager_methods[] = {
1152 { GDBUS_METHOD("GetManagedObjects", NULL,
1153 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1157 static const GDBusSignalTable manager_signals[] = {
1158 { GDBUS_SIGNAL("InterfacesAdded",
1159 GDBUS_ARGS({ "object", "o" },
1160 { "interfaces", "a{sa{sv}}" })) },
1161 { GDBUS_SIGNAL("InterfacesRemoved",
1162 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1166 static gboolean add_interface(struct generic_data *data,
1168 const GDBusMethodTable *methods,
1169 const GDBusSignalTable *signals,
1170 const GDBusPropertyTable *properties,
1172 GDBusDestroyFunction destroy)
1174 struct interface_data *iface;
1175 const GDBusMethodTable *method;
1176 const GDBusSignalTable *signal;
1177 const GDBusPropertyTable *property;
1179 for (method = methods; method && method->name; method++) {
1180 if (!check_experimental(method->flags,
1181 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1185 for (signal = signals; signal && signal->name; signal++) {
1186 if (!check_experimental(signal->flags,
1187 G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1191 for (property = properties; property && property->name; property++) {
1192 if (!check_experimental(property->flags,
1193 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1197 /* Nothing to register */
1201 iface = g_new0(struct interface_data, 1);
1202 iface->name = g_strdup(name);
1203 iface->methods = methods;
1204 iface->signals = signals;
1205 iface->properties = properties;
1206 iface->user_data = user_data;
1207 iface->destroy = destroy;
1209 data->interfaces = g_slist_append(data->interfaces, iface);
1210 if (data->parent == NULL)
1213 data->added = g_slist_append(data->added, iface);
1214 if (data->process_id > 0)
1217 data->process_id = g_idle_add(process_changes, data);
1222 static struct generic_data *object_path_ref(DBusConnection *connection,
1225 struct generic_data *data;
1227 if (dbus_connection_get_object_path_data(connection, path,
1228 (void *) &data) == TRUE) {
1235 data = g_new0(struct generic_data, 1);
1236 data->conn = dbus_connection_ref(connection);
1237 data->path = g_strdup(path);
1240 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1242 if (!dbus_connection_register_object_path(connection, path,
1243 &generic_table, data)) {
1244 g_free(data->introspect);
1249 invalidate_parent_data(connection, path);
1251 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1252 NULL, NULL, data, NULL);
1257 static void object_path_unref(DBusConnection *connection, const char *path)
1259 struct generic_data *data = NULL;
1261 if (dbus_connection_get_object_path_data(connection, path,
1262 (void *) &data) == FALSE)
1270 if (data->refcount > 0)
1273 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1274 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1276 invalidate_parent_data(data->conn, data->path);
1278 dbus_connection_unregister_object_path(data->conn, data->path);
1281 static gboolean check_signal(DBusConnection *conn, const char *path,
1282 const char *interface, const char *name,
1283 const GDBusArgInfo **args)
1285 struct generic_data *data = NULL;
1286 struct interface_data *iface;
1287 const GDBusSignalTable *signal;
1290 if (!dbus_connection_get_object_path_data(conn, path,
1291 (void *) &data) || data == NULL) {
1292 error("dbus_connection_emit_signal: path %s isn't registered",
1297 iface = find_interface(data->interfaces, interface);
1298 if (iface == NULL) {
1299 error("dbus_connection_emit_signal: %s does not implement %s",
1304 for (signal = iface->signals; signal && signal->name; signal++) {
1305 if (strcmp(signal->name, name) != 0)
1308 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1309 const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1310 if (g_strcmp0(env, "1") != 0)
1314 *args = signal->args;
1318 error("No signal named %s on interface %s", name, interface);
1322 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1324 const char *interface,
1329 DBusMessage *signal;
1331 const GDBusArgInfo *args;
1333 if (!check_signal(conn, path, interface, name, &args))
1336 signal = dbus_message_new_signal(path, interface, name);
1337 if (signal == NULL) {
1338 error("Unable to allocate new %s.%s signal", interface, name);
1342 ret = dbus_message_append_args_valist(signal, first, var_args);
1346 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1347 error("%s.%s: got unexpected signature '%s'", interface, name,
1348 dbus_message_get_signature(signal));
1353 ret = dbus_connection_send(conn, signal, NULL);
1356 dbus_message_unref(signal);
1361 gboolean g_dbus_register_interface(DBusConnection *connection,
1362 const char *path, const char *name,
1363 const GDBusMethodTable *methods,
1364 const GDBusSignalTable *signals,
1365 const GDBusPropertyTable *properties,
1367 GDBusDestroyFunction destroy)
1369 struct generic_data *data;
1371 data = object_path_ref(connection, path);
1375 if (find_interface(data->interfaces, name)) {
1376 object_path_unref(connection, path);
1380 if (!add_interface(data, name, methods, signals, properties, user_data,
1382 object_path_unref(connection, path);
1386 if (properties != NULL && !find_interface(data->interfaces,
1387 DBUS_INTERFACE_PROPERTIES))
1388 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1389 properties_methods, properties_signals, NULL,
1392 g_free(data->introspect);
1393 data->introspect = NULL;
1398 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1399 const char *path, const char *name)
1401 struct generic_data *data = NULL;
1406 if (dbus_connection_get_object_path_data(connection, path,
1407 (void *) &data) == FALSE)
1413 if (remove_interface(data, name) == FALSE)
1416 g_free(data->introspect);
1417 data->introspect = NULL;
1419 object_path_unref(connection, data->path);
1424 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1426 if (security_table != NULL)
1429 security_table = security;
1434 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1436 security_table = NULL;
1441 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1442 const char *format, va_list args)
1446 vsnprintf(str, sizeof(str), format, args);
1448 return dbus_message_new_error(message, name, str);
1451 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1452 const char *format, ...)
1457 va_start(args, format);
1459 reply = g_dbus_create_error_valist(message, name, format, args);
1466 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1467 int type, va_list args)
1471 reply = dbus_message_new_method_return(message);
1475 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1476 dbus_message_unref(reply);
1483 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1488 va_start(args, type);
1490 reply = g_dbus_create_reply_valist(message, type, args);
1497 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1499 dbus_bool_t result = FALSE;
1501 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1502 dbus_message_set_no_reply(message, TRUE);
1503 else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1504 const char *path = dbus_message_get_path(message);
1505 const char *interface = dbus_message_get_interface(message);
1506 const char *name = dbus_message_get_member(message);
1507 const GDBusArgInfo *args;
1509 if (!check_signal(connection, path, interface, name, &args))
1513 result = dbus_connection_send(connection, message, NULL);
1516 dbus_message_unref(message);
1521 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1522 DBusMessage *message, const char *name,
1523 const char *format, va_list args)
1528 vsnprintf(str, sizeof(str), format, args);
1530 error = dbus_message_new_error(message, name, str);
1534 return g_dbus_send_message(connection, error);
1537 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1538 const char *name, const char *format, ...)
1543 va_start(args, format);
1545 result = g_dbus_send_error_valist(connection, message, name,
1553 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1554 DBusMessage *message, int type, va_list args)
1558 reply = dbus_message_new_method_return(message);
1562 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1563 dbus_message_unref(reply);
1567 return g_dbus_send_message(connection, reply);
1570 gboolean g_dbus_send_reply(DBusConnection *connection,
1571 DBusMessage *message, int type, ...)
1576 va_start(args, type);
1578 result = g_dbus_send_reply_valist(connection, message, type, args);
1585 gboolean g_dbus_emit_signal(DBusConnection *connection,
1586 const char *path, const char *interface,
1587 const char *name, int type, ...)
1592 va_start(args, type);
1594 result = emit_signal_valist(connection, path, interface,
1602 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1603 const char *path, const char *interface,
1604 const char *name, int type, va_list args)
1606 return emit_signal_valist(connection, path, interface,
1610 static void process_properties_from_interface(struct generic_data *data,
1611 struct interface_data *iface)
1614 DBusMessage *signal;
1615 DBusMessageIter iter, dict, array;
1616 GSList *invalidated;
1618 if (iface->pending_prop == NULL)
1621 signal = dbus_message_new_signal(data->path,
1622 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1623 if (signal == NULL) {
1624 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1625 ".PropertiesChanged signal");
1629 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1631 dbus_message_iter_init_append(signal, &iter);
1632 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1633 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1634 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1635 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1636 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1640 for (l = iface->pending_prop; l != NULL; l = l->next) {
1641 GDBusPropertyTable *p = l->data;
1646 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1647 invalidated = g_slist_prepend(invalidated, p);
1651 append_property(iface, p, &dict);
1654 dbus_message_iter_close_container(&iter, &dict);
1656 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1657 DBUS_TYPE_STRING_AS_STRING, &array);
1658 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1659 GDBusPropertyTable *p = l->data;
1661 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1664 g_slist_free(invalidated);
1665 dbus_message_iter_close_container(&iter, &array);
1667 g_dbus_send_message(data->conn, signal);
1669 g_slist_free(iface->pending_prop);
1670 iface->pending_prop = NULL;
1673 static void process_property_changes(struct generic_data *data)
1677 for (l = data->interfaces; l != NULL; l = l->next) {
1678 struct interface_data *iface = l->data;
1680 process_properties_from_interface(data, iface);
1683 data->pending_prop = FALSE;
1686 void g_dbus_emit_property_changed(DBusConnection *connection,
1687 const char *path, const char *interface,
1690 const GDBusPropertyTable *property;
1691 struct generic_data *data;
1692 struct interface_data *iface;
1697 if (!dbus_connection_get_object_path_data(connection, path,
1698 (void **) &data) || data == NULL)
1701 iface = find_interface(data->interfaces, interface);
1706 * If ObjectManager is attached, don't emit property changed if
1707 * interface is not yet published
1709 if (root && g_slist_find(data->added, iface))
1712 property = find_property(iface->properties, name);
1713 if (property == NULL) {
1714 error("Could not find property %s in %p", name,
1719 if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1722 data->pending_prop = TRUE;
1723 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1726 if (!data->process_id) {
1727 data->process_id = g_idle_add(process_changes, data);
1732 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1733 const char *interface, DBusMessageIter *iter)
1735 struct generic_data *data;
1736 struct interface_data *iface;
1741 if (!dbus_connection_get_object_path_data(connection, path,
1742 (void **) &data) || data == NULL)
1745 iface = find_interface(data->interfaces, interface);
1749 append_properties(iface, iter);
1754 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1756 struct generic_data *data;
1758 data = object_path_ref(connection, "/");
1762 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1763 manager_methods, manager_signals,
1770 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1772 if (!g_dbus_unregister_interface(connection, "/",
1773 DBUS_INTERFACE_OBJECT_MANAGER))
1781 void g_dbus_set_flags(int flags)
1783 global_flags = flags;