5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <dbus/dbus.h>
40 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
42 #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
43 #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
46 #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
47 #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
51 unsigned int refcount;
59 gboolean pending_prop;
61 struct generic_data *parent;
64 struct interface_data {
66 const GDBusMethodTable *methods;
67 const GDBusSignalTable *signals;
68 const GDBusPropertyTable *properties;
71 GDBusDestroyFunction destroy;
74 struct security_data {
75 GDBusPendingReply pending;
77 const GDBusMethodTable *method;
78 void *iface_user_data;
81 struct property_data {
83 GDBusPendingPropertySet id;
87 static int global_flags = 0;
88 static struct generic_data *root;
90 static gboolean process_changes(gpointer user_data);
91 static void process_properties_from_interface(struct generic_data *data,
92 struct interface_data *iface);
93 static void process_property_changes(struct generic_data *data);
95 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
96 const char *direction)
98 for (; args && args->name; args++) {
99 g_string_append_printf(gstr,
100 "\t\t\t<arg name=\"%s\" type=\"%s\"",
101 args->name, args->signature);
104 g_string_append_printf(gstr,
105 " direction=\"%s\"/>\n", direction);
107 g_string_append_printf(gstr, "/>\n");
112 #define G_DBUS_ANNOTATE(prefix_, name_, value_) \
113 prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
114 "value=\"" value_ "\"/>\n"
116 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
117 G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
119 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
120 G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
122 static gboolean check_experimental(int flags, int flag)
127 return !(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL);
130 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
132 const GDBusMethodTable *method;
133 const GDBusSignalTable *signal;
134 const GDBusPropertyTable *property;
136 for (method = iface->methods; method && method->name; method++) {
137 gboolean deprecated = method->flags &
138 G_DBUS_METHOD_FLAG_DEPRECATED;
139 gboolean noreply = method->flags &
140 G_DBUS_METHOD_FLAG_NOREPLY;
142 if (check_experimental(method->flags,
143 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
146 if (!deprecated && !noreply &&
147 !(method->in_args && method->in_args->name) &&
148 !(method->out_args && method->out_args->name))
149 g_string_append_printf(gstr,
150 "\t\t<method name=\"%s\"/>\n",
153 g_string_append_printf(gstr,
154 "\t\t<method name=\"%s\">\n",
156 print_arguments(gstr, method->in_args, "in");
157 print_arguments(gstr, method->out_args, "out");
160 g_string_append_printf(gstr,
161 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
163 g_string_append_printf(gstr,
164 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
166 g_string_append_printf(gstr, "\t\t</method>\n");
170 for (signal = iface->signals; signal && signal->name; signal++) {
171 gboolean deprecated = signal->flags &
172 G_DBUS_SIGNAL_FLAG_DEPRECATED;
174 if (check_experimental(signal->flags,
175 G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
178 if (!deprecated && !(signal->args && signal->args->name))
179 g_string_append_printf(gstr,
180 "\t\t<signal name=\"%s\"/>\n",
183 g_string_append_printf(gstr,
184 "\t\t<signal name=\"%s\">\n",
186 print_arguments(gstr, signal->args, NULL);
189 g_string_append_printf(gstr,
190 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
192 g_string_append_printf(gstr, "\t\t</signal>\n");
196 for (property = iface->properties; property && property->name;
198 gboolean deprecated = property->flags &
199 G_DBUS_PROPERTY_FLAG_DEPRECATED;
201 if (check_experimental(property->flags,
202 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
205 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
206 " type=\"%s\" access=\"%s%s\"",
207 property->name, property->type,
208 property->get ? "read" : "",
209 property->set ? "write" : "");
212 g_string_append_printf(gstr, "/>\n");
214 g_string_append_printf(gstr,
215 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
219 static void generate_introspection_xml(DBusConnection *conn,
220 struct generic_data *data, const char *path)
227 g_free(data->introspect);
229 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
231 g_string_append_printf(gstr, "<node>\n");
233 for (list = data->interfaces; list; list = list->next) {
234 struct interface_data *iface = list->data;
236 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
239 generate_interface_xml(gstr, iface);
241 g_string_append_printf(gstr, "\t</interface>\n");
244 if (!dbus_connection_list_registered(conn, path, &children))
247 for (i = 0; children[i]; i++)
248 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
251 dbus_free_string_array(children);
254 g_string_append_printf(gstr, "</node>\n");
256 data->introspect = g_string_free(gstr, FALSE);
259 static DBusMessage *introspect(DBusConnection *connection,
260 DBusMessage *message, void *user_data)
262 struct generic_data *data = user_data;
265 if (data->introspect == NULL)
266 generate_introspection_xml(connection, data,
267 dbus_message_get_path(message));
269 reply = dbus_message_new_method_return(message);
273 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
279 static DBusHandlerResult process_message(DBusConnection *connection,
280 DBusMessage *message, const GDBusMethodTable *method,
281 void *iface_user_data)
285 reply = method->function(connection, message, iface_user_data);
287 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
289 dbus_message_unref(reply);
290 return DBUS_HANDLER_RESULT_HANDLED;
293 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
295 return DBUS_HANDLER_RESULT_HANDLED;
299 return DBUS_HANDLER_RESULT_NEED_MEMORY;
301 dbus_connection_send(connection, reply, NULL);
302 dbus_message_unref(reply);
304 return DBUS_HANDLER_RESULT_HANDLED;
307 static GDBusPendingReply next_pending = 1;
308 static GSList *pending_security = NULL;
310 static const GDBusSecurityTable *security_table = NULL;
312 void g_dbus_pending_success(DBusConnection *connection,
313 GDBusPendingReply pending)
317 for (list = pending_security; list; list = list->next) {
318 struct security_data *secdata = list->data;
320 if (secdata->pending != pending)
323 pending_security = g_slist_remove(pending_security, secdata);
325 process_message(connection, secdata->message,
326 secdata->method, secdata->iface_user_data);
328 dbus_message_unref(secdata->message);
334 void g_dbus_pending_error_valist(DBusConnection *connection,
335 GDBusPendingReply pending, const char *name,
336 const char *format, va_list args)
340 for (list = pending_security; list; list = list->next) {
341 struct security_data *secdata = list->data;
344 if (secdata->pending != pending)
347 pending_security = g_slist_remove(pending_security, secdata);
349 reply = g_dbus_create_error_valist(secdata->message,
352 dbus_connection_send(connection, reply, NULL);
353 dbus_message_unref(reply);
356 dbus_message_unref(secdata->message);
362 void g_dbus_pending_error(DBusConnection *connection,
363 GDBusPendingReply pending,
364 const char *name, const char *format, ...)
368 va_start(args, format);
370 g_dbus_pending_error_valist(connection, pending, name, format, args);
375 int polkit_check_authorization(DBusConnection *conn,
376 const char *action, gboolean interaction,
377 void (*function) (dbus_bool_t authorized,
379 void *user_data, int timeout);
381 struct builtin_security_data {
382 DBusConnection *conn;
383 GDBusPendingReply pending;
386 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
388 struct builtin_security_data *data = user_data;
390 if (authorized == TRUE)
391 g_dbus_pending_success(data->conn, data->pending);
393 g_dbus_pending_error(data->conn, data->pending,
394 DBUS_ERROR_AUTH_FAILED, NULL);
399 static void builtin_security_function(DBusConnection *conn,
401 gboolean interaction,
402 GDBusPendingReply pending)
404 struct builtin_security_data *data;
406 data = g_new0(struct builtin_security_data, 1);
408 data->pending = pending;
410 if (polkit_check_authorization(conn, action, interaction,
411 builtin_security_result, data, 30000) < 0)
412 g_dbus_pending_error(conn, pending, NULL, NULL);
415 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
416 const GDBusMethodTable *method, void *iface_user_data)
418 const GDBusSecurityTable *security;
420 for (security = security_table; security && security->privilege;
422 struct security_data *secdata;
423 gboolean interaction;
425 if (security->privilege != method->privilege)
428 secdata = g_new(struct security_data, 1);
429 secdata->pending = next_pending++;
430 secdata->message = dbus_message_ref(msg);
431 secdata->method = method;
432 secdata->iface_user_data = iface_user_data;
434 pending_security = g_slist_prepend(pending_security, secdata);
436 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
441 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
443 security->function(conn, security->action,
444 interaction, secdata->pending);
446 builtin_security_function(conn, security->action,
447 interaction, secdata->pending);
455 static GDBusPendingPropertySet next_pending_property = 1;
456 static GSList *pending_property_set;
458 static struct property_data *remove_pending_property_data(
459 GDBusPendingPropertySet id)
461 struct property_data *propdata;
464 for (l = pending_property_set; l != NULL; l = l->next) {
466 if (propdata->id != id)
475 pending_property_set = g_slist_delete_link(pending_property_set, l);
480 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
482 struct property_data *propdata;
484 propdata = remove_pending_property_data(id);
485 if (propdata == NULL)
488 g_dbus_send_reply(propdata->conn, propdata->message,
490 dbus_message_unref(propdata->message);
494 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
495 const char *name, const char *format,
498 struct property_data *propdata;
501 propdata = remove_pending_property_data(id);
502 if (propdata == NULL)
505 reply = g_dbus_create_error_valist(propdata->message, name, format,
508 dbus_connection_send(propdata->conn, reply, NULL);
509 dbus_message_unref(reply);
512 dbus_message_unref(propdata->message);
516 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
517 const char *format, ...)
521 va_start(args, format);
523 g_dbus_pending_property_error_valist(id, name, format, args);
528 static void reset_parent(gpointer data, gpointer user_data)
530 struct generic_data *child = data;
531 struct generic_data *parent = user_data;
533 child->parent = parent;
536 static void append_property(struct interface_data *iface,
537 const GDBusPropertyTable *p, DBusMessageIter *dict)
539 DBusMessageIter entry, value;
541 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
543 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
544 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
547 p->get(p, &value, iface->user_data);
549 dbus_message_iter_close_container(&entry, &value);
550 dbus_message_iter_close_container(dict, &entry);
553 static void append_properties(struct interface_data *data,
554 DBusMessageIter *iter)
556 DBusMessageIter dict;
557 const GDBusPropertyTable *p;
559 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
560 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
561 DBUS_TYPE_STRING_AS_STRING
562 DBUS_TYPE_VARIANT_AS_STRING
563 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
565 for (p = data->properties; p && p->name; p++) {
566 if (check_experimental(p->flags,
567 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
573 if (p->exists != NULL && !p->exists(p, data->user_data))
576 append_property(data, p, &dict);
579 dbus_message_iter_close_container(iter, &dict);
582 static void append_interface(gpointer data, gpointer user_data)
584 struct interface_data *iface = data;
585 DBusMessageIter *array = user_data;
586 DBusMessageIter entry;
588 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
590 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
591 append_properties(data, &entry);
592 dbus_message_iter_close_container(array, &entry);
595 static void emit_interfaces_added(struct generic_data *data)
598 DBusMessageIter iter, array;
600 if (root == NULL || data == root)
603 signal = dbus_message_new_signal(root->path,
604 DBUS_INTERFACE_OBJECT_MANAGER,
609 dbus_message_iter_init_append(signal, &iter);
610 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
613 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
614 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
615 DBUS_TYPE_STRING_AS_STRING
616 DBUS_TYPE_ARRAY_AS_STRING
617 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
618 DBUS_TYPE_STRING_AS_STRING
619 DBUS_TYPE_VARIANT_AS_STRING
620 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
621 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
623 g_slist_foreach(data->added, append_interface, &array);
624 g_slist_free(data->added);
627 dbus_message_iter_close_container(&iter, &array);
629 g_dbus_send_message(data->conn, signal);
632 static struct interface_data *find_interface(GSList *interfaces,
640 for (list = interfaces; list; list = list->next) {
641 struct interface_data *iface = list->data;
642 if (!strcmp(name, iface->name))
649 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
650 DBusMessage *message)
652 const char *sig = dbus_message_get_signature(message);
653 const char *p = NULL;
655 for (; args && args->signature && *sig; args++) {
658 for (; *sig && *p; sig++, p++) {
664 if (*sig || (p && *p) || (args && args->signature))
670 static gboolean remove_interface(struct generic_data *data, const char *name)
672 struct interface_data *iface;
674 iface = find_interface(data->interfaces, name);
678 process_properties_from_interface(data, iface);
680 data->interfaces = g_slist_remove(data->interfaces, iface);
682 if (iface->destroy) {
683 iface->destroy(iface->user_data);
684 iface->user_data = NULL;
688 * Interface being removed was just added, on the same mainloop
689 * iteration? Don't send any signal
691 if (g_slist_find(data->added, iface)) {
692 data->added = g_slist_remove(data->added, iface);
698 if (data->parent == NULL) {
704 data->removed = g_slist_prepend(data->removed, iface->name);
707 if (data->process_id > 0)
710 data->process_id = g_idle_add(process_changes, data);
715 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
716 const char *child_path)
718 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
719 char *parent_path, *slash;
721 parent_path = g_strdup(child_path);
722 slash = strrchr(parent_path, '/');
726 if (slash == parent_path && parent_path[1] != '\0')
727 parent_path[1] = '\0';
731 if (!strlen(parent_path))
734 if (dbus_connection_get_object_path_data(conn, parent_path,
735 (void *) &data) == FALSE) {
739 parent = invalidate_parent_data(conn, parent_path);
747 g_free(data->introspect);
748 data->introspect = NULL;
750 if (!dbus_connection_get_object_path_data(conn, child_path,
754 if (child == NULL || g_slist_find(data->objects, child) != NULL)
757 data->objects = g_slist_prepend(data->objects, child);
758 child->parent = data;
765 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
768 const GDBusPropertyTable *p;
770 for (p = properties; p && p->name; p++) {
771 if (strcmp(name, p->name) != 0)
774 if (check_experimental(p->flags,
775 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
784 static DBusMessage *properties_get(DBusConnection *connection,
785 DBusMessage *message, void *user_data)
787 struct generic_data *data = user_data;
788 struct interface_data *iface;
789 const GDBusPropertyTable *property;
790 const char *interface, *name;
791 DBusMessageIter iter, value;
794 if (!dbus_message_get_args(message, NULL,
795 DBUS_TYPE_STRING, &interface,
796 DBUS_TYPE_STRING, &name,
800 iface = find_interface(data->interfaces, interface);
802 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
803 "No such interface '%s'", interface);
805 property = find_property(iface->properties, name);
806 if (property == NULL)
807 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
808 "No such property '%s'", name);
810 if (property->exists != NULL &&
811 !property->exists(property, iface->user_data))
812 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
813 "No such property '%s'", name);
815 if (property->get == NULL)
816 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
817 "Property '%s' is not readable", name);
819 reply = dbus_message_new_method_return(message);
823 dbus_message_iter_init_append(reply, &iter);
824 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
825 property->type, &value);
827 if (!property->get(property, &value, iface->user_data)) {
828 dbus_message_unref(reply);
832 dbus_message_iter_close_container(&iter, &value);
837 static DBusMessage *properties_get_all(DBusConnection *connection,
838 DBusMessage *message, void *user_data)
840 struct generic_data *data = user_data;
841 struct interface_data *iface;
842 const char *interface;
843 DBusMessageIter iter;
846 if (!dbus_message_get_args(message, NULL,
847 DBUS_TYPE_STRING, &interface,
851 iface = find_interface(data->interfaces, interface);
853 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
854 "No such interface '%s'", interface);
856 reply = dbus_message_new_method_return(message);
860 dbus_message_iter_init_append(reply, &iter);
862 append_properties(iface, &iter);
867 static DBusMessage *properties_set(DBusConnection *connection,
868 DBusMessage *message, void *user_data)
870 struct generic_data *data = user_data;
871 DBusMessageIter iter, sub;
872 struct interface_data *iface;
873 const GDBusPropertyTable *property;
874 const char *name, *interface;
875 struct property_data *propdata;
877 if (!dbus_message_iter_init(message, &iter))
878 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
879 "No arguments given");
881 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
882 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
883 "Invalid argument type: '%c'",
884 dbus_message_iter_get_arg_type(&iter));
886 dbus_message_iter_get_basic(&iter, &interface);
887 dbus_message_iter_next(&iter);
889 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
890 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
891 "Invalid argument type: '%c'",
892 dbus_message_iter_get_arg_type(&iter));
894 dbus_message_iter_get_basic(&iter, &name);
895 dbus_message_iter_next(&iter);
897 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
898 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
899 "Invalid argument type: '%c'",
900 dbus_message_iter_get_arg_type(&iter));
902 dbus_message_iter_recurse(&iter, &sub);
904 iface = find_interface(data->interfaces, interface);
906 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
907 "No such interface '%s'", interface);
909 property = find_property(iface->properties, name);
910 if (property == NULL)
911 return g_dbus_create_error(message,
912 DBUS_ERROR_UNKNOWN_PROPERTY,
913 "No such property '%s'", name);
915 if (property->set == NULL)
916 return g_dbus_create_error(message,
917 DBUS_ERROR_PROPERTY_READ_ONLY,
918 "Property '%s' is not writable", name);
920 if (property->exists != NULL &&
921 !property->exists(property, iface->user_data))
922 return g_dbus_create_error(message,
923 DBUS_ERROR_UNKNOWN_PROPERTY,
924 "No such property '%s'", name);
926 propdata = g_new(struct property_data, 1);
927 propdata->id = next_pending_property++;
928 propdata->message = dbus_message_ref(message);
929 propdata->conn = connection;
930 pending_property_set = g_slist_prepend(pending_property_set, propdata);
932 property->set(property, &sub, propdata->id, iface->user_data);
937 static const GDBusMethodTable properties_methods[] = {
938 { GDBUS_METHOD("Get",
939 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
940 GDBUS_ARGS({ "value", "v" }),
942 { GDBUS_ASYNC_METHOD("Set",
943 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
947 { GDBUS_METHOD("GetAll",
948 GDBUS_ARGS({ "interface", "s" }),
949 GDBUS_ARGS({ "properties", "a{sv}" }),
950 properties_get_all) },
954 static const GDBusSignalTable properties_signals[] = {
955 { GDBUS_SIGNAL("PropertiesChanged",
956 GDBUS_ARGS({ "interface", "s" },
957 { "changed_properties", "a{sv}" },
958 { "invalidated_properties", "as"})) },
962 static void append_name(gpointer data, gpointer user_data)
965 DBusMessageIter *iter = user_data;
967 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
970 static void emit_interfaces_removed(struct generic_data *data)
973 DBusMessageIter iter, array;
975 if (root == NULL || data == root)
978 signal = dbus_message_new_signal(root->path,
979 DBUS_INTERFACE_OBJECT_MANAGER,
980 "InterfacesRemoved");
984 dbus_message_iter_init_append(signal, &iter);
985 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
987 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
988 DBUS_TYPE_STRING_AS_STRING, &array);
990 g_slist_foreach(data->removed, append_name, &array);
991 g_slist_free_full(data->removed, g_free);
992 data->removed = NULL;
994 dbus_message_iter_close_container(&iter, &array);
996 g_dbus_send_message(data->conn, signal);
999 static gboolean process_changes(gpointer user_data)
1001 struct generic_data *data = user_data;
1003 data->process_id = 0;
1005 if (data->added != NULL)
1006 emit_interfaces_added(data);
1008 /* Flush pending properties */
1009 if (data->pending_prop == TRUE)
1010 process_property_changes(data);
1012 if (data->removed != NULL)
1013 emit_interfaces_removed(data);
1018 static void generic_unregister(DBusConnection *connection, void *user_data)
1020 struct generic_data *data = user_data;
1021 struct generic_data *parent = data->parent;
1024 parent->objects = g_slist_remove(parent->objects, data);
1026 if (data->process_id > 0) {
1027 g_source_remove(data->process_id);
1028 process_changes(data);
1031 g_slist_foreach(data->objects, reset_parent, data->parent);
1032 g_slist_free(data->objects);
1034 dbus_connection_unref(data->conn);
1035 g_free(data->introspect);
1040 static DBusHandlerResult generic_message(DBusConnection *connection,
1041 DBusMessage *message, void *user_data)
1043 struct generic_data *data = user_data;
1044 struct interface_data *iface;
1045 const GDBusMethodTable *method;
1046 const char *interface;
1048 interface = dbus_message_get_interface(message);
1050 iface = find_interface(data->interfaces, interface);
1052 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1054 for (method = iface->methods; method &&
1055 method->name && method->function; method++) {
1057 if (dbus_message_is_method_call(message, iface->name,
1058 method->name) == FALSE)
1061 if (check_experimental(method->flags,
1062 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1063 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1065 if (g_dbus_args_have_signature(method->in_args,
1069 if (check_privilege(connection, message, method,
1070 iface->user_data) == TRUE)
1071 return DBUS_HANDLER_RESULT_HANDLED;
1073 return process_message(connection, message, method,
1077 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1080 static DBusObjectPathVTable generic_table = {
1081 .unregister_function = generic_unregister,
1082 .message_function = generic_message,
1085 static const GDBusMethodTable introspect_methods[] = {
1086 { GDBUS_METHOD("Introspect", NULL,
1087 GDBUS_ARGS({ "xml", "s" }), introspect) },
1091 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1093 DBusMessageIter array;
1095 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1096 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1097 DBUS_TYPE_STRING_AS_STRING
1098 DBUS_TYPE_ARRAY_AS_STRING
1099 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1100 DBUS_TYPE_STRING_AS_STRING
1101 DBUS_TYPE_VARIANT_AS_STRING
1102 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1103 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1105 g_slist_foreach(data->interfaces, append_interface, &array);
1107 dbus_message_iter_close_container(iter, &array);
1110 static void append_object(gpointer data, gpointer user_data)
1112 struct generic_data *child = data;
1113 DBusMessageIter *array = user_data;
1114 DBusMessageIter entry;
1116 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1118 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1120 append_interfaces(child, &entry);
1121 dbus_message_iter_close_container(array, &entry);
1123 g_slist_foreach(child->objects, append_object, user_data);
1126 static DBusMessage *get_objects(DBusConnection *connection,
1127 DBusMessage *message, void *user_data)
1129 struct generic_data *data = user_data;
1131 DBusMessageIter iter;
1132 DBusMessageIter array;
1134 reply = dbus_message_new_method_return(message);
1138 dbus_message_iter_init_append(reply, &iter);
1140 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1141 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1142 DBUS_TYPE_OBJECT_PATH_AS_STRING
1143 DBUS_TYPE_ARRAY_AS_STRING
1144 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1145 DBUS_TYPE_STRING_AS_STRING
1146 DBUS_TYPE_ARRAY_AS_STRING
1147 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1148 DBUS_TYPE_STRING_AS_STRING
1149 DBUS_TYPE_VARIANT_AS_STRING
1150 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1151 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1152 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1155 g_slist_foreach(data->objects, append_object, &array);
1157 dbus_message_iter_close_container(&iter, &array);
1162 static const GDBusMethodTable manager_methods[] = {
1163 { GDBUS_METHOD("GetManagedObjects", NULL,
1164 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1168 static const GDBusSignalTable manager_signals[] = {
1169 { GDBUS_SIGNAL("InterfacesAdded",
1170 GDBUS_ARGS({ "object", "o" },
1171 { "interfaces", "a{sa{sv}}" })) },
1172 { GDBUS_SIGNAL("InterfacesRemoved",
1173 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1177 static void add_interface(struct generic_data *data,
1179 const GDBusMethodTable *methods,
1180 const GDBusSignalTable *signals,
1181 const GDBusPropertyTable *properties,
1183 GDBusDestroyFunction destroy)
1185 struct interface_data *iface;
1187 iface = g_new0(struct interface_data, 1);
1188 iface->name = g_strdup(name);
1189 iface->methods = methods;
1190 iface->signals = signals;
1191 iface->properties = properties;
1192 iface->user_data = user_data;
1193 iface->destroy = destroy;
1195 data->interfaces = g_slist_append(data->interfaces, iface);
1196 if (data->parent == NULL)
1199 data->added = g_slist_append(data->added, iface);
1200 if (data->process_id > 0)
1203 data->process_id = g_idle_add(process_changes, data);
1206 static struct generic_data *object_path_ref(DBusConnection *connection,
1209 struct generic_data *data;
1211 if (dbus_connection_get_object_path_data(connection, path,
1212 (void *) &data) == TRUE) {
1219 data = g_new0(struct generic_data, 1);
1220 data->conn = dbus_connection_ref(connection);
1221 data->path = g_strdup(path);
1224 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1226 if (!dbus_connection_register_object_path(connection, path,
1227 &generic_table, data)) {
1228 g_free(data->introspect);
1233 invalidate_parent_data(connection, path);
1235 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1236 NULL, NULL, data, NULL);
1241 static void object_path_unref(DBusConnection *connection, const char *path)
1243 struct generic_data *data = NULL;
1245 if (dbus_connection_get_object_path_data(connection, path,
1246 (void *) &data) == FALSE)
1254 if (data->refcount > 0)
1257 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1258 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1260 invalidate_parent_data(data->conn, data->path);
1262 dbus_connection_unregister_object_path(data->conn, data->path);
1265 static gboolean check_signal(DBusConnection *conn, const char *path,
1266 const char *interface, const char *name,
1267 const GDBusArgInfo **args)
1269 struct generic_data *data = NULL;
1270 struct interface_data *iface;
1271 const GDBusSignalTable *signal;
1274 if (!dbus_connection_get_object_path_data(conn, path,
1275 (void *) &data) || data == NULL) {
1276 error("dbus_connection_emit_signal: path %s isn't registered",
1281 iface = find_interface(data->interfaces, interface);
1282 if (iface == NULL) {
1283 error("dbus_connection_emit_signal: %s does not implement %s",
1288 for (signal = iface->signals; signal && signal->name; signal++) {
1289 if (strcmp(signal->name, name) != 0)
1292 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1293 const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1294 if (g_strcmp0(env, "1") != 0)
1298 *args = signal->args;
1302 error("No signal named %s on interface %s", name, interface);
1306 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1308 const char *interface,
1313 DBusMessage *signal;
1315 const GDBusArgInfo *args;
1317 if (!check_signal(conn, path, interface, name, &args))
1320 signal = dbus_message_new_signal(path, interface, name);
1321 if (signal == NULL) {
1322 error("Unable to allocate new %s.%s signal", interface, name);
1326 ret = dbus_message_append_args_valist(signal, first, var_args);
1330 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1331 error("%s.%s: got unexpected signature '%s'", interface, name,
1332 dbus_message_get_signature(signal));
1337 ret = dbus_connection_send(conn, signal, NULL);
1340 dbus_message_unref(signal);
1345 gboolean g_dbus_register_interface(DBusConnection *connection,
1346 const char *path, const char *name,
1347 const GDBusMethodTable *methods,
1348 const GDBusSignalTable *signals,
1349 const GDBusPropertyTable *properties,
1351 GDBusDestroyFunction destroy)
1353 struct generic_data *data;
1355 data = object_path_ref(connection, path);
1359 if (find_interface(data->interfaces, name)) {
1360 object_path_unref(connection, path);
1364 if (properties != NULL && !find_interface(data->interfaces,
1365 DBUS_INTERFACE_PROPERTIES))
1366 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1367 properties_methods, properties_signals, NULL,
1370 add_interface(data, name, methods, signals, properties, user_data,
1373 g_free(data->introspect);
1374 data->introspect = NULL;
1379 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1380 const char *path, const char *name)
1382 struct generic_data *data = NULL;
1387 if (dbus_connection_get_object_path_data(connection, path,
1388 (void *) &data) == FALSE)
1394 if (remove_interface(data, name) == FALSE)
1397 g_free(data->introspect);
1398 data->introspect = NULL;
1400 object_path_unref(connection, data->path);
1405 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1407 if (security_table != NULL)
1410 security_table = security;
1415 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1417 security_table = NULL;
1422 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1423 const char *format, va_list args)
1427 vsnprintf(str, sizeof(str), format, args);
1429 return dbus_message_new_error(message, name, str);
1432 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1433 const char *format, ...)
1438 va_start(args, format);
1440 reply = g_dbus_create_error_valist(message, name, format, args);
1447 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1448 int type, va_list args)
1452 reply = dbus_message_new_method_return(message);
1456 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1457 dbus_message_unref(reply);
1464 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1469 va_start(args, type);
1471 reply = g_dbus_create_reply_valist(message, type, args);
1478 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1482 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1483 dbus_message_set_no_reply(message, TRUE);
1485 result = dbus_connection_send(connection, message, NULL);
1487 dbus_message_unref(message);
1492 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1493 DBusMessage *message, const char *name,
1494 const char *format, va_list args)
1499 vsnprintf(str, sizeof(str), format, args);
1501 error = dbus_message_new_error(message, name, str);
1505 return g_dbus_send_message(connection, error);
1508 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1509 const char *name, const char *format, ...)
1514 va_start(args, format);
1516 result = g_dbus_send_error_valist(connection, message, name,
1524 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1525 DBusMessage *message, int type, va_list args)
1529 reply = dbus_message_new_method_return(message);
1533 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1534 dbus_message_unref(reply);
1538 return g_dbus_send_message(connection, reply);
1541 gboolean g_dbus_send_reply(DBusConnection *connection,
1542 DBusMessage *message, int type, ...)
1547 va_start(args, type);
1549 result = g_dbus_send_reply_valist(connection, message, type, args);
1556 gboolean g_dbus_emit_signal(DBusConnection *connection,
1557 const char *path, const char *interface,
1558 const char *name, int type, ...)
1563 va_start(args, type);
1565 result = emit_signal_valist(connection, path, interface,
1573 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1574 const char *path, const char *interface,
1575 const char *name, int type, va_list args)
1577 return emit_signal_valist(connection, path, interface,
1581 static void process_properties_from_interface(struct generic_data *data,
1582 struct interface_data *iface)
1585 DBusMessage *signal;
1586 DBusMessageIter iter, dict, array;
1587 GSList *invalidated;
1589 if (iface->pending_prop == NULL)
1592 signal = dbus_message_new_signal(data->path,
1593 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1594 if (signal == NULL) {
1595 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1596 ".PropertiesChanged signal");
1600 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1602 dbus_message_iter_init_append(signal, &iter);
1603 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1604 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1605 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1606 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1607 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1611 for (l = iface->pending_prop; l != NULL; l = l->next) {
1612 GDBusPropertyTable *p = l->data;
1617 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1618 invalidated = g_slist_prepend(invalidated, p);
1622 append_property(iface, p, &dict);
1625 dbus_message_iter_close_container(&iter, &dict);
1627 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1628 DBUS_TYPE_STRING_AS_STRING, &array);
1629 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1630 GDBusPropertyTable *p = l->data;
1632 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1635 g_slist_free(invalidated);
1636 dbus_message_iter_close_container(&iter, &array);
1638 g_dbus_send_message(data->conn, signal);
1640 g_slist_free(iface->pending_prop);
1641 iface->pending_prop = NULL;
1644 static void process_property_changes(struct generic_data *data)
1648 for (l = data->interfaces; l != NULL; l = l->next) {
1649 struct interface_data *iface = l->data;
1651 process_properties_from_interface(data, iface);
1654 data->pending_prop = FALSE;
1657 void g_dbus_emit_property_changed(DBusConnection *connection,
1658 const char *path, const char *interface,
1661 const GDBusPropertyTable *property;
1662 struct generic_data *data;
1663 struct interface_data *iface;
1665 if (!dbus_connection_get_object_path_data(connection, path,
1666 (void **) &data) || data == NULL)
1669 iface = find_interface(data->interfaces, interface);
1673 property = find_property(iface->properties, name);
1674 if (property == NULL) {
1675 error("Could not find property %s in %p", name,
1680 if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1683 data->pending_prop = TRUE;
1684 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1687 if (!data->process_id) {
1688 data->process_id = g_idle_add(process_changes, data);
1693 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1694 const char *interface, DBusMessageIter *iter)
1696 struct generic_data *data;
1697 struct interface_data *iface;
1699 if (!dbus_connection_get_object_path_data(connection, path,
1700 (void **) &data) || data == NULL)
1703 iface = find_interface(data->interfaces, interface);
1707 append_properties(iface, iter);
1712 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1714 struct generic_data *data;
1716 data = object_path_ref(connection, "/");
1720 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1721 manager_methods, manager_signals,
1728 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1730 if (!g_dbus_unregister_interface(connection, "/",
1731 DBUS_INTERFACE_OBJECT_MANAGER))
1739 void g_dbus_set_flags(int flags)
1741 global_flags = flags;