439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina Pavel Březina <pbrezina@redhat.com>
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina Copyright (C) 2016 Red Hat
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina This program is free software; you can redistribute it and/or modify
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina it under the terms of the GNU General Public License as published by
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina the Free Software Foundation; either version 3 of the License, or
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina (at your option) any later version.
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina This program is distributed in the hope that it will be useful,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina GNU General Public License for more details.
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina You should have received a copy of the GNU General Public License
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březinastatic int sbus_talloc_msg_destructor(struct sbus_talloc_msg *talloc_msg)
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina /* There may exist more references to this message but this talloc
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * context is no longer valid. We remove dbus message data to invoke
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * dbus destructor now. */
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina dbus_message_set_data(talloc_msg->msg, talloc_msg->data_slot, NULL, NULL);
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březinastatic void sbus_msg_data_destructor(void *ctx)
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina talloc_msg = talloc_get_type(ctx, struct sbus_talloc_msg);
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina dbus_message_free_data_slot(&talloc_msg->data_slot);
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina /* References to this message dropped to zero but through
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * dbus_message_unref(), not by calling talloc_free(). We need to free
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * the talloc context and avoid running talloc desctuctor. */
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březinaerrno_t sbus_talloc_bound_message(TALLOC_CTX *mem_ctx, DBusMessage *msg)
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina /* Create a talloc context that will unreference this message when
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * the parent context is freed. */
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina talloc_msg = talloc(mem_ctx, struct sbus_talloc_msg);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina "Unable to bound D-Bus message with talloc context!\n");
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina /* Allocate a dbus message data slot that will contain point to the
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * talloc context so we can pick up cases when the dbus message is
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina * freed through dbus api. */
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina bret = dbus_message_allocate_data_slot(&data_slot);
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to allocate data slot!\n");
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina bret = dbus_message_set_data(msg, data_slot, talloc_msg, free_fn);
5d556f70f00c43864d8495d7caacfadf962799dfPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set message data!\n");
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina talloc_set_destructor(talloc_msg, sbus_talloc_msg_destructor);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina static struct {
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina } list[] = { { SBUS_ERROR_INTERNAL, ERR_INTERNAL },
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina { SBUS_ERROR_UNKNOWN_DOMAIN, ERR_DOMAIN_NOT_FOUND },
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina if (dbus_error_has_name(error, list[i].name)) {
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina bret = dbus_set_error_from_message(&error, reply);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina if (bret == false) {
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to read error from message\n");
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "D-Bus error [%s]: %s\n",
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina error.name, (error.message == NULL ? "(null)" : error.message));
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected D-Bus message type?\n");
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel BřezinaDBusMessage *sbus_create_message_valist(TALLOC_CTX *mem_ctx,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina const char *bus,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina msg = dbus_message_new_method_call(bus, path, iface, method);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create message\n");
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina bret = dbus_message_append_args_valist(msg, first_arg_type, va);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Failed to build message\n");
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina ret = sbus_talloc_bound_message(mem_ctx, msg);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel BřezinaDBusMessage *_sbus_create_message(TALLOC_CTX *mem_ctx,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina const char *bus,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina msg = sbus_create_message_valist(mem_ctx, bus, path, iface, method,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březinaerrno_t sbus_parse_message_valist(DBusMessage *msg,
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina bret = dbus_message_get_args_valist(msg, &error, first_arg_type, va);
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina if (bret == false) {
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse D-Bus message\n");
439e08cdc5c83b3e5835cb0435983f1da2ffbaf1Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse D-Bus message [%s]: %s\n",