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 if (!(method->in_args && method->in_args->name) &&
86 !(method->out_args && method->out_args->name))
87 g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
90 g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
92 print_arguments(gstr, method->in_args, "in");
93 print_arguments(gstr, method->out_args, "out");
94 g_string_append_printf(gstr, "\t\t</method>\n");
98 for (signal = iface->signals; signal && signal->name; signal++) {
99 if (!(signal->args && signal->args->name))
100 g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
103 g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
105 print_arguments(gstr, signal->args, NULL);
106 g_string_append_printf(gstr, "\t\t</signal>\n");
111 static void generate_introspection_xml(DBusConnection *conn,
112 struct generic_data *data, const char *path)
119 g_free(data->introspect);
121 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
123 g_string_append_printf(gstr, "<node>\n");
125 for (list = data->interfaces; list; list = list->next) {
126 struct interface_data *iface = list->data;
128 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
131 generate_interface_xml(gstr, iface);
133 g_string_append_printf(gstr, "\t</interface>\n");
136 if (!dbus_connection_list_registered(conn, path, &children))
139 for (i = 0; children[i]; i++)
140 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
143 dbus_free_string_array(children);
146 g_string_append_printf(gstr, "</node>\n");
148 data->introspect = g_string_free(gstr, FALSE);
151 static DBusMessage *introspect(DBusConnection *connection,
152 DBusMessage *message, void *user_data)
154 struct generic_data *data = user_data;
157 if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
158 error("Unexpected signature to introspect call");
162 if (data->introspect == NULL)
163 generate_introspection_xml(connection, data,
164 dbus_message_get_path(message));
166 reply = dbus_message_new_method_return(message);
170 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
176 static DBusHandlerResult process_message(DBusConnection *connection,
177 DBusMessage *message, const GDBusMethodTable *method,
178 void *iface_user_data)
182 reply = method->function(connection, message, iface_user_data);
184 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
186 dbus_message_unref(reply);
187 return DBUS_HANDLER_RESULT_HANDLED;
190 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
192 return DBUS_HANDLER_RESULT_HANDLED;
196 return DBUS_HANDLER_RESULT_NEED_MEMORY;
198 dbus_connection_send(connection, reply, NULL);
199 dbus_message_unref(reply);
201 return DBUS_HANDLER_RESULT_HANDLED;
204 static GDBusPendingReply next_pending = 1;
205 static GSList *pending_security = NULL;
207 static const GDBusSecurityTable *security_table = NULL;
209 void g_dbus_pending_success(DBusConnection *connection,
210 GDBusPendingReply pending)
214 for (list = pending_security; list; list = list->next) {
215 struct security_data *secdata = list->data;
217 if (secdata->pending != pending)
220 pending_security = g_slist_remove(pending_security, secdata);
222 process_message(connection, secdata->message,
223 secdata->method, secdata->iface_user_data);
225 dbus_message_unref(secdata->message);
231 void g_dbus_pending_error_valist(DBusConnection *connection,
232 GDBusPendingReply pending, const char *name,
233 const char *format, va_list args)
237 for (list = pending_security; list; list = list->next) {
238 struct security_data *secdata = list->data;
241 if (secdata->pending != pending)
244 pending_security = g_slist_remove(pending_security, secdata);
246 reply = g_dbus_create_error_valist(secdata->message,
249 dbus_connection_send(connection, reply, NULL);
250 dbus_message_unref(reply);
253 dbus_message_unref(secdata->message);
259 void g_dbus_pending_error(DBusConnection *connection,
260 GDBusPendingReply pending,
261 const char *name, const char *format, ...)
265 va_start(args, format);
267 g_dbus_pending_error_valist(connection, pending, name, format, args);
272 int polkit_check_authorization(DBusConnection *conn,
273 const char *action, gboolean interaction,
274 void (*function) (dbus_bool_t authorized,
276 void *user_data, int timeout);
278 struct builtin_security_data {
279 DBusConnection *conn;
280 GDBusPendingReply pending;
283 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
285 struct builtin_security_data *data = user_data;
287 if (authorized == TRUE)
288 g_dbus_pending_success(data->conn, data->pending);
290 g_dbus_pending_error(data->conn, data->pending,
291 DBUS_ERROR_AUTH_FAILED, NULL);
296 static void builtin_security_function(DBusConnection *conn,
298 gboolean interaction,
299 GDBusPendingReply pending)
301 struct builtin_security_data *data;
303 data = g_new0(struct builtin_security_data, 1);
305 data->pending = pending;
307 if (polkit_check_authorization(conn, action, interaction,
308 builtin_security_result, data, 30000) < 0)
309 g_dbus_pending_error(conn, pending, NULL, NULL);
312 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
313 const GDBusMethodTable *method, void *iface_user_data)
315 const GDBusSecurityTable *security;
317 for (security = security_table; security && security->privilege;
319 struct security_data *secdata;
320 gboolean interaction;
322 if (security->privilege != method->privilege)
325 secdata = g_new(struct security_data, 1);
326 secdata->pending = next_pending++;
327 secdata->message = dbus_message_ref(msg);
328 secdata->method = method;
329 secdata->iface_user_data = iface_user_data;
331 pending_security = g_slist_prepend(pending_security, secdata);
333 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
338 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
340 security->function(conn, security->action,
341 interaction, secdata->pending);
343 builtin_security_function(conn, security->action,
344 interaction, secdata->pending);
352 static void generic_unregister(DBusConnection *connection, void *user_data)
354 struct generic_data *data = user_data;
356 g_free(data->introspect);
360 static struct interface_data *find_interface(GSList *interfaces,
368 for (list = interfaces; list; list = list->next) {
369 struct interface_data *iface = list->data;
370 if (!strcmp(name, iface->name))
377 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
378 DBusMessage *message)
380 const char *sig = dbus_message_get_signature(message);
381 const char *p = NULL;
383 for (; args && args->signature && *sig; args++) {
386 for (; *sig && *p; sig++, p++) {
392 if (*sig || (p && *p) || (args && args->signature))
398 static DBusHandlerResult generic_message(DBusConnection *connection,
399 DBusMessage *message, void *user_data)
401 struct generic_data *data = user_data;
402 struct interface_data *iface;
403 const GDBusMethodTable *method;
404 const char *interface;
406 interface = dbus_message_get_interface(message);
408 iface = find_interface(data->interfaces, interface);
410 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
412 for (method = iface->methods; method &&
413 method->name && method->function; method++) {
414 if (dbus_message_is_method_call(message, iface->name,
415 method->name) == FALSE)
418 if (g_dbus_args_have_signature(method->in_args,
422 if (check_privilege(connection, message, method,
423 iface->user_data) == TRUE)
424 return DBUS_HANDLER_RESULT_HANDLED;
426 return process_message(connection, message, method,
430 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
433 static DBusObjectPathVTable generic_table = {
434 .unregister_function = generic_unregister,
435 .message_function = generic_message,
438 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
440 struct generic_data *data = NULL;
441 char *parent_path, *slash;
443 parent_path = g_strdup(child_path);
444 slash = strrchr(parent_path, '/');
448 if (slash == parent_path && parent_path[1] != '\0')
449 parent_path[1] = '\0';
453 if (!strlen(parent_path))
456 if (dbus_connection_get_object_path_data(conn, parent_path,
457 (void *) &data) == FALSE) {
461 invalidate_parent_data(conn, parent_path);
466 g_free(data->introspect);
467 data->introspect = NULL;
473 static const GDBusMethodTable introspect_methods[] = {
474 { GDBUS_METHOD("Introspect", NULL,
475 GDBUS_ARGS({ "xml", "s" }), introspect) },
479 static void add_interface(struct generic_data *data, const char *name,
480 const GDBusMethodTable *methods,
481 const GDBusSignalTable *signals,
482 const GDBusPropertyTable *properties,
484 GDBusDestroyFunction destroy)
486 struct interface_data *iface;
488 iface = g_new0(struct interface_data, 1);
489 iface->name = g_strdup(name);
490 iface->methods = methods;
491 iface->signals = signals;
492 iface->properties = properties;
493 iface->user_data = user_data;
494 iface->destroy = destroy;
496 data->interfaces = g_slist_append(data->interfaces, iface);
499 static struct generic_data *object_path_ref(DBusConnection *connection,
502 struct generic_data *data;
504 if (dbus_connection_get_object_path_data(connection, path,
505 (void *) &data) == TRUE) {
512 data = g_new0(struct generic_data, 1);
515 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
517 if (!dbus_connection_register_object_path(connection, path,
518 &generic_table, data)) {
519 g_free(data->introspect);
524 invalidate_parent_data(connection, path);
526 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
527 introspect_methods, NULL, NULL, data, NULL);
532 static gboolean remove_interface(struct generic_data *data, const char *name)
534 struct interface_data *iface;
536 iface = find_interface(data->interfaces, name);
540 data->interfaces = g_slist_remove(data->interfaces, iface);
543 iface->destroy(iface->user_data);
551 static void object_path_unref(DBusConnection *connection, const char *path)
553 struct generic_data *data = NULL;
555 if (dbus_connection_get_object_path_data(connection, path,
556 (void *) &data) == FALSE)
564 if (data->refcount > 0)
567 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
569 invalidate_parent_data(connection, path);
571 dbus_connection_unregister_object_path(connection, path);
574 static gboolean check_signal(DBusConnection *conn, const char *path,
575 const char *interface, const char *name,
576 const GDBusArgInfo **args)
578 struct generic_data *data = NULL;
579 struct interface_data *iface;
580 const GDBusSignalTable *signal;
583 if (!dbus_connection_get_object_path_data(conn, path,
584 (void *) &data) || data == NULL) {
585 error("dbus_connection_emit_signal: path %s isn't registered",
590 iface = find_interface(data->interfaces, interface);
592 error("dbus_connection_emit_signal: %s does not implement %s",
597 for (signal = iface->signals; signal && signal->name; signal++) {
598 if (!strcmp(signal->name, name)) {
599 *args = signal->args;
605 error("No signal named %s on interface %s", name, interface);
612 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
614 const char *interface,
621 const GDBusArgInfo *args;
623 if (!check_signal(conn, path, interface, name, &args))
626 signal = dbus_message_new_signal(path, interface, name);
627 if (signal == NULL) {
628 error("Unable to allocate new %s.%s signal", interface, name);
632 ret = dbus_message_append_args_valist(signal, first, var_args);
636 if (g_dbus_args_have_signature(args, signal) == FALSE) {
637 error("%s.%s: expected signature'%s' but got '%s'",
638 interface, name, args, signature);
643 ret = dbus_connection_send(conn, signal, NULL);
646 dbus_message_unref(signal);
651 gboolean g_dbus_register_interface(DBusConnection *connection,
652 const char *path, const char *name,
653 const GDBusMethodTable *methods,
654 const GDBusSignalTable *signals,
655 const GDBusPropertyTable *properties,
657 GDBusDestroyFunction destroy)
659 struct generic_data *data;
661 data = object_path_ref(connection, path);
665 if (find_interface(data->interfaces, name)) {
666 object_path_unref(connection, path);
670 add_interface(data, name, methods, signals,
671 properties, user_data, destroy);
673 g_free(data->introspect);
674 data->introspect = NULL;
679 gboolean g_dbus_unregister_interface(DBusConnection *connection,
680 const char *path, const char *name)
682 struct generic_data *data = NULL;
687 if (dbus_connection_get_object_path_data(connection, path,
688 (void *) &data) == FALSE)
694 if (remove_interface(data, name) == FALSE)
697 g_free(data->introspect);
698 data->introspect = NULL;
700 object_path_unref(connection, path);
705 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
707 if (security_table != NULL)
710 security_table = security;
715 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
717 security_table = NULL;
722 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
723 const char *format, va_list args)
727 vsnprintf(str, sizeof(str), format, args);
729 return dbus_message_new_error(message, name, str);
732 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
733 const char *format, ...)
738 va_start(args, format);
740 reply = g_dbus_create_error_valist(message, name, format, args);
747 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
748 int type, va_list args)
752 reply = dbus_message_new_method_return(message);
756 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
757 dbus_message_unref(reply);
764 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
769 va_start(args, type);
771 reply = g_dbus_create_reply_valist(message, type, args);
778 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
782 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
783 dbus_message_set_no_reply(message, TRUE);
785 result = dbus_connection_send(connection, message, NULL);
787 dbus_message_unref(message);
792 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
793 DBusMessage *message, int type, va_list args)
797 reply = dbus_message_new_method_return(message);
801 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
802 dbus_message_unref(reply);
806 return g_dbus_send_message(connection, reply);
809 gboolean g_dbus_send_reply(DBusConnection *connection,
810 DBusMessage *message, int type, ...)
815 va_start(args, type);
817 result = g_dbus_send_reply_valist(connection, message, type, args);
824 gboolean g_dbus_emit_signal(DBusConnection *connection,
825 const char *path, const char *interface,
826 const char *name, int type, ...)
831 va_start(args, type);
833 result = emit_signal_valist(connection, path, interface,
841 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
842 const char *path, const char *interface,
843 const char *name, int type, va_list args)
845 return emit_signal_valist(connection, path, interface,