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 void generate_interface_xml(GString *gstr, struct interface_data *iface)
124 const GDBusMethodTable *method;
125 const GDBusSignalTable *signal;
126 const GDBusPropertyTable *property;
128 for (method = iface->methods; method && method->name; method++) {
129 gboolean deprecated = method->flags &
130 G_DBUS_METHOD_FLAG_DEPRECATED;
131 gboolean noreply = method->flags &
132 G_DBUS_METHOD_FLAG_NOREPLY;
133 gboolean experimental = method->flags &
134 G_DBUS_METHOD_FLAG_EXPERIMENTAL;
136 if (!(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) &&
140 if (!deprecated && !noreply &&
141 !(method->in_args && method->in_args->name) &&
142 !(method->out_args && method->out_args->name))
143 g_string_append_printf(gstr,
144 "\t\t<method name=\"%s\"/>\n",
147 g_string_append_printf(gstr,
148 "\t\t<method name=\"%s\">\n",
150 print_arguments(gstr, method->in_args, "in");
151 print_arguments(gstr, method->out_args, "out");
154 g_string_append_printf(gstr,
155 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
157 g_string_append_printf(gstr,
158 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
160 g_string_append_printf(gstr, "\t\t</method>\n");
164 for (signal = iface->signals; signal && signal->name; signal++) {
165 gboolean deprecated = signal->flags &
166 G_DBUS_SIGNAL_FLAG_DEPRECATED;
168 if (!deprecated && !(signal->args && signal->args->name))
169 g_string_append_printf(gstr,
170 "\t\t<signal name=\"%s\"/>\n",
173 g_string_append_printf(gstr,
174 "\t\t<signal name=\"%s\">\n",
176 print_arguments(gstr, signal->args, NULL);
179 g_string_append_printf(gstr,
180 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
182 g_string_append_printf(gstr, "\t\t</signal>\n");
186 for (property = iface->properties; property && property->name;
188 gboolean deprecated = property->flags &
189 G_DBUS_PROPERTY_FLAG_DEPRECATED;
191 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
192 " type=\"%s\" access=\"%s%s\"",
193 property->name, property->type,
194 property->get ? "read" : "",
195 property->set ? "write" : "");
198 g_string_append_printf(gstr, "/>\n");
200 g_string_append_printf(gstr,
201 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
205 static void generate_introspection_xml(DBusConnection *conn,
206 struct generic_data *data, const char *path)
213 g_free(data->introspect);
215 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
217 g_string_append_printf(gstr, "<node>\n");
219 for (list = data->interfaces; list; list = list->next) {
220 struct interface_data *iface = list->data;
222 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
225 generate_interface_xml(gstr, iface);
227 g_string_append_printf(gstr, "\t</interface>\n");
230 if (!dbus_connection_list_registered(conn, path, &children))
233 for (i = 0; children[i]; i++)
234 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
237 dbus_free_string_array(children);
240 g_string_append_printf(gstr, "</node>\n");
242 data->introspect = g_string_free(gstr, FALSE);
245 static DBusMessage *introspect(DBusConnection *connection,
246 DBusMessage *message, void *user_data)
248 struct generic_data *data = user_data;
251 if (data->introspect == NULL)
252 generate_introspection_xml(connection, data,
253 dbus_message_get_path(message));
255 reply = dbus_message_new_method_return(message);
259 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
265 static DBusHandlerResult process_message(DBusConnection *connection,
266 DBusMessage *message, const GDBusMethodTable *method,
267 void *iface_user_data)
271 reply = method->function(connection, message, iface_user_data);
273 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
275 dbus_message_unref(reply);
276 return DBUS_HANDLER_RESULT_HANDLED;
279 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
281 return DBUS_HANDLER_RESULT_HANDLED;
285 return DBUS_HANDLER_RESULT_NEED_MEMORY;
287 dbus_connection_send(connection, reply, NULL);
288 dbus_message_unref(reply);
290 return DBUS_HANDLER_RESULT_HANDLED;
293 static GDBusPendingReply next_pending = 1;
294 static GSList *pending_security = NULL;
296 static const GDBusSecurityTable *security_table = NULL;
298 void g_dbus_pending_success(DBusConnection *connection,
299 GDBusPendingReply pending)
303 for (list = pending_security; list; list = list->next) {
304 struct security_data *secdata = list->data;
306 if (secdata->pending != pending)
309 pending_security = g_slist_remove(pending_security, secdata);
311 process_message(connection, secdata->message,
312 secdata->method, secdata->iface_user_data);
314 dbus_message_unref(secdata->message);
320 void g_dbus_pending_error_valist(DBusConnection *connection,
321 GDBusPendingReply pending, const char *name,
322 const char *format, va_list args)
326 for (list = pending_security; list; list = list->next) {
327 struct security_data *secdata = list->data;
330 if (secdata->pending != pending)
333 pending_security = g_slist_remove(pending_security, secdata);
335 reply = g_dbus_create_error_valist(secdata->message,
338 dbus_connection_send(connection, reply, NULL);
339 dbus_message_unref(reply);
342 dbus_message_unref(secdata->message);
348 void g_dbus_pending_error(DBusConnection *connection,
349 GDBusPendingReply pending,
350 const char *name, const char *format, ...)
354 va_start(args, format);
356 g_dbus_pending_error_valist(connection, pending, name, format, args);
361 int polkit_check_authorization(DBusConnection *conn,
362 const char *action, gboolean interaction,
363 void (*function) (dbus_bool_t authorized,
365 void *user_data, int timeout);
367 struct builtin_security_data {
368 DBusConnection *conn;
369 GDBusPendingReply pending;
372 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
374 struct builtin_security_data *data = user_data;
376 if (authorized == TRUE)
377 g_dbus_pending_success(data->conn, data->pending);
379 g_dbus_pending_error(data->conn, data->pending,
380 DBUS_ERROR_AUTH_FAILED, NULL);
385 static void builtin_security_function(DBusConnection *conn,
387 gboolean interaction,
388 GDBusPendingReply pending)
390 struct builtin_security_data *data;
392 data = g_new0(struct builtin_security_data, 1);
394 data->pending = pending;
396 if (polkit_check_authorization(conn, action, interaction,
397 builtin_security_result, data, 30000) < 0)
398 g_dbus_pending_error(conn, pending, NULL, NULL);
401 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
402 const GDBusMethodTable *method, void *iface_user_data)
404 const GDBusSecurityTable *security;
406 for (security = security_table; security && security->privilege;
408 struct security_data *secdata;
409 gboolean interaction;
411 if (security->privilege != method->privilege)
414 secdata = g_new(struct security_data, 1);
415 secdata->pending = next_pending++;
416 secdata->message = dbus_message_ref(msg);
417 secdata->method = method;
418 secdata->iface_user_data = iface_user_data;
420 pending_security = g_slist_prepend(pending_security, secdata);
422 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
427 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
429 security->function(conn, security->action,
430 interaction, secdata->pending);
432 builtin_security_function(conn, security->action,
433 interaction, secdata->pending);
441 static GDBusPendingPropertySet next_pending_property = 1;
442 static GSList *pending_property_set;
444 static struct property_data *remove_pending_property_data(
445 GDBusPendingPropertySet id)
447 struct property_data *propdata;
450 for (l = pending_property_set; l != NULL; l = l->next) {
452 if (propdata->id != id)
461 pending_property_set = g_slist_delete_link(pending_property_set, l);
466 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
468 struct property_data *propdata;
470 propdata = remove_pending_property_data(id);
471 if (propdata == NULL)
474 g_dbus_send_reply(propdata->conn, propdata->message,
476 dbus_message_unref(propdata->message);
480 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
481 const char *name, const char *format,
484 struct property_data *propdata;
487 propdata = remove_pending_property_data(id);
488 if (propdata == NULL)
491 reply = g_dbus_create_error_valist(propdata->message, name, format,
494 dbus_connection_send(propdata->conn, reply, NULL);
495 dbus_message_unref(reply);
498 dbus_message_unref(propdata->message);
502 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
503 const char *format, ...)
507 va_start(args, format);
509 g_dbus_pending_property_error_valist(id, name, format, args);
514 static void reset_parent(gpointer data, gpointer user_data)
516 struct generic_data *child = data;
517 struct generic_data *parent = user_data;
519 child->parent = parent;
522 static void append_property(struct interface_data *iface,
523 const GDBusPropertyTable *p, DBusMessageIter *dict)
525 DBusMessageIter entry, value;
527 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
529 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
530 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
533 p->get(p, &value, iface->user_data);
535 dbus_message_iter_close_container(&entry, &value);
536 dbus_message_iter_close_container(dict, &entry);
539 static void append_properties(struct interface_data *data,
540 DBusMessageIter *iter)
542 DBusMessageIter dict;
543 const GDBusPropertyTable *p;
545 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
546 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
547 DBUS_TYPE_STRING_AS_STRING
548 DBUS_TYPE_VARIANT_AS_STRING
549 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
551 for (p = data->properties; p && p->name; p++) {
555 if (p->exists != NULL && !p->exists(p, data->user_data))
558 append_property(data, p, &dict);
561 dbus_message_iter_close_container(iter, &dict);
564 static void append_interface(gpointer data, gpointer user_data)
566 struct interface_data *iface = data;
567 DBusMessageIter *array = user_data;
568 DBusMessageIter entry;
570 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
572 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
573 append_properties(data, &entry);
574 dbus_message_iter_close_container(array, &entry);
577 static void emit_interfaces_added(struct generic_data *data)
580 DBusMessageIter iter, array;
582 if (root == NULL || data == root)
585 signal = dbus_message_new_signal(root->path,
586 DBUS_INTERFACE_OBJECT_MANAGER,
591 dbus_message_iter_init_append(signal, &iter);
592 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
595 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
596 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
597 DBUS_TYPE_STRING_AS_STRING
598 DBUS_TYPE_ARRAY_AS_STRING
599 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
600 DBUS_TYPE_STRING_AS_STRING
601 DBUS_TYPE_VARIANT_AS_STRING
602 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
603 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
605 g_slist_foreach(data->added, append_interface, &array);
606 g_slist_free(data->added);
609 dbus_message_iter_close_container(&iter, &array);
611 g_dbus_send_message(data->conn, signal);
614 static struct interface_data *find_interface(GSList *interfaces,
622 for (list = interfaces; list; list = list->next) {
623 struct interface_data *iface = list->data;
624 if (!strcmp(name, iface->name))
631 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
632 DBusMessage *message)
634 const char *sig = dbus_message_get_signature(message);
635 const char *p = NULL;
637 for (; args && args->signature && *sig; args++) {
640 for (; *sig && *p; sig++, p++) {
646 if (*sig || (p && *p) || (args && args->signature))
652 static gboolean remove_interface(struct generic_data *data, const char *name)
654 struct interface_data *iface;
656 iface = find_interface(data->interfaces, name);
660 process_properties_from_interface(data, iface);
662 data->interfaces = g_slist_remove(data->interfaces, iface);
664 if (iface->destroy) {
665 iface->destroy(iface->user_data);
666 iface->user_data = NULL;
670 * Interface being removed was just added, on the same mainloop
671 * iteration? Don't send any signal
673 if (g_slist_find(data->added, iface)) {
674 data->added = g_slist_remove(data->added, iface);
680 if (data->parent == NULL) {
686 data->removed = g_slist_prepend(data->removed, iface->name);
689 if (data->process_id > 0)
692 data->process_id = g_idle_add(process_changes, data);
697 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
698 const char *child_path)
700 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
701 char *parent_path, *slash;
703 parent_path = g_strdup(child_path);
704 slash = strrchr(parent_path, '/');
708 if (slash == parent_path && parent_path[1] != '\0')
709 parent_path[1] = '\0';
713 if (!strlen(parent_path))
716 if (dbus_connection_get_object_path_data(conn, parent_path,
717 (void *) &data) == FALSE) {
721 parent = invalidate_parent_data(conn, parent_path);
729 g_free(data->introspect);
730 data->introspect = NULL;
732 if (!dbus_connection_get_object_path_data(conn, child_path,
736 if (child == NULL || g_slist_find(data->objects, child) != NULL)
739 data->objects = g_slist_prepend(data->objects, child);
740 child->parent = data;
747 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
750 const GDBusPropertyTable *p;
752 for (p = properties; p && p->name; p++) {
753 if (strcmp(name, p->name) == 0)
760 static DBusMessage *properties_get(DBusConnection *connection,
761 DBusMessage *message, void *user_data)
763 struct generic_data *data = user_data;
764 struct interface_data *iface;
765 const GDBusPropertyTable *property;
766 const char *interface, *name;
767 DBusMessageIter iter, value;
770 if (!dbus_message_get_args(message, NULL,
771 DBUS_TYPE_STRING, &interface,
772 DBUS_TYPE_STRING, &name,
776 iface = find_interface(data->interfaces, interface);
778 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
779 "No such interface '%s'", interface);
781 property = find_property(iface->properties, name);
782 if (property == NULL)
783 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
784 "No such property '%s'", name);
786 if (property->exists != NULL &&
787 !property->exists(property, iface->user_data))
788 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
789 "No such property '%s'", name);
791 if (property->get == NULL)
792 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
793 "Property '%s' is not readable", name);
795 reply = dbus_message_new_method_return(message);
799 dbus_message_iter_init_append(reply, &iter);
800 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
801 property->type, &value);
803 if (!property->get(property, &value, iface->user_data)) {
804 dbus_message_unref(reply);
808 dbus_message_iter_close_container(&iter, &value);
813 static DBusMessage *properties_get_all(DBusConnection *connection,
814 DBusMessage *message, void *user_data)
816 struct generic_data *data = user_data;
817 struct interface_data *iface;
818 const char *interface;
819 DBusMessageIter iter;
822 if (!dbus_message_get_args(message, NULL,
823 DBUS_TYPE_STRING, &interface,
827 iface = find_interface(data->interfaces, interface);
829 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
830 "No such interface '%s'", interface);
832 reply = dbus_message_new_method_return(message);
836 dbus_message_iter_init_append(reply, &iter);
838 append_properties(iface, &iter);
843 static DBusMessage *properties_set(DBusConnection *connection,
844 DBusMessage *message, void *user_data)
846 struct generic_data *data = user_data;
847 DBusMessageIter iter, sub;
848 struct interface_data *iface;
849 const GDBusPropertyTable *property;
850 const char *name, *interface;
851 struct property_data *propdata;
853 if (!dbus_message_iter_init(message, &iter))
854 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
855 "No arguments given");
857 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
858 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
859 "Invalid argument type: '%c'",
860 dbus_message_iter_get_arg_type(&iter));
862 dbus_message_iter_get_basic(&iter, &interface);
863 dbus_message_iter_next(&iter);
865 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
866 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
867 "Invalid argument type: '%c'",
868 dbus_message_iter_get_arg_type(&iter));
870 dbus_message_iter_get_basic(&iter, &name);
871 dbus_message_iter_next(&iter);
873 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
874 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
875 "Invalid argument type: '%c'",
876 dbus_message_iter_get_arg_type(&iter));
878 dbus_message_iter_recurse(&iter, &sub);
880 iface = find_interface(data->interfaces, interface);
882 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
883 "No such interface '%s'", interface);
885 property = find_property(iface->properties, name);
886 if (property == NULL)
887 return g_dbus_create_error(message,
888 DBUS_ERROR_UNKNOWN_PROPERTY,
889 "No such property '%s'", name);
891 if (property->set == NULL)
892 return g_dbus_create_error(message,
893 DBUS_ERROR_PROPERTY_READ_ONLY,
894 "Property '%s' is not writable", name);
896 if (property->exists != NULL &&
897 !property->exists(property, iface->user_data))
898 return g_dbus_create_error(message,
899 DBUS_ERROR_UNKNOWN_PROPERTY,
900 "No such property '%s'", name);
902 propdata = g_new(struct property_data, 1);
903 propdata->id = next_pending_property++;
904 propdata->message = dbus_message_ref(message);
905 propdata->conn = connection;
906 pending_property_set = g_slist_prepend(pending_property_set, propdata);
908 property->set(property, &sub, propdata->id, iface->user_data);
913 static const GDBusMethodTable properties_methods[] = {
914 { GDBUS_METHOD("Get",
915 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
916 GDBUS_ARGS({ "value", "v" }),
918 { GDBUS_ASYNC_METHOD("Set",
919 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
923 { GDBUS_METHOD("GetAll",
924 GDBUS_ARGS({ "interface", "s" }),
925 GDBUS_ARGS({ "properties", "a{sv}" }),
926 properties_get_all) },
930 static const GDBusSignalTable properties_signals[] = {
931 { GDBUS_SIGNAL("PropertiesChanged",
932 GDBUS_ARGS({ "interface", "s" },
933 { "changed_properties", "a{sv}" },
934 { "invalidated_properties", "as"})) },
938 static void append_name(gpointer data, gpointer user_data)
941 DBusMessageIter *iter = user_data;
943 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
946 static void emit_interfaces_removed(struct generic_data *data)
949 DBusMessageIter iter, array;
951 if (root == NULL || data == root)
954 signal = dbus_message_new_signal(root->path,
955 DBUS_INTERFACE_OBJECT_MANAGER,
956 "InterfacesRemoved");
960 dbus_message_iter_init_append(signal, &iter);
961 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
963 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
964 DBUS_TYPE_STRING_AS_STRING, &array);
966 g_slist_foreach(data->removed, append_name, &array);
967 g_slist_free_full(data->removed, g_free);
968 data->removed = NULL;
970 dbus_message_iter_close_container(&iter, &array);
972 g_dbus_send_message(data->conn, signal);
975 static gboolean process_changes(gpointer user_data)
977 struct generic_data *data = user_data;
979 data->process_id = 0;
981 if (data->added != NULL)
982 emit_interfaces_added(data);
984 /* Flush pending properties */
985 if (data->pending_prop == TRUE)
986 process_property_changes(data);
988 if (data->removed != NULL)
989 emit_interfaces_removed(data);
994 static void generic_unregister(DBusConnection *connection, void *user_data)
996 struct generic_data *data = user_data;
997 struct generic_data *parent = data->parent;
1000 parent->objects = g_slist_remove(parent->objects, data);
1002 if (data->process_id > 0) {
1003 g_source_remove(data->process_id);
1004 process_changes(data);
1007 g_slist_foreach(data->objects, reset_parent, data->parent);
1008 g_slist_free(data->objects);
1010 dbus_connection_unref(data->conn);
1011 g_free(data->introspect);
1016 static DBusHandlerResult generic_message(DBusConnection *connection,
1017 DBusMessage *message, void *user_data)
1019 struct generic_data *data = user_data;
1020 struct interface_data *iface;
1021 const GDBusMethodTable *method;
1022 const char *interface;
1024 interface = dbus_message_get_interface(message);
1026 iface = find_interface(data->interfaces, interface);
1028 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1030 for (method = iface->methods; method &&
1031 method->name && method->function; method++) {
1032 gboolean experimental = method->flags &
1033 G_DBUS_METHOD_FLAG_EXPERIMENTAL;
1035 if (dbus_message_is_method_call(message, iface->name,
1036 method->name) == FALSE)
1040 const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1041 if (g_strcmp0(env, "1") != 0)
1042 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1045 if (g_dbus_args_have_signature(method->in_args,
1049 if (check_privilege(connection, message, method,
1050 iface->user_data) == TRUE)
1051 return DBUS_HANDLER_RESULT_HANDLED;
1053 return process_message(connection, message, method,
1057 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1060 static DBusObjectPathVTable generic_table = {
1061 .unregister_function = generic_unregister,
1062 .message_function = generic_message,
1065 static const GDBusMethodTable introspect_methods[] = {
1066 { GDBUS_METHOD("Introspect", NULL,
1067 GDBUS_ARGS({ "xml", "s" }), introspect) },
1071 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1073 DBusMessageIter array;
1075 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1076 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1077 DBUS_TYPE_STRING_AS_STRING
1078 DBUS_TYPE_ARRAY_AS_STRING
1079 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1080 DBUS_TYPE_STRING_AS_STRING
1081 DBUS_TYPE_VARIANT_AS_STRING
1082 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1083 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1085 g_slist_foreach(data->interfaces, append_interface, &array);
1087 dbus_message_iter_close_container(iter, &array);
1090 static void append_object(gpointer data, gpointer user_data)
1092 struct generic_data *child = data;
1093 DBusMessageIter *array = user_data;
1094 DBusMessageIter entry;
1096 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1098 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1100 append_interfaces(child, &entry);
1101 dbus_message_iter_close_container(array, &entry);
1103 g_slist_foreach(child->objects, append_object, user_data);
1106 static DBusMessage *get_objects(DBusConnection *connection,
1107 DBusMessage *message, void *user_data)
1109 struct generic_data *data = user_data;
1111 DBusMessageIter iter;
1112 DBusMessageIter array;
1114 reply = dbus_message_new_method_return(message);
1118 dbus_message_iter_init_append(reply, &iter);
1120 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1121 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1122 DBUS_TYPE_OBJECT_PATH_AS_STRING
1123 DBUS_TYPE_ARRAY_AS_STRING
1124 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1125 DBUS_TYPE_STRING_AS_STRING
1126 DBUS_TYPE_ARRAY_AS_STRING
1127 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1128 DBUS_TYPE_STRING_AS_STRING
1129 DBUS_TYPE_VARIANT_AS_STRING
1130 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1131 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1132 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1135 g_slist_foreach(data->objects, append_object, &array);
1137 dbus_message_iter_close_container(&iter, &array);
1142 static const GDBusMethodTable manager_methods[] = {
1143 { GDBUS_METHOD("GetManagedObjects", NULL,
1144 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1148 static const GDBusSignalTable manager_signals[] = {
1149 { GDBUS_SIGNAL("InterfacesAdded",
1150 GDBUS_ARGS({ "object", "o" },
1151 { "interfaces", "a{sa{sv}}" })) },
1152 { GDBUS_SIGNAL("InterfacesRemoved",
1153 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1157 static void add_interface(struct generic_data *data,
1159 const GDBusMethodTable *methods,
1160 const GDBusSignalTable *signals,
1161 const GDBusPropertyTable *properties,
1163 GDBusDestroyFunction destroy)
1165 struct interface_data *iface;
1167 iface = g_new0(struct interface_data, 1);
1168 iface->name = g_strdup(name);
1169 iface->methods = methods;
1170 iface->signals = signals;
1171 iface->properties = properties;
1172 iface->user_data = user_data;
1173 iface->destroy = destroy;
1175 data->interfaces = g_slist_append(data->interfaces, iface);
1176 if (data->parent == NULL)
1179 data->added = g_slist_append(data->added, iface);
1180 if (data->process_id > 0)
1183 data->process_id = g_idle_add(process_changes, data);
1186 static struct generic_data *object_path_ref(DBusConnection *connection,
1189 struct generic_data *data;
1191 if (dbus_connection_get_object_path_data(connection, path,
1192 (void *) &data) == TRUE) {
1199 data = g_new0(struct generic_data, 1);
1200 data->conn = dbus_connection_ref(connection);
1201 data->path = g_strdup(path);
1204 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1206 if (!dbus_connection_register_object_path(connection, path,
1207 &generic_table, data)) {
1208 g_free(data->introspect);
1213 invalidate_parent_data(connection, path);
1215 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1216 NULL, NULL, data, NULL);
1221 static void object_path_unref(DBusConnection *connection, const char *path)
1223 struct generic_data *data = NULL;
1225 if (dbus_connection_get_object_path_data(connection, path,
1226 (void *) &data) == FALSE)
1234 if (data->refcount > 0)
1237 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1238 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1240 invalidate_parent_data(data->conn, data->path);
1242 dbus_connection_unregister_object_path(data->conn, data->path);
1245 static gboolean check_signal(DBusConnection *conn, const char *path,
1246 const char *interface, const char *name,
1247 const GDBusArgInfo **args)
1249 struct generic_data *data = NULL;
1250 struct interface_data *iface;
1251 const GDBusSignalTable *signal;
1254 if (!dbus_connection_get_object_path_data(conn, path,
1255 (void *) &data) || data == NULL) {
1256 error("dbus_connection_emit_signal: path %s isn't registered",
1261 iface = find_interface(data->interfaces, interface);
1262 if (iface == NULL) {
1263 error("dbus_connection_emit_signal: %s does not implement %s",
1268 for (signal = iface->signals; signal && signal->name; signal++) {
1269 if (!strcmp(signal->name, name)) {
1270 *args = signal->args;
1275 error("No signal named %s on interface %s", name, interface);
1279 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1281 const char *interface,
1286 DBusMessage *signal;
1288 const GDBusArgInfo *args;
1290 if (!check_signal(conn, path, interface, name, &args))
1293 signal = dbus_message_new_signal(path, interface, name);
1294 if (signal == NULL) {
1295 error("Unable to allocate new %s.%s signal", interface, name);
1299 ret = dbus_message_append_args_valist(signal, first, var_args);
1303 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1304 error("%s.%s: got unexpected signature '%s'", interface, name,
1305 dbus_message_get_signature(signal));
1310 ret = dbus_connection_send(conn, signal, NULL);
1313 dbus_message_unref(signal);
1318 gboolean g_dbus_register_interface(DBusConnection *connection,
1319 const char *path, const char *name,
1320 const GDBusMethodTable *methods,
1321 const GDBusSignalTable *signals,
1322 const GDBusPropertyTable *properties,
1324 GDBusDestroyFunction destroy)
1326 struct generic_data *data;
1328 data = object_path_ref(connection, path);
1332 if (find_interface(data->interfaces, name)) {
1333 object_path_unref(connection, path);
1337 if (properties != NULL && !find_interface(data->interfaces,
1338 DBUS_INTERFACE_PROPERTIES))
1339 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1340 properties_methods, properties_signals, NULL,
1343 add_interface(data, name, methods, signals, properties, user_data,
1346 g_free(data->introspect);
1347 data->introspect = NULL;
1352 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1353 const char *path, const char *name)
1355 struct generic_data *data = NULL;
1360 if (dbus_connection_get_object_path_data(connection, path,
1361 (void *) &data) == FALSE)
1367 if (remove_interface(data, name) == FALSE)
1370 g_free(data->introspect);
1371 data->introspect = NULL;
1373 object_path_unref(connection, data->path);
1378 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1380 if (security_table != NULL)
1383 security_table = security;
1388 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1390 security_table = NULL;
1395 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1396 const char *format, va_list args)
1400 vsnprintf(str, sizeof(str), format, args);
1402 return dbus_message_new_error(message, name, str);
1405 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1406 const char *format, ...)
1411 va_start(args, format);
1413 reply = g_dbus_create_error_valist(message, name, format, args);
1420 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1421 int type, va_list args)
1425 reply = dbus_message_new_method_return(message);
1429 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1430 dbus_message_unref(reply);
1437 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1442 va_start(args, type);
1444 reply = g_dbus_create_reply_valist(message, type, args);
1451 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1455 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1456 dbus_message_set_no_reply(message, TRUE);
1458 result = dbus_connection_send(connection, message, NULL);
1460 dbus_message_unref(message);
1465 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1466 DBusMessage *message, const char *name,
1467 const char *format, va_list args)
1472 vsnprintf(str, sizeof(str), format, args);
1474 error = dbus_message_new_error(message, name, str);
1478 return g_dbus_send_message(connection, error);
1481 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1482 const char *name, const char *format, ...)
1487 va_start(args, format);
1489 result = g_dbus_send_error_valist(connection, message, name,
1497 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1498 DBusMessage *message, int type, va_list args)
1502 reply = dbus_message_new_method_return(message);
1506 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1507 dbus_message_unref(reply);
1511 return g_dbus_send_message(connection, reply);
1514 gboolean g_dbus_send_reply(DBusConnection *connection,
1515 DBusMessage *message, int type, ...)
1520 va_start(args, type);
1522 result = g_dbus_send_reply_valist(connection, message, type, args);
1529 gboolean g_dbus_emit_signal(DBusConnection *connection,
1530 const char *path, const char *interface,
1531 const char *name, int type, ...)
1536 va_start(args, type);
1538 result = emit_signal_valist(connection, path, interface,
1546 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1547 const char *path, const char *interface,
1548 const char *name, int type, va_list args)
1550 return emit_signal_valist(connection, path, interface,
1554 static void process_properties_from_interface(struct generic_data *data,
1555 struct interface_data *iface)
1558 DBusMessage *signal;
1559 DBusMessageIter iter, dict, array;
1560 GSList *invalidated;
1562 if (iface->pending_prop == NULL)
1565 signal = dbus_message_new_signal(data->path,
1566 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1567 if (signal == NULL) {
1568 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1569 ".PropertiesChanged signal");
1573 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1575 dbus_message_iter_init_append(signal, &iter);
1576 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1577 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1578 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1579 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1580 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1584 for (l = iface->pending_prop; l != NULL; l = l->next) {
1585 GDBusPropertyTable *p = l->data;
1590 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1591 invalidated = g_slist_prepend(invalidated, p);
1595 append_property(iface, p, &dict);
1598 dbus_message_iter_close_container(&iter, &dict);
1600 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1601 DBUS_TYPE_STRING_AS_STRING, &array);
1602 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1603 GDBusPropertyTable *p = l->data;
1605 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1608 g_slist_free(invalidated);
1609 dbus_message_iter_close_container(&iter, &array);
1611 g_dbus_send_message(data->conn, signal);
1613 g_slist_free(iface->pending_prop);
1614 iface->pending_prop = NULL;
1617 static void process_property_changes(struct generic_data *data)
1621 for (l = data->interfaces; l != NULL; l = l->next) {
1622 struct interface_data *iface = l->data;
1624 process_properties_from_interface(data, iface);
1627 data->pending_prop = FALSE;
1630 void g_dbus_emit_property_changed(DBusConnection *connection,
1631 const char *path, const char *interface,
1634 const GDBusPropertyTable *property;
1635 struct generic_data *data;
1636 struct interface_data *iface;
1638 if (!dbus_connection_get_object_path_data(connection, path,
1639 (void **) &data) || data == NULL)
1642 iface = find_interface(data->interfaces, interface);
1646 property = find_property(iface->properties, name);
1647 if (property == NULL) {
1648 error("Could not find property %s in %p", name,
1653 if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1656 data->pending_prop = TRUE;
1657 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1660 if (!data->process_id) {
1661 data->process_id = g_idle_add(process_changes, data);
1666 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1667 const char *interface, DBusMessageIter *iter)
1669 struct generic_data *data;
1670 struct interface_data *iface;
1672 if (!dbus_connection_get_object_path_data(connection, path,
1673 (void **) &data) || data == NULL)
1676 iface = find_interface(data->interfaces, interface);
1680 append_properties(iface, iter);
1685 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1687 struct generic_data *data;
1689 data = object_path_ref(connection, "/");
1693 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1694 manager_methods, manager_signals,
1701 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1703 if (!g_dbus_unregister_interface(connection, "/",
1704 DBUS_INTERFACE_OBJECT_MANAGER))
1712 void g_dbus_set_flags(int flags)
1714 global_flags = flags;