2007-01-19 01:56:00 +08:00
|
|
|
/*
|
|
|
|
*
|
2008-12-06 22:40:08 +08:00
|
|
|
* D-Bus helper library
|
2007-01-19 01:56:00 +08:00
|
|
|
*
|
2011-01-02 09:31:09 +08:00
|
|
|
* Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
|
2007-01-19 01:56:00 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2008-05-09 06:19:14 +08:00
|
|
|
#include <stdio.h>
|
2007-01-19 01:56:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-01-20 13:26:15 +08:00
|
|
|
#include <glib.h>
|
2007-01-19 01:56:00 +08:00
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
2008-05-09 06:19:14 +08:00
|
|
|
#include "gdbus.h"
|
2007-01-19 01:56:00 +08:00
|
|
|
|
2008-05-09 06:19:14 +08:00
|
|
|
#define info(fmt...)
|
|
|
|
#define error(fmt...)
|
|
|
|
#define debug(fmt...)
|
2007-01-19 01:56:00 +08:00
|
|
|
|
|
|
|
struct generic_data {
|
2008-06-06 21:52:43 +08:00
|
|
|
unsigned int refcount;
|
2007-01-19 07:39:27 +08:00
|
|
|
GSList *interfaces;
|
|
|
|
char *introspect;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct interface_data {
|
2007-05-03 18:58:08 +08:00
|
|
|
char *name;
|
2010-03-08 06:32:48 +08:00
|
|
|
const GDBusMethodTable *methods;
|
|
|
|
const GDBusSignalTable *signals;
|
|
|
|
const GDBusPropertyTable *properties;
|
2008-05-27 15:20:56 +08:00
|
|
|
void *user_data;
|
|
|
|
GDBusDestroyFunction destroy;
|
2007-01-19 01:56:00 +08:00
|
|
|
};
|
|
|
|
|
2010-08-29 06:18:47 +08:00
|
|
|
struct security_data {
|
|
|
|
GDBusPendingReply pending;
|
|
|
|
DBusMessage *message;
|
|
|
|
const GDBusMethodTable *method;
|
|
|
|
void *iface_user_data;
|
|
|
|
};
|
|
|
|
|
2012-05-18 11:23:30 +08:00
|
|
|
static void print_arguments(GString *gstr, const GDBusArgInfo *args,
|
2008-06-06 18:45:39 +08:00
|
|
|
const char *direction)
|
2007-05-03 22:19:09 +08:00
|
|
|
{
|
2012-05-18 11:23:30 +08:00
|
|
|
for (; args && args->name; args++) {
|
|
|
|
g_string_append_printf(gstr,
|
|
|
|
"\t\t\t<arg name=\"%s\" type=\"%s\"",
|
|
|
|
args->name, args->signature);
|
2007-05-04 05:08:32 +08:00
|
|
|
|
2007-05-03 22:19:09 +08:00
|
|
|
if (direction)
|
|
|
|
g_string_append_printf(gstr,
|
2012-05-18 11:23:30 +08:00
|
|
|
" direction=\"%s\"/>\n", direction);
|
2007-05-03 22:19:09 +08:00
|
|
|
else
|
2012-05-18 11:23:30 +08:00
|
|
|
g_string_append_printf(gstr, "/>\n");
|
|
|
|
|
2007-05-03 22:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 15:57:04 +08:00
|
|
|
static void generate_interface_xml(GString *gstr, struct interface_data *iface)
|
|
|
|
{
|
2010-03-08 06:32:48 +08:00
|
|
|
const GDBusMethodTable *method;
|
|
|
|
const GDBusSignalTable *signal;
|
2008-05-27 15:57:04 +08:00
|
|
|
|
|
|
|
for (method = iface->methods; method && method->name; method++) {
|
2012-05-18 11:23:34 +08:00
|
|
|
gboolean deprecated = method->flags &
|
|
|
|
G_DBUS_METHOD_FLAG_DEPRECATED;
|
2012-05-18 11:23:35 +08:00
|
|
|
gboolean noreply = method->flags &
|
|
|
|
G_DBUS_METHOD_FLAG_NOREPLY;
|
2012-05-18 11:23:34 +08:00
|
|
|
|
2012-05-18 11:23:35 +08:00
|
|
|
if (!deprecated && !noreply &&
|
2012-05-18 11:23:34 +08:00
|
|
|
!(method->in_args && method->in_args->name) &&
|
2012-05-18 11:23:30 +08:00
|
|
|
!(method->out_args && method->out_args->name))
|
2008-05-27 15:57:04 +08:00
|
|
|
g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
|
|
|
|
method->name);
|
|
|
|
else {
|
|
|
|
g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
|
|
|
|
method->name);
|
2012-05-18 11:23:30 +08:00
|
|
|
print_arguments(gstr, method->in_args, "in");
|
|
|
|
print_arguments(gstr, method->out_args, "out");
|
2012-05-18 11:23:34 +08:00
|
|
|
|
|
|
|
if (deprecated)
|
|
|
|
g_string_append_printf(gstr, "\t\t\t<annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n");
|
|
|
|
|
2012-05-18 11:23:35 +08:00
|
|
|
if (noreply)
|
|
|
|
g_string_append_printf(gstr, "\t\t\t<annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n");
|
|
|
|
|
2008-05-27 15:57:04 +08:00
|
|
|
g_string_append_printf(gstr, "\t\t</method>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (signal = iface->signals; signal && signal->name; signal++) {
|
2012-05-18 11:23:34 +08:00
|
|
|
gboolean deprecated = signal->flags &
|
|
|
|
G_DBUS_SIGNAL_FLAG_DEPRECATED;
|
|
|
|
|
|
|
|
if (!deprecated && !(signal->args && signal->args->name))
|
2008-05-27 15:57:04 +08:00
|
|
|
g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
|
|
|
|
signal->name);
|
|
|
|
else {
|
|
|
|
g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
|
|
|
|
signal->name);
|
2012-05-18 11:23:30 +08:00
|
|
|
print_arguments(gstr, signal->args, NULL);
|
2012-05-18 11:23:34 +08:00
|
|
|
|
|
|
|
if (deprecated)
|
|
|
|
g_string_append_printf(gstr, "\t\t\t<annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n");
|
|
|
|
|
2008-05-27 15:57:04 +08:00
|
|
|
g_string_append_printf(gstr, "\t\t</signal>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-05 07:47:27 +08:00
|
|
|
static void generate_introspection_xml(DBusConnection *conn,
|
2008-05-27 15:57:04 +08:00
|
|
|
struct generic_data *data, const char *path)
|
2007-05-03 22:19:09 +08:00
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
GString *gstr;
|
2007-05-05 00:31:44 +08:00
|
|
|
char **children;
|
|
|
|
int i;
|
2007-05-03 22:19:09 +08:00
|
|
|
|
|
|
|
g_free(data->introspect);
|
|
|
|
|
|
|
|
gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
|
|
|
|
|
2011-01-14 23:14:21 +08:00
|
|
|
g_string_append_printf(gstr, "<node>\n");
|
2007-05-03 22:19:09 +08:00
|
|
|
|
|
|
|
for (list = data->interfaces; list; list = list->next) {
|
|
|
|
struct interface_data *iface = list->data;
|
|
|
|
|
2008-05-27 15:57:04 +08:00
|
|
|
g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
|
|
|
|
iface->name);
|
2007-05-03 22:19:09 +08:00
|
|
|
|
2008-05-27 15:57:04 +08:00
|
|
|
generate_interface_xml(gstr, iface);
|
2007-05-03 22:19:09 +08:00
|
|
|
|
|
|
|
g_string_append_printf(gstr, "\t</interface>\n");
|
|
|
|
}
|
|
|
|
|
2007-05-05 00:31:44 +08:00
|
|
|
if (!dbus_connection_list_registered(conn, path, &children))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (i = 0; children[i]; i++)
|
2008-05-27 15:57:04 +08:00
|
|
|
g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
|
|
|
|
children[i]);
|
2007-05-05 00:31:44 +08:00
|
|
|
|
|
|
|
dbus_free_string_array(children);
|
|
|
|
|
|
|
|
done:
|
2007-05-03 22:19:09 +08:00
|
|
|
g_string_append_printf(gstr, "</node>\n");
|
|
|
|
|
|
|
|
data->introspect = g_string_free(gstr, FALSE);
|
|
|
|
}
|
2007-01-19 07:39:27 +08:00
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
static DBusMessage *introspect(DBusConnection *connection,
|
|
|
|
DBusMessage *message, void *user_data)
|
2007-05-05 07:47:27 +08:00
|
|
|
{
|
2009-09-16 20:03:12 +08:00
|
|
|
struct generic_data *data = user_data;
|
2007-05-05 07:47:27 +08:00
|
|
|
DBusMessage *reply;
|
|
|
|
|
2010-11-28 03:39:01 +08:00
|
|
|
if (data->introspect == NULL)
|
2007-05-05 07:47:27 +08:00
|
|
|
generate_introspection_xml(connection, data,
|
|
|
|
dbus_message_get_path(message));
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(message);
|
2010-11-28 03:39:01 +08:00
|
|
|
if (reply == NULL)
|
2009-09-16 20:03:12 +08:00
|
|
|
return NULL;
|
2007-05-05 07:47:27 +08:00
|
|
|
|
|
|
|
dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
return reply;
|
2007-05-05 07:47:27 +08:00
|
|
|
}
|
|
|
|
|
2010-08-29 06:18:47 +08:00
|
|
|
static DBusHandlerResult process_message(DBusConnection *connection,
|
|
|
|
DBusMessage *message, const GDBusMethodTable *method,
|
|
|
|
void *iface_user_data)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
|
|
|
reply = method->function(connection, message, iface_user_data);
|
|
|
|
|
|
|
|
if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
|
|
|
|
if (reply != NULL)
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
|
|
|
|
if (reply == NULL)
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reply == NULL)
|
|
|
|
return DBUS_HANDLER_RESULT_NEED_MEMORY;
|
|
|
|
|
|
|
|
dbus_connection_send(connection, reply, NULL);
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GDBusPendingReply next_pending = 1;
|
|
|
|
static GSList *pending_security = NULL;
|
|
|
|
|
|
|
|
static const GDBusSecurityTable *security_table = NULL;
|
|
|
|
|
|
|
|
void g_dbus_pending_success(DBusConnection *connection,
|
|
|
|
GDBusPendingReply pending)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
for (list = pending_security; list; list = list->next) {
|
|
|
|
struct security_data *secdata = list->data;
|
|
|
|
|
|
|
|
if (secdata->pending != pending)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pending_security = g_slist_remove(pending_security, secdata);
|
|
|
|
|
2011-05-10 21:58:04 +08:00
|
|
|
process_message(connection, secdata->message,
|
2010-08-29 06:18:47 +08:00
|
|
|
secdata->method, secdata->iface_user_data);
|
|
|
|
|
|
|
|
dbus_message_unref(secdata->message);
|
|
|
|
g_free(secdata);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 18:31:20 +08:00
|
|
|
void g_dbus_pending_error_valist(DBusConnection *connection,
|
|
|
|
GDBusPendingReply pending, const char *name,
|
|
|
|
const char *format, va_list args)
|
2010-08-29 06:18:47 +08:00
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
for (list = pending_security; list; list = list->next) {
|
|
|
|
struct security_data *secdata = list->data;
|
2010-08-29 18:31:20 +08:00
|
|
|
DBusMessage *reply;
|
2010-08-29 06:18:47 +08:00
|
|
|
|
|
|
|
if (secdata->pending != pending)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pending_security = g_slist_remove(pending_security, secdata);
|
|
|
|
|
2010-08-29 18:31:20 +08:00
|
|
|
reply = g_dbus_create_error_valist(secdata->message,
|
|
|
|
name, format, args);
|
|
|
|
if (reply != NULL) {
|
|
|
|
dbus_connection_send(connection, reply, NULL);
|
|
|
|
dbus_message_unref(reply);
|
2010-08-29 06:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_unref(secdata->message);
|
|
|
|
g_free(secdata);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 18:31:20 +08:00
|
|
|
void g_dbus_pending_error(DBusConnection *connection,
|
|
|
|
GDBusPendingReply pending,
|
|
|
|
const char *name, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
g_dbus_pending_error_valist(connection, pending, name, format, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2010-09-09 23:04:40 +08:00
|
|
|
int polkit_check_authorization(DBusConnection *conn,
|
|
|
|
const char *action, gboolean interaction,
|
|
|
|
void (*function) (dbus_bool_t authorized,
|
|
|
|
void *user_data),
|
|
|
|
void *user_data, int timeout);
|
|
|
|
|
|
|
|
struct builtin_security_data {
|
|
|
|
DBusConnection *conn;
|
|
|
|
GDBusPendingReply pending;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void builtin_security_result(dbus_bool_t authorized, void *user_data)
|
|
|
|
{
|
|
|
|
struct builtin_security_data *data = user_data;
|
|
|
|
|
|
|
|
if (authorized == TRUE)
|
|
|
|
g_dbus_pending_success(data->conn, data->pending);
|
|
|
|
else
|
|
|
|
g_dbus_pending_error(data->conn, data->pending,
|
|
|
|
DBUS_ERROR_AUTH_FAILED, NULL);
|
|
|
|
|
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void builtin_security_function(DBusConnection *conn,
|
|
|
|
const char *action,
|
|
|
|
gboolean interaction,
|
|
|
|
GDBusPendingReply pending)
|
|
|
|
{
|
|
|
|
struct builtin_security_data *data;
|
|
|
|
|
|
|
|
data = g_new0(struct builtin_security_data, 1);
|
|
|
|
data->conn = conn;
|
|
|
|
data->pending = pending;
|
|
|
|
|
|
|
|
if (polkit_check_authorization(conn, action, interaction,
|
|
|
|
builtin_security_result, data, 30000) < 0)
|
|
|
|
g_dbus_pending_error(conn, pending, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2010-08-29 06:18:47 +08:00
|
|
|
static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
const GDBusMethodTable *method, void *iface_user_data)
|
|
|
|
{
|
|
|
|
const GDBusSecurityTable *security;
|
|
|
|
|
2010-09-09 22:39:46 +08:00
|
|
|
for (security = security_table; security && security->privilege;
|
|
|
|
security++) {
|
2010-08-29 06:18:47 +08:00
|
|
|
struct security_data *secdata;
|
2010-09-09 22:39:46 +08:00
|
|
|
gboolean interaction;
|
2010-08-29 06:18:47 +08:00
|
|
|
|
|
|
|
if (security->privilege != method->privilege)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
secdata = g_new(struct security_data, 1);
|
|
|
|
secdata->pending = next_pending++;
|
|
|
|
secdata->message = dbus_message_ref(msg);
|
|
|
|
secdata->method = method;
|
|
|
|
secdata->iface_user_data = iface_user_data;
|
|
|
|
|
|
|
|
pending_security = g_slist_prepend(pending_security, secdata);
|
|
|
|
|
2010-09-09 22:39:46 +08:00
|
|
|
if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
|
|
|
|
interaction = TRUE;
|
|
|
|
else
|
|
|
|
interaction = FALSE;
|
|
|
|
|
2010-09-09 23:04:40 +08:00
|
|
|
if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
|
|
|
|
security->function)
|
2010-09-09 22:39:46 +08:00
|
|
|
security->function(conn, security->action,
|
|
|
|
interaction, secdata->pending);
|
2010-09-09 23:04:40 +08:00
|
|
|
else
|
|
|
|
builtin_security_function(conn, security->action,
|
|
|
|
interaction, secdata->pending);
|
2010-08-29 06:18:47 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-05-05 07:47:27 +08:00
|
|
|
static void generic_unregister(DBusConnection *connection, void *user_data)
|
|
|
|
{
|
|
|
|
struct generic_data *data = user_data;
|
|
|
|
|
|
|
|
g_free(data->introspect);
|
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct interface_data *find_interface(GSList *interfaces,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
2010-11-28 03:39:01 +08:00
|
|
|
if (name == NULL)
|
2009-03-01 05:42:10 +08:00
|
|
|
return NULL;
|
|
|
|
|
2007-05-05 07:47:27 +08:00
|
|
|
for (list = interfaces; list; list = list->next) {
|
|
|
|
struct interface_data *iface = list->data;
|
|
|
|
if (!strcmp(name, iface->name))
|
|
|
|
return iface;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:23:31 +08:00
|
|
|
static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
|
|
|
|
DBusMessage *message)
|
|
|
|
{
|
|
|
|
const char *sig = dbus_message_get_signature(message);
|
|
|
|
const char *p = NULL;
|
|
|
|
|
|
|
|
for (; args && args->signature && *sig; args++) {
|
|
|
|
p = args->signature;
|
|
|
|
|
|
|
|
for (; *sig && *p; sig++, p++) {
|
|
|
|
if (*p != *sig)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*sig || (p && *p) || (args && args->signature))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-05-05 07:47:27 +08:00
|
|
|
static DBusHandlerResult generic_message(DBusConnection *connection,
|
|
|
|
DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
struct generic_data *data = user_data;
|
|
|
|
struct interface_data *iface;
|
2010-03-08 06:32:48 +08:00
|
|
|
const GDBusMethodTable *method;
|
2007-05-05 07:47:27 +08:00
|
|
|
const char *interface;
|
|
|
|
|
|
|
|
interface = dbus_message_get_interface(message);
|
|
|
|
|
|
|
|
iface = find_interface(data->interfaces, interface);
|
2010-11-28 03:39:01 +08:00
|
|
|
if (iface == NULL)
|
2007-05-05 07:47:27 +08:00
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
2008-05-27 17:07:28 +08:00
|
|
|
for (method = iface->methods; method &&
|
2008-05-27 16:50:36 +08:00
|
|
|
method->name && method->function; method++) {
|
|
|
|
if (dbus_message_is_method_call(message, iface->name,
|
|
|
|
method->name) == FALSE)
|
|
|
|
continue;
|
|
|
|
|
2012-05-18 11:23:31 +08:00
|
|
|
if (g_dbus_args_have_signature(method->in_args,
|
|
|
|
message) == FALSE)
|
2008-05-27 16:50:36 +08:00
|
|
|
continue;
|
|
|
|
|
2010-08-29 06:18:47 +08:00
|
|
|
if (check_privilege(connection, message, method,
|
|
|
|
iface->user_data) == TRUE)
|
2008-05-27 16:50:36 +08:00
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
2010-08-29 06:18:47 +08:00
|
|
|
return process_message(connection, message, method,
|
|
|
|
iface->user_data);
|
2008-05-27 16:50:36 +08:00
|
|
|
}
|
|
|
|
|
2007-05-05 07:47:27 +08:00
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusObjectPathVTable generic_table = {
|
|
|
|
.unregister_function = generic_unregister,
|
|
|
|
.message_function = generic_message,
|
|
|
|
};
|
|
|
|
|
2007-07-19 19:22:06 +08:00
|
|
|
static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
|
2007-05-05 00:31:44 +08:00
|
|
|
{
|
2007-06-22 05:52:51 +08:00
|
|
|
struct generic_data *data = NULL;
|
2007-05-05 00:31:44 +08:00
|
|
|
char *parent_path, *slash;
|
|
|
|
|
|
|
|
parent_path = g_strdup(child_path);
|
|
|
|
slash = strrchr(parent_path, '/');
|
2010-11-28 03:39:01 +08:00
|
|
|
if (slash == NULL)
|
2007-05-05 00:31:44 +08:00
|
|
|
goto done;
|
|
|
|
|
2009-05-12 00:49:53 +08:00
|
|
|
if (slash == parent_path && parent_path[1] != '\0')
|
|
|
|
parent_path[1] = '\0';
|
|
|
|
else
|
|
|
|
*slash = '\0';
|
|
|
|
|
2007-05-05 00:31:44 +08:00
|
|
|
if (!strlen(parent_path))
|
|
|
|
goto done;
|
|
|
|
|
2011-01-14 23:14:20 +08:00
|
|
|
if (dbus_connection_get_object_path_data(conn, parent_path,
|
|
|
|
(void *) &data) == FALSE) {
|
2007-05-05 00:31:44 +08:00
|
|
|
goto done;
|
2010-04-30 06:23:45 +08:00
|
|
|
}
|
2007-05-05 00:31:44 +08:00
|
|
|
|
2011-01-14 23:14:20 +08:00
|
|
|
invalidate_parent_data(conn, parent_path);
|
|
|
|
|
2010-11-28 03:39:01 +08:00
|
|
|
if (data == NULL)
|
2007-05-05 00:31:44 +08:00
|
|
|
goto done;
|
|
|
|
|
2007-05-05 07:47:27 +08:00
|
|
|
g_free(data->introspect);
|
|
|
|
data->introspect = NULL;
|
2007-05-05 00:31:44 +08:00
|
|
|
|
|
|
|
done:
|
|
|
|
g_free(parent_path);
|
|
|
|
}
|
|
|
|
|
2012-05-18 10:56:27 +08:00
|
|
|
static const GDBusMethodTable introspect_methods[] = {
|
2012-05-18 11:23:33 +08:00
|
|
|
{ GDBUS_METHOD("Introspect", NULL,
|
2012-05-18 11:23:28 +08:00
|
|
|
GDBUS_ARGS({ "xml", "s" }), introspect) },
|
2009-09-16 20:03:12 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void add_interface(struct generic_data *data, const char *name,
|
2010-03-08 06:32:48 +08:00
|
|
|
const GDBusMethodTable *methods,
|
|
|
|
const GDBusSignalTable *signals,
|
|
|
|
const GDBusPropertyTable *properties,
|
2009-09-16 20:03:12 +08:00
|
|
|
void *user_data,
|
|
|
|
GDBusDestroyFunction destroy)
|
|
|
|
{
|
|
|
|
struct interface_data *iface;
|
|
|
|
|
|
|
|
iface = g_new0(struct interface_data, 1);
|
|
|
|
iface->name = g_strdup(name);
|
|
|
|
iface->methods = methods;
|
|
|
|
iface->signals = signals;
|
|
|
|
iface->properties = properties;
|
|
|
|
iface->user_data = user_data;
|
|
|
|
iface->destroy = destroy;
|
|
|
|
|
|
|
|
data->interfaces = g_slist_append(data->interfaces, iface);
|
|
|
|
}
|
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
static struct generic_data *object_path_ref(DBusConnection *connection,
|
|
|
|
const char *path)
|
2007-01-19 01:56:00 +08:00
|
|
|
{
|
2008-05-27 16:33:45 +08:00
|
|
|
struct generic_data *data;
|
2007-01-19 01:56:00 +08:00
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
if (dbus_connection_get_object_path_data(connection, path,
|
2008-05-27 16:33:45 +08:00
|
|
|
(void *) &data) == TRUE) {
|
|
|
|
if (data != NULL) {
|
|
|
|
data->refcount++;
|
|
|
|
return data;
|
|
|
|
}
|
2008-05-27 15:47:26 +08:00
|
|
|
}
|
2007-01-19 01:56:00 +08:00
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
data = g_new0(struct generic_data, 1);
|
2010-08-29 06:18:47 +08:00
|
|
|
data->refcount = 1;
|
2007-01-19 01:56:00 +08:00
|
|
|
|
2007-05-03 22:19:09 +08:00
|
|
|
data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
|
2007-01-19 07:39:27 +08:00
|
|
|
|
2007-05-07 22:46:57 +08:00
|
|
|
if (!dbus_connection_register_object_path(connection, path,
|
|
|
|
&generic_table, data)) {
|
2007-12-06 20:56:11 +08:00
|
|
|
g_free(data->introspect);
|
2007-02-26 21:44:45 +08:00
|
|
|
g_free(data);
|
2008-05-27 15:47:26 +08:00
|
|
|
return NULL;
|
2007-01-19 01:56:00 +08:00
|
|
|
}
|
|
|
|
|
2007-07-19 19:22:06 +08:00
|
|
|
invalidate_parent_data(connection, path);
|
2007-05-05 00:31:44 +08:00
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
|
|
|
|
introspect_methods, NULL, NULL, data, NULL);
|
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
static gboolean remove_interface(struct generic_data *data, const char *name)
|
|
|
|
{
|
|
|
|
struct interface_data *iface;
|
|
|
|
|
|
|
|
iface = find_interface(data->interfaces, name);
|
2010-11-28 03:39:01 +08:00
|
|
|
if (iface == NULL)
|
2009-09-16 20:03:12 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
data->interfaces = g_slist_remove(data->interfaces, iface);
|
|
|
|
|
|
|
|
if (iface->destroy)
|
|
|
|
iface->destroy(iface->user_data);
|
|
|
|
|
|
|
|
g_free(iface->name);
|
|
|
|
g_free(iface);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
static void object_path_unref(DBusConnection *connection, const char *path)
|
|
|
|
{
|
|
|
|
struct generic_data *data = NULL;
|
|
|
|
|
2008-05-27 16:33:45 +08:00
|
|
|
if (dbus_connection_get_object_path_data(connection, path,
|
|
|
|
(void *) &data) == FALSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (data == NULL)
|
2008-05-27 15:47:26 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
data->refcount--;
|
|
|
|
|
|
|
|
if (data->refcount > 0)
|
|
|
|
return;
|
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
|
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
invalidate_parent_data(connection, path);
|
|
|
|
|
|
|
|
dbus_connection_unregister_object_path(connection, path);
|
|
|
|
}
|
|
|
|
|
2008-03-20 10:22:20 +08:00
|
|
|
static gboolean check_signal(DBusConnection *conn, const char *path,
|
|
|
|
const char *interface, const char *name,
|
2012-05-18 11:23:31 +08:00
|
|
|
const GDBusArgInfo **args)
|
2007-05-07 18:52:10 +08:00
|
|
|
{
|
2007-06-22 05:52:51 +08:00
|
|
|
struct generic_data *data = NULL;
|
2007-05-07 18:52:10 +08:00
|
|
|
struct interface_data *iface;
|
2010-03-08 06:32:48 +08:00
|
|
|
const GDBusSignalTable *signal;
|
2007-05-07 18:52:10 +08:00
|
|
|
|
2008-03-20 10:22:20 +08:00
|
|
|
*args = NULL;
|
2007-06-22 05:52:51 +08:00
|
|
|
if (!dbus_connection_get_object_path_data(conn, path,
|
2010-11-28 03:39:01 +08:00
|
|
|
(void *) &data) || data == NULL) {
|
2007-05-07 22:46:57 +08:00
|
|
|
error("dbus_connection_emit_signal: path %s isn't registered",
|
|
|
|
path);
|
2007-05-07 18:52:10 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
iface = find_interface(data->interfaces, interface);
|
2010-11-28 03:39:01 +08:00
|
|
|
if (iface == NULL) {
|
2007-05-07 18:52:10 +08:00
|
|
|
error("dbus_connection_emit_signal: %s does not implement %s",
|
|
|
|
path, interface);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-27 20:17:15 +08:00
|
|
|
for (signal = iface->signals; signal && signal->name; signal++) {
|
|
|
|
if (!strcmp(signal->name, name)) {
|
2012-05-18 11:23:31 +08:00
|
|
|
*args = signal->args;
|
2008-05-27 20:17:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 03:39:01 +08:00
|
|
|
if (*args == NULL) {
|
2007-05-07 18:52:10 +08:00
|
|
|
error("No signal named %s on interface %s", name, interface);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-03-20 10:22:20 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-08 00:22:47 +08:00
|
|
|
static dbus_bool_t emit_signal_valist(DBusConnection *conn,
|
2008-03-20 10:22:20 +08:00
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *name,
|
|
|
|
int first,
|
|
|
|
va_list var_args)
|
|
|
|
{
|
|
|
|
DBusMessage *signal;
|
|
|
|
dbus_bool_t ret;
|
2012-05-18 11:23:31 +08:00
|
|
|
const GDBusArgInfo *args;
|
2008-03-20 10:22:20 +08:00
|
|
|
|
|
|
|
if (!check_signal(conn, path, interface, name, &args))
|
|
|
|
return FALSE;
|
|
|
|
|
2007-05-07 18:52:10 +08:00
|
|
|
signal = dbus_message_new_signal(path, interface, name);
|
2010-11-28 03:39:01 +08:00
|
|
|
if (signal == NULL) {
|
2007-05-07 18:52:10 +08:00
|
|
|
error("Unable to allocate new %s.%s signal", interface, name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-06-19 20:59:59 +08:00
|
|
|
ret = dbus_message_append_args_valist(signal, first, var_args);
|
|
|
|
if (!ret)
|
|
|
|
goto fail;
|
2007-05-07 18:52:10 +08:00
|
|
|
|
2012-05-18 11:23:31 +08:00
|
|
|
if (g_dbus_args_have_signature(args, signal) == FALSE) {
|
2007-06-19 20:59:59 +08:00
|
|
|
error("%s.%s: expected signature'%s' but got '%s'",
|
|
|
|
interface, name, args, signature);
|
|
|
|
ret = FALSE;
|
|
|
|
goto fail;
|
2007-05-07 18:52:10 +08:00
|
|
|
}
|
|
|
|
|
2007-06-19 20:59:59 +08:00
|
|
|
ret = dbus_connection_send(conn, signal, NULL);
|
2008-06-06 21:52:43 +08:00
|
|
|
|
2007-06-19 20:59:59 +08:00
|
|
|
fail:
|
2007-05-07 18:52:10 +08:00
|
|
|
dbus_message_unref(signal);
|
|
|
|
|
2007-06-19 20:59:59 +08:00
|
|
|
return ret;
|
2007-05-07 18:52:10 +08:00
|
|
|
}
|
|
|
|
|
2008-05-27 15:06:07 +08:00
|
|
|
gboolean g_dbus_register_interface(DBusConnection *connection,
|
|
|
|
const char *path, const char *name,
|
2010-03-08 06:32:48 +08:00
|
|
|
const GDBusMethodTable *methods,
|
|
|
|
const GDBusSignalTable *signals,
|
|
|
|
const GDBusPropertyTable *properties,
|
2008-05-27 15:06:07 +08:00
|
|
|
void *user_data,
|
|
|
|
GDBusDestroyFunction destroy)
|
|
|
|
{
|
2008-05-27 15:47:26 +08:00
|
|
|
struct generic_data *data;
|
2008-05-27 15:20:56 +08:00
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
data = object_path_ref(connection, path);
|
|
|
|
if (data == NULL)
|
2008-05-27 15:20:56 +08:00
|
|
|
return FALSE;
|
|
|
|
|
2010-04-30 05:27:54 +08:00
|
|
|
if (find_interface(data->interfaces, name)) {
|
|
|
|
object_path_unref(connection, path);
|
2008-05-27 15:20:56 +08:00
|
|
|
return FALSE;
|
2010-04-30 05:27:54 +08:00
|
|
|
}
|
2008-05-27 15:20:56 +08:00
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
add_interface(data, name, methods, signals,
|
|
|
|
properties, user_data, destroy);
|
2008-05-27 15:20:56 +08:00
|
|
|
|
|
|
|
g_free(data->introspect);
|
|
|
|
data->introspect = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
2008-05-27 15:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_dbus_unregister_interface(DBusConnection *connection,
|
|
|
|
const char *path, const char *name)
|
|
|
|
{
|
2008-05-27 15:20:56 +08:00
|
|
|
struct generic_data *data = NULL;
|
|
|
|
|
2010-11-28 03:39:01 +08:00
|
|
|
if (path == NULL)
|
2008-12-24 14:15:50 +08:00
|
|
|
return FALSE;
|
|
|
|
|
2008-05-27 16:33:45 +08:00
|
|
|
if (dbus_connection_get_object_path_data(connection, path,
|
|
|
|
(void *) &data) == FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (data == NULL)
|
2008-05-27 15:20:56 +08:00
|
|
|
return FALSE;
|
|
|
|
|
2009-09-16 20:03:12 +08:00
|
|
|
if (remove_interface(data, name) == FALSE)
|
2008-05-27 15:20:56 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_free(data->introspect);
|
|
|
|
data->introspect = NULL;
|
|
|
|
|
2008-05-27 15:47:26 +08:00
|
|
|
object_path_unref(connection, path);
|
|
|
|
|
2008-05-27 15:20:56 +08:00
|
|
|
return TRUE;
|
2008-05-27 15:06:07 +08:00
|
|
|
}
|
2008-05-27 19:42:46 +08:00
|
|
|
|
2010-08-29 06:18:47 +08:00
|
|
|
gboolean g_dbus_register_security(const GDBusSecurityTable *security)
|
|
|
|
{
|
|
|
|
if (security_table != NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
security_table = security;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
|
|
|
|
{
|
|
|
|
security_table = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-05-27 19:42:46 +08:00
|
|
|
DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
|
|
|
|
const char *format, va_list args)
|
|
|
|
{
|
2008-09-11 19:33:31 +08:00
|
|
|
char str[1024];
|
|
|
|
|
|
|
|
vsnprintf(str, sizeof(str), format, args);
|
|
|
|
|
|
|
|
return dbus_message_new_error(message, name, str);
|
2008-05-27 19:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
reply = g_dbus_create_error_valist(message, name, format, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
2008-05-28 01:48:01 +08:00
|
|
|
|
|
|
|
DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
|
|
|
|
int type, va_list args)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(message);
|
|
|
|
if (reply == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
|
|
|
va_start(args, type);
|
|
|
|
|
|
|
|
reply = g_dbus_create_reply_valist(message, type, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
2008-05-28 02:02:23 +08:00
|
|
|
|
|
|
|
gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
|
|
|
|
{
|
|
|
|
dbus_bool_t result;
|
|
|
|
|
2008-11-26 21:33:21 +08:00
|
|
|
if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
|
|
|
dbus_message_set_no_reply(message, TRUE);
|
|
|
|
|
2008-05-28 02:02:23 +08:00
|
|
|
result = dbus_connection_send(connection, message, NULL);
|
|
|
|
|
|
|
|
dbus_message_unref(message);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_dbus_send_reply_valist(DBusConnection *connection,
|
|
|
|
DBusMessage *message, int type, va_list args)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(message);
|
|
|
|
if (reply == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_dbus_send_message(connection, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_dbus_send_reply(DBusConnection *connection,
|
|
|
|
DBusMessage *message, int type, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
va_start(args, type);
|
|
|
|
|
|
|
|
result = g_dbus_send_reply_valist(connection, message, type, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2008-06-08 00:22:47 +08:00
|
|
|
|
|
|
|
gboolean g_dbus_emit_signal(DBusConnection *connection,
|
|
|
|
const char *path, const char *interface,
|
|
|
|
const char *name, int type, ...)
|
|
|
|
{
|
2008-12-21 04:51:39 +08:00
|
|
|
va_list args;
|
2008-06-08 00:22:47 +08:00
|
|
|
gboolean result;
|
|
|
|
|
2008-12-21 04:51:39 +08:00
|
|
|
va_start(args, type);
|
2008-06-08 00:22:47 +08:00
|
|
|
|
|
|
|
result = emit_signal_valist(connection, path, interface,
|
|
|
|
name, type, args);
|
|
|
|
|
2008-12-21 04:51:39 +08:00
|
|
|
va_end(args);
|
2008-06-08 00:22:47 +08:00
|
|
|
|
2008-12-21 04:51:39 +08:00
|
|
|
return result;
|
2008-06-08 00:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
|
|
|
|
const char *path, const char *interface,
|
|
|
|
const char *name, int type, va_list args)
|
|
|
|
{
|
|
|
|
return emit_signal_valist(connection, path, interface,
|
|
|
|
name, type, args);
|
|
|
|
}
|