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;
89 static GSList *pending = NULL;
91 static gboolean process_changes(gpointer user_data);
92 static void process_properties_from_interface(struct generic_data *data,
93 struct interface_data *iface);
94 static void process_property_changes(struct generic_data *data);
96 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
97 const char *direction)
99 for (; args && args->name; args++) {
100 g_string_append_printf(gstr,
101 "<arg name=\"%s\" type=\"%s\"",
102 args->name, args->signature);
105 g_string_append_printf(gstr,
106 " direction=\"%s\"/>\n", direction);
108 g_string_append_printf(gstr, "/>\n");
113 #define G_DBUS_ANNOTATE(name_, value_) \
114 "<annotation name=\"org.freedesktop.DBus." name_ "\" " \
115 "value=\"" value_ "\"/>"
117 #define G_DBUS_ANNOTATE_DEPRECATED \
118 G_DBUS_ANNOTATE("Deprecated", "true")
120 #define G_DBUS_ANNOTATE_NOREPLY \
121 G_DBUS_ANNOTATE("Method.NoReply", "true")
123 static gboolean check_experimental(int flags, int flag)
128 return !(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL);
131 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
133 const GDBusMethodTable *method;
134 const GDBusSignalTable *signal;
135 const GDBusPropertyTable *property;
137 for (method = iface->methods; method && method->name; method++) {
138 if (check_experimental(method->flags,
139 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
142 g_string_append_printf(gstr, "<method name=\"%s\">",
144 print_arguments(gstr, method->in_args, "in");
145 print_arguments(gstr, method->out_args, "out");
147 if (method->flags & G_DBUS_METHOD_FLAG_DEPRECATED)
148 g_string_append_printf(gstr,
149 G_DBUS_ANNOTATE_DEPRECATED);
151 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY)
152 g_string_append_printf(gstr, G_DBUS_ANNOTATE_NOREPLY);
154 g_string_append_printf(gstr, "</method>");
157 for (signal = iface->signals; signal && signal->name; signal++) {
158 if (check_experimental(signal->flags,
159 G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
162 g_string_append_printf(gstr, "<signal name=\"%s\">",
164 print_arguments(gstr, signal->args, NULL);
166 if (signal->flags & G_DBUS_SIGNAL_FLAG_DEPRECATED)
167 g_string_append_printf(gstr,
168 G_DBUS_ANNOTATE_DEPRECATED);
170 g_string_append_printf(gstr, "</signal>\n");
173 for (property = iface->properties; property && property->name;
175 if (check_experimental(property->flags,
176 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
179 g_string_append_printf(gstr, "<property name=\"%s\""
180 " type=\"%s\" access=\"%s%s\">",
181 property->name, property->type,
182 property->get ? "read" : "",
183 property->set ? "write" : "");
185 if (property->flags & G_DBUS_PROPERTY_FLAG_DEPRECATED)
186 g_string_append_printf(gstr,
187 G_DBUS_ANNOTATE_DEPRECATED);
189 g_string_append_printf(gstr, "</property>");
193 static void generate_introspection_xml(DBusConnection *conn,
194 struct generic_data *data, const char *path)
201 g_free(data->introspect);
203 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
205 g_string_append_printf(gstr, "<node>");
207 for (list = data->interfaces; list; list = list->next) {
208 struct interface_data *iface = list->data;
210 g_string_append_printf(gstr, "<interface name=\"%s\">",
213 generate_interface_xml(gstr, iface);
215 g_string_append_printf(gstr, "</interface>");
218 if (!dbus_connection_list_registered(conn, path, &children))
221 for (i = 0; children[i]; i++)
222 g_string_append_printf(gstr, "<node name=\"%s\"/>",
225 dbus_free_string_array(children);
228 g_string_append_printf(gstr, "</node>");
230 data->introspect = g_string_free(gstr, FALSE);
233 static DBusMessage *introspect(DBusConnection *connection,
234 DBusMessage *message, void *user_data)
236 struct generic_data *data = user_data;
239 if (data->introspect == NULL)
240 generate_introspection_xml(connection, data,
241 dbus_message_get_path(message));
243 reply = dbus_message_new_method_return(message);
247 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
253 static DBusHandlerResult process_message(DBusConnection *connection,
254 DBusMessage *message, const GDBusMethodTable *method,
255 void *iface_user_data)
259 reply = method->function(connection, message, iface_user_data);
261 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY ||
262 dbus_message_get_no_reply(message)) {
264 dbus_message_unref(reply);
265 return DBUS_HANDLER_RESULT_HANDLED;
268 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
270 return DBUS_HANDLER_RESULT_HANDLED;
274 return DBUS_HANDLER_RESULT_NEED_MEMORY;
276 g_dbus_send_message(connection, reply);
278 return DBUS_HANDLER_RESULT_HANDLED;
281 static GDBusPendingReply next_pending = 1;
282 static GSList *pending_security = NULL;
284 static const GDBusSecurityTable *security_table = NULL;
286 void g_dbus_pending_success(DBusConnection *connection,
287 GDBusPendingReply pending)
291 for (list = pending_security; list; list = list->next) {
292 struct security_data *secdata = list->data;
294 if (secdata->pending != pending)
297 pending_security = g_slist_remove(pending_security, secdata);
299 process_message(connection, secdata->message,
300 secdata->method, secdata->iface_user_data);
302 dbus_message_unref(secdata->message);
308 void g_dbus_pending_error_valist(DBusConnection *connection,
309 GDBusPendingReply pending, const char *name,
310 const char *format, va_list args)
314 for (list = pending_security; list; list = list->next) {
315 struct security_data *secdata = list->data;
317 if (secdata->pending != pending)
320 pending_security = g_slist_remove(pending_security, secdata);
322 g_dbus_send_error_valist(connection, secdata->message,
325 dbus_message_unref(secdata->message);
331 void g_dbus_pending_error(DBusConnection *connection,
332 GDBusPendingReply pending,
333 const char *name, const char *format, ...)
337 va_start(args, format);
339 g_dbus_pending_error_valist(connection, pending, name, format, args);
344 int polkit_check_authorization(DBusConnection *conn,
345 const char *action, gboolean interaction,
346 void (*function) (dbus_bool_t authorized,
348 void *user_data, int timeout);
350 struct builtin_security_data {
351 DBusConnection *conn;
352 GDBusPendingReply pending;
355 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
357 struct builtin_security_data *data = user_data;
359 if (authorized == TRUE)
360 g_dbus_pending_success(data->conn, data->pending);
362 g_dbus_pending_error(data->conn, data->pending,
363 DBUS_ERROR_AUTH_FAILED, NULL);
368 static void builtin_security_function(DBusConnection *conn,
370 gboolean interaction,
371 GDBusPendingReply pending)
373 struct builtin_security_data *data;
375 data = g_new0(struct builtin_security_data, 1);
377 data->pending = pending;
379 if (polkit_check_authorization(conn, action, interaction,
380 builtin_security_result, data, 30000) < 0)
381 g_dbus_pending_error(conn, pending, NULL, NULL);
384 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
385 const GDBusMethodTable *method, void *iface_user_data)
387 const GDBusSecurityTable *security;
389 for (security = security_table; security && security->privilege;
391 struct security_data *secdata;
392 gboolean interaction;
394 if (security->privilege != method->privilege)
397 secdata = g_new(struct security_data, 1);
398 secdata->pending = next_pending++;
399 secdata->message = dbus_message_ref(msg);
400 secdata->method = method;
401 secdata->iface_user_data = iface_user_data;
403 pending_security = g_slist_prepend(pending_security, secdata);
405 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
410 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
412 security->function(conn, security->action,
413 interaction, secdata->pending);
415 builtin_security_function(conn, security->action,
416 interaction, secdata->pending);
424 static GDBusPendingPropertySet next_pending_property = 1;
425 static GSList *pending_property_set;
427 static struct property_data *remove_pending_property_data(
428 GDBusPendingPropertySet id)
430 struct property_data *propdata;
433 for (l = pending_property_set; l != NULL; l = l->next) {
435 if (propdata->id != id)
444 pending_property_set = g_slist_delete_link(pending_property_set, l);
449 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
451 struct property_data *propdata;
453 propdata = remove_pending_property_data(id);
454 if (propdata == NULL)
457 g_dbus_send_reply(propdata->conn, propdata->message,
459 dbus_message_unref(propdata->message);
463 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
464 const char *name, const char *format,
467 struct property_data *propdata;
469 propdata = remove_pending_property_data(id);
470 if (propdata == NULL)
473 g_dbus_send_error_valist(propdata->conn, propdata->message, name,
476 dbus_message_unref(propdata->message);
480 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
481 const char *format, ...)
485 va_start(args, format);
487 g_dbus_pending_property_error_valist(id, name, format, args);
492 static void reset_parent(gpointer data, gpointer user_data)
494 struct generic_data *child = data;
495 struct generic_data *parent = user_data;
497 child->parent = parent;
500 static void append_property(struct interface_data *iface,
501 const GDBusPropertyTable *p, DBusMessageIter *dict)
503 DBusMessageIter entry, value;
505 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
507 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
508 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
511 p->get(p, &value, iface->user_data);
513 dbus_message_iter_close_container(&entry, &value);
514 dbus_message_iter_close_container(dict, &entry);
517 static void append_properties(struct interface_data *data,
518 DBusMessageIter *iter)
520 DBusMessageIter dict;
521 const GDBusPropertyTable *p;
523 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
524 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
525 DBUS_TYPE_STRING_AS_STRING
526 DBUS_TYPE_VARIANT_AS_STRING
527 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
529 for (p = data->properties; p && p->name; p++) {
530 if (check_experimental(p->flags,
531 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
537 if (p->exists != NULL && !p->exists(p, data->user_data))
540 append_property(data, p, &dict);
543 dbus_message_iter_close_container(iter, &dict);
546 static void append_interface(gpointer data, gpointer user_data)
548 struct interface_data *iface = data;
549 DBusMessageIter *array = user_data;
550 DBusMessageIter entry;
552 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
554 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
555 append_properties(data, &entry);
556 dbus_message_iter_close_container(array, &entry);
559 static void emit_interfaces_added(struct generic_data *data)
562 DBusMessageIter iter, array;
564 if (root == NULL || data == root)
567 signal = dbus_message_new_signal(root->path,
568 DBUS_INTERFACE_OBJECT_MANAGER,
573 dbus_message_iter_init_append(signal, &iter);
574 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
577 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
578 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
579 DBUS_TYPE_STRING_AS_STRING
580 DBUS_TYPE_ARRAY_AS_STRING
581 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
582 DBUS_TYPE_STRING_AS_STRING
583 DBUS_TYPE_VARIANT_AS_STRING
584 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
585 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
587 g_slist_foreach(data->added, append_interface, &array);
588 g_slist_free(data->added);
591 dbus_message_iter_close_container(&iter, &array);
593 /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
594 dbus_connection_send(data->conn, signal, NULL);
595 dbus_message_unref(signal);
598 static struct interface_data *find_interface(GSList *interfaces,
606 for (list = interfaces; list; list = list->next) {
607 struct interface_data *iface = list->data;
608 if (!strcmp(name, iface->name))
615 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
616 DBusMessage *message)
618 const char *sig = dbus_message_get_signature(message);
619 const char *p = NULL;
621 for (; args && args->signature && *sig; args++) {
624 for (; *sig && *p; sig++, p++) {
630 if (*sig || (p && *p) || (args && args->signature))
636 static void add_pending(struct generic_data *data)
638 if (data->process_id > 0)
641 data->process_id = g_idle_add(process_changes, data);
643 pending = g_slist_append(pending, data);
646 static gboolean remove_interface(struct generic_data *data, const char *name)
648 struct interface_data *iface;
650 iface = find_interface(data->interfaces, name);
654 process_properties_from_interface(data, iface);
656 data->interfaces = g_slist_remove(data->interfaces, iface);
658 if (iface->destroy) {
659 iface->destroy(iface->user_data);
660 iface->user_data = NULL;
664 * Interface being removed was just added, on the same mainloop
665 * iteration? Don't send any signal
667 if (g_slist_find(data->added, iface)) {
668 data->added = g_slist_remove(data->added, iface);
674 if (data->parent == NULL) {
680 data->removed = g_slist_prepend(data->removed, iface->name);
688 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
689 const char *child_path)
691 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
692 char *parent_path, *slash;
694 parent_path = g_strdup(child_path);
695 slash = strrchr(parent_path, '/');
699 if (slash == parent_path && parent_path[1] != '\0')
700 parent_path[1] = '\0';
704 if (!strlen(parent_path))
707 if (dbus_connection_get_object_path_data(conn, parent_path,
708 (void *) &data) == FALSE) {
712 parent = invalidate_parent_data(conn, parent_path);
720 g_free(data->introspect);
721 data->introspect = NULL;
723 if (!dbus_connection_get_object_path_data(conn, child_path,
727 if (child == NULL || g_slist_find(data->objects, child) != NULL)
730 data->objects = g_slist_prepend(data->objects, child);
731 child->parent = data;
738 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
741 const GDBusPropertyTable *p;
743 for (p = properties; p && p->name; p++) {
744 if (strcmp(name, p->name) != 0)
747 if (check_experimental(p->flags,
748 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
757 static DBusMessage *properties_get(DBusConnection *connection,
758 DBusMessage *message, void *user_data)
760 struct generic_data *data = user_data;
761 struct interface_data *iface;
762 const GDBusPropertyTable *property;
763 const char *interface, *name;
764 DBusMessageIter iter, value;
767 if (!dbus_message_get_args(message, NULL,
768 DBUS_TYPE_STRING, &interface,
769 DBUS_TYPE_STRING, &name,
773 iface = find_interface(data->interfaces, interface);
775 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
776 "No such interface '%s'", interface);
778 property = find_property(iface->properties, name);
779 if (property == NULL)
780 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
781 "No such property '%s'", name);
783 if (property->exists != NULL &&
784 !property->exists(property, iface->user_data))
785 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
786 "No such property '%s'", name);
788 if (property->get == NULL)
789 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
790 "Property '%s' is not readable", name);
792 reply = dbus_message_new_method_return(message);
796 dbus_message_iter_init_append(reply, &iter);
797 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
798 property->type, &value);
800 if (!property->get(property, &value, iface->user_data)) {
801 dbus_message_unref(reply);
805 dbus_message_iter_close_container(&iter, &value);
810 static DBusMessage *properties_get_all(DBusConnection *connection,
811 DBusMessage *message, void *user_data)
813 struct generic_data *data = user_data;
814 struct interface_data *iface;
815 const char *interface;
816 DBusMessageIter iter;
819 if (!dbus_message_get_args(message, NULL,
820 DBUS_TYPE_STRING, &interface,
824 iface = find_interface(data->interfaces, interface);
826 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
827 "No such interface '%s'", interface);
829 reply = dbus_message_new_method_return(message);
833 dbus_message_iter_init_append(reply, &iter);
835 append_properties(iface, &iter);
840 static DBusMessage *properties_set(DBusConnection *connection,
841 DBusMessage *message, void *user_data)
843 struct generic_data *data = user_data;
844 DBusMessageIter iter, sub;
845 struct interface_data *iface;
846 const GDBusPropertyTable *property;
847 const char *name, *interface;
848 struct property_data *propdata;
849 gboolean valid_signature;
852 if (!dbus_message_iter_init(message, &iter))
853 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
854 "No arguments given");
856 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
857 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
858 "Invalid argument type: '%c'",
859 dbus_message_iter_get_arg_type(&iter));
861 dbus_message_iter_get_basic(&iter, &interface);
862 dbus_message_iter_next(&iter);
864 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
865 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
866 "Invalid argument type: '%c'",
867 dbus_message_iter_get_arg_type(&iter));
869 dbus_message_iter_get_basic(&iter, &name);
870 dbus_message_iter_next(&iter);
872 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
873 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
874 "Invalid argument type: '%c'",
875 dbus_message_iter_get_arg_type(&iter));
877 dbus_message_iter_recurse(&iter, &sub);
879 iface = find_interface(data->interfaces, interface);
881 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
882 "No such interface '%s'", interface);
884 property = find_property(iface->properties, name);
885 if (property == NULL)
886 return g_dbus_create_error(message,
887 DBUS_ERROR_UNKNOWN_PROPERTY,
888 "No such property '%s'", name);
890 if (property->set == NULL)
891 return g_dbus_create_error(message,
892 DBUS_ERROR_PROPERTY_READ_ONLY,
893 "Property '%s' is not writable", name);
895 if (property->exists != NULL &&
896 !property->exists(property, iface->user_data))
897 return g_dbus_create_error(message,
898 DBUS_ERROR_UNKNOWN_PROPERTY,
899 "No such property '%s'", name);
901 signature = dbus_message_iter_get_signature(&sub);
902 valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
903 dbus_free(signature);
904 if (!valid_signature)
905 return g_dbus_create_error(message,
906 DBUS_ERROR_INVALID_SIGNATURE,
907 "Invalid signature for '%s'", name);
909 propdata = g_new(struct property_data, 1);
910 propdata->id = next_pending_property++;
911 propdata->message = dbus_message_ref(message);
912 propdata->conn = connection;
913 pending_property_set = g_slist_prepend(pending_property_set, propdata);
915 property->set(property, &sub, propdata->id, iface->user_data);
920 static const GDBusMethodTable properties_methods[] = {
921 { GDBUS_METHOD("Get",
922 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
923 GDBUS_ARGS({ "value", "v" }),
925 { GDBUS_ASYNC_METHOD("Set",
926 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
930 { GDBUS_METHOD("GetAll",
931 GDBUS_ARGS({ "interface", "s" }),
932 GDBUS_ARGS({ "properties", "a{sv}" }),
933 properties_get_all) },
937 static const GDBusSignalTable properties_signals[] = {
938 { GDBUS_SIGNAL("PropertiesChanged",
939 GDBUS_ARGS({ "interface", "s" },
940 { "changed_properties", "a{sv}" },
941 { "invalidated_properties", "as"})) },
945 static void append_name(gpointer data, gpointer user_data)
948 DBusMessageIter *iter = user_data;
950 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
953 static void emit_interfaces_removed(struct generic_data *data)
956 DBusMessageIter iter, array;
958 if (root == NULL || data == root)
961 signal = dbus_message_new_signal(root->path,
962 DBUS_INTERFACE_OBJECT_MANAGER,
963 "InterfacesRemoved");
967 dbus_message_iter_init_append(signal, &iter);
968 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
970 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
971 DBUS_TYPE_STRING_AS_STRING, &array);
973 g_slist_foreach(data->removed, append_name, &array);
974 g_slist_free_full(data->removed, g_free);
975 data->removed = NULL;
977 dbus_message_iter_close_container(&iter, &array);
979 /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
980 dbus_connection_send(data->conn, signal, NULL);
981 dbus_message_unref(signal);
984 static void remove_pending(struct generic_data *data)
986 if (data->process_id > 0) {
987 g_source_remove(data->process_id);
988 data->process_id = 0;
991 pending = g_slist_remove(pending, data);
994 static gboolean process_changes(gpointer user_data)
996 struct generic_data *data = user_data;
998 remove_pending(data);
1000 if (data->added != NULL)
1001 emit_interfaces_added(data);
1003 /* Flush pending properties */
1004 if (data->pending_prop == TRUE)
1005 process_property_changes(data);
1007 if (data->removed != NULL)
1008 emit_interfaces_removed(data);
1010 data->process_id = 0;
1015 static void generic_unregister(DBusConnection *connection, void *user_data)
1017 struct generic_data *data = user_data;
1018 struct generic_data *parent = data->parent;
1021 parent->objects = g_slist_remove(parent->objects, data);
1023 if (data->process_id > 0) {
1024 g_source_remove(data->process_id);
1025 data->process_id = 0;
1026 process_changes(data);
1029 g_slist_foreach(data->objects, reset_parent, data->parent);
1030 g_slist_free(data->objects);
1032 dbus_connection_unref(data->conn);
1033 g_free(data->introspect);
1038 static DBusHandlerResult generic_message(DBusConnection *connection,
1039 DBusMessage *message, void *user_data)
1041 struct generic_data *data = user_data;
1042 struct interface_data *iface;
1043 const GDBusMethodTable *method;
1044 const char *interface;
1046 interface = dbus_message_get_interface(message);
1048 iface = find_interface(data->interfaces, interface);
1050 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1052 for (method = iface->methods; method &&
1053 method->name && method->function; method++) {
1055 if (dbus_message_is_method_call(message, iface->name,
1056 method->name) == FALSE)
1059 if (check_experimental(method->flags,
1060 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1061 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1063 if (g_dbus_args_have_signature(method->in_args,
1067 if (check_privilege(connection, message, method,
1068 iface->user_data) == TRUE)
1069 return DBUS_HANDLER_RESULT_HANDLED;
1071 return process_message(connection, message, method,
1075 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1078 static DBusObjectPathVTable generic_table = {
1079 .unregister_function = generic_unregister,
1080 .message_function = generic_message,
1083 static const GDBusMethodTable introspect_methods[] = {
1084 { GDBUS_METHOD("Introspect", NULL,
1085 GDBUS_ARGS({ "xml", "s" }), introspect) },
1089 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1091 DBusMessageIter array;
1093 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1094 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1095 DBUS_TYPE_STRING_AS_STRING
1096 DBUS_TYPE_ARRAY_AS_STRING
1097 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1098 DBUS_TYPE_STRING_AS_STRING
1099 DBUS_TYPE_VARIANT_AS_STRING
1100 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1101 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1103 g_slist_foreach(data->interfaces, append_interface, &array);
1105 dbus_message_iter_close_container(iter, &array);
1108 static void append_object(gpointer data, gpointer user_data)
1110 struct generic_data *child = data;
1111 DBusMessageIter *array = user_data;
1112 DBusMessageIter entry;
1114 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1116 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1118 append_interfaces(child, &entry);
1119 dbus_message_iter_close_container(array, &entry);
1121 g_slist_foreach(child->objects, append_object, user_data);
1124 static DBusMessage *get_objects(DBusConnection *connection,
1125 DBusMessage *message, void *user_data)
1127 struct generic_data *data = user_data;
1129 DBusMessageIter iter;
1130 DBusMessageIter array;
1132 reply = dbus_message_new_method_return(message);
1136 dbus_message_iter_init_append(reply, &iter);
1138 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1139 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1140 DBUS_TYPE_OBJECT_PATH_AS_STRING
1141 DBUS_TYPE_ARRAY_AS_STRING
1142 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1143 DBUS_TYPE_STRING_AS_STRING
1144 DBUS_TYPE_ARRAY_AS_STRING
1145 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1146 DBUS_TYPE_STRING_AS_STRING
1147 DBUS_TYPE_VARIANT_AS_STRING
1148 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1149 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1150 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1153 g_slist_foreach(data->objects, append_object, &array);
1155 dbus_message_iter_close_container(&iter, &array);
1160 static const GDBusMethodTable manager_methods[] = {
1161 { GDBUS_METHOD("GetManagedObjects", NULL,
1162 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1166 static const GDBusSignalTable manager_signals[] = {
1167 { GDBUS_SIGNAL("InterfacesAdded",
1168 GDBUS_ARGS({ "object", "o" },
1169 { "interfaces", "a{sa{sv}}" })) },
1170 { GDBUS_SIGNAL("InterfacesRemoved",
1171 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1175 static gboolean add_interface(struct generic_data *data,
1177 const GDBusMethodTable *methods,
1178 const GDBusSignalTable *signals,
1179 const GDBusPropertyTable *properties,
1181 GDBusDestroyFunction destroy)
1183 struct interface_data *iface;
1184 const GDBusMethodTable *method;
1185 const GDBusSignalTable *signal;
1186 const GDBusPropertyTable *property;
1188 for (method = methods; method && method->name; method++) {
1189 if (!check_experimental(method->flags,
1190 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1194 for (signal = signals; signal && signal->name; signal++) {
1195 if (!check_experimental(signal->flags,
1196 G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1200 for (property = properties; property && property->name; property++) {
1201 if (!check_experimental(property->flags,
1202 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1206 /* Nothing to register */
1210 iface = g_new0(struct interface_data, 1);
1211 iface->name = g_strdup(name);
1212 iface->methods = methods;
1213 iface->signals = signals;
1214 iface->properties = properties;
1215 iface->user_data = user_data;
1216 iface->destroy = destroy;
1218 data->interfaces = g_slist_append(data->interfaces, iface);
1219 if (data->parent == NULL)
1222 data->added = g_slist_append(data->added, iface);
1229 static struct generic_data *object_path_ref(DBusConnection *connection,
1232 struct generic_data *data;
1234 if (dbus_connection_get_object_path_data(connection, path,
1235 (void *) &data) == TRUE) {
1242 data = g_new0(struct generic_data, 1);
1243 data->conn = dbus_connection_ref(connection);
1244 data->path = g_strdup(path);
1247 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1249 if (!dbus_connection_register_object_path(connection, path,
1250 &generic_table, data)) {
1251 #if !defined TIZEN_EXT
1252 dbus_connection_unref(data->conn);
1255 g_free(data->introspect);
1260 invalidate_parent_data(connection, path);
1262 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1263 NULL, NULL, data, NULL);
1268 static void object_path_unref(DBusConnection *connection, const char *path)
1270 struct generic_data *data = NULL;
1272 if (dbus_connection_get_object_path_data(connection, path,
1273 (void *) &data) == FALSE)
1281 if (data->refcount > 0)
1284 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1285 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1287 invalidate_parent_data(data->conn, data->path);
1289 dbus_connection_unregister_object_path(data->conn, data->path);
1292 static gboolean check_signal(DBusConnection *conn, const char *path,
1293 const char *interface, const char *name,
1294 const GDBusArgInfo **args)
1296 struct generic_data *data = NULL;
1297 struct interface_data *iface;
1298 const GDBusSignalTable *signal;
1301 if (!dbus_connection_get_object_path_data(conn, path,
1302 (void *) &data) || data == NULL) {
1303 error("dbus_connection_emit_signal: path %s isn't registered",
1308 iface = find_interface(data->interfaces, interface);
1309 if (iface == NULL) {
1310 error("dbus_connection_emit_signal: %s does not implement %s",
1315 for (signal = iface->signals; signal && signal->name; signal++) {
1316 if (strcmp(signal->name, name) != 0)
1319 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1320 const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1321 if (g_strcmp0(env, "1") != 0)
1325 *args = signal->args;
1329 error("No signal named %s on interface %s", name, interface);
1333 gboolean g_dbus_register_interface(DBusConnection *connection,
1334 const char *path, const char *name,
1335 const GDBusMethodTable *methods,
1336 const GDBusSignalTable *signals,
1337 const GDBusPropertyTable *properties,
1339 GDBusDestroyFunction destroy)
1341 struct generic_data *data;
1343 data = object_path_ref(connection, path);
1347 if (find_interface(data->interfaces, name)) {
1348 object_path_unref(connection, path);
1352 if (!add_interface(data, name, methods, signals, properties, user_data,
1354 object_path_unref(connection, path);
1358 if (properties != NULL && !find_interface(data->interfaces,
1359 DBUS_INTERFACE_PROPERTIES))
1360 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1361 properties_methods, properties_signals, NULL,
1364 g_free(data->introspect);
1365 data->introspect = NULL;
1370 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1371 const char *path, const char *name)
1373 struct generic_data *data = NULL;
1378 if (dbus_connection_get_object_path_data(connection, path,
1379 (void *) &data) == FALSE)
1385 if (remove_interface(data, name) == FALSE)
1388 g_free(data->introspect);
1389 data->introspect = NULL;
1391 object_path_unref(connection, data->path);
1396 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1398 if (security_table != NULL)
1401 security_table = security;
1406 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1408 security_table = NULL;
1413 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1414 const char *format, va_list args)
1419 vsnprintf(str, sizeof(str), format, args);
1423 return dbus_message_new_error(message, name, str);
1426 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1427 const char *format, ...)
1432 va_start(args, format);
1434 reply = g_dbus_create_error_valist(message, name, format, args);
1441 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1442 int type, va_list args)
1446 reply = dbus_message_new_method_return(message);
1450 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1451 dbus_message_unref(reply);
1458 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1463 va_start(args, type);
1465 reply = g_dbus_create_reply_valist(message, type, args);
1472 static void g_dbus_flush(DBusConnection *connection)
1476 for (l = pending; l;) {
1477 struct generic_data *data = l->data;
1480 if (data->conn != connection)
1483 process_changes(data);
1487 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1489 dbus_bool_t result = FALSE;
1491 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1492 dbus_message_set_no_reply(message, TRUE);
1493 else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1494 const char *path = dbus_message_get_path(message);
1495 const char *interface = dbus_message_get_interface(message);
1496 const char *name = dbus_message_get_member(message);
1497 const GDBusArgInfo *args;
1499 if (!check_signal(connection, path, interface, name, &args))
1503 /* Flush pending signal to guarantee message order */
1504 g_dbus_flush(connection);
1506 result = dbus_connection_send(connection, message, NULL);
1509 dbus_message_unref(message);
1514 gboolean g_dbus_send_message_with_reply(DBusConnection *connection,
1515 DBusMessage *message,
1516 DBusPendingCall **call, int timeout)
1520 /* Flush pending signal to guarantee message order */
1521 g_dbus_flush(connection);
1523 ret = dbus_connection_send_with_reply(connection, message, call,
1526 if (ret == TRUE && call != NULL && *call == NULL) {
1527 error("Unable to send message (passing fd blocked?)");
1534 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1535 DBusMessage *message, const char *name,
1536 const char *format, va_list args)
1540 error = g_dbus_create_error_valist(message, name, format, args);
1544 return g_dbus_send_message(connection, error);
1547 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1548 const char *name, const char *format, ...)
1553 va_start(args, format);
1555 result = g_dbus_send_error_valist(connection, message, name,
1563 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1564 DBusMessage *message, int type, va_list args)
1568 reply = dbus_message_new_method_return(message);
1572 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1573 dbus_message_unref(reply);
1577 return g_dbus_send_message(connection, reply);
1580 gboolean g_dbus_send_reply(DBusConnection *connection,
1581 DBusMessage *message, int type, ...)
1586 va_start(args, type);
1588 result = g_dbus_send_reply_valist(connection, message, type, args);
1595 gboolean g_dbus_emit_signal(DBusConnection *connection,
1596 const char *path, const char *interface,
1597 const char *name, int type, ...)
1602 va_start(args, type);
1604 result = g_dbus_emit_signal_valist(connection, path, interface,
1612 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1613 const char *path, const char *interface,
1614 const char *name, int type, va_list args)
1616 DBusMessage *signal;
1618 const GDBusArgInfo *args_info;
1620 if (!check_signal(connection, path, interface, name, &args_info))
1623 signal = dbus_message_new_signal(path, interface, name);
1624 if (signal == NULL) {
1625 error("Unable to allocate new %s.%s signal", interface, name);
1629 ret = dbus_message_append_args_valist(signal, type, args);
1633 if (g_dbus_args_have_signature(args_info, signal) == FALSE) {
1634 error("%s.%s: got unexpected signature '%s'", interface, name,
1635 dbus_message_get_signature(signal));
1640 return g_dbus_send_message(connection, signal);
1643 dbus_message_unref(signal);
1648 static void process_properties_from_interface(struct generic_data *data,
1649 struct interface_data *iface)
1652 DBusMessage *signal;
1653 DBusMessageIter iter, dict, array;
1654 GSList *invalidated;
1656 data->pending_prop = FALSE;
1658 if (iface->pending_prop == NULL)
1661 signal = dbus_message_new_signal(data->path,
1662 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1663 if (signal == NULL) {
1664 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1665 ".PropertiesChanged signal");
1669 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1671 dbus_message_iter_init_append(signal, &iter);
1672 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1673 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1674 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1675 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1676 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1680 for (l = iface->pending_prop; l != NULL; l = l->next) {
1681 GDBusPropertyTable *p = l->data;
1686 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1687 invalidated = g_slist_prepend(invalidated, p);
1691 append_property(iface, p, &dict);
1694 dbus_message_iter_close_container(&iter, &dict);
1696 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1697 DBUS_TYPE_STRING_AS_STRING, &array);
1698 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1699 GDBusPropertyTable *p = l->data;
1701 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1704 g_slist_free(invalidated);
1705 dbus_message_iter_close_container(&iter, &array);
1707 g_slist_free(iface->pending_prop);
1708 iface->pending_prop = NULL;
1710 /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
1711 dbus_connection_send(data->conn, signal, NULL);
1712 dbus_message_unref(signal);
1715 static void process_property_changes(struct generic_data *data)
1719 for (l = data->interfaces; l != NULL; l = l->next) {
1720 struct interface_data *iface = l->data;
1722 process_properties_from_interface(data, iface);
1726 void g_dbus_emit_property_changed_full(DBusConnection *connection,
1727 const char *path, const char *interface,
1729 GDbusPropertyChangedFlags flags)
1731 const GDBusPropertyTable *property;
1732 struct generic_data *data;
1733 struct interface_data *iface;
1738 if (!dbus_connection_get_object_path_data(connection, path,
1739 (void **) &data) || data == NULL)
1742 iface = find_interface(data->interfaces, interface);
1747 * If ObjectManager is attached, don't emit property changed if
1748 * interface is not yet published
1750 if (root && g_slist_find(data->added, iface))
1753 property = find_property(iface->properties, name);
1754 if (property == NULL) {
1755 error("Could not find property %s in %p", name,
1760 if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1763 data->pending_prop = TRUE;
1764 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1767 if (flags & G_DBUS_PROPERTY_CHANGED_FLAG_FLUSH)
1768 process_property_changes(data);
1773 void g_dbus_emit_property_changed(DBusConnection *connection, const char *path,
1774 const char *interface, const char *name)
1776 g_dbus_emit_property_changed_full(connection, path, interface, name, 0);
1779 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1780 const char *interface, DBusMessageIter *iter)
1782 struct generic_data *data;
1783 struct interface_data *iface;
1788 if (!dbus_connection_get_object_path_data(connection, path,
1789 (void **) &data) || data == NULL)
1792 iface = find_interface(data->interfaces, interface);
1796 append_properties(iface, iter);
1801 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1803 struct generic_data *data;
1805 data = object_path_ref(connection, "/");
1809 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1810 manager_methods, manager_signals,
1817 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1819 if (!g_dbus_unregister_interface(connection, "/",
1820 DBUS_INTERFACE_OBJECT_MANAGER))
1828 void g_dbus_set_flags(int flags)
1830 global_flags = flags;
1833 int g_dbus_get_flags(void)
1835 return global_flags;