sssd_dbus_request.c revision d9577dbd92555b0755881e37724019ef9c578404
d9577dbd92555b0755881e37724019ef9c578404Stef Walter/*
d9577dbd92555b0755881e37724019ef9c578404Stef Walter Authors:
d9577dbd92555b0755881e37724019ef9c578404Stef Walter Stef Walter <stefw@redhat.com>
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter Copyright (C) 2014 Red Hat
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter This program is free software; you can redistribute it and/or modify
d9577dbd92555b0755881e37724019ef9c578404Stef Walter it under the terms of the GNU General Public License as published by
d9577dbd92555b0755881e37724019ef9c578404Stef Walter the Free Software Foundation; either version 3 of the License, or
d9577dbd92555b0755881e37724019ef9c578404Stef Walter (at your option) any later version.
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter This program is distributed in the hope that it will be useful,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter but WITHOUT ANY WARRANTY; without even the implied warranty of
d9577dbd92555b0755881e37724019ef9c578404Stef Walter MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d9577dbd92555b0755881e37724019ef9c578404Stef Walter GNU General Public License for more details.
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter You should have received a copy of the GNU General Public License
d9577dbd92555b0755881e37724019ef9c578404Stef Walter along with this program. If not, see <http://www.gnu.org/licenses/>.
d9577dbd92555b0755881e37724019ef9c578404Stef Walter*/
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter#include "util/util.h"
d9577dbd92555b0755881e37724019ef9c578404Stef Walter#include "sbus/sssd_dbus.h"
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter#include <sys/time.h>
d9577dbd92555b0755881e37724019ef9c578404Stef Walter#include <dbus/dbus.h>
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walterstatic int sbus_request_destructor(struct sbus_request *dbus_req)
d9577dbd92555b0755881e37724019ef9c578404Stef Walter{
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_message_unref(dbus_req->message);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return 0;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter}
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walterstruct sbus_request *
d9577dbd92555b0755881e37724019ef9c578404Stef Waltersbus_new_request(struct sbus_connection *conn,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter struct sbus_interface *intf,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DBusMessage *message)
d9577dbd92555b0755881e37724019ef9c578404Stef Walter{
d9577dbd92555b0755881e37724019ef9c578404Stef Walter struct sbus_request *dbus_req;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_req = talloc_zero(conn, struct sbus_request);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter if (!dbus_req) {
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus request\n");
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return NULL;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter }
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_req->intf = intf;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_req->conn = conn;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_req->message = dbus_message_ref(message);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter talloc_set_destructor(dbus_req, sbus_request_destructor);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return dbus_req;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter}
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walterint sbus_request_finish(struct sbus_request *dbus_req,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DBusMessage *reply)
d9577dbd92555b0755881e37724019ef9c578404Stef Walter{
d9577dbd92555b0755881e37724019ef9c578404Stef Walter if (reply) {
d9577dbd92555b0755881e37724019ef9c578404Stef Walter sbus_conn_send_reply(dbus_req->conn, reply);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter }
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return talloc_free(dbus_req);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter}
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walterint sbus_request_return_and_finish(struct sbus_request *dbus_req,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter int first_arg_type,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter ...)
d9577dbd92555b0755881e37724019ef9c578404Stef Walter{
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DBusMessage *reply;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_bool_t dbret;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter va_list va;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter int ret;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter reply = dbus_message_new_method_return(dbus_req->message);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter if (!reply) {
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus message\n");
d9577dbd92555b0755881e37724019ef9c578404Stef Walter sbus_request_finish(dbus_req, NULL);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return ENOMEM;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter }
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter va_start(va, first_arg_type);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbret = dbus_message_append_args_valist(reply, first_arg_type, va);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter va_end(va);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter if (dbret) {
d9577dbd92555b0755881e37724019ef9c578404Stef Walter ret = sbus_request_finish(dbus_req, reply);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter } else {
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DEBUG(SSSDBG_CRIT_FAILURE, "Couldn't build DBus message\n");
d9577dbd92555b0755881e37724019ef9c578404Stef Walter sbus_request_finish(dbus_req, NULL);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter ret = EINVAL;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter }
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_message_unref(reply);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return ret;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter}
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walterint sbus_request_fail_and_finish(struct sbus_request *dbus_req,
d9577dbd92555b0755881e37724019ef9c578404Stef Walter const DBusError *error)
d9577dbd92555b0755881e37724019ef9c578404Stef Walter{
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DBusMessage *reply;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter int ret;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter reply = dbus_message_new_error(dbus_req->message, error->name, error->message);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter if (!reply) {
d9577dbd92555b0755881e37724019ef9c578404Stef Walter DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating DBus message\n");
d9577dbd92555b0755881e37724019ef9c578404Stef Walter sbus_request_finish(dbus_req, NULL);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return ENOMEM;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter }
d9577dbd92555b0755881e37724019ef9c578404Stef Walter
d9577dbd92555b0755881e37724019ef9c578404Stef Walter ret = sbus_request_finish(dbus_req, reply);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter dbus_message_unref(reply);
d9577dbd92555b0755881e37724019ef9c578404Stef Walter return ret;
d9577dbd92555b0755881e37724019ef9c578404Stef Walter}