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_property(struct interface_data *iface,
500 const GDBusPropertyTable *p, DBusMessageIter *dict)
502 DBusMessageIter entry, value;
504 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
506 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
507 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
510 p->get(p, &value, iface->user_data);
512 dbus_message_iter_close_container(&entry, &value);
513 dbus_message_iter_close_container(dict, &entry);
516 static void append_properties(struct interface_data *data,
517 DBusMessageIter *iter)
519 DBusMessageIter dict;
520 const GDBusPropertyTable *p;
522 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
523 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
524 DBUS_TYPE_STRING_AS_STRING
525 DBUS_TYPE_VARIANT_AS_STRING
526 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
528 for (p = data->properties; p && p->name; p++) {
532 if (p->exists != NULL && !p->exists(p, data->user_data))
535 append_property(data, p, &dict);
538 dbus_message_iter_close_container(iter, &dict);
541 static void append_interface(gpointer data, gpointer user_data)
543 struct interface_data *iface = data;
544 DBusMessageIter *array = user_data;
545 DBusMessageIter entry;
547 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
549 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
550 append_properties(data, &entry);
551 dbus_message_iter_close_container(array, &entry);
554 static void emit_interfaces_added(struct generic_data *data)
557 DBusMessageIter iter, array;
558 struct generic_data *parent = data->parent;
564 while (parent->parent)
565 parent = parent->parent;
567 signal = dbus_message_new_signal(parent->path,
568 DBUS_INTERFACE_OBJECT_MANAGER,
573 dbus_message_iter_init_append(signal, &iter);
574 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
577 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
578 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
579 DBUS_TYPE_STRING_AS_STRING
580 DBUS_TYPE_ARRAY_AS_STRING
581 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
582 DBUS_TYPE_STRING_AS_STRING
583 DBUS_TYPE_VARIANT_AS_STRING
584 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
585 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
587 g_slist_foreach(data->added, append_interface, &array);
588 g_slist_free(data->added);
591 dbus_message_iter_close_container(&iter, &array);
593 g_dbus_send_message(data->conn, signal);
596 static struct interface_data *find_interface(GSList *interfaces,
604 for (list = interfaces; list; list = list->next) {
605 struct interface_data *iface = list->data;
606 if (!strcmp(name, iface->name))
613 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
614 DBusMessage *message)
616 const char *sig = dbus_message_get_signature(message);
617 const char *p = NULL;
619 for (; args && args->signature && *sig; args++) {
622 for (; *sig && *p; sig++, p++) {
628 if (*sig || (p && *p) || (args && args->signature))
634 static gboolean remove_interface(struct generic_data *data, const char *name)
636 struct interface_data *iface;
638 iface = find_interface(data->interfaces, name);
642 data->interfaces = g_slist_remove(data->interfaces, iface);
644 if (iface->destroy) {
645 iface->destroy(iface->user_data);
646 iface->user_data = NULL;
649 if (data->parent == NULL) {
655 data->removed = g_slist_prepend(data->removed, iface->name);
658 if (data->process_id > 0)
661 data->process_id = g_idle_add(process_changes, data);
666 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
667 const char *child_path)
669 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
670 char *parent_path, *slash;
672 parent_path = g_strdup(child_path);
673 slash = strrchr(parent_path, '/');
677 if (slash == parent_path && parent_path[1] != '\0')
678 parent_path[1] = '\0';
682 if (!strlen(parent_path))
685 if (dbus_connection_get_object_path_data(conn, parent_path,
686 (void *) &data) == FALSE) {
690 parent = invalidate_parent_data(conn, parent_path);
698 g_free(data->introspect);
699 data->introspect = NULL;
701 if (!dbus_connection_get_object_path_data(conn, child_path,
705 if (child == NULL || g_slist_find(data->objects, child) != NULL)
708 data->objects = g_slist_prepend(data->objects, child);
709 child->parent = data;
716 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
719 const GDBusPropertyTable *p;
721 for (p = properties; p && p->name; p++) {
722 if (strcmp(name, p->name) == 0)
729 static DBusMessage *properties_get(DBusConnection *connection,
730 DBusMessage *message, void *user_data)
732 struct generic_data *data = user_data;
733 struct interface_data *iface;
734 const GDBusPropertyTable *property;
735 const char *interface, *name;
736 DBusMessageIter iter, value;
739 if (!dbus_message_get_args(message, NULL,
740 DBUS_TYPE_STRING, &interface,
741 DBUS_TYPE_STRING, &name,
745 iface = find_interface(data->interfaces, interface);
747 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
748 "No such interface '%s'", interface);
750 property = find_property(iface->properties, name);
751 if (property == NULL)
752 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
753 "No such property '%s'", name);
755 if (property->exists != NULL &&
756 !property->exists(property, iface->user_data))
757 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
758 "No such property '%s'", name);
760 if (property->get == NULL)
761 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
762 "Property '%s' is not readable", name);
764 reply = dbus_message_new_method_return(message);
768 dbus_message_iter_init_append(reply, &iter);
769 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
770 property->type, &value);
772 if (!property->get(property, &value, iface->user_data)) {
773 dbus_message_unref(reply);
777 dbus_message_iter_close_container(&iter, &value);
782 static DBusMessage *properties_get_all(DBusConnection *connection,
783 DBusMessage *message, void *user_data)
785 struct generic_data *data = user_data;
786 struct interface_data *iface;
787 const char *interface;
788 DBusMessageIter iter;
791 if (!dbus_message_get_args(message, NULL,
792 DBUS_TYPE_STRING, &interface,
796 iface = find_interface(data->interfaces, interface);
798 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
799 "No such interface '%s'", interface);
801 reply = dbus_message_new_method_return(message);
805 dbus_message_iter_init_append(reply, &iter);
807 append_properties(iface, &iter);
812 static DBusMessage *properties_set(DBusConnection *connection,
813 DBusMessage *message, void *user_data)
815 struct generic_data *data = user_data;
816 DBusMessageIter iter, sub;
817 struct interface_data *iface;
818 const GDBusPropertyTable *property;
819 const char *name, *interface;
820 struct property_data *propdata;
822 if (!dbus_message_iter_init(message, &iter))
823 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
824 "No arguments given");
826 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
827 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
828 "Invalid argument type: '%c'",
829 dbus_message_iter_get_arg_type(&iter));
831 dbus_message_iter_get_basic(&iter, &name);
832 dbus_message_iter_next(&iter);
834 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
835 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
836 "Invalid argument type: '%c'",
837 dbus_message_iter_get_arg_type(&iter));
839 dbus_message_iter_get_basic(&iter, &interface);
840 dbus_message_iter_next(&iter);
842 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
843 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
844 "Invalid argument type: '%c'",
845 dbus_message_iter_get_arg_type(&iter));
847 dbus_message_iter_recurse(&iter, &sub);
849 iface = find_interface(data->interfaces, interface);
851 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
852 "No such interface '%s'", interface);
854 property = find_property(iface->properties, name);
855 if (property == NULL)
856 return g_dbus_create_error(message,
857 DBUS_ERROR_UNKNOWN_PROPERTY,
858 "No such property '%s'", name);
860 if (property->set == NULL)
861 return g_dbus_create_error(message,
862 DBUS_ERROR_PROPERTY_READ_ONLY,
863 "Property '%s' is not writable", name);
865 if (property->exists != NULL &&
866 !property->exists(property, iface->user_data))
867 return g_dbus_create_error(message,
868 DBUS_ERROR_UNKNOWN_PROPERTY,
869 "No such property '%s'", name);
871 propdata = g_new(struct property_data, 1);
872 propdata->id = next_pending_property++;
873 propdata->message = dbus_message_ref(message);
875 property->set(property, &sub, propdata->id, iface->user_data);
880 static const GDBusMethodTable properties_methods[] = {
881 { GDBUS_METHOD("Get",
882 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
883 GDBUS_ARGS({ "value", "v" }),
885 { GDBUS_ASYNC_METHOD("Set", NULL,
886 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
889 { GDBUS_METHOD("GetAll",
890 GDBUS_ARGS({ "interface", "s" }),
891 GDBUS_ARGS({ "properties", "a{sv}" }),
892 properties_get_all) },
896 static const GDBusSignalTable properties_signals[] = {
897 { GDBUS_SIGNAL("PropertiesChanged",
898 GDBUS_ARGS({ "interface", "s" },
899 { "changed_properties", "a{sv}" },
900 { "invalidated_properties", "as"})) },
904 static void append_name(gpointer data, gpointer user_data)
907 DBusMessageIter *iter = user_data;
909 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
912 static void emit_interfaces_removed(struct generic_data *data)
915 DBusMessageIter iter, array;
916 struct generic_data *parent = data->parent;
922 while (parent->parent)
923 parent = parent->parent;
925 signal = dbus_message_new_signal(parent->path,
926 DBUS_INTERFACE_OBJECT_MANAGER,
927 "InterfacesRemoved");
931 dbus_message_iter_init_append(signal, &iter);
932 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
934 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
935 DBUS_TYPE_STRING_AS_STRING, &array);
937 g_slist_foreach(data->removed, append_name, &array);
938 g_slist_free_full(data->removed, g_free);
939 data->removed = NULL;
941 dbus_message_iter_close_container(&iter, &array);
943 g_dbus_send_message(data->conn, signal);
946 static gboolean process_changes(gpointer user_data)
948 struct generic_data *data = user_data;
950 data->process_id = 0;
952 if (data->added != NULL)
953 emit_interfaces_added(data);
955 if (data->removed != NULL)
956 emit_interfaces_removed(data);
961 static void generic_unregister(DBusConnection *connection, void *user_data)
963 struct generic_data *data = user_data;
964 struct generic_data *parent = data->parent;
967 parent->objects = g_slist_remove(parent->objects, data);
969 if (data->process_id > 0) {
970 g_source_remove(data->process_id);
971 if (data->removed != NULL)
972 emit_interfaces_removed(data);
975 g_slist_foreach(data->objects, reset_parent, data->parent);
976 g_slist_free(data->objects);
978 dbus_connection_unref(data->conn);
979 g_free(data->introspect);
984 static DBusHandlerResult generic_message(DBusConnection *connection,
985 DBusMessage *message, void *user_data)
987 struct generic_data *data = user_data;
988 struct interface_data *iface;
989 const GDBusMethodTable *method;
990 const char *interface;
992 interface = dbus_message_get_interface(message);
994 iface = find_interface(data->interfaces, interface);
996 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
998 for (method = iface->methods; method &&
999 method->name && method->function; method++) {
1000 if (dbus_message_is_method_call(message, iface->name,
1001 method->name) == FALSE)
1004 if (g_dbus_args_have_signature(method->in_args,
1008 if (check_privilege(connection, message, method,
1009 iface->user_data) == TRUE)
1010 return DBUS_HANDLER_RESULT_HANDLED;
1012 return process_message(connection, message, method,
1016 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1019 static DBusObjectPathVTable generic_table = {
1020 .unregister_function = generic_unregister,
1021 .message_function = generic_message,
1024 static const GDBusMethodTable introspect_methods[] = {
1025 { GDBUS_METHOD("Introspect", NULL,
1026 GDBUS_ARGS({ "xml", "s" }), introspect) },
1030 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1032 DBusMessageIter array;
1034 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1035 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1036 DBUS_TYPE_STRING_AS_STRING
1037 DBUS_TYPE_ARRAY_AS_STRING
1038 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1039 DBUS_TYPE_STRING_AS_STRING
1040 DBUS_TYPE_VARIANT_AS_STRING
1041 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1042 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1044 g_slist_foreach(data->interfaces, append_interface, &array);
1046 dbus_message_iter_close_container(iter, &array);
1049 static void append_object(gpointer data, gpointer user_data)
1051 struct generic_data *child = data;
1052 DBusMessageIter *array = user_data;
1053 DBusMessageIter entry;
1055 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1057 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1059 append_interfaces(child, &entry);
1060 dbus_message_iter_close_container(array, &entry);
1062 g_slist_foreach(child->objects, append_object, user_data);
1065 static DBusMessage *get_objects(DBusConnection *connection,
1066 DBusMessage *message, void *user_data)
1068 struct generic_data *data = user_data;
1070 DBusMessageIter iter;
1071 DBusMessageIter array;
1073 reply = dbus_message_new_method_return(message);
1077 dbus_message_iter_init_append(reply, &iter);
1079 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1080 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1081 DBUS_TYPE_OBJECT_PATH_AS_STRING
1082 DBUS_TYPE_ARRAY_AS_STRING
1083 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1084 DBUS_TYPE_STRING_AS_STRING
1085 DBUS_TYPE_ARRAY_AS_STRING
1086 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1087 DBUS_TYPE_STRING_AS_STRING
1088 DBUS_TYPE_VARIANT_AS_STRING
1089 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1090 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1091 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1094 g_slist_foreach(data->objects, append_object, &array);
1096 dbus_message_iter_close_container(&iter, &array);
1101 static const GDBusMethodTable manager_methods[] = {
1102 { GDBUS_METHOD("GetManagedObjects", NULL,
1103 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1107 static const GDBusSignalTable manager_signals[] = {
1108 { GDBUS_SIGNAL("InterfacesAdded",
1109 GDBUS_ARGS({ "object", "o" },
1110 { "interfaces", "a{sa{sv}}" })) },
1111 { GDBUS_SIGNAL("InterfacesRemoved",
1112 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1116 static void add_interface(struct generic_data *data,
1118 const GDBusMethodTable *methods,
1119 const GDBusSignalTable *signals,
1120 const GDBusPropertyTable *properties,
1122 GDBusDestroyFunction destroy)
1124 struct interface_data *iface;
1126 iface = g_new0(struct interface_data, 1);
1127 iface->name = g_strdup(name);
1128 iface->methods = methods;
1129 iface->signals = signals;
1130 iface->properties = properties;
1131 iface->user_data = user_data;
1132 iface->destroy = destroy;
1134 data->interfaces = g_slist_append(data->interfaces, iface);
1135 if (data->parent == NULL)
1138 data->added = g_slist_append(data->added, iface);
1139 if (data->process_id > 0)
1142 data->process_id = g_idle_add(process_changes, data);
1145 static struct generic_data *object_path_ref(DBusConnection *connection,
1148 struct generic_data *data;
1150 if (dbus_connection_get_object_path_data(connection, path,
1151 (void *) &data) == TRUE) {
1158 data = g_new0(struct generic_data, 1);
1159 data->conn = dbus_connection_ref(connection);
1160 data->path = g_strdup(path);
1163 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1165 if (!dbus_connection_register_object_path(connection, path,
1166 &generic_table, data)) {
1167 g_free(data->introspect);
1172 invalidate_parent_data(connection, path);
1174 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1175 NULL, NULL, data, NULL);
1177 /* Only root path export ObjectManager interface */
1178 if (data->parent == NULL)
1179 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1180 manager_methods, manager_signals,
1183 add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1184 properties_signals, NULL, data, NULL);
1189 static void object_path_unref(DBusConnection *connection, const char *path)
1191 struct generic_data *data = NULL;
1193 if (dbus_connection_get_object_path_data(connection, path,
1194 (void *) &data) == FALSE)
1202 if (data->refcount > 0)
1205 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1206 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1207 remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1209 invalidate_parent_data(data->conn, data->path);
1211 dbus_connection_unregister_object_path(data->conn, data->path);
1214 static gboolean check_signal(DBusConnection *conn, const char *path,
1215 const char *interface, const char *name,
1216 const GDBusArgInfo **args)
1218 struct generic_data *data = NULL;
1219 struct interface_data *iface;
1220 const GDBusSignalTable *signal;
1223 if (!dbus_connection_get_object_path_data(conn, path,
1224 (void *) &data) || data == NULL) {
1225 error("dbus_connection_emit_signal: path %s isn't registered",
1230 iface = find_interface(data->interfaces, interface);
1231 if (iface == NULL) {
1232 error("dbus_connection_emit_signal: %s does not implement %s",
1237 for (signal = iface->signals; signal && signal->name; signal++) {
1238 if (!strcmp(signal->name, name)) {
1239 *args = signal->args;
1244 error("No signal named %s on interface %s", name, interface);
1248 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1250 const char *interface,
1255 DBusMessage *signal;
1257 const GDBusArgInfo *args;
1259 if (!check_signal(conn, path, interface, name, &args))
1262 signal = dbus_message_new_signal(path, interface, name);
1263 if (signal == NULL) {
1264 error("Unable to allocate new %s.%s signal", interface, name);
1268 ret = dbus_message_append_args_valist(signal, first, var_args);
1272 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1273 error("%s.%s: got unexpected signature '%s'", interface, name,
1274 dbus_message_get_signature(signal));
1279 ret = dbus_connection_send(conn, signal, NULL);
1282 dbus_message_unref(signal);
1287 gboolean g_dbus_register_interface(DBusConnection *connection,
1288 const char *path, const char *name,
1289 const GDBusMethodTable *methods,
1290 const GDBusSignalTable *signals,
1291 const GDBusPropertyTable *properties,
1293 GDBusDestroyFunction destroy)
1295 struct generic_data *data;
1297 data = object_path_ref(connection, path);
1301 if (find_interface(data->interfaces, name)) {
1302 object_path_unref(connection, path);
1306 add_interface(data, name, methods, signals, properties, user_data,
1309 g_free(data->introspect);
1310 data->introspect = NULL;
1315 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1316 const char *path, const char *name)
1318 struct generic_data *data = NULL;
1323 if (dbus_connection_get_object_path_data(connection, path,
1324 (void *) &data) == FALSE)
1330 if (remove_interface(data, name) == FALSE)
1333 g_free(data->introspect);
1334 data->introspect = NULL;
1336 object_path_unref(connection, data->path);
1341 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1343 if (security_table != NULL)
1346 security_table = security;
1351 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1353 security_table = NULL;
1358 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1359 const char *format, va_list args)
1363 vsnprintf(str, sizeof(str), format, args);
1365 return dbus_message_new_error(message, name, str);
1368 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1369 const char *format, ...)
1374 va_start(args, format);
1376 reply = g_dbus_create_error_valist(message, name, format, args);
1383 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1384 int type, va_list args)
1388 reply = dbus_message_new_method_return(message);
1392 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1393 dbus_message_unref(reply);
1400 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1405 va_start(args, type);
1407 reply = g_dbus_create_reply_valist(message, type, args);
1414 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1418 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1419 dbus_message_set_no_reply(message, TRUE);
1421 result = dbus_connection_send(connection, message, NULL);
1423 dbus_message_unref(message);
1428 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1429 DBusMessage *message, int type, va_list args)
1433 reply = dbus_message_new_method_return(message);
1437 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1438 dbus_message_unref(reply);
1442 return g_dbus_send_message(connection, reply);
1445 gboolean g_dbus_send_reply(DBusConnection *connection,
1446 DBusMessage *message, int type, ...)
1451 va_start(args, type);
1453 result = g_dbus_send_reply_valist(connection, message, type, args);
1460 gboolean g_dbus_emit_signal(DBusConnection *connection,
1461 const char *path, const char *interface,
1462 const char *name, int type, ...)
1467 va_start(args, type);
1469 result = emit_signal_valist(connection, path, interface,
1477 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1478 const char *path, const char *interface,
1479 const char *name, int type, va_list args)
1481 return emit_signal_valist(connection, path, interface,