5 * Copyright (C) 2004-2010 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 char *sig,
63 const char *direction)
67 for (i = 0; sig[i]; i++) {
69 int struct_level, dict_level;
74 struct_level = dict_level = 0;
75 memset(type, 0, sizeof(type));
77 /* Gather enough data to have a single complete type */
78 for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
85 if (struct_level <= 0 && dict_level <= 0)
93 if (struct_level <= 0 && dict_level <= 0)
99 if (struct_level <= 0 && dict_level <= 0)
112 g_string_append_printf(gstr,
113 "\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
116 g_string_append_printf(gstr,
117 "\t\t\t<arg type=\"%s\"/>\n",
122 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
124 const GDBusMethodTable *method;
125 const GDBusSignalTable *signal;
127 for (method = iface->methods; method && method->name; method++) {
128 if (!strlen(method->signature) && !strlen(method->reply))
129 g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
132 g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
134 print_arguments(gstr, method->signature, "in");
135 print_arguments(gstr, method->reply, "out");
136 g_string_append_printf(gstr, "\t\t</method>\n");
140 for (signal = iface->signals; signal && signal->name; signal++) {
141 if (!strlen(signal->signature))
142 g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
145 g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
147 print_arguments(gstr, signal->signature, NULL);
148 g_string_append_printf(gstr, "\t\t</signal>\n");
153 static void generate_introspection_xml(DBusConnection *conn,
154 struct generic_data *data, const char *path)
161 g_free(data->introspect);
163 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
165 g_string_append_printf(gstr, "<node name=\"%s\">\n", path);
167 for (list = data->interfaces; list; list = list->next) {
168 struct interface_data *iface = list->data;
170 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
173 generate_interface_xml(gstr, iface);
175 g_string_append_printf(gstr, "\t</interface>\n");
178 if (!dbus_connection_list_registered(conn, path, &children))
181 for (i = 0; children[i]; i++)
182 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
185 dbus_free_string_array(children);
188 g_string_append_printf(gstr, "</node>\n");
190 data->introspect = g_string_free(gstr, FALSE);
193 static DBusMessage *introspect(DBusConnection *connection,
194 DBusMessage *message, void *user_data)
196 struct generic_data *data = user_data;
199 if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
200 error("Unexpected signature to introspect call");
204 if (!data->introspect)
205 generate_introspection_xml(connection, data,
206 dbus_message_get_path(message));
208 reply = dbus_message_new_method_return(message);
212 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
218 static DBusHandlerResult process_message(DBusConnection *connection,
219 DBusMessage *message, const GDBusMethodTable *method,
220 void *iface_user_data)
224 reply = method->function(connection, message, iface_user_data);
226 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
228 dbus_message_unref(reply);
229 return DBUS_HANDLER_RESULT_HANDLED;
232 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
234 return DBUS_HANDLER_RESULT_HANDLED;
238 return DBUS_HANDLER_RESULT_NEED_MEMORY;
240 dbus_connection_send(connection, reply, NULL);
241 dbus_message_unref(reply);
243 return DBUS_HANDLER_RESULT_HANDLED;
246 static GDBusPendingReply next_pending = 1;
247 static GSList *pending_security = NULL;
249 static const GDBusSecurityTable *security_table = NULL;
251 void g_dbus_pending_success(DBusConnection *connection,
252 GDBusPendingReply pending)
256 for (list = pending_security; list; list = list->next) {
257 struct security_data *secdata = list->data;
258 DBusHandlerResult result;
260 if (secdata->pending != pending)
263 pending_security = g_slist_remove(pending_security, secdata);
265 result = process_message(connection, secdata->message,
266 secdata->method, secdata->iface_user_data);
268 dbus_message_unref(secdata->message);
274 void g_dbus_pending_error_valist(DBusConnection *connection,
275 GDBusPendingReply pending, const char *name,
276 const char *format, va_list args)
280 for (list = pending_security; list; list = list->next) {
281 struct security_data *secdata = list->data;
284 if (secdata->pending != pending)
287 pending_security = g_slist_remove(pending_security, secdata);
289 reply = g_dbus_create_error_valist(secdata->message,
292 dbus_connection_send(connection, reply, NULL);
293 dbus_message_unref(reply);
296 dbus_message_unref(secdata->message);
302 void g_dbus_pending_error(DBusConnection *connection,
303 GDBusPendingReply pending,
304 const char *name, const char *format, ...)
308 va_start(args, format);
310 g_dbus_pending_error_valist(connection, pending, name, format, args);
315 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
316 const GDBusMethodTable *method, void *iface_user_data)
318 const GDBusSecurityTable *security;
320 for (security = security_table; security && security->function &&
321 security->privilege; security++) {
322 struct security_data *secdata;
324 if (security->privilege != method->privilege)
327 secdata = g_new(struct security_data, 1);
328 secdata->pending = next_pending++;
329 secdata->message = dbus_message_ref(msg);
330 secdata->method = method;
331 secdata->iface_user_data = iface_user_data;
333 pending_security = g_slist_prepend(pending_security, secdata);
335 security->function(conn, secdata->pending);
343 static void generic_unregister(DBusConnection *connection, void *user_data)
345 struct generic_data *data = user_data;
347 g_free(data->introspect);
351 static struct interface_data *find_interface(GSList *interfaces,
359 for (list = interfaces; list; list = list->next) {
360 struct interface_data *iface = list->data;
361 if (!strcmp(name, iface->name))
368 static DBusHandlerResult generic_message(DBusConnection *connection,
369 DBusMessage *message, void *user_data)
371 struct generic_data *data = user_data;
372 struct interface_data *iface;
373 const GDBusMethodTable *method;
374 const char *interface;
376 interface = dbus_message_get_interface(message);
378 iface = find_interface(data->interfaces, interface);
380 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
382 for (method = iface->methods; method &&
383 method->name && method->function; method++) {
384 if (dbus_message_is_method_call(message, iface->name,
385 method->name) == FALSE)
388 if (dbus_message_has_signature(message,
389 method->signature) == FALSE)
392 if (check_privilege(connection, message, method,
393 iface->user_data) == TRUE)
394 return DBUS_HANDLER_RESULT_HANDLED;
396 return process_message(connection, message, method,
400 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
403 static DBusObjectPathVTable generic_table = {
404 .unregister_function = generic_unregister,
405 .message_function = generic_message,
408 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
410 struct generic_data *data = NULL;
411 char *parent_path, *slash;
413 parent_path = g_strdup(child_path);
414 slash = strrchr(parent_path, '/');
418 if (slash == parent_path && parent_path[1] != '\0')
419 parent_path[1] = '\0';
423 if (!strlen(parent_path))
426 if (!dbus_connection_get_object_path_data(conn, parent_path,
428 invalidate_parent_data(conn, parent_path);
435 g_free(data->introspect);
436 data->introspect = NULL;
442 static GDBusMethodTable introspect_methods[] = {
443 { "Introspect", "", "s", introspect },
447 static void add_interface(struct generic_data *data, const char *name,
448 const GDBusMethodTable *methods,
449 const GDBusSignalTable *signals,
450 const GDBusPropertyTable *properties,
452 GDBusDestroyFunction destroy)
454 struct interface_data *iface;
456 iface = g_new0(struct interface_data, 1);
457 iface->name = g_strdup(name);
458 iface->methods = methods;
459 iface->signals = signals;
460 iface->properties = properties;
461 iface->user_data = user_data;
462 iface->destroy = destroy;
464 data->interfaces = g_slist_append(data->interfaces, iface);
467 static struct generic_data *object_path_ref(DBusConnection *connection,
470 struct generic_data *data;
472 if (dbus_connection_get_object_path_data(connection, path,
473 (void *) &data) == TRUE) {
480 data = g_new0(struct generic_data, 1);
483 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
485 if (!dbus_connection_register_object_path(connection, path,
486 &generic_table, data)) {
487 g_free(data->introspect);
492 invalidate_parent_data(connection, path);
494 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
495 introspect_methods, NULL, NULL, data, NULL);
500 static gboolean remove_interface(struct generic_data *data, const char *name)
502 struct interface_data *iface;
504 iface = find_interface(data->interfaces, name);
508 data->interfaces = g_slist_remove(data->interfaces, iface);
511 iface->destroy(iface->user_data);
519 static void object_path_unref(DBusConnection *connection, const char *path)
521 struct generic_data *data = NULL;
523 if (dbus_connection_get_object_path_data(connection, path,
524 (void *) &data) == FALSE)
532 if (data->refcount > 0)
535 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
537 invalidate_parent_data(connection, path);
539 dbus_connection_unregister_object_path(connection, path);
542 static gboolean check_signal(DBusConnection *conn, const char *path,
543 const char *interface, const char *name,
546 struct generic_data *data = NULL;
547 struct interface_data *iface;
548 const GDBusSignalTable *signal;
551 if (!dbus_connection_get_object_path_data(conn, path,
552 (void *) &data) || !data) {
553 error("dbus_connection_emit_signal: path %s isn't registered",
558 iface = find_interface(data->interfaces, interface);
560 error("dbus_connection_emit_signal: %s does not implement %s",
565 for (signal = iface->signals; signal && signal->name; signal++) {
566 if (!strcmp(signal->name, name)) {
567 *args = signal->signature;
573 error("No signal named %s on interface %s", name, interface);
580 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
582 const char *interface,
589 const char *signature, *args;
591 if (!check_signal(conn, path, interface, name, &args))
594 signal = dbus_message_new_signal(path, interface, name);
596 error("Unable to allocate new %s.%s signal", interface, name);
600 ret = dbus_message_append_args_valist(signal, first, var_args);
604 signature = dbus_message_get_signature(signal);
605 if (strcmp(args, signature) != 0) {
606 error("%s.%s: expected signature'%s' but got '%s'",
607 interface, name, args, signature);
612 ret = dbus_connection_send(conn, signal, NULL);
615 dbus_message_unref(signal);
620 gboolean g_dbus_register_interface(DBusConnection *connection,
621 const char *path, const char *name,
622 const GDBusMethodTable *methods,
623 const GDBusSignalTable *signals,
624 const GDBusPropertyTable *properties,
626 GDBusDestroyFunction destroy)
628 struct generic_data *data;
630 data = object_path_ref(connection, path);
634 if (find_interface(data->interfaces, name)) {
635 object_path_unref(connection, path);
639 add_interface(data, name, methods, signals,
640 properties, user_data, destroy);
642 g_free(data->introspect);
643 data->introspect = NULL;
648 gboolean g_dbus_unregister_interface(DBusConnection *connection,
649 const char *path, const char *name)
651 struct generic_data *data = NULL;
656 if (dbus_connection_get_object_path_data(connection, path,
657 (void *) &data) == FALSE)
663 if (remove_interface(data, name) == FALSE)
666 g_free(data->introspect);
667 data->introspect = NULL;
669 object_path_unref(connection, path);
674 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
676 if (security_table != NULL)
679 security_table = security;
684 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
686 security_table = NULL;
691 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
692 const char *format, va_list args)
696 vsnprintf(str, sizeof(str), format, args);
698 return dbus_message_new_error(message, name, str);
701 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
702 const char *format, ...)
707 va_start(args, format);
709 reply = g_dbus_create_error_valist(message, name, format, args);
716 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
717 int type, va_list args)
721 reply = dbus_message_new_method_return(message);
725 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
726 dbus_message_unref(reply);
733 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
738 va_start(args, type);
740 reply = g_dbus_create_reply_valist(message, type, args);
747 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
751 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
752 dbus_message_set_no_reply(message, TRUE);
754 result = dbus_connection_send(connection, message, NULL);
756 dbus_message_unref(message);
761 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
762 DBusMessage *message, int type, va_list args)
766 reply = dbus_message_new_method_return(message);
770 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
771 dbus_message_unref(reply);
775 return g_dbus_send_message(connection, reply);
778 gboolean g_dbus_send_reply(DBusConnection *connection,
779 DBusMessage *message, int type, ...)
784 va_start(args, type);
786 result = g_dbus_send_reply_valist(connection, message, type, args);
793 gboolean g_dbus_emit_signal(DBusConnection *connection,
794 const char *path, const char *interface,
795 const char *name, int type, ...)
800 va_start(args, type);
802 result = emit_signal_valist(connection, path, interface,
810 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
811 const char *path, const char *interface,
812 const char *name, int type, va_list args)
814 return emit_signal_valist(connection, path, interface,