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 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;
76 /* Gather enough data to have a single complete type */
77 for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
84 if (struct_level <= 0 && dict_level <= 0)
92 if (struct_level <= 0 && dict_level <= 0)
98 if (struct_level <= 0 && dict_level <= 0)
109 type[len + 1] = '\0';
112 error("Unexpected signature: %s", sig);
117 g_string_append_printf(gstr,
118 "\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
121 g_string_append_printf(gstr,
122 "\t\t\t<arg type=\"%s\"/>\n",
127 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
129 const GDBusMethodTable *method;
130 const GDBusSignalTable *signal;
132 for (method = iface->methods; method && method->name; method++) {
133 if (!strlen(method->signature) && !strlen(method->reply))
134 g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
137 g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
139 print_arguments(gstr, method->signature, "in");
140 print_arguments(gstr, method->reply, "out");
141 g_string_append_printf(gstr, "\t\t</method>\n");
145 for (signal = iface->signals; signal && signal->name; signal++) {
146 if (!strlen(signal->signature))
147 g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
150 g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
152 print_arguments(gstr, signal->signature, NULL);
153 g_string_append_printf(gstr, "\t\t</signal>\n");
158 static void generate_introspection_xml(DBusConnection *conn,
159 struct generic_data *data, const char *path)
166 g_free(data->introspect);
168 gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
170 g_string_append_printf(gstr, "<node>\n");
172 for (list = data->interfaces; list; list = list->next) {
173 struct interface_data *iface = list->data;
175 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
178 generate_interface_xml(gstr, iface);
180 g_string_append_printf(gstr, "\t</interface>\n");
183 if (!dbus_connection_list_registered(conn, path, &children))
186 for (i = 0; children[i]; i++)
187 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
190 dbus_free_string_array(children);
193 g_string_append_printf(gstr, "</node>\n");
195 data->introspect = g_string_free(gstr, FALSE);
198 static DBusMessage *introspect(DBusConnection *connection,
199 DBusMessage *message, void *user_data)
201 struct generic_data *data = user_data;
204 if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
205 error("Unexpected signature to introspect call");
209 if (data->introspect == NULL)
210 generate_introspection_xml(connection, data,
211 dbus_message_get_path(message));
213 reply = dbus_message_new_method_return(message);
217 dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
223 static DBusHandlerResult process_message(DBusConnection *connection,
224 DBusMessage *message, const GDBusMethodTable *method,
225 void *iface_user_data)
229 reply = method->function(connection, message, iface_user_data);
231 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
233 dbus_message_unref(reply);
234 return DBUS_HANDLER_RESULT_HANDLED;
237 if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
239 return DBUS_HANDLER_RESULT_HANDLED;
243 return DBUS_HANDLER_RESULT_NEED_MEMORY;
245 dbus_connection_send(connection, reply, NULL);
246 dbus_message_unref(reply);
248 return DBUS_HANDLER_RESULT_HANDLED;
251 static GDBusPendingReply next_pending = 1;
252 static GSList *pending_security = NULL;
254 static const GDBusSecurityTable *security_table = NULL;
256 void g_dbus_pending_success(DBusConnection *connection,
257 GDBusPendingReply pending)
261 for (list = pending_security; list; list = list->next) {
262 struct security_data *secdata = list->data;
264 if (secdata->pending != pending)
267 pending_security = g_slist_remove(pending_security, secdata);
269 process_message(connection, secdata->message,
270 secdata->method, secdata->iface_user_data);
272 dbus_message_unref(secdata->message);
278 void g_dbus_pending_error_valist(DBusConnection *connection,
279 GDBusPendingReply pending, const char *name,
280 const char *format, va_list args)
284 for (list = pending_security; list; list = list->next) {
285 struct security_data *secdata = list->data;
288 if (secdata->pending != pending)
291 pending_security = g_slist_remove(pending_security, secdata);
293 reply = g_dbus_create_error_valist(secdata->message,
296 dbus_connection_send(connection, reply, NULL);
297 dbus_message_unref(reply);
300 dbus_message_unref(secdata->message);
306 void g_dbus_pending_error(DBusConnection *connection,
307 GDBusPendingReply pending,
308 const char *name, const char *format, ...)
312 va_start(args, format);
314 g_dbus_pending_error_valist(connection, pending, name, format, args);
319 int polkit_check_authorization(DBusConnection *conn,
320 const char *action, gboolean interaction,
321 void (*function) (dbus_bool_t authorized,
323 void *user_data, int timeout);
325 struct builtin_security_data {
326 DBusConnection *conn;
327 GDBusPendingReply pending;
330 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
332 struct builtin_security_data *data = user_data;
334 if (authorized == TRUE)
335 g_dbus_pending_success(data->conn, data->pending);
337 g_dbus_pending_error(data->conn, data->pending,
338 DBUS_ERROR_AUTH_FAILED, NULL);
343 static void builtin_security_function(DBusConnection *conn,
345 gboolean interaction,
346 GDBusPendingReply pending)
348 struct builtin_security_data *data;
350 data = g_new0(struct builtin_security_data, 1);
352 data->pending = pending;
354 if (polkit_check_authorization(conn, action, interaction,
355 builtin_security_result, data, 30000) < 0)
356 g_dbus_pending_error(conn, pending, NULL, NULL);
359 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
360 const GDBusMethodTable *method, void *iface_user_data)
362 const GDBusSecurityTable *security;
364 for (security = security_table; security && security->privilege;
366 struct security_data *secdata;
367 gboolean interaction;
369 if (security->privilege != method->privilege)
372 secdata = g_new(struct security_data, 1);
373 secdata->pending = next_pending++;
374 secdata->message = dbus_message_ref(msg);
375 secdata->method = method;
376 secdata->iface_user_data = iface_user_data;
378 pending_security = g_slist_prepend(pending_security, secdata);
380 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
385 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
387 security->function(conn, security->action,
388 interaction, secdata->pending);
390 builtin_security_function(conn, security->action,
391 interaction, secdata->pending);
399 static void generic_unregister(DBusConnection *connection, void *user_data)
401 struct generic_data *data = user_data;
403 g_free(data->introspect);
407 static struct interface_data *find_interface(GSList *interfaces,
415 for (list = interfaces; list; list = list->next) {
416 struct interface_data *iface = list->data;
417 if (!strcmp(name, iface->name))
424 static DBusHandlerResult generic_message(DBusConnection *connection,
425 DBusMessage *message, void *user_data)
427 struct generic_data *data = user_data;
428 struct interface_data *iface;
429 const GDBusMethodTable *method;
430 const char *interface;
432 interface = dbus_message_get_interface(message);
434 iface = find_interface(data->interfaces, interface);
436 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
438 for (method = iface->methods; method &&
439 method->name && method->function; method++) {
440 if (dbus_message_is_method_call(message, iface->name,
441 method->name) == FALSE)
444 if (dbus_message_has_signature(message,
445 method->signature) == FALSE)
448 if (check_privilege(connection, message, method,
449 iface->user_data) == TRUE)
450 return DBUS_HANDLER_RESULT_HANDLED;
452 return process_message(connection, message, method,
456 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
459 static DBusObjectPathVTable generic_table = {
460 .unregister_function = generic_unregister,
461 .message_function = generic_message,
464 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
466 struct generic_data *data = NULL;
467 char *parent_path, *slash;
469 parent_path = g_strdup(child_path);
470 slash = strrchr(parent_path, '/');
474 if (slash == parent_path && parent_path[1] != '\0')
475 parent_path[1] = '\0';
479 if (!strlen(parent_path))
482 if (dbus_connection_get_object_path_data(conn, parent_path,
483 (void *) &data) == FALSE) {
487 invalidate_parent_data(conn, parent_path);
492 g_free(data->introspect);
493 data->introspect = NULL;
499 static const GDBusMethodTable introspect_methods[] = {
500 { "Introspect", "", "s", introspect },
504 static void add_interface(struct generic_data *data, const char *name,
505 const GDBusMethodTable *methods,
506 const GDBusSignalTable *signals,
507 const GDBusPropertyTable *properties,
509 GDBusDestroyFunction destroy)
511 struct interface_data *iface;
513 iface = g_new0(struct interface_data, 1);
514 iface->name = g_strdup(name);
515 iface->methods = methods;
516 iface->signals = signals;
517 iface->properties = properties;
518 iface->user_data = user_data;
519 iface->destroy = destroy;
521 data->interfaces = g_slist_append(data->interfaces, iface);
524 static struct generic_data *object_path_ref(DBusConnection *connection,
527 struct generic_data *data;
529 if (dbus_connection_get_object_path_data(connection, path,
530 (void *) &data) == TRUE) {
537 data = g_new0(struct generic_data, 1);
540 data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
542 if (!dbus_connection_register_object_path(connection, path,
543 &generic_table, data)) {
544 g_free(data->introspect);
549 invalidate_parent_data(connection, path);
551 add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
552 introspect_methods, NULL, NULL, data, NULL);
557 static gboolean remove_interface(struct generic_data *data, const char *name)
559 struct interface_data *iface;
561 iface = find_interface(data->interfaces, name);
565 data->interfaces = g_slist_remove(data->interfaces, iface);
568 iface->destroy(iface->user_data);
576 static void object_path_unref(DBusConnection *connection, const char *path)
578 struct generic_data *data = NULL;
580 if (dbus_connection_get_object_path_data(connection, path,
581 (void *) &data) == FALSE)
589 if (data->refcount > 0)
592 remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
594 invalidate_parent_data(connection, path);
596 dbus_connection_unregister_object_path(connection, path);
599 static gboolean check_signal(DBusConnection *conn, const char *path,
600 const char *interface, const char *name,
603 struct generic_data *data = NULL;
604 struct interface_data *iface;
605 const GDBusSignalTable *signal;
608 if (!dbus_connection_get_object_path_data(conn, path,
609 (void *) &data) || data == NULL) {
610 error("dbus_connection_emit_signal: path %s isn't registered",
615 iface = find_interface(data->interfaces, interface);
617 error("dbus_connection_emit_signal: %s does not implement %s",
622 for (signal = iface->signals; signal && signal->name; signal++) {
623 if (!strcmp(signal->name, name)) {
624 *args = signal->signature;
630 error("No signal named %s on interface %s", name, interface);
637 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
639 const char *interface,
646 const char *signature, *args;
648 if (!check_signal(conn, path, interface, name, &args))
651 signal = dbus_message_new_signal(path, interface, name);
652 if (signal == NULL) {
653 error("Unable to allocate new %s.%s signal", interface, name);
657 ret = dbus_message_append_args_valist(signal, first, var_args);
661 signature = dbus_message_get_signature(signal);
662 if (strcmp(args, signature) != 0) {
663 error("%s.%s: expected signature'%s' but got '%s'",
664 interface, name, args, signature);
669 ret = dbus_connection_send(conn, signal, NULL);
672 dbus_message_unref(signal);
677 gboolean g_dbus_register_interface(DBusConnection *connection,
678 const char *path, const char *name,
679 const GDBusMethodTable *methods,
680 const GDBusSignalTable *signals,
681 const GDBusPropertyTable *properties,
683 GDBusDestroyFunction destroy)
685 struct generic_data *data;
687 data = object_path_ref(connection, path);
691 if (find_interface(data->interfaces, name)) {
692 object_path_unref(connection, path);
696 add_interface(data, name, methods, signals,
697 properties, user_data, destroy);
699 g_free(data->introspect);
700 data->introspect = NULL;
705 gboolean g_dbus_unregister_interface(DBusConnection *connection,
706 const char *path, const char *name)
708 struct generic_data *data = NULL;
713 if (dbus_connection_get_object_path_data(connection, path,
714 (void *) &data) == FALSE)
720 if (remove_interface(data, name) == FALSE)
723 g_free(data->introspect);
724 data->introspect = NULL;
726 object_path_unref(connection, path);
731 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
733 if (security_table != NULL)
736 security_table = security;
741 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
743 security_table = NULL;
748 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
749 const char *format, va_list args)
753 vsnprintf(str, sizeof(str), format, args);
755 return dbus_message_new_error(message, name, str);
758 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
759 const char *format, ...)
764 va_start(args, format);
766 reply = g_dbus_create_error_valist(message, name, format, args);
773 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
774 int type, va_list args)
778 reply = dbus_message_new_method_return(message);
782 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
783 dbus_message_unref(reply);
790 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
795 va_start(args, type);
797 reply = g_dbus_create_reply_valist(message, type, args);
804 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
808 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
809 dbus_message_set_no_reply(message, TRUE);
811 result = dbus_connection_send(connection, message, NULL);
813 dbus_message_unref(message);
818 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
819 DBusMessage *message, int type, va_list args)
823 reply = dbus_message_new_method_return(message);
827 if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
828 dbus_message_unref(reply);
832 return g_dbus_send_message(connection, reply);
835 gboolean g_dbus_send_reply(DBusConnection *connection,
836 DBusMessage *message, int type, ...)
841 va_start(args, type);
843 result = g_dbus_send_reply_valist(connection, message, type, args);
850 gboolean g_dbus_emit_signal(DBusConnection *connection,
851 const char *path, const char *interface,
852 const char *name, int type, ...)
857 va_start(args, type);
859 result = emit_signal_valist(connection, path, interface,
867 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
868 const char *path, const char *interface,
869 const char *name, int type, va_list args)
871 return emit_signal_valist(connection, path, interface,