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"
43 unsigned int refcount;
51 gboolean pending_prop;
53 struct generic_data *parent;
56 struct interface_data {
58 const GDBusMethodTable *methods;
59 const GDBusSignalTable *signals;
60 const GDBusPropertyTable *properties;
63 GDBusDestroyFunction destroy;
66 struct security_data {
67 GDBusPendingReply pending;
69 const GDBusMethodTable *method;
70 void *iface_user_data;
73 struct property_data {
74 GDBusPendingPropertySet id;
78 static gboolean process_changes(gpointer user_data);
79 static void process_properties_from_interface(struct generic_data *data,
80 struct interface_data *iface);
81 static void process_property_changes(struct generic_data *data);
83 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
84 const char *direction)
86 for (; args && args->name; args++) {
87 g_string_append_printf(gstr,
88 "\t\t\t<arg name=\"%s\" type=\"%s\"",
89 args->name, args->signature);
92 g_string_append_printf(gstr,
93 " direction=\"%s\"/>\n", direction);
95 g_string_append_printf(gstr, "/>\n");
100 #define G_DBUS_ANNOTATE(prefix_, name_, value_) \
101 prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
102 "value=\"" value_ "\"/>\n"
104 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
105 G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
107 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
108 G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
110 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
112 const GDBusMethodTable *method;
113 const GDBusSignalTable *signal;
114 const GDBusPropertyTable *property;
116 for (method = iface->methods; method && method->name; method++) {
117 gboolean deprecated = method->flags &
118 G_DBUS_METHOD_FLAG_DEPRECATED;
119 gboolean noreply = method->flags &
120 G_DBUS_METHOD_FLAG_NOREPLY;
122 if (!deprecated && !noreply &&
123 !(method->in_args && method->in_args->name) &&
124 !(method->out_args && method->out_args->name))
125 g_string_append_printf(gstr,
126 "\t\t<method name=\"%s\"/>\n",
129 g_string_append_printf(gstr,
130 "\t\t<method name=\"%s\">\n",
132 print_arguments(gstr, method->in_args, "in");
133 print_arguments(gstr, method->out_args, "out");
136 g_string_append_printf(gstr,
137 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
139 g_string_append_printf(gstr,
140 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
142 g_string_append_printf(gstr, "\t\t</method>\n");
146 for (signal = iface->signals; signal && signal->name; signal++) {
147 gboolean deprecated = signal->flags &
148 G_DBUS_SIGNAL_FLAG_DEPRECATED;
150 if (!deprecated && !(signal->args && signal->args->name))
151 g_string_append_printf(gstr,
152 "\t\t<signal name=\"%s\"/>\n",
155 g_string_append_printf(gstr,
156 "\t\t<signal name=\"%s\">\n",
158 print_arguments(gstr, signal->args, NULL);
161 g_string_append_printf(gstr,
162 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
164 g_string_append_printf(gstr, "\t\t</signal>\n");
168 for (property = iface->properties; property && property->name;
170 gboolean deprecated = property->flags &
171 G_DBUS_PROPERTY_FLAG_DEPRECATED;
173 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
174 " type=\"%s\" access=\"%s%s\"",
175 property->name, property->type,
176 property->get ? "read" : "",
177 property->set ? "write" : "");
180 g_string_append_printf(gstr, "/>\n");
182 g_string_append_printf(gstr,
183 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
187 static void generate_introspection_xml(DBusConnection *conn,
188 struct generic_data *data, const char *path)
195 g_free(data->introspect);
197 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
199 g_string_append_printf(gstr, "<node>\n");
201 for (list = data->interfaces; list; list = list->next) {
202 struct interface_data *iface = list->data;
204 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
207 generate_interface_xml(gstr, iface);
209 g_string_append_printf(gstr, "\t</interface>\n");
212 if (!dbus_connection_list_registered(conn, path, &children))
215 for (i = 0; children[i]; i++)
216 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
219 dbus_free_string_array(children);
222 g_string_append_printf(gstr, "</node>\n");
224 data->introspect = g_string_free(gstr, FALSE);
227 static DBusMessage *introspect(DBusConnection *connection,
228 DBusMessage *message, void *user_data)
230 struct generic_data *data = user_data;
233 if (data->introspect == NULL)
234 generate_introspection_xml(connection, data,
235 dbus_message_get_path(message));
237 reply = dbus_message_new_method_return(message);
241 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
247 static DBusHandlerResult process_message(DBusConnection *connection,
248 DBusMessage *message, const GDBusMethodTable *method,
249 void *iface_user_data)
253 reply = method->function(connection, message, iface_user_data);
255 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
257 dbus_message_unref(reply);
258 return DBUS_HANDLER_RESULT_HANDLED;
261 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
263 return DBUS_HANDLER_RESULT_HANDLED;
267 return DBUS_HANDLER_RESULT_NEED_MEMORY;
269 dbus_connection_send(connection, reply, NULL);
270 dbus_message_unref(reply);
272 return DBUS_HANDLER_RESULT_HANDLED;
275 static GDBusPendingReply next_pending = 1;
276 static GSList *pending_security = NULL;
278 static const GDBusSecurityTable *security_table = NULL;
280 void g_dbus_pending_success(DBusConnection *connection,
281 GDBusPendingReply pending)
285 for (list = pending_security; list; list = list->next) {
286 struct security_data *secdata = list->data;
288 if (secdata->pending != pending)
291 pending_security = g_slist_remove(pending_security, secdata);
293 process_message(connection, secdata->message,
294 secdata->method, secdata->iface_user_data);
296 dbus_message_unref(secdata->message);
302 void g_dbus_pending_error_valist(DBusConnection *connection,
303 GDBusPendingReply pending, const char *name,
304 const char *format, va_list args)
308 for (list = pending_security; list; list = list->next) {
309 struct security_data *secdata = list->data;
312 if (secdata->pending != pending)
315 pending_security = g_slist_remove(pending_security, secdata);
317 reply = g_dbus_create_error_valist(secdata->message,
320 dbus_connection_send(connection, reply, NULL);
321 dbus_message_unref(reply);
324 dbus_message_unref(secdata->message);
330 void g_dbus_pending_error(DBusConnection *connection,
331 GDBusPendingReply pending,
332 const char *name, const char *format, ...)
336 va_start(args, format);
338 g_dbus_pending_error_valist(connection, pending, name, format, args);
343 int polkit_check_authorization(DBusConnection *conn,
344 const char *action, gboolean interaction,
345 void (*function) (dbus_bool_t authorized,
347 void *user_data, int timeout);
349 struct builtin_security_data {
350 DBusConnection *conn;
351 GDBusPendingReply pending;
354 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
356 struct builtin_security_data *data = user_data;
358 if (authorized == TRUE)
359 g_dbus_pending_success(data->conn, data->pending);
361 g_dbus_pending_error(data->conn, data->pending,
362 DBUS_ERROR_AUTH_FAILED, NULL);
367 static void builtin_security_function(DBusConnection *conn,
369 gboolean interaction,
370 GDBusPendingReply pending)
372 struct builtin_security_data *data;
374 data = g_new0(struct builtin_security_data, 1);
376 data->pending = pending;
378 if (polkit_check_authorization(conn, action, interaction,
379 builtin_security_result, data, 30000) < 0)
380 g_dbus_pending_error(conn, pending, NULL, NULL);
383 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
384 const GDBusMethodTable *method, void *iface_user_data)
386 const GDBusSecurityTable *security;
388 for (security = security_table; security && security->privilege;
390 struct security_data *secdata;
391 gboolean interaction;
393 if (security->privilege != method->privilege)
396 secdata = g_new(struct security_data, 1);
397 secdata->pending = next_pending++;
398 secdata->message = dbus_message_ref(msg);
399 secdata->method = method;
400 secdata->iface_user_data = iface_user_data;
402 pending_security = g_slist_prepend(pending_security, secdata);
404 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
409 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
411 security->function(conn, security->action,
412 interaction, secdata->pending);
414 builtin_security_function(conn, security->action,
415 interaction, secdata->pending);
423 static GDBusPendingPropertySet next_pending_property = 1;
424 static GSList *pending_property_set;
426 static struct property_data *remove_pending_property_data(
427 GDBusPendingPropertySet id)
429 struct property_data *propdata;
432 for (l = pending_property_set; l != NULL; l = l->next) {
434 if (propdata->id != id)
443 pending_property_set = g_slist_delete_link(pending_property_set, l);
448 void g_dbus_pending_property_success(DBusConnection *connection,
449 GDBusPendingPropertySet id)
451 struct property_data *propdata;
453 propdata = remove_pending_property_data(id);
454 if (propdata == NULL)
457 g_dbus_send_reply(connection, propdata->message, DBUS_TYPE_INVALID);
458 dbus_message_unref(propdata->message);
462 void g_dbus_pending_property_error_valist(DBusConnection *connection,
463 GDBusPendingReply id, const char *name,
464 const char *format, va_list args)
466 struct property_data *propdata;
469 propdata = remove_pending_property_data(id);
470 if (propdata == NULL)
473 reply = g_dbus_create_error_valist(propdata->message, name, format,
476 dbus_connection_send(connection, reply, NULL);
477 dbus_message_unref(reply);
480 dbus_message_unref(propdata->message);
484 void g_dbus_pending_property_error(DBusConnection *connection,
485 GDBusPendingReply id, const char *name,
486 const char *format, ...)
490 va_start(args, format);
492 g_dbus_pending_property_error_valist(connection, id, name, format,
498 static void reset_parent(gpointer data, gpointer user_data)
500 struct generic_data *child = data;
501 struct generic_data *parent = user_data;
503 child->parent = parent;
506 static void append_property(struct interface_data *iface,
507 const GDBusPropertyTable *p, DBusMessageIter *dict)
509 DBusMessageIter entry, value;
511 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
513 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
514 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
517 p->get(p, &value, iface->user_data);
519 dbus_message_iter_close_container(&entry, &value);
520 dbus_message_iter_close_container(dict, &entry);
523 static void append_properties(struct interface_data *data,
524 DBusMessageIter *iter)
526 DBusMessageIter dict;
527 const GDBusPropertyTable *p;
529 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
530 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
531 DBUS_TYPE_STRING_AS_STRING
532 DBUS_TYPE_VARIANT_AS_STRING
533 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
535 for (p = data->properties; p && p->name; p++) {
539 if (p->exists != NULL && !p->exists(p, data->user_data))
542 append_property(data, p, &dict);
545 dbus_message_iter_close_container(iter, &dict);
548 static void append_interface(gpointer data, gpointer user_data)
550 struct interface_data *iface = data;
551 DBusMessageIter *array = user_data;
552 DBusMessageIter entry;
554 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
556 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
557 append_properties(data, &entry);
558 dbus_message_iter_close_container(array, &entry);
561 static void emit_interfaces_added(struct generic_data *data)
564 DBusMessageIter iter, array;
565 struct generic_data *parent = data->parent;
571 while (parent->parent)
572 parent = parent->parent;
574 signal = dbus_message_new_signal(parent->path,
575 DBUS_INTERFACE_OBJECT_MANAGER,
580 dbus_message_iter_init_append(signal, &iter);
581 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
584 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
585 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
586 DBUS_TYPE_STRING_AS_STRING
587 DBUS_TYPE_ARRAY_AS_STRING
588 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
589 DBUS_TYPE_STRING_AS_STRING
590 DBUS_TYPE_VARIANT_AS_STRING
591 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
592 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
594 g_slist_foreach(data->added, append_interface, &array);
595 g_slist_free(data->added);
598 dbus_message_iter_close_container(&iter, &array);
600 g_dbus_send_message(data->conn, signal);
603 static struct interface_data *find_interface(GSList *interfaces,
611 for (list = interfaces; list; list = list->next) {
612 struct interface_data *iface = list->data;
613 if (!strcmp(name, iface->name))
620 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
621 DBusMessage *message)
623 const char *sig = dbus_message_get_signature(message);
624 const char *p = NULL;
626 for (; args && args->signature && *sig; args++) {
629 for (; *sig && *p; sig++, p++) {
635 if (*sig || (p && *p) || (args && args->signature))
641 static gboolean remove_interface(struct generic_data *data, const char *name)
643 struct interface_data *iface;
645 iface = find_interface(data->interfaces, name);
649 data->interfaces = g_slist_remove(data->interfaces, iface);
651 if (iface->destroy) {
652 iface->destroy(iface->user_data);
653 iface->user_data = NULL;
656 if (data->parent == NULL) {
663 * Interface being removed was just added, on the same mainloop
664 * iteration? Don't send any signal
666 if (g_slist_find(data->added, iface)) {
667 data->added = g_slist_remove(data->added, iface);
673 data->removed = g_slist_prepend(data->removed, iface->name);
676 if (data->process_id > 0)
679 data->process_id = g_idle_add(process_changes, data);
684 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
685 const char *child_path)
687 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
688 char *parent_path, *slash;
690 parent_path = g_strdup(child_path);
691 slash = strrchr(parent_path, '/');
695 if (slash == parent_path && parent_path[1] != '\0')
696 parent_path[1] = '\0';
700 if (!strlen(parent_path))
703 if (dbus_connection_get_object_path_data(conn, parent_path,
704 (void *) &data) == FALSE) {
708 parent = invalidate_parent_data(conn, parent_path);
716 g_free(data->introspect);
717 data->introspect = NULL;
719 if (!dbus_connection_get_object_path_data(conn, child_path,
723 if (child == NULL || g_slist_find(data->objects, child) != NULL)
726 data->objects = g_slist_prepend(data->objects, child);
727 child->parent = data;
734 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
737 const GDBusPropertyTable *p;
739 for (p = properties; p && p->name; p++) {
740 if (strcmp(name, p->name) == 0)
747 static DBusMessage *properties_get(DBusConnection *connection,
748 DBusMessage *message, void *user_data)
750 struct generic_data *data = user_data;
751 struct interface_data *iface;
752 const GDBusPropertyTable *property;
753 const char *interface, *name;
754 DBusMessageIter iter, value;
757 if (!dbus_message_get_args(message, NULL,
758 DBUS_TYPE_STRING, &interface,
759 DBUS_TYPE_STRING, &name,
763 iface = find_interface(data->interfaces, interface);
765 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
766 "No such interface '%s'", interface);
768 property = find_property(iface->properties, name);
769 if (property == NULL)
770 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
771 "No such property '%s'", name);
773 if (property->exists != NULL &&
774 !property->exists(property, iface->user_data))
775 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
776 "No such property '%s'", name);
778 if (property->get == NULL)
779 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
780 "Property '%s' is not readable", name);
782 reply = dbus_message_new_method_return(message);
786 dbus_message_iter_init_append(reply, &iter);
787 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
788 property->type, &value);
790 if (!property->get(property, &value, iface->user_data)) {
791 dbus_message_unref(reply);
795 dbus_message_iter_close_container(&iter, &value);
800 static DBusMessage *properties_get_all(DBusConnection *connection,
801 DBusMessage *message, void *user_data)
803 struct generic_data *data = user_data;
804 struct interface_data *iface;
805 const char *interface;
806 DBusMessageIter iter;
809 if (!dbus_message_get_args(message, NULL,
810 DBUS_TYPE_STRING, &interface,
814 iface = find_interface(data->interfaces, interface);
816 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
817 "No such interface '%s'", interface);
819 reply = dbus_message_new_method_return(message);
823 dbus_message_iter_init_append(reply, &iter);
825 append_properties(iface, &iter);
830 static DBusMessage *properties_set(DBusConnection *connection,
831 DBusMessage *message, void *user_data)
833 struct generic_data *data = user_data;
834 DBusMessageIter iter, sub;
835 struct interface_data *iface;
836 const GDBusPropertyTable *property;
837 const char *name, *interface;
838 struct property_data *propdata;
840 if (!dbus_message_iter_init(message, &iter))
841 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
842 "No arguments given");
844 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
845 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
846 "Invalid argument type: '%c'",
847 dbus_message_iter_get_arg_type(&iter));
849 dbus_message_iter_get_basic(&iter, &interface);
850 dbus_message_iter_next(&iter);
852 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
853 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
854 "Invalid argument type: '%c'",
855 dbus_message_iter_get_arg_type(&iter));
857 dbus_message_iter_get_basic(&iter, &name);
858 dbus_message_iter_next(&iter);
860 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
861 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
862 "Invalid argument type: '%c'",
863 dbus_message_iter_get_arg_type(&iter));
865 dbus_message_iter_recurse(&iter, &sub);
867 iface = find_interface(data->interfaces, interface);
869 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
870 "No such interface '%s'", interface);
872 property = find_property(iface->properties, name);
873 if (property == NULL)
874 return g_dbus_create_error(message,
875 DBUS_ERROR_UNKNOWN_PROPERTY,
876 "No such property '%s'", name);
878 if (property->set == NULL)
879 return g_dbus_create_error(message,
880 DBUS_ERROR_PROPERTY_READ_ONLY,
881 "Property '%s' is not writable", name);
883 if (property->exists != NULL &&
884 !property->exists(property, iface->user_data))
885 return g_dbus_create_error(message,
886 DBUS_ERROR_UNKNOWN_PROPERTY,
887 "No such property '%s'", name);
889 propdata = g_new(struct property_data, 1);
890 propdata->id = next_pending_property++;
891 propdata->message = dbus_message_ref(message);
892 pending_property_set = g_slist_prepend(pending_property_set, propdata);
894 property->set(property, &sub, propdata->id, iface->user_data);
899 static const GDBusMethodTable properties_methods[] = {
900 { GDBUS_METHOD("Get",
901 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
902 GDBUS_ARGS({ "value", "v" }),
904 { GDBUS_ASYNC_METHOD("Set",
905 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
909 { GDBUS_METHOD("GetAll",
910 GDBUS_ARGS({ "interface", "s" }),
911 GDBUS_ARGS({ "properties", "a{sv}" }),
912 properties_get_all) },
916 static const GDBusSignalTable properties_signals[] = {
917 { GDBUS_SIGNAL("PropertiesChanged",
918 GDBUS_ARGS({ "interface", "s" },
919 { "changed_properties", "a{sv}" },
920 { "invalidated_properties", "as"})) },
924 static void append_name(gpointer data, gpointer user_data)
927 DBusMessageIter *iter = user_data;
929 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
932 static void emit_interfaces_removed(struct generic_data *data)
935 DBusMessageIter iter, array;
936 struct generic_data *parent = data->parent;
942 while (parent->parent)
943 parent = parent->parent;
945 signal = dbus_message_new_signal(parent->path,
946 DBUS_INTERFACE_OBJECT_MANAGER,
947 "InterfacesRemoved");
951 dbus_message_iter_init_append(signal, &iter);
952 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
954 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
955 DBUS_TYPE_STRING_AS_STRING, &array);
957 g_slist_foreach(data->removed, append_name, &array);
958 g_slist_free_full(data->removed, g_free);
959 data->removed = NULL;
961 dbus_message_iter_close_container(&iter, &array);
963 g_dbus_send_message(data->conn, signal);
966 static gboolean process_changes(gpointer user_data)
968 struct generic_data *data = user_data;
970 data->process_id = 0;
972 if (data->added != NULL)
973 emit_interfaces_added(data);
975 /* Flush pending properties */
976 if (data->pending_prop == TRUE)
977 process_property_changes(data);
979 if (data->removed != NULL)
980 emit_interfaces_removed(data);
985 static void generic_unregister(DBusConnection *connection, void *user_data)
987 struct generic_data *data = user_data;
988 struct generic_data *parent = data->parent;
991 parent->objects = g_slist_remove(parent->objects, data);
993 if (data->process_id > 0) {
994 g_source_remove(data->process_id);
995 process_changes(data);
998 g_slist_foreach(data->objects, reset_parent, data->parent);
999 g_slist_free(data->objects);
1001 dbus_connection_unref(data->conn);
1002 g_free(data->introspect);
1007 static DBusHandlerResult generic_message(DBusConnection *connection,
1008 DBusMessage *message, void *user_data)
1010 struct generic_data *data = user_data;
1011 struct interface_data *iface;
1012 const GDBusMethodTable *method;
1013 const char *interface;
1015 interface = dbus_message_get_interface(message);
1017 iface = find_interface(data->interfaces, interface);
1019 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1021 for (method = iface->methods; method &&
1022 method->name && method->function; method++) {
1023 if (dbus_message_is_method_call(message, iface->name,
1024 method->name) == FALSE)
1027 if (g_dbus_args_have_signature(method->in_args,
1031 if (check_privilege(connection, message, method,
1032 iface->user_data) == TRUE)
1033 return DBUS_HANDLER_RESULT_HANDLED;
1035 return process_message(connection, message, method,
1039 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1042 static DBusObjectPathVTable generic_table = {
1043 .unregister_function = generic_unregister,
1044 .message_function = generic_message,
1047 static const GDBusMethodTable introspect_methods[] = {
1048 { GDBUS_METHOD("Introspect", NULL,
1049 GDBUS_ARGS({ "xml", "s" }), introspect) },
1053 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1055 DBusMessageIter array;
1057 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1058 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1059 DBUS_TYPE_STRING_AS_STRING
1060 DBUS_TYPE_ARRAY_AS_STRING
1061 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1062 DBUS_TYPE_STRING_AS_STRING
1063 DBUS_TYPE_VARIANT_AS_STRING
1064 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1065 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1067 g_slist_foreach(data->interfaces, append_interface, &array);
1069 dbus_message_iter_close_container(iter, &array);
1072 static void append_object(gpointer data, gpointer user_data)
1074 struct generic_data *child = data;
1075 DBusMessageIter *array = user_data;
1076 DBusMessageIter entry;
1078 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1080 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1082 append_interfaces(child, &entry);
1083 dbus_message_iter_close_container(array, &entry);
1085 g_slist_foreach(child->objects, append_object, user_data);
1088 static DBusMessage *get_objects(DBusConnection *connection,
1089 DBusMessage *message, void *user_data)
1091 struct generic_data *data = user_data;
1093 DBusMessageIter iter;
1094 DBusMessageIter array;
1096 reply = dbus_message_new_method_return(message);
1100 dbus_message_iter_init_append(reply, &iter);
1102 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1103 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1104 DBUS_TYPE_OBJECT_PATH_AS_STRING
1105 DBUS_TYPE_ARRAY_AS_STRING
1106 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1107 DBUS_TYPE_STRING_AS_STRING
1108 DBUS_TYPE_ARRAY_AS_STRING
1109 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1110 DBUS_TYPE_STRING_AS_STRING
1111 DBUS_TYPE_VARIANT_AS_STRING
1112 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1113 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1114 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1117 g_slist_foreach(data->objects, append_object, &array);
1119 dbus_message_iter_close_container(&iter, &array);
1124 static const GDBusMethodTable manager_methods[] = {
1125 { GDBUS_METHOD("GetManagedObjects", NULL,
1126 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1130 static const GDBusSignalTable manager_signals[] = {
1131 { GDBUS_SIGNAL("InterfacesAdded",
1132 GDBUS_ARGS({ "object", "o" },
1133 { "interfaces", "a{sa{sv}}" })) },
1134 { GDBUS_SIGNAL("InterfacesRemoved",
1135 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1139 static void add_interface(struct generic_data *data,
1141 const GDBusMethodTable *methods,
1142 const GDBusSignalTable *signals,
1143 const GDBusPropertyTable *properties,
1145 GDBusDestroyFunction destroy)
1147 struct interface_data *iface;
1149 iface = g_new0(struct interface_data, 1);
1150 iface->name = g_strdup(name);
1151 iface->methods = methods;
1152 iface->signals = signals;
1153 iface->properties = properties;
1154 iface->user_data = user_data;
1155 iface->destroy = destroy;
1157 data->interfaces = g_slist_append(data->interfaces, iface);
1158 if (data->parent == NULL)
1161 data->added = g_slist_append(data->added, iface);
1162 if (data->process_id > 0)
1165 data->process_id = g_idle_add(process_changes, data);
1168 static struct generic_data *object_path_ref(DBusConnection *connection,
1171 struct generic_data *data;
1173 if (dbus_connection_get_object_path_data(connection, path,
1174 (void *) &data) == TRUE) {
1181 data = g_new0(struct generic_data, 1);
1182 data->conn = dbus_connection_ref(connection);
1183 data->path = g_strdup(path);
1186 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1188 if (!dbus_connection_register_object_path(connection, path,
1189 &generic_table, data)) {
1190 g_free(data->introspect);
1195 invalidate_parent_data(connection, path);
1197 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1198 NULL, NULL, data, NULL);
1200 /* Only root path export ObjectManager interface */
1201 if (data->parent == NULL)
1202 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1203 manager_methods, manager_signals,
1206 add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1207 properties_signals, NULL, data, NULL);
1212 static void object_path_unref(DBusConnection *connection, const char *path)
1214 struct generic_data *data = NULL;
1216 if (dbus_connection_get_object_path_data(connection, path,
1217 (void *) &data) == FALSE)
1225 if (data->refcount > 0)
1228 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1229 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1230 remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1232 invalidate_parent_data(data->conn, data->path);
1234 dbus_connection_unregister_object_path(data->conn, data->path);
1237 static gboolean check_signal(DBusConnection *conn, const char *path,
1238 const char *interface, const char *name,
1239 const GDBusArgInfo **args)
1241 struct generic_data *data = NULL;
1242 struct interface_data *iface;
1243 const GDBusSignalTable *signal;
1246 if (!dbus_connection_get_object_path_data(conn, path,
1247 (void *) &data) || data == NULL) {
1248 error("dbus_connection_emit_signal: path %s isn't registered",
1253 iface = find_interface(data->interfaces, interface);
1254 if (iface == NULL) {
1255 error("dbus_connection_emit_signal: %s does not implement %s",
1260 for (signal = iface->signals; signal && signal->name; signal++) {
1261 if (!strcmp(signal->name, name)) {
1262 *args = signal->args;
1267 error("No signal named %s on interface %s", name, interface);
1271 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1273 const char *interface,
1278 DBusMessage *signal;
1280 const GDBusArgInfo *args;
1282 if (!check_signal(conn, path, interface, name, &args))
1285 signal = dbus_message_new_signal(path, interface, name);
1286 if (signal == NULL) {
1287 error("Unable to allocate new %s.%s signal", interface, name);
1291 ret = dbus_message_append_args_valist(signal, first, var_args);
1295 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1296 error("%s.%s: got unexpected signature '%s'", interface, name,
1297 dbus_message_get_signature(signal));
1302 ret = dbus_connection_send(conn, signal, NULL);
1305 dbus_message_unref(signal);
1310 gboolean g_dbus_register_interface(DBusConnection *connection,
1311 const char *path, const char *name,
1312 const GDBusMethodTable *methods,
1313 const GDBusSignalTable *signals,
1314 const GDBusPropertyTable *properties,
1316 GDBusDestroyFunction destroy)
1318 struct generic_data *data;
1320 data = object_path_ref(connection, path);
1324 if (find_interface(data->interfaces, name)) {
1325 object_path_unref(connection, path);
1329 add_interface(data, name, methods, signals, properties, user_data,
1332 g_free(data->introspect);
1333 data->introspect = NULL;
1338 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1339 const char *path, const char *name)
1341 struct generic_data *data = NULL;
1346 if (dbus_connection_get_object_path_data(connection, path,
1347 (void *) &data) == FALSE)
1353 if (remove_interface(data, name) == FALSE)
1356 g_free(data->introspect);
1357 data->introspect = NULL;
1359 object_path_unref(connection, data->path);
1364 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1366 if (security_table != NULL)
1369 security_table = security;
1374 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1376 security_table = NULL;
1381 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1382 const char *format, va_list args)
1386 vsnprintf(str, sizeof(str), format, args);
1388 return dbus_message_new_error(message, name, str);
1391 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1392 const char *format, ...)
1397 va_start(args, format);
1399 reply = g_dbus_create_error_valist(message, name, format, args);
1406 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1407 int type, va_list args)
1411 reply = dbus_message_new_method_return(message);
1415 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1416 dbus_message_unref(reply);
1423 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1428 va_start(args, type);
1430 reply = g_dbus_create_reply_valist(message, type, args);
1437 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1441 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1442 dbus_message_set_no_reply(message, TRUE);
1444 result = dbus_connection_send(connection, message, NULL);
1446 dbus_message_unref(message);
1451 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1452 DBusMessage *message, int type, va_list args)
1456 reply = dbus_message_new_method_return(message);
1460 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1461 dbus_message_unref(reply);
1465 return g_dbus_send_message(connection, reply);
1468 gboolean g_dbus_send_reply(DBusConnection *connection,
1469 DBusMessage *message, int type, ...)
1474 va_start(args, type);
1476 result = g_dbus_send_reply_valist(connection, message, type, args);
1483 gboolean g_dbus_emit_signal(DBusConnection *connection,
1484 const char *path, const char *interface,
1485 const char *name, int type, ...)
1490 va_start(args, type);
1492 result = emit_signal_valist(connection, path, interface,
1500 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1501 const char *path, const char *interface,
1502 const char *name, int type, va_list args)
1504 return emit_signal_valist(connection, path, interface,
1508 static void process_properties_from_interface(struct generic_data *data,
1509 struct interface_data *iface)
1512 DBusMessage *signal;
1513 DBusMessageIter iter, dict, array;
1515 if (iface->pending_prop == NULL)
1518 signal = dbus_message_new_signal(data->path,
1519 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1520 if (signal == NULL) {
1521 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1522 ".PropertiesChanged signal");
1526 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1528 dbus_message_iter_init_append(signal, &iter);
1529 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1530 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1531 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1532 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1533 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1535 for (l = iface->pending_prop; l != NULL; l = l->next) {
1536 GDBusPropertyTable *p = l->data;
1541 if (p->exists != NULL && !p->exists(p, iface->user_data))
1544 append_property(iface, p, &dict);
1547 dbus_message_iter_close_container(&iter, &dict);
1548 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1549 DBUS_TYPE_STRING_AS_STRING, &array);
1550 dbus_message_iter_close_container(&iter, &array);
1552 g_dbus_send_message(data->conn, signal);
1554 g_slist_free(iface->pending_prop);
1555 iface->pending_prop = NULL;
1558 static void process_property_changes(struct generic_data *data)
1562 for (l = data->interfaces; l != NULL; l = l->next) {
1563 struct interface_data *iface = l->data;
1565 process_properties_from_interface(data, iface);
1568 data->pending_prop = FALSE;
1571 void g_dbus_emit_property_changed(DBusConnection *connection,
1572 const char *path, const char *interface,
1575 const GDBusPropertyTable *property;
1576 struct generic_data *data;
1577 struct interface_data *iface;
1579 if (!dbus_connection_get_object_path_data(connection, path,
1580 (void **) &data) || data == NULL)
1583 iface = find_interface(data->interfaces, interface);
1587 property = find_property(iface->properties, name);
1588 if (property == NULL) {
1589 error("Could not find property %s in %p", name,
1594 data->pending_prop = TRUE;
1595 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1598 if (!data->process_id) {
1599 data->process_id = g_idle_add(process_changes, data);