]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/bus-endpoint.c
hashmap: introduce hash_ops to make struct Hashmap smaller
[thirdparty/systemd.git] / src / core / bus-endpoint.c
CommitLineData
bb7dd0b0
DM
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Daniel Mack
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <stdlib.h>
21
22#include "bus-endpoint.h"
23
24int bus_endpoint_new(BusEndpoint **ep)
25{
26 assert(ep);
27
28 *ep = new0(BusEndpoint, 1);
29 if (!*ep)
30 return -ENOMEM;
31
32 return 0;
33}
34
35int bus_endpoint_add_policy(BusEndpoint *ep, const char *name, BusPolicyAccess access)
36{
37 _cleanup_free_ BusEndpointPolicy *po;
38 _cleanup_free_ char *key;
39 int r;
40
41 assert(ep);
42 assert(name);
43 assert(access > _BUS_POLICY_ACCESS_INVALID && access < _BUS_POLICY_ACCESS_MAX);
44
45 /* check if we already have this name in the policy list. If we do, see if the new access level
46 * is higher than the exising one, and upgrade the entry in that case. Otherwise, do nothing.
47 */
48
49 if (ep->policy_hash) {
50 po = hashmap_get(ep->policy_hash, name);
51 if (po) {
52 if (po->access < access)
53 po->access = access;
54
55 return 0;
56 }
57 } else {
d5099efc 58 ep->policy_hash = hashmap_new(&string_hash_ops);
bb7dd0b0
DM
59 if (!ep->policy_hash)
60 return -ENOMEM;
61 }
62
63 po = new0(BusEndpointPolicy, 1);
64 if (!po)
65 return -ENOMEM;
66
67 key = strdup(name);
68 if (!key)
69 return -ENOMEM;
70
71 po->name = key;
72 po->access = access;
73
74 r = hashmap_put(ep->policy_hash, key, po);
75 if (r < 0)
76 return r;
77
78 po = NULL;
79 key = NULL;
80 return 0;
81}
82
83void bus_endpoint_free(BusEndpoint *endpoint)
84{
85 if (!endpoint)
86 return;
87
88 hashmap_free_free_free(endpoint->policy_hash);
89 free(endpoint);
90}