summaryrefslogtreecommitdiffstats
path: root/src/sbus/sssd_dbus_request.c
diff options
context:
space:
mode:
authorStef Walter <stefw@gnome.org>2014-01-17 12:54:42 +0100
committerJakub Hrozek <jhrozek@redhat.com>2014-03-14 13:42:17 +0100
commitd9577dbd92555b0755881e37724019ef9c578404 (patch)
tree243d1ee45c7f8d0a7f30750bbb7cfd93983e6182 /src/sbus/sssd_dbus_request.c
parent5bad17538eab85ce69e0355cd25b52b4a473cc36 (diff)
downloadsssd-d9577dbd92555b0755881e37724019ef9c578404.tar.gz
sssd-d9577dbd92555b0755881e37724019ef9c578404.tar.xz
sssd-d9577dbd92555b0755881e37724019ef9c578404.zip
sbus: Add struct sbus_request to represent a DBus invocation
struct sbus_request represents a request from a dbus client being handled by a dbus server implementation. The struct contains the message, connection and method (and in the future teh property) which is being requested. In the future it will contain caller information as well. sbus_request is a talloc memory context, and is a good place to attach any allocations and memory specific to the request. Each handler accepts an sbus_request. If a handler returns EOK, it is assumed that the handler will finish the request. Any of the sbus_request_*finish() methods can be used to complete the request and send back a reply. sbus_request_return_and_finish() uses the same argument varargs syntax as dbus_message_append_args(), which isn't a great syntax. Document it a bit, but don't try to redesign: The marshalling work (will follow this patch set) will remove the need to use varargs for most DBus implementation code. This patch migrates the monitor and data provider dbus code to use sbus_request, but does not try to rework the talloc context's to use it. Reviewed-by: Jakub Hrozek <jhrozek@redhat.com> Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
Diffstat (limited to 'src/sbus/sssd_dbus_request.c')
-rw-r--r--src/sbus/sssd_dbus_request.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/sbus/sssd_dbus_request.c b/src/sbus/sssd_dbus_request.c
new file mode 100644
index 000000000..973089c4b
--- /dev/null
+++ b/src/sbus/sssd_dbus_request.c
@@ -0,0 +1,112 @@
+/*
+ Authors:
+ Stef Walter <stefw@redhat.com>
+
+ Copyright (C) 2014 Red Hat
+
+ 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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "util/util.h"
+#include "sbus/sssd_dbus.h"
+
+#include <sys/time.h>
+#include <dbus/dbus.h>
+
+static int sbus_request_destructor(struct sbus_request *dbus_req)
+{
+ dbus_message_unref(dbus_req->message);
+ return 0;
+}
+
+struct sbus_request *
+sbus_new_request(struct sbus_connection *conn,
+ struct sbus_interface *intf,
+ DBusMessage *message)
+{
+ struct sbus_request *dbus_req;
+
+ dbus_req = talloc_zero(conn, struct sbus_request);
+ if (!dbus_req) {
+ DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus request\n");
+ return NULL;
+ }
+
+ dbus_req->intf = intf;
+ dbus_req->conn = conn;
+ dbus_req->message = dbus_message_ref(message);
+ talloc_set_destructor(dbus_req, sbus_request_destructor);
+
+ return dbus_req;
+}
+
+int sbus_request_finish(struct sbus_request *dbus_req,
+ DBusMessage *reply)
+{
+ if (reply) {
+ sbus_conn_send_reply(dbus_req->conn, reply);
+ }
+ return talloc_free(dbus_req);
+}
+
+int sbus_request_return_and_finish(struct sbus_request *dbus_req,
+ int first_arg_type,
+ ...)
+{
+ DBusMessage *reply;
+ dbus_bool_t dbret;
+ va_list va;
+ int ret;
+
+ reply = dbus_message_new_method_return(dbus_req->message);
+ if (!reply) {
+ DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus message\n");
+ sbus_request_finish(dbus_req, NULL);
+ return ENOMEM;
+ }
+
+ va_start(va, first_arg_type);
+ dbret = dbus_message_append_args_valist(reply, first_arg_type, va);
+ va_end(va);
+
+ if (dbret) {
+ ret = sbus_request_finish(dbus_req, reply);
+
+ } else {
+ DEBUG(SSSDBG_CRIT_FAILURE, "Couldn't build DBus message\n");
+ sbus_request_finish(dbus_req, NULL);
+ ret = EINVAL;
+ }
+
+ dbus_message_unref(reply);
+ return ret;
+}
+
+int sbus_request_fail_and_finish(struct sbus_request *dbus_req,
+ const DBusError *error)
+{
+ DBusMessage *reply;
+ int ret;
+
+ reply = dbus_message_new_error(dbus_req->message, error->name, error->message);
+ if (!reply) {
+ DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus message\n");
+ sbus_request_finish(dbus_req, NULL);
+ return ENOMEM;
+ }
+
+ ret = sbus_request_finish(dbus_req, reply);
+ dbus_message_unref(reply);
+ return ret;
+}