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;
537 signal = dbus_message_new_signal(parent->path,
538 DBUS_INTERFACE_OBJECT_MANAGER,
543 dbus_message_iter_init_append(signal, &iter);
544 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
547 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
548 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
549 DBUS_TYPE_STRING_AS_STRING
550 DBUS_TYPE_ARRAY_AS_STRING
551 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
552 DBUS_TYPE_STRING_AS_STRING
553 DBUS_TYPE_VARIANT_AS_STRING
554 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
555 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
557 g_slist_foreach(data->added, append_interface, &array);
558 g_slist_free(data->added);
561 dbus_message_iter_close_container(&iter, &array);
563 g_dbus_send_message(data->conn, signal);
566 static struct interface_data *find_interface(GSList *interfaces,
574 for (list = interfaces; list; list = list->next) {
575 struct interface_data *iface = list->data;
576 if (!strcmp(name, iface->name))
583 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
584 DBusMessage *message)
586 const char *sig = dbus_message_get_signature(message);
587 const char *p = NULL;
589 for (; args && args->signature && *sig; args++) {
592 for (; *sig && *p; sig++, p++) {
598 if (*sig || (p && *p) || (args && args->signature))
604 static gboolean remove_interface(struct generic_data *data, const char *name)
606 struct interface_data *iface;
608 iface = find_interface(data->interfaces, name);
612 data->interfaces = g_slist_remove(data->interfaces, iface);
614 if (iface->destroy) {
615 iface->destroy(iface->user_data);
616 iface->user_data = NULL;
619 if (data->parent == NULL) {
625 data->removed = g_slist_prepend(data->removed, iface->name);
628 if (data->process_id > 0)
631 data->process_id = g_idle_add(process_changes, data);
636 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
637 const char *child_path)
639 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
640 char *parent_path, *slash;
642 parent_path = g_strdup(child_path);
643 slash = strrchr(parent_path, '/');
647 if (slash == parent_path && parent_path[1] != '\0')
648 parent_path[1] = '\0';
652 if (!strlen(parent_path))
655 if (dbus_connection_get_object_path_data(conn, parent_path,
656 (void *) &data) == FALSE) {
660 parent = invalidate_parent_data(conn, parent_path);
668 g_free(data->introspect);
669 data->introspect = NULL;
671 if (!dbus_connection_get_object_path_data(conn, child_path,
675 if (child == NULL || g_slist_find(data->objects, child) != NULL)
678 data->objects = g_slist_prepend(data->objects, child);
679 child->parent = data;
686 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
689 const GDBusPropertyTable *p;
691 for (p = properties; p && p->name; p++) {
692 if (strcmp(name, p->name) == 0)
699 static DBusMessage *properties_get(DBusConnection *connection,
700 DBusMessage *message, void *user_data)
702 struct generic_data *data = user_data;
703 struct interface_data *iface;
704 const GDBusPropertyTable *property;
705 const char *interface, *name;
706 DBusMessageIter iter, value;
709 if (!dbus_message_get_args(message, NULL,
710 DBUS_TYPE_STRING, &interface,
711 DBUS_TYPE_STRING, &name,
715 iface = find_interface(data->interfaces, interface);
717 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
718 "No such interface '%s'", interface);
720 property = find_property(iface->properties, name);
721 if (property == NULL)
722 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
723 "No such property '%s'", name);
725 if (property->exists != NULL &&
726 !property->exists(property, iface->user_data))
727 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
728 "No such property '%s'", name);
730 if (property->get == NULL)
731 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
732 "Property '%s' is not readable", name);
734 reply = dbus_message_new_method_return(message);
738 dbus_message_iter_init_append(reply, &iter);
739 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
740 property->type, &value);
742 if (!property->get(property, &value, iface->user_data)) {
743 dbus_message_unref(reply);
747 dbus_message_iter_close_container(&iter, &value);
752 static DBusMessage *properties_get_all(DBusConnection *connection,
753 DBusMessage *message, void *user_data)
755 struct generic_data *data = user_data;
756 struct interface_data *iface;
757 const GDBusPropertyTable *p;
758 const char *interface;
759 DBusMessageIter iter, dict;
762 if (!dbus_message_get_args(message, NULL,
763 DBUS_TYPE_STRING, &interface,
767 iface = find_interface(data->interfaces, interface);
769 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
770 "No such interface '%s'", interface);
772 reply = dbus_message_new_method_return(message);
776 dbus_message_iter_init_append(reply, &iter);
777 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
778 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
779 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
780 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
782 for (p = iface->properties; p && p->name; p++) {
783 DBusMessageIter entry, value;
788 if (p->exists != NULL && !p->exists(p, iface->user_data))
791 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
793 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
795 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
798 if (!p->get(p, &value, iface->user_data)) {
799 dbus_message_unref(reply);
803 dbus_message_iter_close_container(&entry, &value);
804 dbus_message_iter_close_container(&dict, &entry);
807 dbus_message_iter_close_container(&iter, &dict);
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;
921 signal = dbus_message_new_signal(parent->path,
922 DBUS_INTERFACE_OBJECT_MANAGER,
923 "InterfacesRemoved");
927 dbus_message_iter_init_append(signal, &iter);
928 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
930 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
931 DBUS_TYPE_STRING_AS_STRING, &array);
933 g_slist_foreach(data->removed, append_name, &array);
934 g_slist_free_full(data->removed, g_free);
935 data->removed = NULL;
937 dbus_message_iter_close_container(&iter, &array);
939 g_dbus_send_message(data->conn, signal);
942 static gboolean process_changes(gpointer user_data)
944 struct generic_data *data = user_data;
946 data->process_id = 0;
948 if (data->added != NULL)
949 emit_interfaces_added(data);
951 if (data->removed != NULL)
952 emit_interfaces_removed(data);
957 static void generic_unregister(DBusConnection *connection, void *user_data)
959 struct generic_data *data = user_data;
960 struct generic_data *parent = data->parent;
963 parent->objects = g_slist_remove(parent->objects, data);
965 if (data->process_id > 0) {
966 g_source_remove(data->process_id);
967 if (data->removed != NULL)
968 emit_interfaces_removed(data);
971 g_slist_foreach(data->objects, reset_parent, data->parent);
972 g_slist_free(data->objects);
974 dbus_connection_unref(data->conn);
975 g_free(data->introspect);
980 static DBusHandlerResult generic_message(DBusConnection *connection,
981 DBusMessage *message, void *user_data)
983 struct generic_data *data = user_data;
984 struct interface_data *iface;
985 const GDBusMethodTable *method;
986 const char *interface;
988 interface = dbus_message_get_interface(message);
990 iface = find_interface(data->interfaces, interface);
992 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
994 for (method = iface->methods; method &&
995 method->name && method->function; method++) {
996 if (dbus_message_is_method_call(message, iface->name,
997 method->name) == FALSE)
1000 if (g_dbus_args_have_signature(method->in_args,
1004 if (check_privilege(connection, message, method,
1005 iface->user_data) == TRUE)
1006 return DBUS_HANDLER_RESULT_HANDLED;
1008 return process_message(connection, message, method,
1012 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1015 static DBusObjectPathVTable generic_table = {
1016 .unregister_function = generic_unregister,
1017 .message_function = generic_message,
1020 static const GDBusMethodTable introspect_methods[] = {
1021 { GDBUS_METHOD("Introspect", NULL,
1022 GDBUS_ARGS({ "xml", "s" }), introspect) },
1026 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1028 DBusMessageIter array;
1030 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1031 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1032 DBUS_TYPE_STRING_AS_STRING
1033 DBUS_TYPE_ARRAY_AS_STRING
1034 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1035 DBUS_TYPE_STRING_AS_STRING
1036 DBUS_TYPE_VARIANT_AS_STRING
1037 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1038 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1040 g_slist_foreach(data->interfaces, append_interface, &array);
1042 dbus_message_iter_close_container(iter, &array);
1045 static void append_object(gpointer data, gpointer user_data)
1047 struct generic_data *child = data;
1048 DBusMessageIter *array = user_data;
1049 DBusMessageIter entry;
1051 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1053 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1055 append_interfaces(child, &entry);
1056 dbus_message_iter_close_container(array, &entry);
1059 static DBusMessage *get_objects(DBusConnection *connection,
1060 DBusMessage *message, void *user_data)
1062 struct generic_data *data = user_data;
1064 DBusMessageIter iter;
1065 DBusMessageIter array;
1067 reply = dbus_message_new_method_return(message);
1071 dbus_message_iter_init_append(reply, &iter);
1073 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1074 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1075 DBUS_TYPE_OBJECT_PATH_AS_STRING
1076 DBUS_TYPE_ARRAY_AS_STRING
1077 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1078 DBUS_TYPE_STRING_AS_STRING
1079 DBUS_TYPE_ARRAY_AS_STRING
1080 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1081 DBUS_TYPE_STRING_AS_STRING
1082 DBUS_TYPE_VARIANT_AS_STRING
1083 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1084 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1085 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1088 g_slist_foreach(data->objects, append_object, &array);
1090 dbus_message_iter_close_container(&iter, &array);
1095 static const GDBusMethodTable manager_methods[] = {
1096 { GDBUS_METHOD("GetManagedObjects", NULL,
1097 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1101 static const GDBusSignalTable manager_signals[] = {
1102 { GDBUS_SIGNAL("InterfacesAdded",
1103 GDBUS_ARGS({ "object", "o" },
1104 { "interfaces", "a{sa{sv}}" })) },
1105 { GDBUS_SIGNAL("InterfacesRemoved",
1106 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1110 static void add_interface(struct generic_data *data,
1112 const GDBusMethodTable *methods,
1113 const GDBusSignalTable *signals,
1114 const GDBusPropertyTable *properties,
1116 GDBusDestroyFunction destroy)
1118 struct interface_data *iface;
1120 iface = g_new0(struct interface_data, 1);
1121 iface->name = g_strdup(name);
1122 iface->methods = methods;
1123 iface->signals = signals;
1124 iface->properties = properties;
1125 iface->user_data = user_data;
1126 iface->destroy = destroy;
1128 data->interfaces = g_slist_append(data->interfaces, iface);
1129 if (data->parent == NULL)
1132 data->added = g_slist_append(data->added, iface);
1133 if (data->process_id > 0)
1136 data->process_id = g_idle_add(process_changes, data);
1139 static struct generic_data *object_path_ref(DBusConnection *connection,
1142 struct generic_data *data;
1144 if (dbus_connection_get_object_path_data(connection, path,
1145 (void *) &data) == TRUE) {
1152 data = g_new0(struct generic_data, 1);
1153 data->conn = dbus_connection_ref(connection);
1154 data->path = g_strdup(path);
1157 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1159 if (!dbus_connection_register_object_path(connection, path,
1160 &generic_table, data)) {
1161 g_free(data->introspect);
1166 invalidate_parent_data(connection, path);
1168 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1169 NULL, NULL, data, NULL);
1171 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER, manager_methods,
1172 manager_signals, NULL, data, NULL);
1174 add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1175 properties_signals, NULL, data, NULL);
1180 static void object_path_unref(DBusConnection *connection, const char *path)
1182 struct generic_data *data = NULL;
1184 if (dbus_connection_get_object_path_data(connection, path,
1185 (void *) &data) == FALSE)
1193 if (data->refcount > 0)
1196 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1197 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1198 remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1200 invalidate_parent_data(data->conn, data->path);
1202 dbus_connection_unregister_object_path(data->conn, data->path);
1205 static gboolean check_signal(DBusConnection *conn, const char *path,
1206 const char *interface, const char *name,
1207 const GDBusArgInfo **args)
1209 struct generic_data *data = NULL;
1210 struct interface_data *iface;
1211 const GDBusSignalTable *signal;
1214 if (!dbus_connection_get_object_path_data(conn, path,
1215 (void *) &data) || data == NULL) {
1216 error("dbus_connection_emit_signal: path %s isn't registered",
1221 iface = find_interface(data->interfaces, interface);
1222 if (iface == NULL) {
1223 error("dbus_connection_emit_signal: %s does not implement %s",
1228 for (signal = iface->signals; signal && signal->name; signal++) {
1229 if (!strcmp(signal->name, name)) {
1230 *args = signal->args;
1235 error("No signal named %s on interface %s", name, interface);
1239 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1241 const char *interface,
1246 DBusMessage *signal;
1248 const GDBusArgInfo *args;
1250 if (!check_signal(conn, path, interface, name, &args))
1253 signal = dbus_message_new_signal(path, interface, name);
1254 if (signal == NULL) {
1255 error("Unable to allocate new %s.%s signal", interface, name);
1259 ret = dbus_message_append_args_valist(signal, first, var_args);
1263 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1264 error("%s.%s: got unexpected signature '%s'", interface, name,
1265 dbus_message_get_signature(signal));
1270 ret = dbus_connection_send(conn, signal, NULL);
1273 dbus_message_unref(signal);
1278 gboolean g_dbus_register_interface(DBusConnection *connection,
1279 const char *path, const char *name,
1280 const GDBusMethodTable *methods,
1281 const GDBusSignalTable *signals,
1282 const GDBusPropertyTable *properties,
1284 GDBusDestroyFunction destroy)
1286 struct generic_data *data;
1288 data = object_path_ref(connection, path);
1292 if (find_interface(data->interfaces, name)) {
1293 object_path_unref(connection, path);
1297 add_interface(data, name, methods, signals, properties, user_data,
1300 g_free(data->introspect);
1301 data->introspect = NULL;
1306 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1307 const char *path, const char *name)
1309 struct generic_data *data = NULL;
1314 if (dbus_connection_get_object_path_data(connection, path,
1315 (void *) &data) == FALSE)
1321 if (remove_interface(data, name) == FALSE)
1324 g_free(data->introspect);
1325 data->introspect = NULL;
1327 object_path_unref(connection, data->path);
1332 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1334 if (security_table != NULL)
1337 security_table = security;
1342 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1344 security_table = NULL;
1349 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1350 const char *format, va_list args)
1354 vsnprintf(str, sizeof(str), format, args);
1356 return dbus_message_new_error(message, name, str);
1359 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1360 const char *format, ...)
1365 va_start(args, format);
1367 reply = g_dbus_create_error_valist(message, name, format, args);
1374 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1375 int type, va_list args)
1379 reply = dbus_message_new_method_return(message);
1383 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1384 dbus_message_unref(reply);
1391 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1396 va_start(args, type);
1398 reply = g_dbus_create_reply_valist(message, type, args);
1405 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1409 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1410 dbus_message_set_no_reply(message, TRUE);
1412 result = dbus_connection_send(connection, message, NULL);
1414 dbus_message_unref(message);
1419 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1420 DBusMessage *message, int type, va_list args)
1424 reply = dbus_message_new_method_return(message);
1428 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1429 dbus_message_unref(reply);
1433 return g_dbus_send_message(connection, reply);
1436 gboolean g_dbus_send_reply(DBusConnection *connection,
1437 DBusMessage *message, int type, ...)
1442 va_start(args, type);
1444 result = g_dbus_send_reply_valist(connection, message, type, args);
1451 gboolean g_dbus_emit_signal(DBusConnection *connection,
1452 const char *path, const char *interface,
1453 const char *name, int type, ...)
1458 va_start(args, type);
1460 result = emit_signal_valist(connection, path, interface,
1468 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1469 const char *path, const char *interface,
1470 const char *name, int type, va_list args)
1472 return emit_signal_valist(connection, path, interface,