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 gboolean process_changes(gpointer user_data);
88 static void process_properties_from_interface(struct generic_data *data,
89 struct interface_data *iface);
90 static void process_property_changes(struct generic_data *data);
92 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
93 const char *direction)
95 for (; args && args->name; args++) {
96 g_string_append_printf(gstr,
97 "\t\t\t<arg name=\"%s\" type=\"%s\"",
98 args->name, args->signature);
101 g_string_append_printf(gstr,
102 " direction=\"%s\"/>\n", direction);
104 g_string_append_printf(gstr, "/>\n");
109 #define G_DBUS_ANNOTATE(prefix_, name_, value_) \
110 prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
111 "value=\"" value_ "\"/>\n"
113 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
114 G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
116 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
117 G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
119 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
121 const GDBusMethodTable *method;
122 const GDBusSignalTable *signal;
123 const GDBusPropertyTable *property;
125 for (method = iface->methods; method && method->name; method++) {
126 gboolean deprecated = method->flags &
127 G_DBUS_METHOD_FLAG_DEPRECATED;
128 gboolean noreply = method->flags &
129 G_DBUS_METHOD_FLAG_NOREPLY;
131 if (!deprecated && !noreply &&
132 !(method->in_args && method->in_args->name) &&
133 !(method->out_args && method->out_args->name))
134 g_string_append_printf(gstr,
135 "\t\t<method name=\"%s\"/>\n",
138 g_string_append_printf(gstr,
139 "\t\t<method name=\"%s\">\n",
141 print_arguments(gstr, method->in_args, "in");
142 print_arguments(gstr, method->out_args, "out");
145 g_string_append_printf(gstr,
146 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
148 g_string_append_printf(gstr,
149 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
151 g_string_append_printf(gstr, "\t\t</method>\n");
155 for (signal = iface->signals; signal && signal->name; signal++) {
156 gboolean deprecated = signal->flags &
157 G_DBUS_SIGNAL_FLAG_DEPRECATED;
159 if (!deprecated && !(signal->args && signal->args->name))
160 g_string_append_printf(gstr,
161 "\t\t<signal name=\"%s\"/>\n",
164 g_string_append_printf(gstr,
165 "\t\t<signal name=\"%s\">\n",
167 print_arguments(gstr, signal->args, NULL);
170 g_string_append_printf(gstr,
171 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
173 g_string_append_printf(gstr, "\t\t</signal>\n");
177 for (property = iface->properties; property && property->name;
179 gboolean deprecated = property->flags &
180 G_DBUS_PROPERTY_FLAG_DEPRECATED;
182 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
183 " type=\"%s\" access=\"%s%s\"",
184 property->name, property->type,
185 property->get ? "read" : "",
186 property->set ? "write" : "");
189 g_string_append_printf(gstr, "/>\n");
191 g_string_append_printf(gstr,
192 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
196 static void generate_introspection_xml(DBusConnection *conn,
197 struct generic_data *data, const char *path)
204 g_free(data->introspect);
206 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
208 g_string_append_printf(gstr, "<node>\n");
210 for (list = data->interfaces; list; list = list->next) {
211 struct interface_data *iface = list->data;
213 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
216 generate_interface_xml(gstr, iface);
218 g_string_append_printf(gstr, "\t</interface>\n");
221 if (!dbus_connection_list_registered(conn, path, &children))
224 for (i = 0; children[i]; i++)
225 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
228 dbus_free_string_array(children);
231 g_string_append_printf(gstr, "</node>\n");
233 data->introspect = g_string_free(gstr, FALSE);
236 static DBusMessage *introspect(DBusConnection *connection,
237 DBusMessage *message, void *user_data)
239 struct generic_data *data = user_data;
242 if (data->introspect == NULL)
243 generate_introspection_xml(connection, data,
244 dbus_message_get_path(message));
246 reply = dbus_message_new_method_return(message);
250 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
256 static DBusHandlerResult process_message(DBusConnection *connection,
257 DBusMessage *message, const GDBusMethodTable *method,
258 void *iface_user_data)
262 reply = method->function(connection, message, iface_user_data);
264 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
266 dbus_message_unref(reply);
267 return DBUS_HANDLER_RESULT_HANDLED;
270 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
272 return DBUS_HANDLER_RESULT_HANDLED;
276 return DBUS_HANDLER_RESULT_NEED_MEMORY;
278 dbus_connection_send(connection, reply, NULL);
279 dbus_message_unref(reply);
281 return DBUS_HANDLER_RESULT_HANDLED;
284 static GDBusPendingReply next_pending = 1;
285 static GSList *pending_security = NULL;
287 static const GDBusSecurityTable *security_table = NULL;
289 void g_dbus_pending_success(DBusConnection *connection,
290 GDBusPendingReply pending)
294 for (list = pending_security; list; list = list->next) {
295 struct security_data *secdata = list->data;
297 if (secdata->pending != pending)
300 pending_security = g_slist_remove(pending_security, secdata);
302 process_message(connection, secdata->message,
303 secdata->method, secdata->iface_user_data);
305 dbus_message_unref(secdata->message);
311 void g_dbus_pending_error_valist(DBusConnection *connection,
312 GDBusPendingReply pending, const char *name,
313 const char *format, va_list args)
317 for (list = pending_security; list; list = list->next) {
318 struct security_data *secdata = list->data;
321 if (secdata->pending != pending)
324 pending_security = g_slist_remove(pending_security, secdata);
326 reply = g_dbus_create_error_valist(secdata->message,
329 dbus_connection_send(connection, reply, NULL);
330 dbus_message_unref(reply);
333 dbus_message_unref(secdata->message);
339 void g_dbus_pending_error(DBusConnection *connection,
340 GDBusPendingReply pending,
341 const char *name, const char *format, ...)
345 va_start(args, format);
347 g_dbus_pending_error_valist(connection, pending, name, format, args);
352 int polkit_check_authorization(DBusConnection *conn,
353 const char *action, gboolean interaction,
354 void (*function) (dbus_bool_t authorized,
356 void *user_data, int timeout);
358 struct builtin_security_data {
359 DBusConnection *conn;
360 GDBusPendingReply pending;
363 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
365 struct builtin_security_data *data = user_data;
367 if (authorized == TRUE)
368 g_dbus_pending_success(data->conn, data->pending);
370 g_dbus_pending_error(data->conn, data->pending,
371 DBUS_ERROR_AUTH_FAILED, NULL);
376 static void builtin_security_function(DBusConnection *conn,
378 gboolean interaction,
379 GDBusPendingReply pending)
381 struct builtin_security_data *data;
383 data = g_new0(struct builtin_security_data, 1);
385 data->pending = pending;
387 if (polkit_check_authorization(conn, action, interaction,
388 builtin_security_result, data, 30000) < 0)
389 g_dbus_pending_error(conn, pending, NULL, NULL);
392 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
393 const GDBusMethodTable *method, void *iface_user_data)
395 const GDBusSecurityTable *security;
397 for (security = security_table; security && security->privilege;
399 struct security_data *secdata;
400 gboolean interaction;
402 if (security->privilege != method->privilege)
405 secdata = g_new(struct security_data, 1);
406 secdata->pending = next_pending++;
407 secdata->message = dbus_message_ref(msg);
408 secdata->method = method;
409 secdata->iface_user_data = iface_user_data;
411 pending_security = g_slist_prepend(pending_security, secdata);
413 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
418 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
420 security->function(conn, security->action,
421 interaction, secdata->pending);
423 builtin_security_function(conn, security->action,
424 interaction, secdata->pending);
432 static GDBusPendingPropertySet next_pending_property = 1;
433 static GSList *pending_property_set;
435 static struct property_data *remove_pending_property_data(
436 GDBusPendingPropertySet id)
438 struct property_data *propdata;
441 for (l = pending_property_set; l != NULL; l = l->next) {
443 if (propdata->id != id)
452 pending_property_set = g_slist_delete_link(pending_property_set, l);
457 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
459 struct property_data *propdata;
461 propdata = remove_pending_property_data(id);
462 if (propdata == NULL)
465 g_dbus_send_reply(propdata->conn, propdata->message,
467 dbus_message_unref(propdata->message);
471 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
472 const char *name, const char *format,
475 struct property_data *propdata;
478 propdata = remove_pending_property_data(id);
479 if (propdata == NULL)
482 reply = g_dbus_create_error_valist(propdata->message, name, format,
485 dbus_connection_send(propdata->conn, reply, NULL);
486 dbus_message_unref(reply);
489 dbus_message_unref(propdata->message);
493 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
494 const char *format, ...)
498 va_start(args, format);
500 g_dbus_pending_property_error_valist(id, name, format, args);
505 static void reset_parent(gpointer data, gpointer user_data)
507 struct generic_data *child = data;
508 struct generic_data *parent = user_data;
510 child->parent = parent;
513 static void append_property(struct interface_data *iface,
514 const GDBusPropertyTable *p, DBusMessageIter *dict)
516 DBusMessageIter entry, value;
518 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
520 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
521 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
524 p->get(p, &value, iface->user_data);
526 dbus_message_iter_close_container(&entry, &value);
527 dbus_message_iter_close_container(dict, &entry);
530 static void append_properties(struct interface_data *data,
531 DBusMessageIter *iter)
533 DBusMessageIter dict;
534 const GDBusPropertyTable *p;
536 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
537 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
538 DBUS_TYPE_STRING_AS_STRING
539 DBUS_TYPE_VARIANT_AS_STRING
540 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
542 for (p = data->properties; p && p->name; p++) {
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;
572 struct generic_data *parent = data->parent;
578 while (parent->parent)
579 parent = parent->parent;
581 signal = dbus_message_new_signal(parent->path,
582 DBUS_INTERFACE_OBJECT_MANAGER,
587 dbus_message_iter_init_append(signal, &iter);
588 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
591 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
592 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
593 DBUS_TYPE_STRING_AS_STRING
594 DBUS_TYPE_ARRAY_AS_STRING
595 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
596 DBUS_TYPE_STRING_AS_STRING
597 DBUS_TYPE_VARIANT_AS_STRING
598 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
599 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
601 g_slist_foreach(data->added, append_interface, &array);
602 g_slist_free(data->added);
605 dbus_message_iter_close_container(&iter, &array);
607 g_dbus_send_message(data->conn, signal);
610 static struct interface_data *find_interface(GSList *interfaces,
618 for (list = interfaces; list; list = list->next) {
619 struct interface_data *iface = list->data;
620 if (!strcmp(name, iface->name))
627 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
628 DBusMessage *message)
630 const char *sig = dbus_message_get_signature(message);
631 const char *p = NULL;
633 for (; args && args->signature && *sig; args++) {
636 for (; *sig && *p; sig++, p++) {
642 if (*sig || (p && *p) || (args && args->signature))
648 static gboolean remove_interface(struct generic_data *data, const char *name)
650 struct interface_data *iface;
652 iface = find_interface(data->interfaces, name);
656 process_properties_from_interface(data, iface);
658 data->interfaces = g_slist_remove(data->interfaces, iface);
660 if (iface->destroy) {
661 iface->destroy(iface->user_data);
662 iface->user_data = NULL;
666 * Interface being removed was just added, on the same mainloop
667 * iteration? Don't send any signal
669 if (g_slist_find(data->added, iface)) {
670 data->added = g_slist_remove(data->added, iface);
676 if (data->parent == NULL) {
682 data->removed = g_slist_prepend(data->removed, iface->name);
685 if (data->process_id > 0)
688 data->process_id = g_idle_add(process_changes, data);
693 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
694 const char *child_path)
696 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
697 char *parent_path, *slash;
699 parent_path = g_strdup(child_path);
700 slash = strrchr(parent_path, '/');
704 if (slash == parent_path && parent_path[1] != '\0')
705 parent_path[1] = '\0';
709 if (!strlen(parent_path))
712 if (dbus_connection_get_object_path_data(conn, parent_path,
713 (void *) &data) == FALSE) {
717 parent = invalidate_parent_data(conn, parent_path);
725 g_free(data->introspect);
726 data->introspect = NULL;
728 if (!dbus_connection_get_object_path_data(conn, child_path,
732 if (child == NULL || g_slist_find(data->objects, child) != NULL)
735 data->objects = g_slist_prepend(data->objects, child);
736 child->parent = data;
743 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
746 const GDBusPropertyTable *p;
748 for (p = properties; p && p->name; p++) {
749 if (strcmp(name, p->name) == 0)
756 static DBusMessage *properties_get(DBusConnection *connection,
757 DBusMessage *message, void *user_data)
759 struct generic_data *data = user_data;
760 struct interface_data *iface;
761 const GDBusPropertyTable *property;
762 const char *interface, *name;
763 DBusMessageIter iter, value;
766 if (!dbus_message_get_args(message, NULL,
767 DBUS_TYPE_STRING, &interface,
768 DBUS_TYPE_STRING, &name,
772 iface = find_interface(data->interfaces, interface);
774 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
775 "No such interface '%s'", interface);
777 property = find_property(iface->properties, name);
778 if (property == NULL)
779 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
780 "No such property '%s'", name);
782 if (property->exists != NULL &&
783 !property->exists(property, iface->user_data))
784 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
785 "No such property '%s'", name);
787 if (property->get == NULL)
788 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
789 "Property '%s' is not readable", name);
791 reply = dbus_message_new_method_return(message);
795 dbus_message_iter_init_append(reply, &iter);
796 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
797 property->type, &value);
799 if (!property->get(property, &value, iface->user_data)) {
800 dbus_message_unref(reply);
804 dbus_message_iter_close_container(&iter, &value);
809 static DBusMessage *properties_get_all(DBusConnection *connection,
810 DBusMessage *message, void *user_data)
812 struct generic_data *data = user_data;
813 struct interface_data *iface;
814 const char *interface;
815 DBusMessageIter iter;
818 if (!dbus_message_get_args(message, NULL,
819 DBUS_TYPE_STRING, &interface,
823 iface = find_interface(data->interfaces, interface);
825 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
826 "No such interface '%s'", interface);
828 reply = dbus_message_new_method_return(message);
832 dbus_message_iter_init_append(reply, &iter);
834 append_properties(iface, &iter);
839 static DBusMessage *properties_set(DBusConnection *connection,
840 DBusMessage *message, void *user_data)
842 struct generic_data *data = user_data;
843 DBusMessageIter iter, sub;
844 struct interface_data *iface;
845 const GDBusPropertyTable *property;
846 const char *name, *interface;
847 struct property_data *propdata;
849 if (!dbus_message_iter_init(message, &iter))
850 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
851 "No arguments given");
853 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
854 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
855 "Invalid argument type: '%c'",
856 dbus_message_iter_get_arg_type(&iter));
858 dbus_message_iter_get_basic(&iter, &interface);
859 dbus_message_iter_next(&iter);
861 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
862 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
863 "Invalid argument type: '%c'",
864 dbus_message_iter_get_arg_type(&iter));
866 dbus_message_iter_get_basic(&iter, &name);
867 dbus_message_iter_next(&iter);
869 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
870 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
871 "Invalid argument type: '%c'",
872 dbus_message_iter_get_arg_type(&iter));
874 dbus_message_iter_recurse(&iter, &sub);
876 iface = find_interface(data->interfaces, interface);
878 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
879 "No such interface '%s'", interface);
881 property = find_property(iface->properties, name);
882 if (property == NULL)
883 return g_dbus_create_error(message,
884 DBUS_ERROR_UNKNOWN_PROPERTY,
885 "No such property '%s'", name);
887 if (property->set == NULL)
888 return g_dbus_create_error(message,
889 DBUS_ERROR_PROPERTY_READ_ONLY,
890 "Property '%s' is not writable", name);
892 if (property->exists != NULL &&
893 !property->exists(property, iface->user_data))
894 return g_dbus_create_error(message,
895 DBUS_ERROR_UNKNOWN_PROPERTY,
896 "No such property '%s'", name);
898 propdata = g_new(struct property_data, 1);
899 propdata->id = next_pending_property++;
900 propdata->message = dbus_message_ref(message);
901 propdata->conn = connection;
902 pending_property_set = g_slist_prepend(pending_property_set, propdata);
904 property->set(property, &sub, propdata->id, iface->user_data);
909 static const GDBusMethodTable properties_methods[] = {
910 { GDBUS_METHOD("Get",
911 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
912 GDBUS_ARGS({ "value", "v" }),
914 { GDBUS_ASYNC_METHOD("Set",
915 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
919 { GDBUS_METHOD("GetAll",
920 GDBUS_ARGS({ "interface", "s" }),
921 GDBUS_ARGS({ "properties", "a{sv}" }),
922 properties_get_all) },
926 static const GDBusSignalTable properties_signals[] = {
927 { GDBUS_SIGNAL("PropertiesChanged",
928 GDBUS_ARGS({ "interface", "s" },
929 { "changed_properties", "a{sv}" },
930 { "invalidated_properties", "as"})) },
934 static void append_name(gpointer data, gpointer user_data)
937 DBusMessageIter *iter = user_data;
939 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
942 static void emit_interfaces_removed(struct generic_data *data)
945 DBusMessageIter iter, array;
946 struct generic_data *parent = data->parent;
952 while (parent->parent)
953 parent = parent->parent;
955 signal = dbus_message_new_signal(parent->path,
956 DBUS_INTERFACE_OBJECT_MANAGER,
957 "InterfacesRemoved");
961 dbus_message_iter_init_append(signal, &iter);
962 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
964 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
965 DBUS_TYPE_STRING_AS_STRING, &array);
967 g_slist_foreach(data->removed, append_name, &array);
968 g_slist_free_full(data->removed, g_free);
969 data->removed = NULL;
971 dbus_message_iter_close_container(&iter, &array);
973 g_dbus_send_message(data->conn, signal);
976 static gboolean process_changes(gpointer user_data)
978 struct generic_data *data = user_data;
980 data->process_id = 0;
982 if (data->added != NULL)
983 emit_interfaces_added(data);
985 /* Flush pending properties */
986 if (data->pending_prop == TRUE)
987 process_property_changes(data);
989 if (data->removed != NULL)
990 emit_interfaces_removed(data);
995 static void generic_unregister(DBusConnection *connection, void *user_data)
997 struct generic_data *data = user_data;
998 struct generic_data *parent = data->parent;
1001 parent->objects = g_slist_remove(parent->objects, data);
1003 if (data->process_id > 0) {
1004 g_source_remove(data->process_id);
1005 process_changes(data);
1008 g_slist_foreach(data->objects, reset_parent, data->parent);
1009 g_slist_free(data->objects);
1011 dbus_connection_unref(data->conn);
1012 g_free(data->introspect);
1017 static DBusHandlerResult generic_message(DBusConnection *connection,
1018 DBusMessage *message, void *user_data)
1020 struct generic_data *data = user_data;
1021 struct interface_data *iface;
1022 const GDBusMethodTable *method;
1023 const char *interface;
1025 interface = dbus_message_get_interface(message);
1027 iface = find_interface(data->interfaces, interface);
1029 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1031 for (method = iface->methods; method &&
1032 method->name && method->function; method++) {
1033 if (dbus_message_is_method_call(message, iface->name,
1034 method->name) == FALSE)
1037 if (g_dbus_args_have_signature(method->in_args,
1041 if (check_privilege(connection, message, method,
1042 iface->user_data) == TRUE)
1043 return DBUS_HANDLER_RESULT_HANDLED;
1045 return process_message(connection, message, method,
1049 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1052 static DBusObjectPathVTable generic_table = {
1053 .unregister_function = generic_unregister,
1054 .message_function = generic_message,
1057 static const GDBusMethodTable introspect_methods[] = {
1058 { GDBUS_METHOD("Introspect", NULL,
1059 GDBUS_ARGS({ "xml", "s" }), introspect) },
1063 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1065 DBusMessageIter array;
1067 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1068 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1069 DBUS_TYPE_STRING_AS_STRING
1070 DBUS_TYPE_ARRAY_AS_STRING
1071 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1072 DBUS_TYPE_STRING_AS_STRING
1073 DBUS_TYPE_VARIANT_AS_STRING
1074 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1075 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1077 g_slist_foreach(data->interfaces, append_interface, &array);
1079 dbus_message_iter_close_container(iter, &array);
1082 static void append_object(gpointer data, gpointer user_data)
1084 struct generic_data *child = data;
1085 DBusMessageIter *array = user_data;
1086 DBusMessageIter entry;
1088 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1090 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1092 append_interfaces(child, &entry);
1093 dbus_message_iter_close_container(array, &entry);
1095 g_slist_foreach(child->objects, append_object, user_data);
1098 static DBusMessage *get_objects(DBusConnection *connection,
1099 DBusMessage *message, void *user_data)
1101 struct generic_data *data = user_data;
1103 DBusMessageIter iter;
1104 DBusMessageIter array;
1106 reply = dbus_message_new_method_return(message);
1110 dbus_message_iter_init_append(reply, &iter);
1112 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1113 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1114 DBUS_TYPE_OBJECT_PATH_AS_STRING
1115 DBUS_TYPE_ARRAY_AS_STRING
1116 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1117 DBUS_TYPE_STRING_AS_STRING
1118 DBUS_TYPE_ARRAY_AS_STRING
1119 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1120 DBUS_TYPE_STRING_AS_STRING
1121 DBUS_TYPE_VARIANT_AS_STRING
1122 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1123 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1124 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1127 g_slist_foreach(data->objects, append_object, &array);
1129 dbus_message_iter_close_container(&iter, &array);
1134 static const GDBusMethodTable manager_methods[] = {
1135 { GDBUS_METHOD("GetManagedObjects", NULL,
1136 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1140 static const GDBusSignalTable manager_signals[] = {
1141 { GDBUS_SIGNAL("InterfacesAdded",
1142 GDBUS_ARGS({ "object", "o" },
1143 { "interfaces", "a{sa{sv}}" })) },
1144 { GDBUS_SIGNAL("InterfacesRemoved",
1145 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1149 static void add_interface(struct generic_data *data,
1151 const GDBusMethodTable *methods,
1152 const GDBusSignalTable *signals,
1153 const GDBusPropertyTable *properties,
1155 GDBusDestroyFunction destroy)
1157 struct interface_data *iface;
1159 iface = g_new0(struct interface_data, 1);
1160 iface->name = g_strdup(name);
1161 iface->methods = methods;
1162 iface->signals = signals;
1163 iface->properties = properties;
1164 iface->user_data = user_data;
1165 iface->destroy = destroy;
1167 data->interfaces = g_slist_append(data->interfaces, iface);
1168 if (data->parent == NULL)
1171 data->added = g_slist_append(data->added, iface);
1172 if (data->process_id > 0)
1175 data->process_id = g_idle_add(process_changes, data);
1178 static struct generic_data *object_path_ref(DBusConnection *connection,
1181 struct generic_data *data;
1183 if (dbus_connection_get_object_path_data(connection, path,
1184 (void *) &data) == TRUE) {
1191 data = g_new0(struct generic_data, 1);
1192 data->conn = dbus_connection_ref(connection);
1193 data->path = g_strdup(path);
1196 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1198 if (!dbus_connection_register_object_path(connection, path,
1199 &generic_table, data)) {
1200 g_free(data->introspect);
1205 invalidate_parent_data(connection, path);
1207 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1208 NULL, NULL, data, NULL);
1210 /* Only root path export ObjectManager interface */
1211 if (data->parent == NULL)
1212 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1213 manager_methods, manager_signals,
1219 static void object_path_unref(DBusConnection *connection, const char *path)
1221 struct generic_data *data = NULL;
1223 if (dbus_connection_get_object_path_data(connection, path,
1224 (void *) &data) == FALSE)
1232 if (data->refcount > 0)
1235 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1236 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1237 remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1239 invalidate_parent_data(data->conn, data->path);
1241 dbus_connection_unregister_object_path(data->conn, data->path);
1244 static gboolean check_signal(DBusConnection *conn, const char *path,
1245 const char *interface, const char *name,
1246 const GDBusArgInfo **args)
1248 struct generic_data *data = NULL;
1249 struct interface_data *iface;
1250 const GDBusSignalTable *signal;
1253 if (!dbus_connection_get_object_path_data(conn, path,
1254 (void *) &data) || data == NULL) {
1255 error("dbus_connection_emit_signal: path %s isn't registered",
1260 iface = find_interface(data->interfaces, interface);
1261 if (iface == NULL) {
1262 error("dbus_connection_emit_signal: %s does not implement %s",
1267 for (signal = iface->signals; signal && signal->name; signal++) {
1268 if (!strcmp(signal->name, name)) {
1269 *args = signal->args;
1274 error("No signal named %s on interface %s", name, interface);
1278 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1280 const char *interface,
1285 DBusMessage *signal;
1287 const GDBusArgInfo *args;
1289 if (!check_signal(conn, path, interface, name, &args))
1292 signal = dbus_message_new_signal(path, interface, name);
1293 if (signal == NULL) {
1294 error("Unable to allocate new %s.%s signal", interface, name);
1298 ret = dbus_message_append_args_valist(signal, first, var_args);
1302 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1303 error("%s.%s: got unexpected signature '%s'", interface, name,
1304 dbus_message_get_signature(signal));
1309 ret = dbus_connection_send(conn, signal, NULL);
1312 dbus_message_unref(signal);
1317 gboolean g_dbus_register_interface(DBusConnection *connection,
1318 const char *path, const char *name,
1319 const GDBusMethodTable *methods,
1320 const GDBusSignalTable *signals,
1321 const GDBusPropertyTable *properties,
1323 GDBusDestroyFunction destroy)
1325 struct generic_data *data;
1327 data = object_path_ref(connection, path);
1331 if (find_interface(data->interfaces, name)) {
1332 object_path_unref(connection, path);
1336 if (properties != NULL && !find_interface(data->interfaces,
1337 DBUS_INTERFACE_PROPERTIES))
1338 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1339 properties_methods, properties_signals, NULL,
1342 add_interface(data, name, methods, signals, properties, user_data,
1345 g_free(data->introspect);
1346 data->introspect = NULL;
1351 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1352 const char *path, const char *name)
1354 struct generic_data *data = NULL;
1359 if (dbus_connection_get_object_path_data(connection, path,
1360 (void *) &data) == FALSE)
1366 if (remove_interface(data, name) == FALSE)
1369 g_free(data->introspect);
1370 data->introspect = NULL;
1372 object_path_unref(connection, data->path);
1377 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1379 if (security_table != NULL)
1382 security_table = security;
1387 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1389 security_table = NULL;
1394 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1395 const char *format, va_list args)
1399 vsnprintf(str, sizeof(str), format, args);
1401 return dbus_message_new_error(message, name, str);
1404 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1405 const char *format, ...)
1410 va_start(args, format);
1412 reply = g_dbus_create_error_valist(message, name, format, args);
1419 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1420 int type, va_list args)
1424 reply = dbus_message_new_method_return(message);
1428 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1429 dbus_message_unref(reply);
1436 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1441 va_start(args, type);
1443 reply = g_dbus_create_reply_valist(message, type, args);
1450 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1454 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1455 dbus_message_set_no_reply(message, TRUE);
1457 result = dbus_connection_send(connection, message, NULL);
1459 dbus_message_unref(message);
1464 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1465 DBusMessage *message, int type, va_list args)
1469 reply = dbus_message_new_method_return(message);
1473 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1474 dbus_message_unref(reply);
1478 return g_dbus_send_message(connection, reply);
1481 gboolean g_dbus_send_reply(DBusConnection *connection,
1482 DBusMessage *message, int type, ...)
1487 va_start(args, type);
1489 result = g_dbus_send_reply_valist(connection, message, type, args);
1496 gboolean g_dbus_emit_signal(DBusConnection *connection,
1497 const char *path, const char *interface,
1498 const char *name, int type, ...)
1503 va_start(args, type);
1505 result = emit_signal_valist(connection, path, interface,
1513 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1514 const char *path, const char *interface,
1515 const char *name, int type, va_list args)
1517 return emit_signal_valist(connection, path, interface,
1521 static void process_properties_from_interface(struct generic_data *data,
1522 struct interface_data *iface)
1525 DBusMessage *signal;
1526 DBusMessageIter iter, dict, array;
1527 GSList *invalidated;
1529 if (iface->pending_prop == NULL)
1532 signal = dbus_message_new_signal(data->path,
1533 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1534 if (signal == NULL) {
1535 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1536 ".PropertiesChanged signal");
1540 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1542 dbus_message_iter_init_append(signal, &iter);
1543 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1544 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1545 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1546 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1547 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1551 for (l = iface->pending_prop; l != NULL; l = l->next) {
1552 GDBusPropertyTable *p = l->data;
1557 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1558 invalidated = g_slist_prepend(invalidated, p);
1562 append_property(iface, p, &dict);
1565 dbus_message_iter_close_container(&iter, &dict);
1567 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1568 DBUS_TYPE_STRING_AS_STRING, &array);
1569 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1570 GDBusPropertyTable *p = l->data;
1572 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1575 g_slist_free(invalidated);
1576 dbus_message_iter_close_container(&iter, &array);
1578 g_dbus_send_message(data->conn, signal);
1580 g_slist_free(iface->pending_prop);
1581 iface->pending_prop = NULL;
1584 static void process_property_changes(struct generic_data *data)
1588 for (l = data->interfaces; l != NULL; l = l->next) {
1589 struct interface_data *iface = l->data;
1591 process_properties_from_interface(data, iface);
1594 data->pending_prop = FALSE;
1597 void g_dbus_emit_property_changed(DBusConnection *connection,
1598 const char *path, const char *interface,
1601 const GDBusPropertyTable *property;
1602 struct generic_data *data;
1603 struct interface_data *iface;
1605 if (!dbus_connection_get_object_path_data(connection, path,
1606 (void **) &data) || data == NULL)
1609 iface = find_interface(data->interfaces, interface);
1613 property = find_property(iface->properties, name);
1614 if (property == NULL) {
1615 error("Could not find property %s in %p", name,
1620 data->pending_prop = TRUE;
1621 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1624 if (!data->process_id) {
1625 data->process_id = g_idle_add(process_changes, data);
1630 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1631 const char *interface, DBusMessageIter *iter)
1633 struct generic_data *data;
1634 struct interface_data *iface;
1636 if (!dbus_connection_get_object_path_data(connection, path,
1637 (void **) &data) || data == NULL)
1640 iface = find_interface(data->interfaces, interface);
1644 append_properties(iface, iter);