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;
52 struct generic_data *parent;
55 struct interface_data {
57 const GDBusMethodTable *methods;
58 const GDBusSignalTable *signals;
59 const GDBusPropertyTable *properties;
61 GDBusDestroyFunction destroy;
64 struct security_data {
65 GDBusPendingReply pending;
67 const GDBusMethodTable *method;
68 void *iface_user_data;
71 struct property_data {
72 GDBusPendingPropertySet id;
76 static gboolean process_changes(gpointer user_data);
78 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
79 const char *direction)
81 for (; args && args->name; args++) {
82 g_string_append_printf(gstr,
83 "\t\t\t<arg name=\"%s\" type=\"%s\"",
84 args->name, args->signature);
87 g_string_append_printf(gstr,
88 " direction=\"%s\"/>\n", direction);
90 g_string_append_printf(gstr, "/>\n");
95 #define G_DBUS_ANNOTATE(prefix_, name_, value_) \
96 prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
97 "value=\"" value_ "\"/>\n"
99 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
100 G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
102 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
103 G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
105 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
107 const GDBusMethodTable *method;
108 const GDBusSignalTable *signal;
109 const GDBusPropertyTable *property;
111 for (method = iface->methods; method && method->name; method++) {
112 gboolean deprecated = method->flags &
113 G_DBUS_METHOD_FLAG_DEPRECATED;
114 gboolean noreply = method->flags &
115 G_DBUS_METHOD_FLAG_NOREPLY;
117 if (!deprecated && !noreply &&
118 !(method->in_args && method->in_args->name) &&
119 !(method->out_args && method->out_args->name))
120 g_string_append_printf(gstr,
121 "\t\t<method name=\"%s\"/>\n",
124 g_string_append_printf(gstr,
125 "\t\t<method name=\"%s\">\n",
127 print_arguments(gstr, method->in_args, "in");
128 print_arguments(gstr, method->out_args, "out");
131 g_string_append_printf(gstr,
132 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
134 g_string_append_printf(gstr,
135 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
137 g_string_append_printf(gstr, "\t\t</method>\n");
141 for (signal = iface->signals; signal && signal->name; signal++) {
142 gboolean deprecated = signal->flags &
143 G_DBUS_SIGNAL_FLAG_DEPRECATED;
145 if (!deprecated && !(signal->args && signal->args->name))
146 g_string_append_printf(gstr,
147 "\t\t<signal name=\"%s\"/>\n",
150 g_string_append_printf(gstr,
151 "\t\t<signal name=\"%s\">\n",
153 print_arguments(gstr, signal->args, NULL);
156 g_string_append_printf(gstr,
157 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
159 g_string_append_printf(gstr, "\t\t</signal>\n");
163 for (property = iface->properties; property && property->name;
165 gboolean deprecated = property->flags &
166 G_DBUS_PROPERTY_FLAG_DEPRECATED;
168 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
169 " type=\"%s\" access=\"%s%s\"",
170 property->name, property->type,
171 property->get ? "read" : "",
172 property->set ? "write" : "");
175 g_string_append_printf(gstr, "/>\n");
177 g_string_append_printf(gstr,
178 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
182 static void generate_introspection_xml(DBusConnection *conn,
183 struct generic_data *data, const char *path)
190 g_free(data->introspect);
192 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
194 g_string_append_printf(gstr, "<node>\n");
196 for (list = data->interfaces; list; list = list->next) {
197 struct interface_data *iface = list->data;
199 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
202 generate_interface_xml(gstr, iface);
204 g_string_append_printf(gstr, "\t</interface>\n");
207 if (!dbus_connection_list_registered(conn, path, &children))
210 for (i = 0; children[i]; i++)
211 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
214 dbus_free_string_array(children);
217 g_string_append_printf(gstr, "</node>\n");
219 data->introspect = g_string_free(gstr, FALSE);
222 static DBusMessage *introspect(DBusConnection *connection,
223 DBusMessage *message, void *user_data)
225 struct generic_data *data = user_data;
228 if (data->introspect == NULL)
229 generate_introspection_xml(connection, data,
230 dbus_message_get_path(message));
232 reply = dbus_message_new_method_return(message);
236 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
242 static DBusHandlerResult process_message(DBusConnection *connection,
243 DBusMessage *message, const GDBusMethodTable *method,
244 void *iface_user_data)
248 reply = method->function(connection, message, iface_user_data);
250 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
252 dbus_message_unref(reply);
253 return DBUS_HANDLER_RESULT_HANDLED;
256 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
258 return DBUS_HANDLER_RESULT_HANDLED;
262 return DBUS_HANDLER_RESULT_NEED_MEMORY;
264 dbus_connection_send(connection, reply, NULL);
265 dbus_message_unref(reply);
267 return DBUS_HANDLER_RESULT_HANDLED;
270 static GDBusPendingReply next_pending = 1;
271 static GSList *pending_security = NULL;
273 static const GDBusSecurityTable *security_table = NULL;
275 void g_dbus_pending_success(DBusConnection *connection,
276 GDBusPendingReply pending)
280 for (list = pending_security; list; list = list->next) {
281 struct security_data *secdata = list->data;
283 if (secdata->pending != pending)
286 pending_security = g_slist_remove(pending_security, secdata);
288 process_message(connection, secdata->message,
289 secdata->method, secdata->iface_user_data);
291 dbus_message_unref(secdata->message);
297 void g_dbus_pending_error_valist(DBusConnection *connection,
298 GDBusPendingReply pending, const char *name,
299 const char *format, va_list args)
303 for (list = pending_security; list; list = list->next) {
304 struct security_data *secdata = list->data;
307 if (secdata->pending != pending)
310 pending_security = g_slist_remove(pending_security, secdata);
312 reply = g_dbus_create_error_valist(secdata->message,
315 dbus_connection_send(connection, reply, NULL);
316 dbus_message_unref(reply);
319 dbus_message_unref(secdata->message);
325 void g_dbus_pending_error(DBusConnection *connection,
326 GDBusPendingReply pending,
327 const char *name, const char *format, ...)
331 va_start(args, format);
333 g_dbus_pending_error_valist(connection, pending, name, format, args);
338 int polkit_check_authorization(DBusConnection *conn,
339 const char *action, gboolean interaction,
340 void (*function) (dbus_bool_t authorized,
342 void *user_data, int timeout);
344 struct builtin_security_data {
345 DBusConnection *conn;
346 GDBusPendingReply pending;
349 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
351 struct builtin_security_data *data = user_data;
353 if (authorized == TRUE)
354 g_dbus_pending_success(data->conn, data->pending);
356 g_dbus_pending_error(data->conn, data->pending,
357 DBUS_ERROR_AUTH_FAILED, NULL);
362 static void builtin_security_function(DBusConnection *conn,
364 gboolean interaction,
365 GDBusPendingReply pending)
367 struct builtin_security_data *data;
369 data = g_new0(struct builtin_security_data, 1);
371 data->pending = pending;
373 if (polkit_check_authorization(conn, action, interaction,
374 builtin_security_result, data, 30000) < 0)
375 g_dbus_pending_error(conn, pending, NULL, NULL);
378 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
379 const GDBusMethodTable *method, void *iface_user_data)
381 const GDBusSecurityTable *security;
383 for (security = security_table; security && security->privilege;
385 struct security_data *secdata;
386 gboolean interaction;
388 if (security->privilege != method->privilege)
391 secdata = g_new(struct security_data, 1);
392 secdata->pending = next_pending++;
393 secdata->message = dbus_message_ref(msg);
394 secdata->method = method;
395 secdata->iface_user_data = iface_user_data;
397 pending_security = g_slist_prepend(pending_security, secdata);
399 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
404 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
406 security->function(conn, security->action,
407 interaction, secdata->pending);
409 builtin_security_function(conn, security->action,
410 interaction, secdata->pending);
418 static GDBusPendingPropertySet next_pending_property = 1;
419 static GSList *pending_property_set;
421 static struct property_data *remove_pending_property_data(
422 GDBusPendingPropertySet id)
424 struct property_data *propdata;
427 for (l = pending_property_set; l != NULL; l = l->next) {
429 if (propdata->id != id)
436 pending_property_set = g_slist_delete_link(pending_property_set, l);
441 void g_dbus_pending_property_success(DBusConnection *connection,
442 GDBusPendingPropertySet id)
444 struct property_data *propdata;
446 propdata = remove_pending_property_data(id);
447 if (propdata == NULL)
450 g_dbus_send_reply(connection, propdata->message, DBUS_TYPE_INVALID);
451 dbus_message_unref(propdata->message);
455 void g_dbus_pending_property_error_valist(DBusConnection *connection,
456 GDBusPendingReply id, const char *name,
457 const char *format, va_list args)
459 struct property_data *propdata;
462 propdata = remove_pending_property_data(id);
463 if (propdata == NULL)
466 reply = g_dbus_create_error_valist(propdata->message, name, format,
469 dbus_connection_send(connection, reply, NULL);
470 dbus_message_unref(reply);
473 dbus_message_unref(propdata->message);
477 void g_dbus_pending_property_error(DBusConnection *connection,
478 GDBusPendingReply id, const char *name,
479 const char *format, ...)
483 va_start(args, format);
485 g_dbus_pending_property_error_valist(connection, id, name, format,
491 static void reset_parent(gpointer data, gpointer user_data)
493 struct generic_data *child = data;
494 struct generic_data *parent = user_data;
496 child->parent = parent;
499 static void append_properties(struct interface_data *data,
500 DBusMessageIter *iter)
502 DBusMessageIter dict;
504 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
505 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
506 DBUS_TYPE_STRING_AS_STRING
507 DBUS_TYPE_VARIANT_AS_STRING
508 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
510 /* TODO: list properties */
512 dbus_message_iter_close_container(iter, &dict);
515 static void append_interface(gpointer data, gpointer user_data)
517 struct interface_data *iface = data;
518 DBusMessageIter *array = user_data;
519 DBusMessageIter entry;
521 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
523 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
524 append_properties(data, &entry);
525 dbus_message_iter_close_container(array, &entry);
528 static void emit_interfaces_added(struct generic_data *data)
531 DBusMessageIter iter, array;
532 struct generic_data *parent = data->parent;
538 while (parent->parent)
539 parent = parent->parent;
541 signal = dbus_message_new_signal(parent->path,
542 DBUS_INTERFACE_OBJECT_MANAGER,
547 dbus_message_iter_init_append(signal, &iter);
548 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
551 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
552 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
553 DBUS_TYPE_STRING_AS_STRING
554 DBUS_TYPE_ARRAY_AS_STRING
555 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
556 DBUS_TYPE_STRING_AS_STRING
557 DBUS_TYPE_VARIANT_AS_STRING
558 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
559 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
561 g_slist_foreach(data->added, append_interface, &array);
562 g_slist_free(data->added);
565 dbus_message_iter_close_container(&iter, &array);
567 g_dbus_send_message(data->conn, signal);
570 static struct interface_data *find_interface(GSList *interfaces,
578 for (list = interfaces; list; list = list->next) {
579 struct interface_data *iface = list->data;
580 if (!strcmp(name, iface->name))
587 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
588 DBusMessage *message)
590 const char *sig = dbus_message_get_signature(message);
591 const char *p = NULL;
593 for (; args && args->signature && *sig; args++) {
596 for (; *sig && *p; sig++, p++) {
602 if (*sig || (p && *p) || (args && args->signature))
608 static gboolean remove_interface(struct generic_data *data, const char *name)
610 struct interface_data *iface;
612 iface = find_interface(data->interfaces, name);
616 data->interfaces = g_slist_remove(data->interfaces, iface);
618 if (iface->destroy) {
619 iface->destroy(iface->user_data);
620 iface->user_data = NULL;
623 if (data->parent == NULL) {
629 data->removed = g_slist_prepend(data->removed, iface->name);
632 if (data->process_id > 0)
635 data->process_id = g_idle_add(process_changes, data);
640 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
641 const char *child_path)
643 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
644 char *parent_path, *slash;
646 parent_path = g_strdup(child_path);
647 slash = strrchr(parent_path, '/');
651 if (slash == parent_path && parent_path[1] != '\0')
652 parent_path[1] = '\0';
656 if (!strlen(parent_path))
659 if (dbus_connection_get_object_path_data(conn, parent_path,
660 (void *) &data) == FALSE) {
664 parent = invalidate_parent_data(conn, parent_path);
672 g_free(data->introspect);
673 data->introspect = NULL;
675 if (!dbus_connection_get_object_path_data(conn, child_path,
679 if (child == NULL || g_slist_find(data->objects, child) != NULL)
682 data->objects = g_slist_prepend(data->objects, child);
683 child->parent = data;
690 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
693 const GDBusPropertyTable *p;
695 for (p = properties; p && p->name; p++) {
696 if (strcmp(name, p->name) == 0)
703 static DBusMessage *properties_get(DBusConnection *connection,
704 DBusMessage *message, void *user_data)
706 struct generic_data *data = user_data;
707 struct interface_data *iface;
708 const GDBusPropertyTable *property;
709 const char *interface, *name;
710 DBusMessageIter iter, value;
713 if (!dbus_message_get_args(message, NULL,
714 DBUS_TYPE_STRING, &interface,
715 DBUS_TYPE_STRING, &name,
719 iface = find_interface(data->interfaces, interface);
721 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
722 "No such interface '%s'", interface);
724 property = find_property(iface->properties, name);
725 if (property == NULL)
726 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
727 "No such property '%s'", name);
729 if (property->exists != NULL &&
730 !property->exists(property, iface->user_data))
731 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
732 "No such property '%s'", name);
734 if (property->get == NULL)
735 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
736 "Property '%s' is not readable", name);
738 reply = dbus_message_new_method_return(message);
742 dbus_message_iter_init_append(reply, &iter);
743 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
744 property->type, &value);
746 if (!property->get(property, &value, iface->user_data)) {
747 dbus_message_unref(reply);
751 dbus_message_iter_close_container(&iter, &value);
756 static DBusMessage *properties_get_all(DBusConnection *connection,
757 DBusMessage *message, void *user_data)
759 struct generic_data *data = user_data;
760 struct interface_data *iface;
761 const GDBusPropertyTable *p;
762 const char *interface;
763 DBusMessageIter iter, dict;
766 if (!dbus_message_get_args(message, NULL,
767 DBUS_TYPE_STRING, &interface,
771 iface = find_interface(data->interfaces, interface);
773 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
774 "No such interface '%s'", interface);
776 reply = dbus_message_new_method_return(message);
780 dbus_message_iter_init_append(reply, &iter);
781 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
782 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
783 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
784 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
786 for (p = iface->properties; p && p->name; p++) {
787 DBusMessageIter entry, value;
792 if (p->exists != NULL && !p->exists(p, iface->user_data))
795 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
797 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
799 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
802 if (!p->get(p, &value, iface->user_data)) {
803 dbus_message_unref(reply);
807 dbus_message_iter_close_container(&entry, &value);
808 dbus_message_iter_close_container(&dict, &entry);
811 dbus_message_iter_close_container(&iter, &dict);
816 static DBusMessage *properties_set(DBusConnection *connection,
817 DBusMessage *message, void *user_data)
819 struct generic_data *data = user_data;
820 DBusMessageIter iter, sub;
821 struct interface_data *iface;
822 const GDBusPropertyTable *property;
823 const char *name, *interface;
824 struct property_data *propdata;
826 if (!dbus_message_iter_init(message, &iter))
827 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
828 "No arguments given");
830 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
831 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
832 "Invalid argument type: '%c'",
833 dbus_message_iter_get_arg_type(&iter));
835 dbus_message_iter_get_basic(&iter, &name);
836 dbus_message_iter_next(&iter);
838 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
839 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
840 "Invalid argument type: '%c'",
841 dbus_message_iter_get_arg_type(&iter));
843 dbus_message_iter_get_basic(&iter, &interface);
844 dbus_message_iter_next(&iter);
846 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
847 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
848 "Invalid argument type: '%c'",
849 dbus_message_iter_get_arg_type(&iter));
851 dbus_message_iter_recurse(&iter, &sub);
853 iface = find_interface(data->interfaces, interface);
855 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
856 "No such interface '%s'", interface);
858 property = find_property(iface->properties, name);
859 if (property == NULL)
860 return g_dbus_create_error(message,
861 DBUS_ERROR_UNKNOWN_PROPERTY,
862 "No such property '%s'", name);
864 if (property->set == NULL)
865 return g_dbus_create_error(message,
866 DBUS_ERROR_PROPERTY_READ_ONLY,
867 "Property '%s' is not writable", name);
869 if (property->exists != NULL &&
870 !property->exists(property, iface->user_data))
871 return g_dbus_create_error(message,
872 DBUS_ERROR_UNKNOWN_PROPERTY,
873 "No such property '%s'", name);
875 propdata = g_new(struct property_data, 1);
876 propdata->id = next_pending_property++;
877 propdata->message = dbus_message_ref(message);
879 property->set(property, &sub, propdata->id, iface->user_data);
884 static const GDBusMethodTable properties_methods[] = {
885 { GDBUS_METHOD("Get",
886 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
887 GDBUS_ARGS({ "value", "v" }),
889 { GDBUS_ASYNC_METHOD("Set", NULL,
890 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
893 { GDBUS_METHOD("GetAll",
894 GDBUS_ARGS({ "interface", "s" }),
895 GDBUS_ARGS({ "properties", "a{sv}" }),
896 properties_get_all) },
900 static const GDBusSignalTable properties_signals[] = {
901 { GDBUS_SIGNAL("PropertiesChanged",
902 GDBUS_ARGS({ "interface", "s" },
903 { "changed_properties", "a{sv}" },
904 { "invalidated_properties", "as"})) },
908 static void append_name(gpointer data, gpointer user_data)
911 DBusMessageIter *iter = user_data;
913 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
916 static void emit_interfaces_removed(struct generic_data *data)
919 DBusMessageIter iter, array;
920 struct generic_data *parent = data->parent;
926 while (parent->parent)
927 parent = parent->parent;
929 signal = dbus_message_new_signal(parent->path,
930 DBUS_INTERFACE_OBJECT_MANAGER,
931 "InterfacesRemoved");
935 dbus_message_iter_init_append(signal, &iter);
936 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
938 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
939 DBUS_TYPE_STRING_AS_STRING, &array);
941 g_slist_foreach(data->removed, append_name, &array);
942 g_slist_free_full(data->removed, g_free);
943 data->removed = NULL;
945 dbus_message_iter_close_container(&iter, &array);
947 g_dbus_send_message(data->conn, signal);
950 static gboolean process_changes(gpointer user_data)
952 struct generic_data *data = user_data;
954 data->process_id = 0;
956 if (data->added != NULL)
957 emit_interfaces_added(data);
959 if (data->removed != NULL)
960 emit_interfaces_removed(data);
965 static void generic_unregister(DBusConnection *connection, void *user_data)
967 struct generic_data *data = user_data;
968 struct generic_data *parent = data->parent;
971 parent->objects = g_slist_remove(parent->objects, data);
973 if (data->process_id > 0) {
974 g_source_remove(data->process_id);
975 if (data->removed != NULL)
976 emit_interfaces_removed(data);
979 g_slist_foreach(data->objects, reset_parent, data->parent);
980 g_slist_free(data->objects);
982 dbus_connection_unref(data->conn);
983 g_free(data->introspect);
988 static DBusHandlerResult generic_message(DBusConnection *connection,
989 DBusMessage *message, void *user_data)
991 struct generic_data *data = user_data;
992 struct interface_data *iface;
993 const GDBusMethodTable *method;
994 const char *interface;
996 interface = dbus_message_get_interface(message);
998 iface = find_interface(data->interfaces, interface);
1000 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1002 for (method = iface->methods; method &&
1003 method->name && method->function; method++) {
1004 if (dbus_message_is_method_call(message, iface->name,
1005 method->name) == FALSE)
1008 if (g_dbus_args_have_signature(method->in_args,
1012 if (check_privilege(connection, message, method,
1013 iface->user_data) == TRUE)
1014 return DBUS_HANDLER_RESULT_HANDLED;
1016 return process_message(connection, message, method,
1020 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1023 static DBusObjectPathVTable generic_table = {
1024 .unregister_function = generic_unregister,
1025 .message_function = generic_message,
1028 static const GDBusMethodTable introspect_methods[] = {
1029 { GDBUS_METHOD("Introspect", NULL,
1030 GDBUS_ARGS({ "xml", "s" }), introspect) },
1034 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1036 DBusMessageIter array;
1038 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1039 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1040 DBUS_TYPE_STRING_AS_STRING
1041 DBUS_TYPE_ARRAY_AS_STRING
1042 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1043 DBUS_TYPE_STRING_AS_STRING
1044 DBUS_TYPE_VARIANT_AS_STRING
1045 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1046 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1048 g_slist_foreach(data->interfaces, append_interface, &array);
1050 dbus_message_iter_close_container(iter, &array);
1053 static void append_object(gpointer data, gpointer user_data)
1055 struct generic_data *child = data;
1056 DBusMessageIter *array = user_data;
1057 DBusMessageIter entry;
1059 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1061 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1063 append_interfaces(child, &entry);
1064 dbus_message_iter_close_container(array, &entry);
1066 g_slist_foreach(child->objects, append_object, user_data);
1069 static DBusMessage *get_objects(DBusConnection *connection,
1070 DBusMessage *message, void *user_data)
1072 struct generic_data *data = user_data;
1074 DBusMessageIter iter;
1075 DBusMessageIter array;
1077 reply = dbus_message_new_method_return(message);
1081 dbus_message_iter_init_append(reply, &iter);
1083 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1084 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1085 DBUS_TYPE_OBJECT_PATH_AS_STRING
1086 DBUS_TYPE_ARRAY_AS_STRING
1087 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1088 DBUS_TYPE_STRING_AS_STRING
1089 DBUS_TYPE_ARRAY_AS_STRING
1090 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1091 DBUS_TYPE_STRING_AS_STRING
1092 DBUS_TYPE_VARIANT_AS_STRING
1093 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1094 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1095 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1098 g_slist_foreach(data->objects, append_object, &array);
1100 dbus_message_iter_close_container(&iter, &array);
1105 static const GDBusMethodTable manager_methods[] = {
1106 { GDBUS_METHOD("GetManagedObjects", NULL,
1107 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1111 static const GDBusSignalTable manager_signals[] = {
1112 { GDBUS_SIGNAL("InterfacesAdded",
1113 GDBUS_ARGS({ "object", "o" },
1114 { "interfaces", "a{sa{sv}}" })) },
1115 { GDBUS_SIGNAL("InterfacesRemoved",
1116 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1120 static void add_interface(struct generic_data *data,
1122 const GDBusMethodTable *methods,
1123 const GDBusSignalTable *signals,
1124 const GDBusPropertyTable *properties,
1126 GDBusDestroyFunction destroy)
1128 struct interface_data *iface;
1130 iface = g_new0(struct interface_data, 1);
1131 iface->name = g_strdup(name);
1132 iface->methods = methods;
1133 iface->signals = signals;
1134 iface->properties = properties;
1135 iface->user_data = user_data;
1136 iface->destroy = destroy;
1138 data->interfaces = g_slist_append(data->interfaces, iface);
1139 if (data->parent == NULL)
1142 data->added = g_slist_append(data->added, iface);
1143 if (data->process_id > 0)
1146 data->process_id = g_idle_add(process_changes, data);
1149 static struct generic_data *object_path_ref(DBusConnection *connection,
1152 struct generic_data *data;
1154 if (dbus_connection_get_object_path_data(connection, path,
1155 (void *) &data) == TRUE) {
1162 data = g_new0(struct generic_data, 1);
1163 data->conn = dbus_connection_ref(connection);
1164 data->path = g_strdup(path);
1167 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1169 if (!dbus_connection_register_object_path(connection, path,
1170 &generic_table, data)) {
1171 g_free(data->introspect);
1176 invalidate_parent_data(connection, path);
1178 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1179 NULL, NULL, data, NULL);
1181 /* Only root path export ObjectManager interface */
1182 if (data->parent == NULL)
1183 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1184 manager_methods, manager_signals,
1187 add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1188 properties_signals, NULL, data, NULL);
1193 static void object_path_unref(DBusConnection *connection, const char *path)
1195 struct generic_data *data = NULL;
1197 if (dbus_connection_get_object_path_data(connection, path,
1198 (void *) &data) == FALSE)
1206 if (data->refcount > 0)
1209 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1210 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1211 remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1213 invalidate_parent_data(data->conn, data->path);
1215 dbus_connection_unregister_object_path(data->conn, data->path);
1218 static gboolean check_signal(DBusConnection *conn, const char *path,
1219 const char *interface, const char *name,
1220 const GDBusArgInfo **args)
1222 struct generic_data *data = NULL;
1223 struct interface_data *iface;
1224 const GDBusSignalTable *signal;
1227 if (!dbus_connection_get_object_path_data(conn, path,
1228 (void *) &data) || data == NULL) {
1229 error("dbus_connection_emit_signal: path %s isn't registered",
1234 iface = find_interface(data->interfaces, interface);
1235 if (iface == NULL) {
1236 error("dbus_connection_emit_signal: %s does not implement %s",
1241 for (signal = iface->signals; signal && signal->name; signal++) {
1242 if (!strcmp(signal->name, name)) {
1243 *args = signal->args;
1248 error("No signal named %s on interface %s", name, interface);
1252 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1254 const char *interface,
1259 DBusMessage *signal;
1261 const GDBusArgInfo *args;
1263 if (!check_signal(conn, path, interface, name, &args))
1266 signal = dbus_message_new_signal(path, interface, name);
1267 if (signal == NULL) {
1268 error("Unable to allocate new %s.%s signal", interface, name);
1272 ret = dbus_message_append_args_valist(signal, first, var_args);
1276 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1277 error("%s.%s: got unexpected signature '%s'", interface, name,
1278 dbus_message_get_signature(signal));
1283 ret = dbus_connection_send(conn, signal, NULL);
1286 dbus_message_unref(signal);
1291 gboolean g_dbus_register_interface(DBusConnection *connection,
1292 const char *path, const char *name,
1293 const GDBusMethodTable *methods,
1294 const GDBusSignalTable *signals,
1295 const GDBusPropertyTable *properties,
1297 GDBusDestroyFunction destroy)
1299 struct generic_data *data;
1301 data = object_path_ref(connection, path);
1305 if (find_interface(data->interfaces, name)) {
1306 object_path_unref(connection, path);
1310 add_interface(data, name, methods, signals, properties, user_data,
1313 g_free(data->introspect);
1314 data->introspect = NULL;
1319 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1320 const char *path, const char *name)
1322 struct generic_data *data = NULL;
1327 if (dbus_connection_get_object_path_data(connection, path,
1328 (void *) &data) == FALSE)
1334 if (remove_interface(data, name) == FALSE)
1337 g_free(data->introspect);
1338 data->introspect = NULL;
1340 object_path_unref(connection, data->path);
1345 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1347 if (security_table != NULL)
1350 security_table = security;
1355 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1357 security_table = NULL;
1362 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1363 const char *format, va_list args)
1367 vsnprintf(str, sizeof(str), format, args);
1369 return dbus_message_new_error(message, name, str);
1372 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1373 const char *format, ...)
1378 va_start(args, format);
1380 reply = g_dbus_create_error_valist(message, name, format, args);
1387 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1388 int type, va_list args)
1392 reply = dbus_message_new_method_return(message);
1396 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1397 dbus_message_unref(reply);
1404 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1409 va_start(args, type);
1411 reply = g_dbus_create_reply_valist(message, type, args);
1418 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1422 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1423 dbus_message_set_no_reply(message, TRUE);
1425 result = dbus_connection_send(connection, message, NULL);
1427 dbus_message_unref(message);
1432 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1433 DBusMessage *message, int type, va_list args)
1437 reply = dbus_message_new_method_return(message);
1441 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1442 dbus_message_unref(reply);
1446 return g_dbus_send_message(connection, reply);
1449 gboolean g_dbus_send_reply(DBusConnection *connection,
1450 DBusMessage *message, int type, ...)
1455 va_start(args, type);
1457 result = g_dbus_send_reply_valist(connection, message, type, args);
1464 gboolean g_dbus_emit_signal(DBusConnection *connection,
1465 const char *path, const char *interface,
1466 const char *name, int type, ...)
1471 va_start(args, type);
1473 result = emit_signal_valist(connection, path, interface,
1481 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1482 const char *path, const char *interface,
1483 const char *name, int type, va_list args)
1485 return emit_signal_valist(connection, path, interface,