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 struct generic_data *root;
89 static gboolean process_changes(gpointer user_data);
90 static void process_properties_from_interface(struct generic_data *data,
91 struct interface_data *iface);
92 static void process_property_changes(struct generic_data *data);
94 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
95 const char *direction)
97 for (; args && args->name; args++) {
98 g_string_append_printf(gstr,
99 "\t\t\t<arg name=\"%s\" type=\"%s\"",
100 args->name, args->signature);
103 g_string_append_printf(gstr,
104 " direction=\"%s\"/>\n", direction);
106 g_string_append_printf(gstr, "/>\n");
111 #define G_DBUS_ANNOTATE(prefix_, name_, value_) \
112 prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
113 "value=\"" value_ "\"/>\n"
115 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
116 G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
118 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
119 G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
121 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
123 const GDBusMethodTable *method;
124 const GDBusSignalTable *signal;
125 const GDBusPropertyTable *property;
127 for (method = iface->methods; method && method->name; method++) {
128 gboolean deprecated = method->flags &
129 G_DBUS_METHOD_FLAG_DEPRECATED;
130 gboolean noreply = method->flags &
131 G_DBUS_METHOD_FLAG_NOREPLY;
133 if (!deprecated && !noreply &&
134 !(method->in_args && method->in_args->name) &&
135 !(method->out_args && method->out_args->name))
136 g_string_append_printf(gstr,
137 "\t\t<method name=\"%s\"/>\n",
140 g_string_append_printf(gstr,
141 "\t\t<method name=\"%s\">\n",
143 print_arguments(gstr, method->in_args, "in");
144 print_arguments(gstr, method->out_args, "out");
147 g_string_append_printf(gstr,
148 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
150 g_string_append_printf(gstr,
151 G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
153 g_string_append_printf(gstr, "\t\t</method>\n");
157 for (signal = iface->signals; signal && signal->name; signal++) {
158 gboolean deprecated = signal->flags &
159 G_DBUS_SIGNAL_FLAG_DEPRECATED;
161 if (!deprecated && !(signal->args && signal->args->name))
162 g_string_append_printf(gstr,
163 "\t\t<signal name=\"%s\"/>\n",
166 g_string_append_printf(gstr,
167 "\t\t<signal name=\"%s\">\n",
169 print_arguments(gstr, signal->args, NULL);
172 g_string_append_printf(gstr,
173 G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
175 g_string_append_printf(gstr, "\t\t</signal>\n");
179 for (property = iface->properties; property && property->name;
181 gboolean deprecated = property->flags &
182 G_DBUS_PROPERTY_FLAG_DEPRECATED;
184 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
185 " type=\"%s\" access=\"%s%s\"",
186 property->name, property->type,
187 property->get ? "read" : "",
188 property->set ? "write" : "");
191 g_string_append_printf(gstr, "/>\n");
193 g_string_append_printf(gstr,
194 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
198 static void generate_introspection_xml(DBusConnection *conn,
199 struct generic_data *data, const char *path)
206 g_free(data->introspect);
208 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
210 g_string_append_printf(gstr, "<node>\n");
212 for (list = data->interfaces; list; list = list->next) {
213 struct interface_data *iface = list->data;
215 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
218 generate_interface_xml(gstr, iface);
220 g_string_append_printf(gstr, "\t</interface>\n");
223 if (!dbus_connection_list_registered(conn, path, &children))
226 for (i = 0; children[i]; i++)
227 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
230 dbus_free_string_array(children);
233 g_string_append_printf(gstr, "</node>\n");
235 data->introspect = g_string_free(gstr, FALSE);
238 static DBusMessage *introspect(DBusConnection *connection,
239 DBusMessage *message, void *user_data)
241 struct generic_data *data = user_data;
244 if (data->introspect == NULL)
245 generate_introspection_xml(connection, data,
246 dbus_message_get_path(message));
248 reply = dbus_message_new_method_return(message);
252 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
258 static DBusHandlerResult process_message(DBusConnection *connection,
259 DBusMessage *message, const GDBusMethodTable *method,
260 void *iface_user_data)
264 reply = method->function(connection, message, iface_user_data);
266 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
268 dbus_message_unref(reply);
269 return DBUS_HANDLER_RESULT_HANDLED;
272 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
274 return DBUS_HANDLER_RESULT_HANDLED;
278 return DBUS_HANDLER_RESULT_NEED_MEMORY;
280 dbus_connection_send(connection, reply, NULL);
281 dbus_message_unref(reply);
283 return DBUS_HANDLER_RESULT_HANDLED;
286 static GDBusPendingReply next_pending = 1;
287 static GSList *pending_security = NULL;
289 static const GDBusSecurityTable *security_table = NULL;
291 void g_dbus_pending_success(DBusConnection *connection,
292 GDBusPendingReply pending)
296 for (list = pending_security; list; list = list->next) {
297 struct security_data *secdata = list->data;
299 if (secdata->pending != pending)
302 pending_security = g_slist_remove(pending_security, secdata);
304 process_message(connection, secdata->message,
305 secdata->method, secdata->iface_user_data);
307 dbus_message_unref(secdata->message);
313 void g_dbus_pending_error_valist(DBusConnection *connection,
314 GDBusPendingReply pending, const char *name,
315 const char *format, va_list args)
319 for (list = pending_security; list; list = list->next) {
320 struct security_data *secdata = list->data;
323 if (secdata->pending != pending)
326 pending_security = g_slist_remove(pending_security, secdata);
328 reply = g_dbus_create_error_valist(secdata->message,
331 dbus_connection_send(connection, reply, NULL);
332 dbus_message_unref(reply);
335 dbus_message_unref(secdata->message);
341 void g_dbus_pending_error(DBusConnection *connection,
342 GDBusPendingReply pending,
343 const char *name, const char *format, ...)
347 va_start(args, format);
349 g_dbus_pending_error_valist(connection, pending, name, format, args);
354 int polkit_check_authorization(DBusConnection *conn,
355 const char *action, gboolean interaction,
356 void (*function) (dbus_bool_t authorized,
358 void *user_data, int timeout);
360 struct builtin_security_data {
361 DBusConnection *conn;
362 GDBusPendingReply pending;
365 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
367 struct builtin_security_data *data = user_data;
369 if (authorized == TRUE)
370 g_dbus_pending_success(data->conn, data->pending);
372 g_dbus_pending_error(data->conn, data->pending,
373 DBUS_ERROR_AUTH_FAILED, NULL);
378 static void builtin_security_function(DBusConnection *conn,
380 gboolean interaction,
381 GDBusPendingReply pending)
383 struct builtin_security_data *data;
385 data = g_new0(struct builtin_security_data, 1);
387 data->pending = pending;
389 if (polkit_check_authorization(conn, action, interaction,
390 builtin_security_result, data, 30000) < 0)
391 g_dbus_pending_error(conn, pending, NULL, NULL);
394 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
395 const GDBusMethodTable *method, void *iface_user_data)
397 const GDBusSecurityTable *security;
399 for (security = security_table; security && security->privilege;
401 struct security_data *secdata;
402 gboolean interaction;
404 if (security->privilege != method->privilege)
407 secdata = g_new(struct security_data, 1);
408 secdata->pending = next_pending++;
409 secdata->message = dbus_message_ref(msg);
410 secdata->method = method;
411 secdata->iface_user_data = iface_user_data;
413 pending_security = g_slist_prepend(pending_security, secdata);
415 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
420 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
422 security->function(conn, security->action,
423 interaction, secdata->pending);
425 builtin_security_function(conn, security->action,
426 interaction, secdata->pending);
434 static GDBusPendingPropertySet next_pending_property = 1;
435 static GSList *pending_property_set;
437 static struct property_data *remove_pending_property_data(
438 GDBusPendingPropertySet id)
440 struct property_data *propdata;
443 for (l = pending_property_set; l != NULL; l = l->next) {
445 if (propdata->id != id)
454 pending_property_set = g_slist_delete_link(pending_property_set, l);
459 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
461 struct property_data *propdata;
463 propdata = remove_pending_property_data(id);
464 if (propdata == NULL)
467 g_dbus_send_reply(propdata->conn, propdata->message,
469 dbus_message_unref(propdata->message);
473 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
474 const char *name, const char *format,
477 struct property_data *propdata;
480 propdata = remove_pending_property_data(id);
481 if (propdata == NULL)
484 reply = g_dbus_create_error_valist(propdata->message, name, format,
487 dbus_connection_send(propdata->conn, reply, NULL);
488 dbus_message_unref(reply);
491 dbus_message_unref(propdata->message);
495 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
496 const char *format, ...)
500 va_start(args, format);
502 g_dbus_pending_property_error_valist(id, name, format, args);
507 static void reset_parent(gpointer data, gpointer user_data)
509 struct generic_data *child = data;
510 struct generic_data *parent = user_data;
512 child->parent = parent;
515 static void append_property(struct interface_data *iface,
516 const GDBusPropertyTable *p, DBusMessageIter *dict)
518 DBusMessageIter entry, value;
520 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
522 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
523 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
526 p->get(p, &value, iface->user_data);
528 dbus_message_iter_close_container(&entry, &value);
529 dbus_message_iter_close_container(dict, &entry);
532 static void append_properties(struct interface_data *data,
533 DBusMessageIter *iter)
535 DBusMessageIter dict;
536 const GDBusPropertyTable *p;
538 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
539 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
540 DBUS_TYPE_STRING_AS_STRING
541 DBUS_TYPE_VARIANT_AS_STRING
542 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
544 for (p = data->properties; p && p->name; p++) {
548 if (p->exists != NULL && !p->exists(p, data->user_data))
551 append_property(data, p, &dict);
554 dbus_message_iter_close_container(iter, &dict);
557 static void append_interface(gpointer data, gpointer user_data)
559 struct interface_data *iface = data;
560 DBusMessageIter *array = user_data;
561 DBusMessageIter entry;
563 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
565 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
566 append_properties(data, &entry);
567 dbus_message_iter_close_container(array, &entry);
570 static void emit_interfaces_added(struct generic_data *data)
573 DBusMessageIter iter, array;
575 if (root == NULL || data == root)
578 signal = dbus_message_new_signal(root->path,
579 DBUS_INTERFACE_OBJECT_MANAGER,
584 dbus_message_iter_init_append(signal, &iter);
585 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
588 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
589 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
590 DBUS_TYPE_STRING_AS_STRING
591 DBUS_TYPE_ARRAY_AS_STRING
592 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
593 DBUS_TYPE_STRING_AS_STRING
594 DBUS_TYPE_VARIANT_AS_STRING
595 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
596 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
598 g_slist_foreach(data->added, append_interface, &array);
599 g_slist_free(data->added);
602 dbus_message_iter_close_container(&iter, &array);
604 g_dbus_send_message(data->conn, signal);
607 static struct interface_data *find_interface(GSList *interfaces,
615 for (list = interfaces; list; list = list->next) {
616 struct interface_data *iface = list->data;
617 if (!strcmp(name, iface->name))
624 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
625 DBusMessage *message)
627 const char *sig = dbus_message_get_signature(message);
628 const char *p = NULL;
630 for (; args && args->signature && *sig; args++) {
633 for (; *sig && *p; sig++, p++) {
639 if (*sig || (p && *p) || (args && args->signature))
645 static gboolean remove_interface(struct generic_data *data, const char *name)
647 struct interface_data *iface;
649 iface = find_interface(data->interfaces, name);
653 process_properties_from_interface(data, iface);
655 data->interfaces = g_slist_remove(data->interfaces, iface);
657 if (iface->destroy) {
658 iface->destroy(iface->user_data);
659 iface->user_data = NULL;
663 * Interface being removed was just added, on the same mainloop
664 * iteration? Don't send any signal
666 if (g_slist_find(data->added, iface)) {
667 data->added = g_slist_remove(data->added, iface);
673 if (data->parent == NULL) {
679 data->removed = g_slist_prepend(data->removed, iface->name);
682 if (data->process_id > 0)
685 data->process_id = g_idle_add(process_changes, data);
690 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
691 const char *child_path)
693 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
694 char *parent_path, *slash;
696 parent_path = g_strdup(child_path);
697 slash = strrchr(parent_path, '/');
701 if (slash == parent_path && parent_path[1] != '\0')
702 parent_path[1] = '\0';
706 if (!strlen(parent_path))
709 if (dbus_connection_get_object_path_data(conn, parent_path,
710 (void *) &data) == FALSE) {
714 parent = invalidate_parent_data(conn, parent_path);
722 g_free(data->introspect);
723 data->introspect = NULL;
725 if (!dbus_connection_get_object_path_data(conn, child_path,
729 if (child == NULL || g_slist_find(data->objects, child) != NULL)
732 data->objects = g_slist_prepend(data->objects, child);
733 child->parent = data;
740 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
743 const GDBusPropertyTable *p;
745 for (p = properties; p && p->name; p++) {
746 if (strcmp(name, p->name) == 0)
753 static DBusMessage *properties_get(DBusConnection *connection,
754 DBusMessage *message, void *user_data)
756 struct generic_data *data = user_data;
757 struct interface_data *iface;
758 const GDBusPropertyTable *property;
759 const char *interface, *name;
760 DBusMessageIter iter, value;
763 if (!dbus_message_get_args(message, NULL,
764 DBUS_TYPE_STRING, &interface,
765 DBUS_TYPE_STRING, &name,
769 iface = find_interface(data->interfaces, interface);
771 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
772 "No such interface '%s'", interface);
774 property = find_property(iface->properties, name);
775 if (property == NULL)
776 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
777 "No such property '%s'", name);
779 if (property->exists != NULL &&
780 !property->exists(property, iface->user_data))
781 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
782 "No such property '%s'", name);
784 if (property->get == NULL)
785 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
786 "Property '%s' is not readable", name);
788 reply = dbus_message_new_method_return(message);
792 dbus_message_iter_init_append(reply, &iter);
793 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
794 property->type, &value);
796 if (!property->get(property, &value, iface->user_data)) {
797 dbus_message_unref(reply);
801 dbus_message_iter_close_container(&iter, &value);
806 static DBusMessage *properties_get_all(DBusConnection *connection,
807 DBusMessage *message, void *user_data)
809 struct generic_data *data = user_data;
810 struct interface_data *iface;
811 const char *interface;
812 DBusMessageIter iter;
815 if (!dbus_message_get_args(message, NULL,
816 DBUS_TYPE_STRING, &interface,
820 iface = find_interface(data->interfaces, interface);
822 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
823 "No such interface '%s'", interface);
825 reply = dbus_message_new_method_return(message);
829 dbus_message_iter_init_append(reply, &iter);
831 append_properties(iface, &iter);
836 static DBusMessage *properties_set(DBusConnection *connection,
837 DBusMessage *message, void *user_data)
839 struct generic_data *data = user_data;
840 DBusMessageIter iter, sub;
841 struct interface_data *iface;
842 const GDBusPropertyTable *property;
843 const char *name, *interface;
844 struct property_data *propdata;
846 if (!dbus_message_iter_init(message, &iter))
847 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
848 "No arguments given");
850 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
851 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
852 "Invalid argument type: '%c'",
853 dbus_message_iter_get_arg_type(&iter));
855 dbus_message_iter_get_basic(&iter, &interface);
856 dbus_message_iter_next(&iter);
858 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
859 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
860 "Invalid argument type: '%c'",
861 dbus_message_iter_get_arg_type(&iter));
863 dbus_message_iter_get_basic(&iter, &name);
864 dbus_message_iter_next(&iter);
866 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
867 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
868 "Invalid argument type: '%c'",
869 dbus_message_iter_get_arg_type(&iter));
871 dbus_message_iter_recurse(&iter, &sub);
873 iface = find_interface(data->interfaces, interface);
875 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
876 "No such interface '%s'", interface);
878 property = find_property(iface->properties, name);
879 if (property == NULL)
880 return g_dbus_create_error(message,
881 DBUS_ERROR_UNKNOWN_PROPERTY,
882 "No such property '%s'", name);
884 if (property->set == NULL)
885 return g_dbus_create_error(message,
886 DBUS_ERROR_PROPERTY_READ_ONLY,
887 "Property '%s' is not writable", name);
889 if (property->exists != NULL &&
890 !property->exists(property, iface->user_data))
891 return g_dbus_create_error(message,
892 DBUS_ERROR_UNKNOWN_PROPERTY,
893 "No such property '%s'", name);
895 propdata = g_new(struct property_data, 1);
896 propdata->id = next_pending_property++;
897 propdata->message = dbus_message_ref(message);
898 propdata->conn = connection;
899 pending_property_set = g_slist_prepend(pending_property_set, propdata);
901 property->set(property, &sub, propdata->id, iface->user_data);
906 static const GDBusMethodTable properties_methods[] = {
907 { GDBUS_METHOD("Get",
908 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
909 GDBUS_ARGS({ "value", "v" }),
911 { GDBUS_ASYNC_METHOD("Set",
912 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
916 { GDBUS_METHOD("GetAll",
917 GDBUS_ARGS({ "interface", "s" }),
918 GDBUS_ARGS({ "properties", "a{sv}" }),
919 properties_get_all) },
923 static const GDBusSignalTable properties_signals[] = {
924 { GDBUS_SIGNAL("PropertiesChanged",
925 GDBUS_ARGS({ "interface", "s" },
926 { "changed_properties", "a{sv}" },
927 { "invalidated_properties", "as"})) },
931 static void append_name(gpointer data, gpointer user_data)
934 DBusMessageIter *iter = user_data;
936 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
939 static void emit_interfaces_removed(struct generic_data *data)
942 DBusMessageIter iter, array;
944 if (root == NULL || data == root)
947 signal = dbus_message_new_signal(root->path,
948 DBUS_INTERFACE_OBJECT_MANAGER,
949 "InterfacesRemoved");
953 dbus_message_iter_init_append(signal, &iter);
954 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
956 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
957 DBUS_TYPE_STRING_AS_STRING, &array);
959 g_slist_foreach(data->removed, append_name, &array);
960 g_slist_free_full(data->removed, g_free);
961 data->removed = NULL;
963 dbus_message_iter_close_container(&iter, &array);
965 g_dbus_send_message(data->conn, signal);
968 static gboolean process_changes(gpointer user_data)
970 struct generic_data *data = user_data;
972 data->process_id = 0;
974 if (data->added != NULL)
975 emit_interfaces_added(data);
977 /* Flush pending properties */
978 if (data->pending_prop == TRUE)
979 process_property_changes(data);
981 if (data->removed != NULL)
982 emit_interfaces_removed(data);
987 static void generic_unregister(DBusConnection *connection, void *user_data)
989 struct generic_data *data = user_data;
990 struct generic_data *parent = data->parent;
993 parent->objects = g_slist_remove(parent->objects, data);
995 if (data->process_id > 0) {
996 g_source_remove(data->process_id);
997 process_changes(data);
1000 g_slist_foreach(data->objects, reset_parent, data->parent);
1001 g_slist_free(data->objects);
1003 dbus_connection_unref(data->conn);
1004 g_free(data->introspect);
1009 static DBusHandlerResult generic_message(DBusConnection *connection,
1010 DBusMessage *message, void *user_data)
1012 struct generic_data *data = user_data;
1013 struct interface_data *iface;
1014 const GDBusMethodTable *method;
1015 const char *interface;
1017 interface = dbus_message_get_interface(message);
1019 iface = find_interface(data->interfaces, interface);
1021 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1023 for (method = iface->methods; method &&
1024 method->name && method->function; method++) {
1025 if (dbus_message_is_method_call(message, iface->name,
1026 method->name) == FALSE)
1029 if (g_dbus_args_have_signature(method->in_args,
1033 if (check_privilege(connection, message, method,
1034 iface->user_data) == TRUE)
1035 return DBUS_HANDLER_RESULT_HANDLED;
1037 return process_message(connection, message, method,
1041 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1044 static DBusObjectPathVTable generic_table = {
1045 .unregister_function = generic_unregister,
1046 .message_function = generic_message,
1049 static const GDBusMethodTable introspect_methods[] = {
1050 { GDBUS_METHOD("Introspect", NULL,
1051 GDBUS_ARGS({ "xml", "s" }), introspect) },
1055 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1057 DBusMessageIter array;
1059 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1060 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1061 DBUS_TYPE_STRING_AS_STRING
1062 DBUS_TYPE_ARRAY_AS_STRING
1063 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1064 DBUS_TYPE_STRING_AS_STRING
1065 DBUS_TYPE_VARIANT_AS_STRING
1066 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1067 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1069 g_slist_foreach(data->interfaces, append_interface, &array);
1071 dbus_message_iter_close_container(iter, &array);
1074 static void append_object(gpointer data, gpointer user_data)
1076 struct generic_data *child = data;
1077 DBusMessageIter *array = user_data;
1078 DBusMessageIter entry;
1080 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1082 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1084 append_interfaces(child, &entry);
1085 dbus_message_iter_close_container(array, &entry);
1087 g_slist_foreach(child->objects, append_object, user_data);
1090 static DBusMessage *get_objects(DBusConnection *connection,
1091 DBusMessage *message, void *user_data)
1093 struct generic_data *data = user_data;
1095 DBusMessageIter iter;
1096 DBusMessageIter array;
1098 reply = dbus_message_new_method_return(message);
1102 dbus_message_iter_init_append(reply, &iter);
1104 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1105 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1106 DBUS_TYPE_OBJECT_PATH_AS_STRING
1107 DBUS_TYPE_ARRAY_AS_STRING
1108 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1109 DBUS_TYPE_STRING_AS_STRING
1110 DBUS_TYPE_ARRAY_AS_STRING
1111 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1112 DBUS_TYPE_STRING_AS_STRING
1113 DBUS_TYPE_VARIANT_AS_STRING
1114 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1115 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1116 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1119 g_slist_foreach(data->objects, append_object, &array);
1121 dbus_message_iter_close_container(&iter, &array);
1126 static const GDBusMethodTable manager_methods[] = {
1127 { GDBUS_METHOD("GetManagedObjects", NULL,
1128 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1132 static const GDBusSignalTable manager_signals[] = {
1133 { GDBUS_SIGNAL("InterfacesAdded",
1134 GDBUS_ARGS({ "object", "o" },
1135 { "interfaces", "a{sa{sv}}" })) },
1136 { GDBUS_SIGNAL("InterfacesRemoved",
1137 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1141 static void add_interface(struct generic_data *data,
1143 const GDBusMethodTable *methods,
1144 const GDBusSignalTable *signals,
1145 const GDBusPropertyTable *properties,
1147 GDBusDestroyFunction destroy)
1149 struct interface_data *iface;
1151 iface = g_new0(struct interface_data, 1);
1152 iface->name = g_strdup(name);
1153 iface->methods = methods;
1154 iface->signals = signals;
1155 iface->properties = properties;
1156 iface->user_data = user_data;
1157 iface->destroy = destroy;
1159 data->interfaces = g_slist_append(data->interfaces, iface);
1160 if (data->parent == NULL)
1163 data->added = g_slist_append(data->added, iface);
1164 if (data->process_id > 0)
1167 data->process_id = g_idle_add(process_changes, data);
1170 static struct generic_data *object_path_ref(DBusConnection *connection,
1173 struct generic_data *data;
1175 if (dbus_connection_get_object_path_data(connection, path,
1176 (void *) &data) == TRUE) {
1183 data = g_new0(struct generic_data, 1);
1184 data->conn = dbus_connection_ref(connection);
1185 data->path = g_strdup(path);
1188 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1190 if (!dbus_connection_register_object_path(connection, path,
1191 &generic_table, data)) {
1192 g_free(data->introspect);
1197 invalidate_parent_data(connection, path);
1199 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1200 NULL, NULL, data, NULL);
1205 static void object_path_unref(DBusConnection *connection, const char *path)
1207 struct generic_data *data = NULL;
1209 if (dbus_connection_get_object_path_data(connection, path,
1210 (void *) &data) == FALSE)
1218 if (data->refcount > 0)
1221 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1222 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1224 invalidate_parent_data(data->conn, data->path);
1226 dbus_connection_unregister_object_path(data->conn, data->path);
1229 static gboolean check_signal(DBusConnection *conn, const char *path,
1230 const char *interface, const char *name,
1231 const GDBusArgInfo **args)
1233 struct generic_data *data = NULL;
1234 struct interface_data *iface;
1235 const GDBusSignalTable *signal;
1238 if (!dbus_connection_get_object_path_data(conn, path,
1239 (void *) &data) || data == NULL) {
1240 error("dbus_connection_emit_signal: path %s isn't registered",
1245 iface = find_interface(data->interfaces, interface);
1246 if (iface == NULL) {
1247 error("dbus_connection_emit_signal: %s does not implement %s",
1252 for (signal = iface->signals; signal && signal->name; signal++) {
1253 if (!strcmp(signal->name, name)) {
1254 *args = signal->args;
1259 error("No signal named %s on interface %s", name, interface);
1263 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1265 const char *interface,
1270 DBusMessage *signal;
1272 const GDBusArgInfo *args;
1274 if (!check_signal(conn, path, interface, name, &args))
1277 signal = dbus_message_new_signal(path, interface, name);
1278 if (signal == NULL) {
1279 error("Unable to allocate new %s.%s signal", interface, name);
1283 ret = dbus_message_append_args_valist(signal, first, var_args);
1287 if (g_dbus_args_have_signature(args, signal) == FALSE) {
1288 error("%s.%s: got unexpected signature '%s'", interface, name,
1289 dbus_message_get_signature(signal));
1294 ret = dbus_connection_send(conn, signal, NULL);
1297 dbus_message_unref(signal);
1302 gboolean g_dbus_register_interface(DBusConnection *connection,
1303 const char *path, const char *name,
1304 const GDBusMethodTable *methods,
1305 const GDBusSignalTable *signals,
1306 const GDBusPropertyTable *properties,
1308 GDBusDestroyFunction destroy)
1310 struct generic_data *data;
1312 data = object_path_ref(connection, path);
1316 if (find_interface(data->interfaces, name)) {
1317 object_path_unref(connection, path);
1321 if (properties != NULL && !find_interface(data->interfaces,
1322 DBUS_INTERFACE_PROPERTIES))
1323 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1324 properties_methods, properties_signals, NULL,
1327 add_interface(data, name, methods, signals, properties, user_data,
1330 g_free(data->introspect);
1331 data->introspect = NULL;
1336 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1337 const char *path, const char *name)
1339 struct generic_data *data = NULL;
1344 if (dbus_connection_get_object_path_data(connection, path,
1345 (void *) &data) == FALSE)
1351 if (remove_interface(data, name) == FALSE)
1354 g_free(data->introspect);
1355 data->introspect = NULL;
1357 object_path_unref(connection, data->path);
1362 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1364 if (security_table != NULL)
1367 security_table = security;
1372 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1374 security_table = NULL;
1379 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1380 const char *format, va_list args)
1384 vsnprintf(str, sizeof(str), format, args);
1386 return dbus_message_new_error(message, name, str);
1389 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1390 const char *format, ...)
1395 va_start(args, format);
1397 reply = g_dbus_create_error_valist(message, name, format, args);
1404 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1405 int type, va_list args)
1409 reply = dbus_message_new_method_return(message);
1413 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1414 dbus_message_unref(reply);
1421 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1426 va_start(args, type);
1428 reply = g_dbus_create_reply_valist(message, type, args);
1435 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1439 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1440 dbus_message_set_no_reply(message, TRUE);
1442 result = dbus_connection_send(connection, message, NULL);
1444 dbus_message_unref(message);
1449 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1450 DBusMessage *message, const char *name,
1451 const char *format, va_list args)
1456 vsnprintf(str, sizeof(str), format, args);
1458 error = dbus_message_new_error(message, name, str);
1462 return g_dbus_send_message(connection, error);
1465 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1466 const char *name, const char *format, ...)
1471 va_start(args, format);
1473 result = g_dbus_send_error_valist(connection, message, name,
1481 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1482 DBusMessage *message, int type, va_list args)
1486 reply = dbus_message_new_method_return(message);
1490 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1491 dbus_message_unref(reply);
1495 return g_dbus_send_message(connection, reply);
1498 gboolean g_dbus_send_reply(DBusConnection *connection,
1499 DBusMessage *message, int type, ...)
1504 va_start(args, type);
1506 result = g_dbus_send_reply_valist(connection, message, type, args);
1513 gboolean g_dbus_emit_signal(DBusConnection *connection,
1514 const char *path, const char *interface,
1515 const char *name, int type, ...)
1520 va_start(args, type);
1522 result = emit_signal_valist(connection, path, interface,
1530 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1531 const char *path, const char *interface,
1532 const char *name, int type, va_list args)
1534 return emit_signal_valist(connection, path, interface,
1538 static void process_properties_from_interface(struct generic_data *data,
1539 struct interface_data *iface)
1542 DBusMessage *signal;
1543 DBusMessageIter iter, dict, array;
1544 GSList *invalidated;
1546 if (iface->pending_prop == NULL)
1549 signal = dbus_message_new_signal(data->path,
1550 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1551 if (signal == NULL) {
1552 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1553 ".PropertiesChanged signal");
1557 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1559 dbus_message_iter_init_append(signal, &iter);
1560 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1561 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1562 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1563 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1564 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1568 for (l = iface->pending_prop; l != NULL; l = l->next) {
1569 GDBusPropertyTable *p = l->data;
1574 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1575 invalidated = g_slist_prepend(invalidated, p);
1579 append_property(iface, p, &dict);
1582 dbus_message_iter_close_container(&iter, &dict);
1584 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1585 DBUS_TYPE_STRING_AS_STRING, &array);
1586 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1587 GDBusPropertyTable *p = l->data;
1589 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1592 g_slist_free(invalidated);
1593 dbus_message_iter_close_container(&iter, &array);
1595 g_dbus_send_message(data->conn, signal);
1597 g_slist_free(iface->pending_prop);
1598 iface->pending_prop = NULL;
1601 static void process_property_changes(struct generic_data *data)
1605 for (l = data->interfaces; l != NULL; l = l->next) {
1606 struct interface_data *iface = l->data;
1608 process_properties_from_interface(data, iface);
1611 data->pending_prop = FALSE;
1614 void g_dbus_emit_property_changed(DBusConnection *connection,
1615 const char *path, const char *interface,
1618 const GDBusPropertyTable *property;
1619 struct generic_data *data;
1620 struct interface_data *iface;
1622 if (!dbus_connection_get_object_path_data(connection, path,
1623 (void **) &data) || data == NULL)
1626 iface = find_interface(data->interfaces, interface);
1630 property = find_property(iface->properties, name);
1631 if (property == NULL) {
1632 error("Could not find property %s in %p", name,
1637 if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1640 data->pending_prop = TRUE;
1641 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1644 if (!data->process_id) {
1645 data->process_id = g_idle_add(process_changes, data);
1650 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1651 const char *interface, DBusMessageIter *iter)
1653 struct generic_data *data;
1654 struct interface_data *iface;
1656 if (!dbus_connection_get_object_path_data(connection, path,
1657 (void **) &data) || data == NULL)
1660 iface = find_interface(data->interfaces, interface);
1664 append_properties(iface, iter);
1669 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1671 struct generic_data *data;
1673 data = object_path_ref(connection, "/");
1677 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1678 manager_methods, manager_signals,
1685 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1687 if (!g_dbus_unregister_interface(connection, "/",
1688 DBUS_INTERFACE_OBJECT_MANAGER))