Skip to content

Commit bb7dd0b

Browse files
committed
bus: add kdbus endpoint types
Add types to describe endpoints and associated policy entries, and add a BusEndpoint instace to ExecContext.
1 parent 5369c77 commit bb7dd0b

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,8 @@ libsystemd_core_la_SOURCES = \
10281028
src/core/busname.h \
10291029
src/core/bus-common.c \
10301030
src/core/bus-common.h \
1031+
src/core/bus-endpoint.c \
1032+
src/core/bus-endpoint.h \
10311033
src/core/target.c \
10321034
src/core/target.h \
10331035
src/core/snapshot.c \

src/core/bus-endpoint.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
24+
int 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+
35+
int 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 {
58+
ep->policy_hash = hashmap_new(string_hash_func, string_compare_func);
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+
83+
void bus_endpoint_free(BusEndpoint *endpoint)
84+
{
85+
if (!endpoint)
86+
return;
87+
88+
hashmap_free_free_free(endpoint->policy_hash);
89+
free(endpoint);
90+
}

src/core/bus-endpoint.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
#pragma once
4+
5+
/***
6+
This file is part of systemd.
7+
8+
Copyright 2014 Daniel Mack
9+
10+
systemd is free software; you can redistribute it and/or modify it
11+
under the terms of the GNU Lesser General Public License as published by
12+
the Free Software Foundation; either version 2.1 of the License, or
13+
(at your option) any later version.
14+
15+
systemd is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public License
21+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
22+
***/
23+
24+
typedef struct BusEndpoint BusEndpoint;
25+
typedef struct BusEndpointPolicy BusEndpointPolicy;
26+
27+
#include "bus-common.h"
28+
#include "hashmap.h"
29+
30+
struct BusEndpointPolicy {
31+
char *name;
32+
BusPolicyAccess access;
33+
};
34+
35+
struct BusEndpoint {
36+
Hashmap *policy_hash;
37+
};
38+
39+
int bus_endpoint_new(BusEndpoint **ep);
40+
void bus_endpoint_free(BusEndpoint *endpoint);
41+
42+
int bus_endpoint_add_policy(BusEndpoint *ep, const char *name, BusPolicyAccess access);

src/core/execute.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,9 @@ void exec_context_done(ExecContext *c) {
19081908

19091909
strv_free(c->runtime_directory);
19101910
c->runtime_directory = NULL;
1911+
1912+
bus_endpoint_free(c->bus_endpoint);
1913+
c->bus_endpoint = NULL;
19111914
}
19121915

19131916
int exec_context_destroy_runtime_directory(ExecContext *c, const char *runtime_prefix) {

src/core/execute.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct ExecParameters ExecParameters;
4141
#include "fdset.h"
4242
#include "missing.h"
4343
#include "namespace.h"
44+
#include "bus-endpoint.h"
4445

4546
typedef enum ExecInput {
4647
EXEC_INPUT_NULL,
@@ -188,6 +189,9 @@ struct ExecContext {
188189
bool ioprio_set:1;
189190
bool cpu_sched_set:1;
190191
bool no_new_privileges_set:1;
192+
193+
/* custom dbus enpoint */
194+
BusEndpoint *bus_endpoint;
191195
};
192196

193197
#include "cgroup.h"

0 commit comments

Comments
 (0)