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