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>
41 unsigned int refcount;
46 struct interface_data {
48 const GDBusMethodTable *methods;
49 const GDBusSignalTable *signals;
50 const GDBusPropertyTable *properties;
52 GDBusDestroyFunction destroy;
55 struct security_data {
56 GDBusPendingReply pending;
58 const GDBusMethodTable *method;
59 void *iface_user_data;
62 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
63 const char *direction)
65 for (; args && args->name; args++) {
66 g_string_append_printf(gstr,
67 "\t\t\t<arg name=\"%s\" type=\"%s\"",
68 args->name, args->signature);
71 g_string_append_printf(gstr,
72 " direction=\"%s\"/>\n", direction);
74 g_string_append_printf(gstr, "/>\n");
79 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
81 const GDBusMethodTable *method;
82 const GDBusSignalTable *signal;
84 for (method = iface->methods; method && method->name; method++) {
85 gboolean deprecated = method->flags &
86 G_DBUS_METHOD_FLAG_DEPRECATED;
87 gboolean noreply = method->flags &
88 G_DBUS_METHOD_FLAG_NOREPLY;
90 if (!deprecated && !noreply &&
91 !(method->in_args && method->in_args->name) &&
92 !(method->out_args && method->out_args->name))
93 g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
96 g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
98 print_arguments(gstr, method->in_args, "in");
99 print_arguments(gstr, method->out_args, "out");
102 g_string_append_printf(gstr, "\t\t\t<annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n");
105 g_string_append_printf(gstr, "\t\t\t<annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n");
107 g_string_append_printf(gstr, "\t\t</method>\n");
111 for (signal = iface->signals; signal && signal->name; signal++) {
112 gboolean deprecated = signal->flags &
113 G_DBUS_SIGNAL_FLAG_DEPRECATED;
115 if (!deprecated && !(signal->args && signal->args->name))
116 g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
119 g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
121 print_arguments(gstr, signal->args, NULL);
124 g_string_append_printf(gstr, "\t\t\t<annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n");
126 g_string_append_printf(gstr, "\t\t</signal>\n");
131 static void generate_introspection_xml(DBusConnection *conn,
132 struct generic_data *data, const char *path)
139 g_free(data->introspect);
141 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
143 g_string_append_printf(gstr, "<node>\n");
145 for (list = data->interfaces; list; list = list->next) {
146 struct interface_data *iface = list->data;
148 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
151 generate_interface_xml(gstr, iface);
153 g_string_append_printf(gstr, "\t</interface>\n");
156 if (!dbus_connection_list_registered(conn, path, &children))
159 for (i = 0; children[i]; i++)
160 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
163 dbus_free_string_array(children);
166 g_string_append_printf(gstr, "</node>\n");
168 data->introspect = g_string_free(gstr, FALSE);
171 static DBusMessage *introspect(DBusConnection *connection,
172 DBusMessage *message, void *user_data)
174 struct generic_data *data = user_data;
177 if (data->introspect == NULL)
178 generate_introspection_xml(connection, data,
179 dbus_message_get_path(message));
181 reply = dbus_message_new_method_return(message);
185 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
191 static DBusHandlerResult process_message(DBusConnection *connection,
192 DBusMessage *message, const GDBusMethodTable *method,
193 void *iface_user_data)
197 reply = method->function(connection, message, iface_user_data);
199 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
201 dbus_message_unref(reply);
202 return DBUS_HANDLER_RESULT_HANDLED;
205 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
207 return DBUS_HANDLER_RESULT_HANDLED;
211 return DBUS_HANDLER_RESULT_NEED_MEMORY;
213 dbus_connection_send(connection, reply, NULL);
214 dbus_message_unref(reply);
216 return DBUS_HANDLER_RESULT_HANDLED;
219 static GDBusPendingReply next_pending = 1;
220 static GSList *pending_security = NULL;
222 static const GDBusSecurityTable *security_table = NULL;
224 void g_dbus_pending_success(DBusConnection *connection,
225 GDBusPendingReply pending)
229 for (list = pending_security; list; list = list->next) {
230 struct security_data *secdata = list->data;
232 if (secdata->pending != pending)
235 pending_security = g_slist_remove(pending_security, secdata);
237 process_message(connection, secdata->message,
238 secdata->method, secdata->iface_user_data);
240 dbus_message_unref(secdata->message);
246 void g_dbus_pending_error_valist(DBusConnection *connection,
247 GDBusPendingReply pending, const char *name,
248 const char *format, va_list args)
252 for (list = pending_security; list; list = list->next) {
253 struct security_data *secdata = list->data;
256 if (secdata->pending != pending)
259 pending_security = g_slist_remove(pending_security, secdata);
261 reply = g_dbus_create_error_valist(secdata->message,
264 dbus_connection_send(connection, reply, NULL);
265 dbus_message_unref(reply);
268 dbus_message_unref(secdata->message);
274 void g_dbus_pending_error(DBusConnection *connection,
275 GDBusPendingReply pending,
276 const char *name, const char *format, ...)
280 va_start(args, format);
282 g_dbus_pending_error_valist(connection, pending, name, format, args);
287 int polkit_check_authorization(DBusConnection *conn,
288 const char *action, gboolean interaction,
289 void (*function) (dbus_bool_t authorized,
291 void *user_data, int timeout);
293 struct builtin_security_data {
294 DBusConnection *conn;
295 GDBusPendingReply pending;
298 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
300 struct builtin_security_data *data = user_data;
302 if (authorized == TRUE)
303 g_dbus_pending_success(data->conn, data->pending);
305 g_dbus_pending_error(data->conn, data->pending,
306 DBUS_ERROR_AUTH_FAILED, NULL);
311 static void builtin_security_function(DBusConnection *conn,
313 gboolean interaction,
314 GDBusPendingReply pending)
316 struct builtin_security_data *data;
318 data = g_new0(struct builtin_security_data, 1);
320 data->pending = pending;
322 if (polkit_check_authorization(conn, action, interaction,
323 builtin_security_result, data, 30000) < 0)
324 g_dbus_pending_error(conn, pending, NULL, NULL);
327 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
328 const GDBusMethodTable *method, void *iface_user_data)
330 const GDBusSecurityTable *security;
332 for (security = security_table; security && security->privilege;
334 struct security_data *secdata;
335 gboolean interaction;
337 if (security->privilege != method->privilege)
340 secdata = g_new(struct security_data, 1);
341 secdata->pending = next_pending++;
342 secdata->message = dbus_message_ref(msg);
343 secdata->method = method;
344 secdata->iface_user_data = iface_user_data;
346 pending_security = g_slist_prepend(pending_security, secdata);
348 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
353 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
355 security->function(conn, security->action,
356 interaction, secdata->pending);
358 builtin_security_function(conn, security->action,
359 interaction, secdata->pending);
367 static void generic_unregister(DBusConnection *connection, void *user_data)
369 struct generic_data *data = user_data;
371 g_free(data->introspect);
375 static struct interface_data *find_interface(GSList *interfaces,
383 for (list = interfaces; list; list = list->next) {
384 struct interface_data *iface = list->data;
385 if (!strcmp(name, iface->name))
392 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
393 DBusMessage *message)
395 const char *sig = dbus_message_get_signature(message);
396 const char *p = NULL;
398 for (; args && args->signature && *sig; args++) {
401 for (; *sig && *p; sig++, p++) {
407 if (*sig || (p && *p) || (args && args->signature))
413 static DBusHandlerResult generic_message(DBusConnection *connection,
414 DBusMessage *message, void *user_data)
416 struct generic_data *data = user_data;
417 struct interface_data *iface;
418 const GDBusMethodTable *method;
419 const char *interface;
421 interface = dbus_message_get_interface(message);
423 iface = find_interface(data->interfaces, interface);
425 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
427 for (method = iface->methods; method &&
428 method->name && method->function; method++) {
429 if (dbus_message_is_method_call(message, iface->name,
430 method->name) == FALSE)
433 if (g_dbus_args_have_signature(method->in_args,
437 if (check_privilege(connection, message, method,
438 iface->user_data) == TRUE)
439 return DBUS_HANDLER_RESULT_HANDLED;
441 return process_message(connection, message, method,
445 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
448 static DBusObjectPathVTable generic_table = {
449 .unregister_function = generic_unregister,
450 .message_function = generic_message,
453 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
455 struct generic_data *data = NULL;
456 char *parent_path, *slash;
458 parent_path = g_strdup(child_path);
459 slash = strrchr(parent_path, '/');
463 if (slash == parent_path && parent_path[1] != '\0')
464 parent_path[1] = '\0';
468 if (!strlen(parent_path))
471 if (dbus_connection_get_object_path_data(conn, parent_path,
472 (void *) &data) == FALSE) {
476 invalidate_parent_data(conn, parent_path);
481 g_free(data->introspect);
482 data->introspect = NULL;
488 static const GDBusMethodTable introspect_methods[] = {
489 { GDBUS_METHOD("Introspect", NULL,
490 GDBUS_ARGS({ "xml", "s" }), introspect) },
494 static void add_interface(struct generic_data *data, const char *name,
495 const GDBusMethodTable *methods,
496 const GDBusSignalTable *signals,
497 const GDBusPropertyTable *properties,
499 GDBusDestroyFunction destroy)
501 struct interface_data *iface;
503 iface = g_new0(struct interface_data, 1);
504 iface->name = g_strdup(name);
505 iface->methods = methods;
506 iface->signals = signals;
507 iface->properties = properties;
508 iface->user_data = user_data;
509 iface->destroy = destroy;
511 data->interfaces = g_slist_append(data->interfaces, iface);
514 static struct generic_data *object_path_ref(DBusConnection *connection,
517 struct generic_data *data;
519 if (dbus_connection_get_object_path_data(connection, path,
520 (void *) &data) == TRUE) {
527 data = g_new0(struct generic_data, 1);
530 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
532 if (!dbus_connection_register_object_path(connection, path,
533 &generic_table, data)) {
534 g_free(data->introspect);
539 invalidate_parent_data(connection, path);
541 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
542 introspect_methods, NULL, NULL, data, NULL);
547 static gboolean remove_interface(struct generic_data *data, const char *name)
549 struct interface_data *iface;
551 iface = find_interface(data->interfaces, name);
555 data->interfaces = g_slist_remove(data->interfaces, iface);
558 iface->destroy(iface->user_data);
566 static void object_path_unref(DBusConnection *connection, const char *path)
568 struct generic_data *data = NULL;
570 if (dbus_connection_get_object_path_data(connection, path,
571 (void *) &data) == FALSE)
579 if (data->refcount > 0)
582 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
584 invalidate_parent_data(connection, path);
586 dbus_connection_unregister_object_path(connection, path);
589 static gboolean check_signal(DBusConnection *conn, const char *path,
590 const char *interface, const char *name,
591 const GDBusArgInfo **args)
593 struct generic_data *data = NULL;
594 struct interface_data *iface;
595 const GDBusSignalTable *signal;
598 if (!dbus_connection_get_object_path_data(conn, path,
599 (void *) &data) || data == NULL) {
600 error("dbus_connection_emit_signal: path %s isn't registered",
605 iface = find_interface(data->interfaces, interface);
607 error("dbus_connection_emit_signal: %s does not implement %s",
612 for (signal = iface->signals; signal && signal->name; signal++) {
613 if (!strcmp(signal->name, name)) {
614 *args = signal->args;
619 error("No signal named %s on interface %s", name, interface);
623 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
625 const char *interface,
632 const GDBusArgInfo *args;
634 if (!check_signal(conn, path, interface, name, &args))
637 signal = dbus_message_new_signal(path, interface, name);
638 if (signal == NULL) {
639 error("Unable to allocate new %s.%s signal", interface, name);
643 ret = dbus_message_append_args_valist(signal, first, var_args);
647 if (g_dbus_args_have_signature(args, signal) == FALSE) {
648 error("%s.%s: expected signature'%s' but got '%s'",
649 interface, name, args, signature);
654 ret = dbus_connection_send(conn, signal, NULL);
657 dbus_message_unref(signal);
662 gboolean g_dbus_register_interface(DBusConnection *connection,
663 const char *path, const char *name,
664 const GDBusMethodTable *methods,
665 const GDBusSignalTable *signals,
666 const GDBusPropertyTable *properties,
668 GDBusDestroyFunction destroy)
670 struct generic_data *data;
672 data = object_path_ref(connection, path);
676 if (find_interface(data->interfaces, name)) {
677 object_path_unref(connection, path);
681 add_interface(data, name, methods, signals,
682 properties, user_data, destroy);
684 g_free(data->introspect);
685 data->introspect = NULL;
690 gboolean g_dbus_unregister_interface(DBusConnection *connection,
691 const char *path, const char *name)
693 struct generic_data *data = NULL;
698 if (dbus_connection_get_object_path_data(connection, path,
699 (void *) &data) == FALSE)
705 if (remove_interface(data, name) == FALSE)
708 g_free(data->introspect);
709 data->introspect = NULL;
711 object_path_unref(connection, path);
716 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
718 if (security_table != NULL)
721 security_table = security;
726 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
728 security_table = NULL;
733 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
734 const char *format, va_list args)
738 vsnprintf(str, sizeof(str), format, args);
740 return dbus_message_new_error(message, name, str);
743 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
744 const char *format, ...)
749 va_start(args, format);
751 reply = g_dbus_create_error_valist(message, name, format, args);
758 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
759 int type, va_list args)
763 reply = dbus_message_new_method_return(message);
767 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
768 dbus_message_unref(reply);
775 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
780 va_start(args, type);
782 reply = g_dbus_create_reply_valist(message, type, args);
789 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
793 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
794 dbus_message_set_no_reply(message, TRUE);
796 result = dbus_connection_send(connection, message, NULL);
798 dbus_message_unref(message);
803 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
804 DBusMessage *message, int type, va_list args)
808 reply = dbus_message_new_method_return(message);
812 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
813 dbus_message_unref(reply);
817 return g_dbus_send_message(connection, reply);
820 gboolean g_dbus_send_reply(DBusConnection *connection,
821 DBusMessage *message, int type, ...)
826 va_start(args, type);
828 result = g_dbus_send_reply_valist(connection, message, type, args);
835 gboolean g_dbus_emit_signal(DBusConnection *connection,
836 const char *path, const char *interface,
837 const char *name, int type, ...)
842 va_start(args, type);
844 result = emit_signal_valist(connection, path, interface,
852 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
853 const char *path, const char *interface,
854 const char *name, int type, va_list args)
856 return emit_signal_valist(connection, path, interface,