bus-endpoint.c revision 96f2f3b1b5f44eb59d23d2abeac12b33a18e1e21
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering/***
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering This file is part of systemd.
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering Copyright 2014 Daniel Mack
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering systemd is free software; you can redistribute it and/or modify it
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering under the terms of the GNU Lesser General Public License as published by
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering (at your option) any later version.
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering systemd is distributed in the hope that it will be useful, but
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering Lesser General Public License for more details.
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering You should have received a copy of the GNU Lesser General Public License
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering***/
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering#include <stdlib.h>
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering#include "bus-endpoint.h"
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringint bus_endpoint_new(BusEndpoint **ep)
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering{
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert(ep);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering *ep = new0(BusEndpoint, 1);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering if (!*ep)
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering return -ENOMEM;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering return 0;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering}
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringint bus_endpoint_add_policy(BusEndpoint *ep, const char *name, BusPolicyAccess access)
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering{
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering _cleanup_free_ BusEndpointPolicy *po = NULL;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering _cleanup_free_ char *key = NULL;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering int r;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert(ep);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert(name);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert(access > _BUS_POLICY_ACCESS_INVALID && access < _BUS_POLICY_ACCESS_MAX);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering /* check if we already have this name in the policy list. If we do, see if the new access level
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering * is higher than the exising one, and upgrade the entry in that case. Otherwise, do nothing.
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering */
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering if (ep->policy_hash) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering po = hashmap_get(ep->policy_hash, name);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering if (po) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering if (po->access < access)
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering po->access = access;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering return 0;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering }
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering } else {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering ep->policy_hash = hashmap_new(&string_hash_ops);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering if (!ep->policy_hash)
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering return -ENOMEM;
}
po = new0(BusEndpointPolicy, 1);
if (!po)
return -ENOMEM;
key = strdup(name);
if (!key)
return -ENOMEM;
po->name = key;
po->access = access;
r = hashmap_put(ep->policy_hash, key, po);
if (r < 0)
return r;
po = NULL;
key = NULL;
return 0;
}
void bus_endpoint_free(BusEndpoint *endpoint)
{
if (!endpoint)
return;
hashmap_free_free_free(endpoint->policy_hash);
free(endpoint);
}