]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-bus/bus-internal.h
libudev: hwdb - use libudev not systemd logging
[thirdparty/systemd.git] / src / libsystemd-bus / bus-internal.h
CommitLineData
de1c301e
LP
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 2013 Lennart Poettering
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#include <sys/socket.h>
25#include <sys/un.h>
26#include <netinet/in.h>
45fbe937 27#include <pthread.h>
de1c301e
LP
28
29#include "hashmap.h"
e3017af9 30#include "prioq.h"
de1c301e
LP
31#include "list.h"
32#include "util.h"
e4ee6e5c 33#include "refcnt.h"
de1c301e
LP
34
35#include "sd-bus.h"
36#include "bus-error.h"
392d5b37 37#include "bus-match.h"
bc7fd8cd 38#include "bus-kernel.h"
de1c301e
LP
39
40struct reply_callback {
52f3ba91 41 sd_bus_message_handler_t callback;
de1c301e
LP
42 void *userdata;
43 usec_t timeout;
44 uint64_t serial;
e3017af9 45 unsigned prioq_idx;
de1c301e
LP
46};
47
48struct filter_callback {
52f3ba91 49 sd_bus_message_handler_t callback;
de1c301e
LP
50 void *userdata;
51
7286037f
LP
52 unsigned last_iteration;
53
de1c301e
LP
54 LIST_FIELDS(struct filter_callback, callbacks);
55};
56
29ddb38f
LP
57struct node {
58 char *path;
59 struct node *parent;
60 LIST_HEAD(struct node, child);
61 LIST_FIELDS(struct node, siblings);
62
63 LIST_HEAD(struct node_callback, callbacks);
64 LIST_HEAD(struct node_vtable, vtables);
65 LIST_HEAD(struct node_enumerator, enumerators);
66
67 bool object_manager;
68};
69
70struct node_callback {
71 struct node *node;
72
73 bool is_fallback;
52f3ba91 74 sd_bus_message_handler_t callback;
a652755d
LP
75 void *userdata;
76
29ddb38f
LP
77 unsigned last_iteration;
78
79 LIST_FIELDS(struct node_callback, callbacks);
80};
81
82struct node_enumerator {
83 struct node *node;
84
85 sd_bus_node_enumerator_t callback;
86 void *userdata;
87
88 unsigned last_iteration;
89
90 LIST_FIELDS(struct node_enumerator, enumerators);
91};
92
93struct node_vtable {
94 struct node *node;
95
96 char *interface;
a652755d 97 bool is_fallback;
29ddb38f
LP
98 const sd_bus_vtable *vtable;
99 void *userdata;
100 sd_bus_object_find_t find;
7286037f
LP
101
102 unsigned last_iteration;
29ddb38f
LP
103
104 LIST_FIELDS(struct node_vtable, vtables);
105};
106
107struct vtable_member {
108 const char *path;
109 const char *interface;
110 const char *member;
111 struct node_vtable *parent;
112 unsigned last_iteration;
113 const sd_bus_vtable *vtable;
a652755d
LP
114};
115
de1c301e 116enum bus_state {
021a1e78 117 BUS_UNSET,
de1c301e
LP
118 BUS_OPENING,
119 BUS_AUTHENTICATING,
120 BUS_HELLO,
f54514f3 121 BUS_RUNNING,
718db961 122 BUS_CLOSING,
f54514f3 123 BUS_CLOSED
de1c301e
LP
124};
125
f54514f3 126static inline bool BUS_IS_OPEN(enum bus_state state) {
718db961 127 return state > BUS_UNSET && state < BUS_CLOSING;
f54514f3
LP
128}
129
2181a7f5
LP
130enum bus_auth {
131 _BUS_AUTH_INVALID,
132 BUS_AUTH_EXTERNAL,
133 BUS_AUTH_ANONYMOUS
134};
135
de1c301e 136struct sd_bus {
e4ee6e5c
LP
137 /* We use atomic ref counting here since sd_bus_message
138 objects retain references to their originating sd_bus but
139 we want to allow them to be processed in a different
140 thread. We won't provide full thread safety, but only the
141 bare minimum that makes it possible to use sd_bus and
142 sd_bus_message objects independently and on different
143 threads as long as each object is used only once at the
144 same time. */
145 RefCount n_ref;
146
de1c301e 147 enum bus_state state;
e82c9509 148 int input_fd, output_fd;
de1c301e 149 int message_version;
021a1e78 150
6629161f 151 bool is_kernel:1;
de1c301e 152 bool can_fds:1;
94bbf1ba 153 bool bus_client:1;
2571ead1 154 bool ucred_valid:1;
2181a7f5
LP
155 bool is_server:1;
156 bool anonymous_auth:1;
15d5af81
LP
157 bool prefer_readv:1;
158 bool prefer_writev:1;
7286037f
LP
159 bool match_callbacks_modified:1;
160 bool filter_callbacks_modified:1;
29ddb38f 161 bool nodes_modified:1;
de1c301e 162
8f155917
LP
163 int use_memfd;
164
de1c301e
LP
165 void *rbuffer;
166 size_t rbuffer_size;
167
168 sd_bus_message **rqueue;
7d22c717 169 unsigned rqueue_size, rqueue_allocated;
de1c301e
LP
170
171 sd_bus_message **wqueue;
172 unsigned wqueue_size;
173 size_t windex;
174
175 uint64_t serial;
176
177 char *unique_name;
219728b3 178 uint64_t unique_id;
de1c301e 179
392d5b37 180 struct bus_match_node match_callbacks;
e3017af9 181 Prioq *reply_callbacks_prioq;
de1c301e
LP
182 Hashmap *reply_callbacks;
183 LIST_HEAD(struct filter_callback, filter_callbacks);
29ddb38f
LP
184
185 Hashmap *nodes;
29ddb38f
LP
186 Hashmap *vtable_methods;
187 Hashmap *vtable_properties;
de1c301e
LP
188
189 union {
190 struct sockaddr sa;
191 struct sockaddr_un un;
192 struct sockaddr_in in;
193 struct sockaddr_in6 in6;
194 } sockaddr;
195 socklen_t sockaddr_size;
196
6629161f 197 char *kernel;
a7893c6b 198 char *machine;
6629161f 199
98178d39 200 sd_id128_t server_id;
de1c301e
LP
201
202 char *address;
203 unsigned address_index;
204
205 int last_connect_error;
206
2181a7f5
LP
207 enum bus_auth auth;
208 size_t auth_rbegin;
de1c301e
LP
209 struct iovec auth_iovec[3];
210 unsigned auth_index;
2181a7f5 211 char *auth_buffer;
e3017af9 212 usec_t auth_timeout;
2571ead1
LP
213
214 struct ucred ucred;
215 char label[NAME_MAX];
2c93b4ef 216
5b12334d
LP
217 uint64_t creds_mask;
218
2c93b4ef
LP
219 int *fds;
220 unsigned n_fds;
2fd9ae2e
LP
221
222 char *exec_path;
223 char **exec_argv;
9d373862
LP
224
225 uint64_t hello_serial;
7286037f 226 unsigned iteration_counter;
fd8d62d9
LP
227
228 void *kdbus_buffer;
bc7fd8cd 229
45fbe937
LP
230 /* We do locking around the memfd cache, since we want to
231 * allow people to process a sd_bus_message in a different
232 * thread then it was generated on and free it there. Since
233 * adding something to the memfd cache might happen when a
234 * message is released, we hence need to protect this bit with
235 * a mutex. */
236 pthread_mutex_t memfd_cache_mutex;
bc7fd8cd
LP
237 struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
238 unsigned n_memfd_cache;
d5a2b9a6
LP
239
240 pid_t original_pid;
264ad849
LP
241
242 uint64_t hello_flags;
d21a7bb1 243 uint64_t attach_flags;
c7819669
LP
244
245 uint64_t match_cookie;
89ffcd2a 246
40ca29a1
LP
247 sd_event_source *input_io_event_source;
248 sd_event_source *output_io_event_source;
249 sd_event_source *time_event_source;
abc5fe72 250 sd_event_source *quit_event_source;
40ca29a1 251 sd_event *event;
affff0b6
LP
252
253 sd_bus_message *current;
76b54375
LP
254
255 sd_bus **default_bus_ptr;
256 pid_t tid;
40ca29a1 257};
e3017af9
LP
258
259#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
25220239 260
7a4a2105
LP
261#define BUS_WQUEUE_MAX 1024
262#define BUS_RQUEUE_MAX 64*1024
25220239
LP
263
264#define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
265#define BUS_AUTH_SIZE_MAX (64*1024)
ac89bf1d 266
ed205a6b
LP
267#define BUS_CONTAINER_DEPTH 128
268
ac89bf1d
LP
269/* Defined by the specification as maximum size of an array in
270 * bytes */
271#define BUS_ARRAY_MAX_SIZE 67108864
272
2c93b4ef
LP
273#define BUS_FDS_MAX 1024
274
2fd9ae2e
LP
275#define BUS_EXEC_ARGV_MAX 256
276
6693860f
LP
277bool interface_name_is_valid(const char *p);
278bool service_name_is_valid(const char *p);
279bool member_name_is_valid(const char *p);
29ddb38f
LP
280bool object_path_is_valid(const char *p);
281char *object_path_startswith(const char *a, const char *b);
6693860f 282
392d5b37
LP
283bool namespace_complex_pattern(const char *pattern, const char *value);
284bool path_complex_pattern(const char *pattern, const char *value);
285
286bool namespace_simple_pattern(const char *pattern, const char *value);
287bool path_simple_pattern(const char *pattern, const char *value);
288
289int bus_message_type_from_string(const char *s, uint8_t *u);
a56f19c4 290const char *bus_message_type_to_string(uint8_t u);
392d5b37 291
6693860f 292#define error_name_is_valid interface_name_is_valid
20902f3e
LP
293
294int bus_ensure_running(sd_bus *bus);
a7e3212d
LP
295int bus_start_running(sd_bus *bus);
296int bus_next_address(sd_bus *bus);
d5a2b9a6 297
7adc46fc 298int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
777d7a61 299
7adc46fc 300int bus_rqueue_make_room(sd_bus *bus);
7d22c717 301
d5a2b9a6 302bool bus_pid_changed(sd_bus *bus);
92e189e5 303
0f8bd8de
LP
304char *bus_address_escape(const char *v);
305
92e189e5
LP
306#define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \
307 for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
308 _slash && !(_slash[(_slash) == (prefix)] = 0); \
309 _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
8ce2afd6
LP
310
311/* If we are invoking callbacks of a bus object, ensure unreffing the
312 * bus from the callback doesn't destroy the object we are working
313 * on */
314#define BUS_DONT_DESTROY(bus) \
d7726e57 315 _cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)