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 {
75 GDBusPendingPropertySet id;
79 static gboolean process_changes(gpointer user_data);
80 static void process_properties_from_interface(struct generic_data *data,
81 struct interface_data *iface);
82 static void process_property_changes(struct generic_data *data);
84 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
85 const char *direction)
87 for (; args && args->name; args++) {
88 g_string_append_printf(gstr,
89 "\t\t\t<arg name=\"%s\" type=\"%s\"",
90 args->name, args->signature);
93 g_string_append_printf(gstr,
94 " direction=\"%s\"/>\n", direction);
96 g_string_append_printf(gstr, "/>\n");
101 #define G_DBUS_ANNOTATE(prefix_, name_, value_) \
102 prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
103 "value=\"" value_ "\"/>\n"
105 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
106 G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
108 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
109 G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
111 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
113 const GDBusMethodTable *method;
114 const GDBusSignalTable *signal;
115 const GDBusPropertyTable *property;
117 for (method = iface->methods; method && method->name; method++) {
118 gboolean deprecated = method->flags &
119 G_DBUS_METHOD_FLAG_DEPRECATED;
120 gboolean noreply = method->flags &
121 G_DBUS_METHOD_FLAG_NOREPLY;
123 if (!deprecated && !noreply &&
124 !(method->in_args && method->in_args->name) &&
125 !(method->out_args && method->out_args->name))
126 g_string_append_printf(gstr,
127 "\t\t<method name=\"%s\"/>\n",
130 g_string_append_printf(gstr,
131 "\t\t<method name=\"%s\">\n",
133 print_arguments(gstr, method->in_args, "in");
134 print_arguments(gstr, method->out_args, "out");
137 g_string_append_printf(gstr,
138 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
140 g_string_append_printf(gstr,
141 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
143 g_string_append_printf(gstr, "\t\t</method>\n");
147 for (signal = iface->signals; signal && signal->name; signal++) {
148 gboolean deprecated = signal->flags &
149 G_DBUS_SIGNAL_FLAG_DEPRECATED;
151 if (!deprecated && !(signal->args && signal->args->name))
152 g_string_append_printf(gstr,
153 "\t\t<signal name=\"%s\"/>\n",
156 g_string_append_printf(gstr,
157 "\t\t<signal name=\"%s\">\n",
159 print_arguments(gstr, signal->args, NULL);
162 g_string_append_printf(gstr,
163 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
165 g_string_append_printf(gstr, "\t\t</signal>\n");
169 for (property = iface->properties; property && property->name;
171 gboolean deprecated = property->flags &
172 G_DBUS_PROPERTY_FLAG_DEPRECATED;
174 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
175 " type=\"%s\" access=\"%s%s\"",
176 property->name, property->type,
177 property->get ? "read" : "",
178 property->set ? "write" : "");
181 g_string_append_printf(gstr, "/>\n");
183 g_string_append_printf(gstr,
184 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
188 static void generate_introspection_xml(DBusConnection *conn,
189 struct generic_data *data, const char *path)
196 g_free(data->introspect);
198 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
200 g_string_append_printf(gstr, "<node>\n");
202 for (list = data->interfaces; list; list = list->next) {
203 struct interface_data *iface = list->data;
205 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
208 generate_interface_xml(gstr, iface);
210 g_string_append_printf(gstr, "\t</interface>\n");
213 if (!dbus_connection_list_registered(conn, path, &children))
216 for (i = 0; children[i]; i++)
217 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
220 dbus_free_string_array(children);
223 g_string_append_printf(gstr, "</node>\n");
225 data->introspect = g_string_free(gstr, FALSE);
228 static DBusMessage *introspect(DBusConnection *connection,
229 DBusMessage *message, void *user_data)
231 struct generic_data *data = user_data;
234 if (data->introspect == NULL)
235 generate_introspection_xml(connection, data,
236 dbus_message_get_path(message));
238 reply = dbus_message_new_method_return(message);
242 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
248 static DBusHandlerResult process_message(DBusConnection *connection,
249 DBusMessage *message, const GDBusMethodTable *method,
250 void *iface_user_data)
254 reply = method->function(connection, message, iface_user_data);
256 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
258 dbus_message_unref(reply);
259 return DBUS_HANDLER_RESULT_HANDLED;
262 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
264 return DBUS_HANDLER_RESULT_HANDLED;
268 return DBUS_HANDLER_RESULT_NEED_MEMORY;
270 dbus_connection_send(connection, reply, NULL);
271 dbus_message_unref(reply);
273 return DBUS_HANDLER_RESULT_HANDLED;
276 static GDBusPendingReply next_pending = 1;
277 static GSList *pending_security = NULL;
279 static const GDBusSecurityTable *security_table = NULL;
281 void g_dbus_pending_success(DBusConnection *connection,
282 GDBusPendingReply pending)
286 for (list = pending_security; list; list = list->next) {
287 struct security_data *secdata = list->data;
289 if (secdata->pending != pending)
292 pending_security = g_slist_remove(pending_security, secdata);
294 process_message(connection, secdata->message,
295 secdata->method, secdata->iface_user_data);
297 dbus_message_unref(secdata->message);
303 void g_dbus_pending_error_valist(DBusConnection *connection,
304 GDBusPendingReply pending, const char *name,
305 const char *format, va_list args)
309 for (list = pending_security; list; list = list->next) {
310 struct security_data *secdata = list->data;
313 if (secdata->pending != pending)
316 pending_security = g_slist_remove(pending_security, secdata);
318 reply = g_dbus_create_error_valist(secdata->message,
321 dbus_connection_send(connection, reply, NULL);
322 dbus_message_unref(reply);
325 dbus_message_unref(secdata->message);
331 void g_dbus_pending_error(DBusConnection *connection,
332 GDBusPendingReply pending,
333 const char *name, const char *format, ...)
337 va_start(args, format);
339 g_dbus_pending_error_valist(connection, pending, name, format, args);
344 int polkit_check_authorization(DBusConnection *conn,
345 const char *action, gboolean interaction,
346 void (*function) (dbus_bool_t authorized,
348 void *user_data, int timeout);
350 struct builtin_security_data {
351 DBusConnection *conn;
352 GDBusPendingReply pending;
355 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
357 struct builtin_security_data *data = user_data;
359 if (authorized == TRUE)
360 g_dbus_pending_success(data->conn, data->pending);
362 g_dbus_pending_error(data->conn, data->pending,
363 DBUS_ERROR_AUTH_FAILED, NULL);
368 static void builtin_security_function(DBusConnection *conn,
370 gboolean interaction,
371 GDBusPendingReply pending)
373 struct builtin_security_data *data;
375 data = g_new0(struct builtin_security_data, 1);
377 data->pending = pending;
379 if (polkit_check_authorization(conn, action, interaction,
380 builtin_security_result, data, 30000) < 0)
381 g_dbus_pending_error(conn, pending, NULL, NULL);
384 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
385 const GDBusMethodTable *method, void *iface_user_data)
387 const GDBusSecurityTable *security;
389 for (security = security_table; security && security->privilege;
391 struct security_data *secdata;
392 gboolean interaction;
394 if (security->privilege != method->privilege)
397 secdata = g_new(struct security_data, 1);
398 secdata->pending = next_pending++;
399 secdata->message = dbus_message_ref(msg);
400 secdata->method = method;
401 secdata->iface_user_data = iface_user_data;
403 pending_security = g_slist_prepend(pending_security, secdata);
405 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
410 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
412 security->function(conn, security->action,
413 interaction, secdata->pending);
415 builtin_security_function(conn, security->action,
416 interaction, secdata->pending);
424 static GDBusPendingPropertySet next_pending_property = 1;
425 static GSList *pending_property_set;
427 static struct property_data *remove_pending_property_data(
428 GDBusPendingPropertySet id)
430 struct property_data *propdata;
433 for (l = pending_property_set; l != NULL; l = l->next) {
435 if (propdata->id != id)
444 pending_property_set = g_slist_delete_link(pending_property_set, l);
449 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
451 struct property_data *propdata;
453 propdata = remove_pending_property_data(id);
454 if (propdata == NULL)
457 g_dbus_send_reply(propdata->conn, propdata->message,
459 dbus_message_unref(propdata->message);
463 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
464 const char *name, const char *format,
467 struct property_data *propdata;
470 propdata = remove_pending_property_data(id);
471 if (propdata == NULL)
474 reply = g_dbus_create_error_valist(propdata->message, name, format,
477 dbus_connection_send(propdata->conn, reply, NULL);
478 dbus_message_unref(reply);
481 dbus_message_unref(propdata->message);
485 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
486 const char *format, ...)
490 va_start(args, format);
492 g_dbus_pending_property_error_valist(id, name, format, args);
497 static void reset_parent(gpointer data, gpointer user_data)
499 struct generic_data *child = data;
500 struct generic_data *parent = user_data;
502 child->parent = parent;
505 static void append_property(struct interface_data *iface,
506 const GDBusPropertyTable *p, DBusMessageIter *dict)
508 DBusMessageIter entry, value;
510 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
512 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
513 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
516 p->get(p, &value, iface->user_data);
518 dbus_message_iter_close_container(&entry, &value);
519 dbus_message_iter_close_container(dict, &entry);
522 static void append_properties(struct interface_data *data,
523 DBusMessageIter *iter)
525 DBusMessageIter dict;
526 const GDBusPropertyTable *p;
528 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
529 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
530 DBUS_TYPE_STRING_AS_STRING
531 DBUS_TYPE_VARIANT_AS_STRING
532 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
534 for (p = data->properties; p && p->name; p++) {
538 if (p->exists != NULL && !p->exists(p, data->user_data))
541 append_property(data, p, &dict);
544 dbus_message_iter_close_container(iter, &dict);
547 static void append_interface(gpointer data, gpointer user_data)
549 struct interface_data *iface = data;
550 DBusMessageIter *array = user_data;
551 DBusMessageIter entry;
553 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
555 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
556 append_properties(data, &entry);
557 dbus_message_iter_close_container(array, &entry);
560 static void emit_interfaces_added(struct generic_data *data)
563 DBusMessageIter iter, array;
564 struct generic_data *parent = data->parent;
570 while (parent->parent)
571 parent = parent->parent;
573 signal = dbus_message_new_signal(parent->path,
574 DBUS_INTERFACE_OBJECT_MANAGER,
579 dbus_message_iter_init_append(signal, &iter);
580 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
583 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
584 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
585 DBUS_TYPE_STRING_AS_STRING
586 DBUS_TYPE_ARRAY_AS_STRING
587 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
588 DBUS_TYPE_STRING_AS_STRING
589 DBUS_TYPE_VARIANT_AS_STRING
590 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
591 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
593 g_slist_foreach(data->added, append_interface, &array);
594 g_slist_free(data->added);
597 dbus_message_iter_close_container(&iter, &array);
599 g_dbus_send_message(data->conn, signal);
602 static struct interface_data *find_interface(GSList *interfaces,
610 for (list = interfaces; list; list = list->next) {
611 struct interface_data *iface = list->data;
612 if (!strcmp(name, iface->name))
619 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
620 DBusMessage *message)
622 const char *sig = dbus_message_get_signature(message);
623 const char *p = NULL;
625 for (; args && args->signature && *sig; args++) {
628 for (; *sig && *p; sig++, p++) {
634 if (*sig || (p && *p) || (args && args->signature))
640 static gboolean remove_interface(struct generic_data *data, const char *name)
642 struct interface_data *iface;
644 iface = find_interface(data->interfaces, name);
648 process_properties_from_interface(data, iface);
650 data->interfaces = g_slist_remove(data->interfaces, iface);
652 if (iface->destroy) {
653 iface->destroy(iface->user_data);
654 iface->user_data = NULL;
658 * Interface being removed was just added, on the same mainloop
659 * iteration? Don't send any signal
661 if (g_slist_find(data->added, iface)) {
662 data->added = g_slist_remove(data->added, iface);
668 if (data->parent == NULL) {
674 data->removed = g_slist_prepend(data->removed, iface->name);
677 if (data->process_id > 0)
680 data->process_id = g_idle_add(process_changes, data);
685 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
686 const char *child_path)
688 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
689 char *parent_path, *slash;
691 parent_path = g_strdup(child_path);
692 slash = strrchr(parent_path, '/');
696 if (slash == parent_path && parent_path[1] != '\0')
697 parent_path[1] = '\0';
701 if (!strlen(parent_path))
704 if (dbus_connection_get_object_path_data(conn, parent_path,
705 (void *) &data) == FALSE) {
709 parent = invalidate_parent_data(conn, parent_path);
717 g_free(data->introspect);
718 data->introspect = NULL;
720 if (!dbus_connection_get_object_path_data(conn, child_path,
724 if (child == NULL || g_slist_find(data->objects, child) != NULL)
727 data->objects = g_slist_prepend(data->objects, child);
728 child->parent = data;
735 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
738 const GDBusPropertyTable *p;
740 for (p = properties; p && p->name; p++) {
741 if (strcmp(name, p->name) == 0)
748 static DBusMessage *properties_get(DBusConnection *connection,
749 DBusMessage *message, void *user_data)
751 struct generic_data *data = user_data;
752 struct interface_data *iface;
753 const GDBusPropertyTable *property;
754 const char *interface, *name;
755 DBusMessageIter iter, value;
758 if (!dbus_message_get_args(message, NULL,
759 DBUS_TYPE_STRING, &interface,
760 DBUS_TYPE_STRING, &name,
764 iface = find_interface(data->interfaces, interface);
766 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
767 "No such interface '%s'", interface);
769 property = find_property(iface->properties, name);
770 if (property == NULL)
771 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
772 "No such property '%s'", name);
774 if (property->exists != NULL &&
775 !property->exists(property, iface->user_data))
776 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
777 "No such property '%s'", name);
779 if (property->get == NULL)
780 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
781 "Property '%s' is not readable", name);
783 reply = dbus_message_new_method_return(message);
787 dbus_message_iter_init_append(reply, &iter);
788 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
789 property->type, &value);
791 if (!property->get(property, &value, iface->user_data)) {
792 dbus_message_unref(reply);
796 dbus_message_iter_close_container(&iter, &value);
801 static DBusMessage *properties_get_all(DBusConnection *connection,
802 DBusMessage *message, void *user_data)
804 struct generic_data *data = user_data;
805 struct interface_data *iface;
806 const char *interface;
807 DBusMessageIter iter;
810 if (!dbus_message_get_args(message, NULL,
811 DBUS_TYPE_STRING, &interface,
815 iface = find_interface(data->interfaces, interface);
817 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
818 "No such interface '%s'", interface);
820 reply = dbus_message_new_method_return(message);
824 dbus_message_iter_init_append(reply, &iter);
826 append_properties(iface, &iter);
831 static DBusMessage *properties_set(DBusConnection *connection,
832 DBusMessage *message, void *user_data)
834 struct generic_data *data = user_data;
835 DBusMessageIter iter, sub;
836 struct interface_data *iface;
837 const GDBusPropertyTable *property;
838 const char *name, *interface;
839 struct property_data *propdata;
841 if (!dbus_message_iter_init(message, &iter))
842 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
843 "No arguments given");
845 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
846 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
847 "Invalid argument type: '%c'",
848 dbus_message_iter_get_arg_type(&iter));
850 dbus_message_iter_get_basic(&iter, &interface);
851 dbus_message_iter_next(&iter);
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, &name);
859 dbus_message_iter_next(&iter);
861 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
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_recurse(&iter, &sub);
868 iface = find_interface(data->interfaces, interface);
870 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
871 "No such interface '%s'", interface);
873 property = find_property(iface->properties, name);
874 if (property == NULL)
875 return g_dbus_create_error(message,
876 DBUS_ERROR_UNKNOWN_PROPERTY,
877 "No such property '%s'", name);
879 if (property->set == NULL)
880 return g_dbus_create_error(message,
881 DBUS_ERROR_PROPERTY_READ_ONLY,
882 "Property '%s' is not writable", name);
884 if (property->exists != NULL &&
885 !property->exists(property, iface->user_data))
886 return g_dbus_create_error(message,
887 DBUS_ERROR_UNKNOWN_PROPERTY,
888 "No such property '%s'", name);
890 propdata = g_new(struct property_data, 1);
891 propdata->id = next_pending_property++;
892 propdata->message = dbus_message_ref(message);
893 propdata->conn = connection;
894 pending_property_set = g_slist_prepend(pending_property_set, propdata);
896 property->set(property, &sub, propdata->id, iface->user_data);
901 static const GDBusMethodTable properties_methods[] = {
902 { GDBUS_METHOD("Get",
903 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
904 GDBUS_ARGS({ "value", "v" }),
906 { GDBUS_ASYNC_METHOD("Set",
907 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
911 { GDBUS_METHOD("GetAll",
912 GDBUS_ARGS({ "interface", "s" }),
913 GDBUS_ARGS({ "properties", "a{sv}" }),
914 properties_get_all) },
918 static const GDBusSignalTable properties_signals[] = {
919 { GDBUS_SIGNAL("PropertiesChanged",
920 GDBUS_ARGS({ "interface", "s" },
921 { "changed_properties", "a{sv}" },
922 { "invalidated_properties", "as"})) },
926 static void append_name(gpointer data, gpointer user_data)
929 DBusMessageIter *iter = user_data;
931 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
934 static void emit_interfaces_removed(struct generic_data *data)
937 DBusMessageIter iter, array;
938 struct generic_data *parent = data->parent;
944 while (parent->parent)
945 parent = parent->parent;
947 signal = dbus_message_new_signal(parent->path,
948 DBUS_INTERFACE_OBJECT_MANAGER,
949 "InterfacesRemoved");
953 dbus_message_iter_init_append(signal, &iter);
954 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
956 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
957 DBUS_TYPE_STRING_AS_STRING, &array);
959 g_slist_foreach(data->removed, append_name, &array);
960 g_slist_free_full(data->removed, g_free);
961 data->removed = NULL;
963 dbus_message_iter_close_container(&iter, &array);
965 g_dbus_send_message(data->conn, signal);
968 static gboolean process_changes(gpointer user_data)
970 struct generic_data *data = user_data;
972 data->process_id = 0;
974 if (data->added != NULL)
975 emit_interfaces_added(data);
977 /* Flush pending properties */
978 if (data->pending_prop == TRUE)
979 process_property_changes(data);
981 if (data->removed != NULL)
982 emit_interfaces_removed(data);
987 static void generic_unregister(DBusConnection *connection, void *user_data)
989 struct generic_data *data = user_data;
990 struct generic_data *parent = data->parent;
993 parent->objects = g_slist_remove(parent->objects, data);
995 if (data->process_id > 0) {
996 g_source_remove(data->process_id);
997 process_changes(data);
1000 g_slist_foreach(data->objects, reset_parent, data->parent);
1001 g_slist_free(data->objects);
1003 dbus_connection_unref(data->conn);
1004 g_free(data->introspect);
1009 static DBusHandlerResult generic_message(DBusConnection *connection,
1010 DBusMessage *message, void *user_data)
1012 struct generic_data *data = user_data;
1013 struct interface_data *iface;
1014 const GDBusMethodTable *method;
1015 const char *interface;
1017 interface = dbus_message_get_interface(message);
1019 iface = find_interface(data->interfaces, interface);
1021 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1023 for (method = iface->methods; method &&
1024 method->name && method->function; method++) {
1025 if (dbus_message_is_method_call(message, iface->name,
1026 method->name) == FALSE)
1029 if (g_dbus_args_have_signature(method->in_args,
1033 if (check_privilege(connection, message, method,
1034 iface->user_data) == TRUE)
1035 return DBUS_HANDLER_RESULT_HANDLED;
1037 return process_message(connection, message, method,
1041 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1044 static DBusObjectPathVTable generic_table = {
1045 .unregister_function = generic_unregister,
1046 .message_function = generic_message,
1049 static const GDBusMethodTable introspect_methods[] = {
1050 { GDBUS_METHOD("Introspect", NULL,
1051 GDBUS_ARGS({ "xml", "s" }), introspect) },
1055 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1057 DBusMessageIter array;
1059 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1060 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1061 DBUS_TYPE_STRING_AS_STRING
1062 DBUS_TYPE_ARRAY_AS_STRING
1063 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1064 DBUS_TYPE_STRING_AS_STRING
1065 DBUS_TYPE_VARIANT_AS_STRING
1066 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1067 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1069 g_slist_foreach(data->interfaces, append_interface, &array);
1071 dbus_message_iter_close_container(iter, &array);
1074 static void append_object(gpointer data, gpointer user_data)
1076 struct generic_data *child = data;
1077 DBusMessageIter *array = user_data;
1078 DBusMessageIter entry;
1080 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1082 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1084 append_interfaces(child, &entry);
1085 dbus_message_iter_close_container(array, &entry);
1087 g_slist_foreach(child->objects, append_object, user_data);
1090 static DBusMessage *get_objects(DBusConnection *connection,
1091 DBusMessage *message, void *user_data)
1093 struct generic_data *data = user_data;
1095 DBusMessageIter iter;
1096 DBusMessageIter array;
1098 reply = dbus_message_new_method_return(message);
1102 dbus_message_iter_init_append(reply, &iter);
1104 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1105 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1106 DBUS_TYPE_OBJECT_PATH_AS_STRING
1107 DBUS_TYPE_ARRAY_AS_STRING
1108 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1109 DBUS_TYPE_STRING_AS_STRING
1110 DBUS_TYPE_ARRAY_AS_STRING
1111 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1112 DBUS_TYPE_STRING_AS_STRING
1113 DBUS_TYPE_VARIANT_AS_STRING
1114 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1115 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1116 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1119 g_slist_foreach(data->objects, append_object, &array);
1121 dbus_message_iter_close_container(&iter, &array);
1126 static const GDBusMethodTable manager_methods[] = {
1127 { GDBUS_METHOD("GetManagedObjects", NULL,
1128 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1132 static const GDBusSignalTable manager_signals[] = {
1133 { GDBUS_SIGNAL("InterfacesAdded",
1134 GDBUS_ARGS({ "object", "o" },
1135 { "interfaces", "a{sa{sv}}" })) },
1136 { GDBUS_SIGNAL("InterfacesRemoved",
1137 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1141 static void add_interface(struct generic_data *data,
1143 const GDBusMethodTable *methods,
1144 const GDBusSignalTable *signals,
1145 const GDBusPropertyTable *properties,
1147 GDBusDestroyFunction destroy)
1149 struct interface_data *iface;
1151 iface = g_new0(struct interface_data, 1);
1152 iface->name = g_strdup(name);
1153 iface->methods = methods;
1154 iface->signals = signals;
1155 iface->properties = properties;
1156 iface->user_data = user_data;
1157 iface->destroy = destroy;
1159 data->interfaces = g_slist_append(data->interfaces, iface);
1160 if (data->parent == NULL)
1163 data->added = g_slist_append(data->added, iface);
1164 if (data->process_id > 0)
1167 data->process_id = g_idle_add(process_changes, data);
1170 static struct generic_data *object_path_ref(DBusConnection *connection,
1173 struct generic_data *data;
1175 if (dbus_connection_get_object_path_data(connection, path,
1176 (void *) &data) == TRUE) {
1183 data = g_new0(struct generic_data, 1);
1184 data->conn = dbus_connection_ref(connection);
1185 data->path = g_strdup(path);
1188 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1190 if (!dbus_connection_register_object_path(connection, path,
1191 &generic_table, data)) {
1192 g_free(data->introspect);
1197 invalidate_parent_data(connection, path);
1199 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1200 NULL, NULL, data, NULL);
1202 /* Only root path export ObjectManager interface */
1203 if (data->parent == NULL)
1204 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1205 manager_methods, manager_signals,
1208 add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1209 properties_signals, NULL, data, NULL);
1214 static void object_path_unref(DBusConnection *connection, const char *path)
1216 struct generic_data *data = NULL;
1218 if (dbus_connection_get_object_path_data(connection, path,
1219 (void *) &data) == FALSE)
1227 if (data->refcount > 0)
1230 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1231 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1232 remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1234 invalidate_parent_data(data->conn, data->path);
1236 dbus_connection_unregister_object_path(data->conn, data->path);
1239 static gboolean check_signal(DBusConnection *conn, const char *path,
1240 const char *interface, const char *name,
1241 const GDBusArgInfo **args)
1243 struct generic_data *data = NULL;
1244 struct interface_data *iface;
1245 const GDBusSignalTable *signal;
1248 if (!dbus_connection_get_object_path_data(conn, path,
1249 (void *) &data) || data == NULL) {
1250 error("dbus_connection_emit_signal: path %s isn't registered",
1255 iface = find_interface(data->interfaces, interface);
1256 if (iface == NULL) {
1257 error("dbus_connection_emit_signal: %s does not implement %s",
1262 for (signal = iface->signals; signal && signal->name; signal++) {
1263 if (!strcmp(signal->name, name)) {
1264 *args = signal->args;
1269 error("No signal named %s on interface %s", name, interface);
1273 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1275 const char *interface,
1280 DBusMessage *signal;
1282 const GDBusArgInfo *args;
1284 if (!check_signal(conn, path, interface, name, &args))
1287 signal = dbus_message_new_signal(path, interface, name);
1288 if (signal == NULL) {
1289 error("Unable to allocate new %s.%s signal", interface, name);
1293 ret = dbus_message_append_args_valist(signal, first, var_args);
1297 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1298 error("%s.%s: got unexpected signature '%s'", interface, name,
1299 dbus_message_get_signature(signal));
1304 ret = dbus_connection_send(conn, signal, NULL);
1307 dbus_message_unref(signal);
1312 gboolean g_dbus_register_interface(DBusConnection *connection,
1313 const char *path, const char *name,
1314 const GDBusMethodTable *methods,
1315 const GDBusSignalTable *signals,
1316 const GDBusPropertyTable *properties,
1318 GDBusDestroyFunction destroy)
1320 struct generic_data *data;
1322 data = object_path_ref(connection, path);
1326 if (find_interface(data->interfaces, name)) {
1327 object_path_unref(connection, path);
1331 add_interface(data, name, methods, signals, properties, user_data,
1334 g_free(data->introspect);
1335 data->introspect = NULL;
1340 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1341 const char *path, const char *name)
1343 struct generic_data *data = NULL;
1348 if (dbus_connection_get_object_path_data(connection, path,
1349 (void *) &data) == FALSE)
1355 if (remove_interface(data, name) == FALSE)
1358 g_free(data->introspect);
1359 data->introspect = NULL;
1361 object_path_unref(connection, data->path);
1366 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1368 if (security_table != NULL)
1371 security_table = security;
1376 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1378 security_table = NULL;
1383 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1384 const char *format, va_list args)
1388 vsnprintf(str, sizeof(str), format, args);
1390 return dbus_message_new_error(message, name, str);
1393 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1394 const char *format, ...)
1399 va_start(args, format);
1401 reply = g_dbus_create_error_valist(message, name, format, args);
1408 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1409 int type, va_list args)
1413 reply = dbus_message_new_method_return(message);
1417 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1418 dbus_message_unref(reply);
1425 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1430 va_start(args, type);
1432 reply = g_dbus_create_reply_valist(message, type, args);
1439 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1443 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1444 dbus_message_set_no_reply(message, TRUE);
1446 result = dbus_connection_send(connection, message, NULL);
1448 dbus_message_unref(message);
1453 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1454 DBusMessage *message, int type, va_list args)
1458 reply = dbus_message_new_method_return(message);
1462 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1463 dbus_message_unref(reply);
1467 return g_dbus_send_message(connection, reply);
1470 gboolean g_dbus_send_reply(DBusConnection *connection,
1471 DBusMessage *message, int type, ...)
1476 va_start(args, type);
1478 result = g_dbus_send_reply_valist(connection, message, type, args);
1485 gboolean g_dbus_emit_signal(DBusConnection *connection,
1486 const char *path, const char *interface,
1487 const char *name, int type, ...)
1492 va_start(args, type);
1494 result = emit_signal_valist(connection, path, interface,
1502 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1503 const char *path, const char *interface,
1504 const char *name, int type, va_list args)
1506 return emit_signal_valist(connection, path, interface,
1510 static void process_properties_from_interface(struct generic_data *data,
1511 struct interface_data *iface)
1514 DBusMessage *signal;
1515 DBusMessageIter iter, dict, array;
1516 GSList *invalidated;
1518 if (iface->pending_prop == NULL)
1521 signal = dbus_message_new_signal(data->path,
1522 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1523 if (signal == NULL) {
1524 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1525 ".PropertiesChanged signal");
1529 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1531 dbus_message_iter_init_append(signal, &iter);
1532 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1533 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1534 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1535 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1536 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1540 for (l = iface->pending_prop; l != NULL; l = l->next) {
1541 GDBusPropertyTable *p = l->data;
1546 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1547 invalidated = g_slist_prepend(invalidated, p);
1551 append_property(iface, p, &dict);
1554 dbus_message_iter_close_container(&iter, &dict);
1556 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1557 DBUS_TYPE_STRING_AS_STRING, &array);
1558 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1559 GDBusPropertyTable *p = l->data;
1561 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1564 g_slist_free(invalidated);
1565 dbus_message_iter_close_container(&iter, &array);
1567 g_dbus_send_message(data->conn, signal);
1569 g_slist_free(iface->pending_prop);
1570 iface->pending_prop = NULL;
1573 static void process_property_changes(struct generic_data *data)
1577 for (l = data->interfaces; l != NULL; l = l->next) {
1578 struct interface_data *iface = l->data;
1580 process_properties_from_interface(data, iface);
1583 data->pending_prop = FALSE;
1586 void g_dbus_emit_property_changed(DBusConnection *connection,
1587 const char *path, const char *interface,
1590 const GDBusPropertyTable *property;
1591 struct generic_data *data;
1592 struct interface_data *iface;
1594 if (!dbus_connection_get_object_path_data(connection, path,
1595 (void **) &data) || data == NULL)
1598 iface = find_interface(data->interfaces, interface);
1602 property = find_property(iface->properties, name);
1603 if (property == NULL) {
1604 error("Could not find property %s in %p", name,
1609 data->pending_prop = TRUE;
1610 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1613 if (!data->process_id) {
1614 data->process_id = g_idle_add(process_changes, data);
1619 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1620 const char *interface, DBusMessageIter *iter)
1622 struct generic_data *data;
1623 struct interface_data *iface;
1625 if (!dbus_connection_get_object_path_data(connection, path,
1626 (void **) &data) || data == NULL)
1629 iface = find_interface(data->interfaces, interface);
1633 append_properties(iface, iter);