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) {
263 dbus_message_unref(reply);
264 return DBUS_HANDLER_RESULT_HANDLED;
267 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
269 return DBUS_HANDLER_RESULT_HANDLED;
273 return DBUS_HANDLER_RESULT_NEED_MEMORY;
275 g_dbus_send_message(connection, reply);
277 return DBUS_HANDLER_RESULT_HANDLED;
280 static GDBusPendingReply next_pending = 1;
281 static GSList *pending_security = NULL;
283 static const GDBusSecurityTable *security_table = NULL;
285 void g_dbus_pending_success(DBusConnection *connection,
286 GDBusPendingReply pending)
290 for (list = pending_security; list; list = list->next) {
291 struct security_data *secdata = list->data;
293 if (secdata->pending != pending)
296 pending_security = g_slist_remove(pending_security, secdata);
298 process_message(connection, secdata->message,
299 secdata->method, secdata->iface_user_data);
301 dbus_message_unref(secdata->message);
307 void g_dbus_pending_error_valist(DBusConnection *connection,
308 GDBusPendingReply pending, const char *name,
309 const char *format, va_list args)
313 for (list = pending_security; list; list = list->next) {
314 struct security_data *secdata = list->data;
316 if (secdata->pending != pending)
319 pending_security = g_slist_remove(pending_security, secdata);
321 g_dbus_send_error_valist(connection, secdata->message,
324 dbus_message_unref(secdata->message);
330 void g_dbus_pending_error(DBusConnection *connection,
331 GDBusPendingReply pending,
332 const char *name, const char *format, ...)
336 va_start(args, format);
338 g_dbus_pending_error_valist(connection, pending, name, format, args);
343 int polkit_check_authorization(DBusConnection *conn,
344 const char *action, gboolean interaction,
345 void (*function) (dbus_bool_t authorized,
347 void *user_data, int timeout);
349 struct builtin_security_data {
350 DBusConnection *conn;
351 GDBusPendingReply pending;
354 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
356 struct builtin_security_data *data = user_data;
358 if (authorized == TRUE)
359 g_dbus_pending_success(data->conn, data->pending);
361 g_dbus_pending_error(data->conn, data->pending,
362 DBUS_ERROR_AUTH_FAILED, NULL);
367 static void builtin_security_function(DBusConnection *conn,
369 gboolean interaction,
370 GDBusPendingReply pending)
372 struct builtin_security_data *data;
374 data = g_new0(struct builtin_security_data, 1);
376 data->pending = pending;
378 if (polkit_check_authorization(conn, action, interaction,
379 builtin_security_result, data, 30000) < 0)
380 g_dbus_pending_error(conn, pending, NULL, NULL);
383 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
384 const GDBusMethodTable *method, void *iface_user_data)
386 const GDBusSecurityTable *security;
388 for (security = security_table; security && security->privilege;
390 struct security_data *secdata;
391 gboolean interaction;
393 if (security->privilege != method->privilege)
396 secdata = g_new(struct security_data, 1);
397 secdata->pending = next_pending++;
398 secdata->message = dbus_message_ref(msg);
399 secdata->method = method;
400 secdata->iface_user_data = iface_user_data;
402 pending_security = g_slist_prepend(pending_security, secdata);
404 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
409 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
411 security->function(conn, security->action,
412 interaction, secdata->pending);
414 builtin_security_function(conn, security->action,
415 interaction, secdata->pending);
423 static GDBusPendingPropertySet next_pending_property = 1;
424 static GSList *pending_property_set;
426 static struct property_data *remove_pending_property_data(
427 GDBusPendingPropertySet id)
429 struct property_data *propdata;
432 for (l = pending_property_set; l != NULL; l = l->next) {
434 if (propdata->id != id)
443 pending_property_set = g_slist_delete_link(pending_property_set, l);
448 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
450 struct property_data *propdata;
452 propdata = remove_pending_property_data(id);
453 if (propdata == NULL)
456 g_dbus_send_reply(propdata->conn, propdata->message,
458 dbus_message_unref(propdata->message);
462 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
463 const char *name, const char *format,
466 struct property_data *propdata;
468 propdata = remove_pending_property_data(id);
469 if (propdata == NULL)
472 g_dbus_send_error_valist(propdata->conn, propdata->message, name,
475 dbus_message_unref(propdata->message);
479 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
480 const char *format, ...)
484 va_start(args, format);
486 g_dbus_pending_property_error_valist(id, name, format, args);
491 static void reset_parent(gpointer data, gpointer user_data)
493 struct generic_data *child = data;
494 struct generic_data *parent = user_data;
496 child->parent = parent;
499 static void append_property(struct interface_data *iface,
500 const GDBusPropertyTable *p, DBusMessageIter *dict)
502 DBusMessageIter entry, value;
504 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
506 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
507 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
510 p->get(p, &value, iface->user_data);
512 dbus_message_iter_close_container(&entry, &value);
513 dbus_message_iter_close_container(dict, &entry);
516 static void append_properties(struct interface_data *data,
517 DBusMessageIter *iter)
519 DBusMessageIter dict;
520 const GDBusPropertyTable *p;
522 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
523 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
524 DBUS_TYPE_STRING_AS_STRING
525 DBUS_TYPE_VARIANT_AS_STRING
526 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
528 for (p = data->properties; p && p->name; p++) {
529 if (check_experimental(p->flags,
530 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
536 if (p->exists != NULL && !p->exists(p, data->user_data))
539 append_property(data, p, &dict);
542 dbus_message_iter_close_container(iter, &dict);
545 static void append_interface(gpointer data, gpointer user_data)
547 struct interface_data *iface = data;
548 DBusMessageIter *array = user_data;
549 DBusMessageIter entry;
551 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
553 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
554 append_properties(data, &entry);
555 dbus_message_iter_close_container(array, &entry);
558 static void emit_interfaces_added(struct generic_data *data)
561 DBusMessageIter iter, array;
563 if (root == NULL || data == root)
566 signal = dbus_message_new_signal(root->path,
567 DBUS_INTERFACE_OBJECT_MANAGER,
572 dbus_message_iter_init_append(signal, &iter);
573 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
576 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
577 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
578 DBUS_TYPE_STRING_AS_STRING
579 DBUS_TYPE_ARRAY_AS_STRING
580 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
581 DBUS_TYPE_STRING_AS_STRING
582 DBUS_TYPE_VARIANT_AS_STRING
583 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
584 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
586 g_slist_foreach(data->added, append_interface, &array);
587 g_slist_free(data->added);
590 dbus_message_iter_close_container(&iter, &array);
592 /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
593 dbus_connection_send(data->conn, signal, NULL);
594 dbus_message_unref(signal);
597 static struct interface_data *find_interface(GSList *interfaces,
605 for (list = interfaces; list; list = list->next) {
606 struct interface_data *iface = list->data;
607 if (!strcmp(name, iface->name))
614 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
615 DBusMessage *message)
617 const char *sig = dbus_message_get_signature(message);
618 const char *p = NULL;
620 for (; args && args->signature && *sig; args++) {
623 for (; *sig && *p; sig++, p++) {
629 if (*sig || (p && *p) || (args && args->signature))
635 static void add_pending(struct generic_data *data)
637 if (data->process_id > 0)
640 data->process_id = g_idle_add(process_changes, data);
642 pending = g_slist_append(pending, data);
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);
687 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
688 const char *child_path)
690 struct generic_data *data = NULL, *child = NULL, *parent = NULL;
691 char *parent_path, *slash;
693 parent_path = g_strdup(child_path);
694 slash = strrchr(parent_path, '/');
698 if (slash == parent_path && parent_path[1] != '\0')
699 parent_path[1] = '\0';
703 if (!strlen(parent_path))
706 if (dbus_connection_get_object_path_data(conn, parent_path,
707 (void *) &data) == FALSE) {
711 parent = invalidate_parent_data(conn, parent_path);
719 g_free(data->introspect);
720 data->introspect = NULL;
722 if (!dbus_connection_get_object_path_data(conn, child_path,
726 if (child == NULL || g_slist_find(data->objects, child) != NULL)
729 data->objects = g_slist_prepend(data->objects, child);
730 child->parent = data;
737 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
740 const GDBusPropertyTable *p;
742 for (p = properties; p && p->name; p++) {
743 if (strcmp(name, p->name) != 0)
746 if (check_experimental(p->flags,
747 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
756 static DBusMessage *properties_get(DBusConnection *connection,
757 DBusMessage *message, void *user_data)
759 struct generic_data *data = user_data;
760 struct interface_data *iface;
761 const GDBusPropertyTable *property;
762 const char *interface, *name;
763 DBusMessageIter iter, value;
766 if (!dbus_message_get_args(message, NULL,
767 DBUS_TYPE_STRING, &interface,
768 DBUS_TYPE_STRING, &name,
772 iface = find_interface(data->interfaces, interface);
774 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
775 "No such interface '%s'", interface);
777 property = find_property(iface->properties, name);
778 if (property == NULL)
779 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
780 "No such property '%s'", name);
782 if (property->exists != NULL &&
783 !property->exists(property, iface->user_data))
784 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
785 "No such property '%s'", name);
787 if (property->get == NULL)
788 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
789 "Property '%s' is not readable", name);
791 reply = dbus_message_new_method_return(message);
795 dbus_message_iter_init_append(reply, &iter);
796 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
797 property->type, &value);
799 if (!property->get(property, &value, iface->user_data)) {
800 dbus_message_unref(reply);
804 dbus_message_iter_close_container(&iter, &value);
809 static DBusMessage *properties_get_all(DBusConnection *connection,
810 DBusMessage *message, void *user_data)
812 struct generic_data *data = user_data;
813 struct interface_data *iface;
814 const char *interface;
815 DBusMessageIter iter;
818 if (!dbus_message_get_args(message, NULL,
819 DBUS_TYPE_STRING, &interface,
823 iface = find_interface(data->interfaces, interface);
825 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
826 "No such interface '%s'", interface);
828 reply = dbus_message_new_method_return(message);
832 dbus_message_iter_init_append(reply, &iter);
834 append_properties(iface, &iter);
839 static DBusMessage *properties_set(DBusConnection *connection,
840 DBusMessage *message, void *user_data)
842 struct generic_data *data = user_data;
843 DBusMessageIter iter, sub;
844 struct interface_data *iface;
845 const GDBusPropertyTable *property;
846 const char *name, *interface;
847 struct property_data *propdata;
848 gboolean valid_signature;
851 if (!dbus_message_iter_init(message, &iter))
852 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
853 "No arguments given");
855 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
856 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
857 "Invalid argument type: '%c'",
858 dbus_message_iter_get_arg_type(&iter));
860 dbus_message_iter_get_basic(&iter, &interface);
861 dbus_message_iter_next(&iter);
863 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
864 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
865 "Invalid argument type: '%c'",
866 dbus_message_iter_get_arg_type(&iter));
868 dbus_message_iter_get_basic(&iter, &name);
869 dbus_message_iter_next(&iter);
871 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
872 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
873 "Invalid argument type: '%c'",
874 dbus_message_iter_get_arg_type(&iter));
876 dbus_message_iter_recurse(&iter, &sub);
878 iface = find_interface(data->interfaces, interface);
880 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
881 "No such interface '%s'", interface);
883 property = find_property(iface->properties, name);
884 if (property == NULL)
885 return g_dbus_create_error(message,
886 DBUS_ERROR_UNKNOWN_PROPERTY,
887 "No such property '%s'", name);
889 if (property->set == NULL)
890 return g_dbus_create_error(message,
891 DBUS_ERROR_PROPERTY_READ_ONLY,
892 "Property '%s' is not writable", name);
894 if (property->exists != NULL &&
895 !property->exists(property, iface->user_data))
896 return g_dbus_create_error(message,
897 DBUS_ERROR_UNKNOWN_PROPERTY,
898 "No such property '%s'", name);
900 signature = dbus_message_iter_get_signature(&sub);
901 valid_signature = strcmp(signature, property->type) ? FALSE : TRUE;
902 dbus_free(signature);
903 if (!valid_signature)
904 return g_dbus_create_error(message,
905 DBUS_ERROR_INVALID_SIGNATURE,
906 "Invalid signature for '%s'", name);
908 propdata = g_new(struct property_data, 1);
909 propdata->id = next_pending_property++;
910 propdata->message = dbus_message_ref(message);
911 propdata->conn = connection;
912 pending_property_set = g_slist_prepend(pending_property_set, propdata);
914 property->set(property, &sub, propdata->id, iface->user_data);
919 static const GDBusMethodTable properties_methods[] = {
920 { GDBUS_METHOD("Get",
921 GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
922 GDBUS_ARGS({ "value", "v" }),
924 { GDBUS_ASYNC_METHOD("Set",
925 GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
929 { GDBUS_METHOD("GetAll",
930 GDBUS_ARGS({ "interface", "s" }),
931 GDBUS_ARGS({ "properties", "a{sv}" }),
932 properties_get_all) },
936 static const GDBusSignalTable properties_signals[] = {
937 { GDBUS_SIGNAL("PropertiesChanged",
938 GDBUS_ARGS({ "interface", "s" },
939 { "changed_properties", "a{sv}" },
940 { "invalidated_properties", "as"})) },
944 static void append_name(gpointer data, gpointer user_data)
947 DBusMessageIter *iter = user_data;
949 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
952 static void emit_interfaces_removed(struct generic_data *data)
955 DBusMessageIter iter, array;
957 if (root == NULL || data == root)
960 signal = dbus_message_new_signal(root->path,
961 DBUS_INTERFACE_OBJECT_MANAGER,
962 "InterfacesRemoved");
966 dbus_message_iter_init_append(signal, &iter);
967 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
969 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
970 DBUS_TYPE_STRING_AS_STRING, &array);
972 g_slist_foreach(data->removed, append_name, &array);
973 g_slist_free_full(data->removed, g_free);
974 data->removed = NULL;
976 dbus_message_iter_close_container(&iter, &array);
978 /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
979 dbus_connection_send(data->conn, signal, NULL);
980 dbus_message_unref(signal);
983 static void remove_pending(struct generic_data *data)
985 if (data->process_id > 0) {
986 g_source_remove(data->process_id);
987 data->process_id = 0;
990 pending = g_slist_remove(pending, data);
993 static gboolean process_changes(gpointer user_data)
995 struct generic_data *data = user_data;
997 remove_pending(data);
999 if (data->added != NULL)
1000 emit_interfaces_added(data);
1002 /* Flush pending properties */
1003 if (data->pending_prop == TRUE)
1004 process_property_changes(data);
1006 if (data->removed != NULL)
1007 emit_interfaces_removed(data);
1009 data->process_id = 0;
1014 static void generic_unregister(DBusConnection *connection, void *user_data)
1016 struct generic_data *data = user_data;
1017 struct generic_data *parent = data->parent;
1020 parent->objects = g_slist_remove(parent->objects, data);
1022 if (data->process_id > 0) {
1023 g_source_remove(data->process_id);
1024 data->process_id = 0;
1025 process_changes(data);
1028 g_slist_foreach(data->objects, reset_parent, data->parent);
1029 g_slist_free(data->objects);
1031 dbus_connection_unref(data->conn);
1032 g_free(data->introspect);
1037 static DBusHandlerResult generic_message(DBusConnection *connection,
1038 DBusMessage *message, void *user_data)
1040 struct generic_data *data = user_data;
1041 struct interface_data *iface;
1042 const GDBusMethodTable *method;
1043 const char *interface;
1045 interface = dbus_message_get_interface(message);
1047 iface = find_interface(data->interfaces, interface);
1049 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1051 for (method = iface->methods; method &&
1052 method->name && method->function; method++) {
1054 if (dbus_message_is_method_call(message, iface->name,
1055 method->name) == FALSE)
1058 if (check_experimental(method->flags,
1059 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1060 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1062 if (g_dbus_args_have_signature(method->in_args,
1066 if (check_privilege(connection, message, method,
1067 iface->user_data) == TRUE)
1068 return DBUS_HANDLER_RESULT_HANDLED;
1070 return process_message(connection, message, method,
1074 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1077 static DBusObjectPathVTable generic_table = {
1078 .unregister_function = generic_unregister,
1079 .message_function = generic_message,
1082 static const GDBusMethodTable introspect_methods[] = {
1083 { GDBUS_METHOD("Introspect", NULL,
1084 GDBUS_ARGS({ "xml", "s" }), introspect) },
1088 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1090 DBusMessageIter array;
1092 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1093 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1094 DBUS_TYPE_STRING_AS_STRING
1095 DBUS_TYPE_ARRAY_AS_STRING
1096 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1097 DBUS_TYPE_STRING_AS_STRING
1098 DBUS_TYPE_VARIANT_AS_STRING
1099 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1100 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1102 g_slist_foreach(data->interfaces, append_interface, &array);
1104 dbus_message_iter_close_container(iter, &array);
1107 static void append_object(gpointer data, gpointer user_data)
1109 struct generic_data *child = data;
1110 DBusMessageIter *array = user_data;
1111 DBusMessageIter entry;
1113 dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1115 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1117 append_interfaces(child, &entry);
1118 dbus_message_iter_close_container(array, &entry);
1120 g_slist_foreach(child->objects, append_object, user_data);
1123 static DBusMessage *get_objects(DBusConnection *connection,
1124 DBusMessage *message, void *user_data)
1126 struct generic_data *data = user_data;
1128 DBusMessageIter iter;
1129 DBusMessageIter array;
1131 reply = dbus_message_new_method_return(message);
1135 dbus_message_iter_init_append(reply, &iter);
1137 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1138 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1139 DBUS_TYPE_OBJECT_PATH_AS_STRING
1140 DBUS_TYPE_ARRAY_AS_STRING
1141 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1142 DBUS_TYPE_STRING_AS_STRING
1143 DBUS_TYPE_ARRAY_AS_STRING
1144 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1145 DBUS_TYPE_STRING_AS_STRING
1146 DBUS_TYPE_VARIANT_AS_STRING
1147 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1148 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1149 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1152 g_slist_foreach(data->objects, append_object, &array);
1154 dbus_message_iter_close_container(&iter, &array);
1159 static const GDBusMethodTable manager_methods[] = {
1160 { GDBUS_METHOD("GetManagedObjects", NULL,
1161 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1165 static const GDBusSignalTable manager_signals[] = {
1166 { GDBUS_SIGNAL("InterfacesAdded",
1167 GDBUS_ARGS({ "object", "o" },
1168 { "interfaces", "a{sa{sv}}" })) },
1169 { GDBUS_SIGNAL("InterfacesRemoved",
1170 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1174 static gboolean add_interface(struct generic_data *data,
1176 const GDBusMethodTable *methods,
1177 const GDBusSignalTable *signals,
1178 const GDBusPropertyTable *properties,
1180 GDBusDestroyFunction destroy)
1182 struct interface_data *iface;
1183 const GDBusMethodTable *method;
1184 const GDBusSignalTable *signal;
1185 const GDBusPropertyTable *property;
1187 for (method = methods; method && method->name; method++) {
1188 if (!check_experimental(method->flags,
1189 G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1193 for (signal = signals; signal && signal->name; signal++) {
1194 if (!check_experimental(signal->flags,
1195 G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1199 for (property = properties; property && property->name; property++) {
1200 if (!check_experimental(property->flags,
1201 G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1205 /* Nothing to register */
1209 iface = g_new0(struct interface_data, 1);
1210 iface->name = g_strdup(name);
1211 iface->methods = methods;
1212 iface->signals = signals;
1213 iface->properties = properties;
1214 iface->user_data = user_data;
1215 iface->destroy = destroy;
1217 data->interfaces = g_slist_append(data->interfaces, iface);
1218 if (data->parent == NULL)
1221 data->added = g_slist_append(data->added, iface);
1228 static struct generic_data *object_path_ref(DBusConnection *connection,
1231 struct generic_data *data;
1233 if (dbus_connection_get_object_path_data(connection, path,
1234 (void *) &data) == TRUE) {
1241 data = g_new0(struct generic_data, 1);
1242 data->conn = dbus_connection_ref(connection);
1243 data->path = g_strdup(path);
1246 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1248 if (!dbus_connection_register_object_path(connection, path,
1249 &generic_table, data)) {
1250 #if !defined TIZEN_EXT
1251 dbus_connection_unref(data->conn);
1254 g_free(data->introspect);
1259 invalidate_parent_data(connection, path);
1261 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1262 NULL, NULL, data, NULL);
1267 static void object_path_unref(DBusConnection *connection, const char *path)
1269 struct generic_data *data = NULL;
1271 if (dbus_connection_get_object_path_data(connection, path,
1272 (void *) &data) == FALSE)
1280 if (data->refcount > 0)
1283 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1284 remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1286 invalidate_parent_data(data->conn, data->path);
1288 dbus_connection_unregister_object_path(data->conn, data->path);
1291 static gboolean check_signal(DBusConnection *conn, const char *path,
1292 const char *interface, const char *name,
1293 const GDBusArgInfo **args)
1295 struct generic_data *data = NULL;
1296 struct interface_data *iface;
1297 const GDBusSignalTable *signal;
1300 if (!dbus_connection_get_object_path_data(conn, path,
1301 (void *) &data) || data == NULL) {
1302 error("dbus_connection_emit_signal: path %s isn't registered",
1307 iface = find_interface(data->interfaces, interface);
1308 if (iface == NULL) {
1309 error("dbus_connection_emit_signal: %s does not implement %s",
1314 for (signal = iface->signals; signal && signal->name; signal++) {
1315 if (strcmp(signal->name, name) != 0)
1318 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1319 const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1320 if (g_strcmp0(env, "1") != 0)
1324 *args = signal->args;
1328 error("No signal named %s on interface %s", name, interface);
1332 gboolean g_dbus_register_interface(DBusConnection *connection,
1333 const char *path, const char *name,
1334 const GDBusMethodTable *methods,
1335 const GDBusSignalTable *signals,
1336 const GDBusPropertyTable *properties,
1338 GDBusDestroyFunction destroy)
1340 struct generic_data *data;
1342 data = object_path_ref(connection, path);
1346 if (find_interface(data->interfaces, name)) {
1347 object_path_unref(connection, path);
1351 if (!add_interface(data, name, methods, signals, properties, user_data,
1353 object_path_unref(connection, path);
1357 if (properties != NULL && !find_interface(data->interfaces,
1358 DBUS_INTERFACE_PROPERTIES))
1359 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1360 properties_methods, properties_signals, NULL,
1363 g_free(data->introspect);
1364 data->introspect = NULL;
1369 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1370 const char *path, const char *name)
1372 struct generic_data *data = NULL;
1377 if (dbus_connection_get_object_path_data(connection, path,
1378 (void *) &data) == FALSE)
1384 if (remove_interface(data, name) == FALSE)
1387 g_free(data->introspect);
1388 data->introspect = NULL;
1390 object_path_unref(connection, data->path);
1395 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1397 if (security_table != NULL)
1400 security_table = security;
1405 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1407 security_table = NULL;
1412 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1413 const char *format, va_list args)
1418 vsnprintf(str, sizeof(str), format, args);
1422 return dbus_message_new_error(message, name, str);
1425 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1426 const char *format, ...)
1431 va_start(args, format);
1433 reply = g_dbus_create_error_valist(message, name, format, args);
1440 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1441 int type, va_list args)
1445 reply = dbus_message_new_method_return(message);
1449 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1450 dbus_message_unref(reply);
1457 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1462 va_start(args, type);
1464 reply = g_dbus_create_reply_valist(message, type, args);
1471 static void g_dbus_flush(DBusConnection *connection)
1475 for (l = pending; l;) {
1476 struct generic_data *data = l->data;
1479 if (data->conn != connection)
1482 process_changes(data);
1486 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1488 dbus_bool_t result = FALSE;
1490 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1491 dbus_message_set_no_reply(message, TRUE);
1492 else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1493 const char *path = dbus_message_get_path(message);
1494 const char *interface = dbus_message_get_interface(message);
1495 const char *name = dbus_message_get_member(message);
1496 const GDBusArgInfo *args;
1498 if (!check_signal(connection, path, interface, name, &args))
1502 /* Flush pending signal to guarantee message order */
1503 g_dbus_flush(connection);
1505 result = dbus_connection_send(connection, message, NULL);
1508 dbus_message_unref(message);
1513 gboolean g_dbus_send_message_with_reply(DBusConnection *connection,
1514 DBusMessage *message,
1515 DBusPendingCall **call, int timeout)
1519 /* Flush pending signal to guarantee message order */
1520 g_dbus_flush(connection);
1522 ret = dbus_connection_send_with_reply(connection, message, call,
1525 if (ret == TRUE && call != NULL && *call == NULL) {
1526 error("Unable to send message (passing fd blocked?)");
1533 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1534 DBusMessage *message, const char *name,
1535 const char *format, va_list args)
1539 error = g_dbus_create_error_valist(message, name, format, args);
1543 return g_dbus_send_message(connection, error);
1546 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1547 const char *name, const char *format, ...)
1552 va_start(args, format);
1554 result = g_dbus_send_error_valist(connection, message, name,
1562 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1563 DBusMessage *message, int type, va_list args)
1567 reply = dbus_message_new_method_return(message);
1571 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1572 dbus_message_unref(reply);
1576 return g_dbus_send_message(connection, reply);
1579 gboolean g_dbus_send_reply(DBusConnection *connection,
1580 DBusMessage *message, int type, ...)
1585 va_start(args, type);
1587 result = g_dbus_send_reply_valist(connection, message, type, args);
1594 gboolean g_dbus_emit_signal(DBusConnection *connection,
1595 const char *path, const char *interface,
1596 const char *name, int type, ...)
1601 va_start(args, type);
1603 result = g_dbus_emit_signal_valist(connection, path, interface,
1611 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1612 const char *path, const char *interface,
1613 const char *name, int type, va_list args)
1615 DBusMessage *signal;
1617 const GDBusArgInfo *args_info;
1619 if (!check_signal(connection, path, interface, name, &args_info))
1622 signal = dbus_message_new_signal(path, interface, name);
1623 if (signal == NULL) {
1624 error("Unable to allocate new %s.%s signal", interface, name);
1628 ret = dbus_message_append_args_valist(signal, type, args);
1632 if (g_dbus_args_have_signature(args_info, signal) == FALSE) {
1633 error("%s.%s: got unexpected signature '%s'", interface, name,
1634 dbus_message_get_signature(signal));
1639 return g_dbus_send_message(connection, signal);
1642 dbus_message_unref(signal);
1647 static void process_properties_from_interface(struct generic_data *data,
1648 struct interface_data *iface)
1651 DBusMessage *signal;
1652 DBusMessageIter iter, dict, array;
1653 GSList *invalidated;
1655 data->pending_prop = FALSE;
1657 if (iface->pending_prop == NULL)
1660 signal = dbus_message_new_signal(data->path,
1661 DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1662 if (signal == NULL) {
1663 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1664 ".PropertiesChanged signal");
1668 iface->pending_prop = g_slist_reverse(iface->pending_prop);
1670 dbus_message_iter_init_append(signal, &iter);
1671 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1672 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1673 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1674 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1675 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1679 for (l = iface->pending_prop; l != NULL; l = l->next) {
1680 GDBusPropertyTable *p = l->data;
1685 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1686 invalidated = g_slist_prepend(invalidated, p);
1690 append_property(iface, p, &dict);
1693 dbus_message_iter_close_container(&iter, &dict);
1695 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1696 DBUS_TYPE_STRING_AS_STRING, &array);
1697 for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1698 GDBusPropertyTable *p = l->data;
1700 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1703 g_slist_free(invalidated);
1704 dbus_message_iter_close_container(&iter, &array);
1706 g_slist_free(iface->pending_prop);
1707 iface->pending_prop = NULL;
1709 /* Use dbus_connection_send to avoid recursive calls to g_dbus_flush */
1710 dbus_connection_send(data->conn, signal, NULL);
1711 dbus_message_unref(signal);
1714 static void process_property_changes(struct generic_data *data)
1718 for (l = data->interfaces; l != NULL; l = l->next) {
1719 struct interface_data *iface = l->data;
1721 process_properties_from_interface(data, iface);
1725 void g_dbus_emit_property_changed(DBusConnection *connection,
1726 const char *path, const char *interface,
1729 const GDBusPropertyTable *property;
1730 struct generic_data *data;
1731 struct interface_data *iface;
1736 if (!dbus_connection_get_object_path_data(connection, path,
1737 (void **) &data) || data == NULL)
1740 iface = find_interface(data->interfaces, interface);
1745 * If ObjectManager is attached, don't emit property changed if
1746 * interface is not yet published
1748 if (root && g_slist_find(data->added, iface))
1751 property = find_property(iface->properties, name);
1752 if (property == NULL) {
1753 error("Could not find property %s in %p", name,
1758 if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1761 data->pending_prop = TRUE;
1762 iface->pending_prop = g_slist_prepend(iface->pending_prop,
1768 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1769 const char *interface, DBusMessageIter *iter)
1771 struct generic_data *data;
1772 struct interface_data *iface;
1777 if (!dbus_connection_get_object_path_data(connection, path,
1778 (void **) &data) || data == NULL)
1781 iface = find_interface(data->interfaces, interface);
1785 append_properties(iface, iter);
1790 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1792 struct generic_data *data;
1794 data = object_path_ref(connection, "/");
1798 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1799 manager_methods, manager_signals,
1806 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1808 if (!g_dbus_unregister_interface(connection, "/",
1809 DBUS_INTERFACE_OBJECT_MANAGER))
1817 void g_dbus_set_flags(int flags)
1819 global_flags = flags;
1822 int g_dbus_get_flags(void)
1824 return global_flags;