]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/bus-internal.h
sd-bus: add new sd_bus_is_ready() API
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-internal.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
de1c301e
LP
2#pragma once
3
4/***
5 This file is part of systemd.
6
7 Copyright 2013 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
45fbe937 23#include <pthread.h>
07630cea 24#include <sys/socket.h>
de1c301e
LP
25
26#include "sd-bus.h"
07630cea 27
de1c301e 28#include "bus-error.h"
bc7fd8cd 29#include "bus-kernel.h"
07630cea 30#include "bus-match.h"
036d61b3 31#include "def.h"
07630cea 32#include "hashmap.h"
07630cea
LP
33#include "list.h"
34#include "prioq.h"
35#include "refcnt.h"
36#include "socket-util.h"
37#include "util.h"
de1c301e
LP
38
39struct reply_callback {
52f3ba91 40 sd_bus_message_handler_t callback;
ac8029fc 41 usec_t timeout_usec; /* this is a relative timeout until we reach the BUS_HELLO state, and an absolute one right after */
693eb9a2 42 uint64_t cookie;
e3017af9 43 unsigned prioq_idx;
de1c301e
LP
44};
45
46struct filter_callback {
52f3ba91 47 sd_bus_message_handler_t callback;
de1c301e 48
7286037f
LP
49 unsigned last_iteration;
50
de1c301e
LP
51 LIST_FIELDS(struct filter_callback, callbacks);
52};
53
19befb2d
LP
54struct match_callback {
55 sd_bus_message_handler_t callback;
7593c7a4
LP
56 sd_bus_message_handler_t install_callback;
57
58 sd_bus_slot *install_slot; /* The AddMatch() call */
19befb2d 59
19befb2d
LP
60 unsigned last_iteration;
61
62 char *match_string;
63
64 struct bus_match_node *match_node;
65};
66
29ddb38f
LP
67struct node {
68 char *path;
69 struct node *parent;
70 LIST_HEAD(struct node, child);
71 LIST_FIELDS(struct node, siblings);
72
73 LIST_HEAD(struct node_callback, callbacks);
74 LIST_HEAD(struct node_vtable, vtables);
75 LIST_HEAD(struct node_enumerator, enumerators);
19befb2d 76 LIST_HEAD(struct node_object_manager, object_managers);
29ddb38f
LP
77};
78
79struct node_callback {
80 struct node *node;
81
82 bool is_fallback;
52f3ba91 83 sd_bus_message_handler_t callback;
a652755d 84
29ddb38f
LP
85 unsigned last_iteration;
86
87 LIST_FIELDS(struct node_callback, callbacks);
88};
89
90struct node_enumerator {
91 struct node *node;
92
93 sd_bus_node_enumerator_t callback;
29ddb38f
LP
94
95 unsigned last_iteration;
96
97 LIST_FIELDS(struct node_enumerator, enumerators);
98};
99
19befb2d
LP
100struct node_object_manager {
101 struct node *node;
102
103 LIST_FIELDS(struct node_object_manager, object_managers);
104};
105
29ddb38f
LP
106struct node_vtable {
107 struct node *node;
108
109 char *interface;
a652755d 110 bool is_fallback;
29ddb38f 111 const sd_bus_vtable *vtable;
29ddb38f 112 sd_bus_object_find_t find;
7286037f
LP
113
114 unsigned last_iteration;
29ddb38f
LP
115
116 LIST_FIELDS(struct node_vtable, vtables);
117};
118
119struct vtable_member {
120 const char *path;
121 const char *interface;
122 const char *member;
123 struct node_vtable *parent;
124 unsigned last_iteration;
125 const sd_bus_vtable *vtable;
a652755d
LP
126};
127
19befb2d 128typedef enum BusSlotType {
19befb2d
LP
129 BUS_REPLY_CALLBACK,
130 BUS_FILTER_CALLBACK,
131 BUS_MATCH_CALLBACK,
132 BUS_NODE_CALLBACK,
133 BUS_NODE_ENUMERATOR,
134 BUS_NODE_VTABLE,
135 BUS_NODE_OBJECT_MANAGER,
a71fe8b8 136 _BUS_SLOT_INVALID = -1,
19befb2d
LP
137} BusSlotType;
138
139struct sd_bus_slot {
140 unsigned n_ref;
141 sd_bus *bus;
142 void *userdata;
a71fe8b8
LP
143 BusSlotType type:5;
144 bool floating:1;
cc65fe5e 145 bool match_added:1;
9cbfc66c 146 char *description;
19befb2d
LP
147
148 LIST_FIELDS(sd_bus_slot, slots);
149
150 union {
151 struct reply_callback reply_callback;
152 struct filter_callback filter_callback;
153 struct match_callback match_callback;
154 struct node_callback node_callback;
155 struct node_enumerator node_enumerator;
156 struct node_object_manager node_object_manager;
157 struct node_vtable node_vtable;
158 };
159};
160
de1c301e 161enum bus_state {
021a1e78 162 BUS_UNSET,
ac8029fc
LP
163 BUS_WATCH_BIND, /* waiting for the socket to appear via inotify */
164 BUS_OPENING, /* the kernel's connect() is still not ready */
165 BUS_AUTHENTICATING, /* we are currently in the "SASL" authorization phase of dbus */
166 BUS_HELLO, /* we are waiting for the Hello() response */
f54514f3 167 BUS_RUNNING,
718db961 168 BUS_CLOSING,
f54514f3 169 BUS_CLOSED
de1c301e
LP
170};
171
f54514f3 172static inline bool BUS_IS_OPEN(enum bus_state state) {
718db961 173 return state > BUS_UNSET && state < BUS_CLOSING;
f54514f3
LP
174}
175
2181a7f5
LP
176enum bus_auth {
177 _BUS_AUTH_INVALID,
178 BUS_AUTH_EXTERNAL,
179 BUS_AUTH_ANONYMOUS
180};
181
de1c301e 182struct sd_bus {
e4ee6e5c
LP
183 /* We use atomic ref counting here since sd_bus_message
184 objects retain references to their originating sd_bus but
185 we want to allow them to be processed in a different
186 thread. We won't provide full thread safety, but only the
187 bare minimum that makes it possible to use sd_bus and
188 sd_bus_message objects independently and on different
189 threads as long as each object is used only once at the
190 same time. */
191 RefCount n_ref;
192
de1c301e 193 enum bus_state state;
e82c9509 194 int input_fd, output_fd;
8a5cd31e 195 int inotify_fd;
de1c301e 196 int message_version;
0f437184 197 int message_endian;
021a1e78 198
de1c301e 199 bool can_fds:1;
94bbf1ba 200 bool bus_client:1;
2571ead1 201 bool ucred_valid:1;
2181a7f5
LP
202 bool is_server:1;
203 bool anonymous_auth:1;
15d5af81
LP
204 bool prefer_readv:1;
205 bool prefer_writev:1;
7286037f
LP
206 bool match_callbacks_modified:1;
207 bool filter_callbacks_modified:1;
29ddb38f 208 bool nodes_modified:1;
adacb957 209 bool trusted:1;
758bf0c7 210 bool manual_peer_interface:1;
5972fe95
LP
211 bool is_system:1;
212 bool is_user:1;
c0765ddb 213 bool allow_interactive_authorization:1;
fbb4603d
LP
214 bool exit_on_disconnect:1;
215 bool exited:1;
216 bool exit_triggered:1;
694859b5 217 bool is_local:1;
8a5cd31e 218 bool watch_bind:1;
c7db1984
LP
219 bool is_monitor:1;
220 bool accept_fd:1;
221 bool attach_timestamp:1;
de1c301e 222
8f155917
LP
223 int use_memfd;
224
de1c301e
LP
225 void *rbuffer;
226 size_t rbuffer_size;
227
228 sd_bus_message **rqueue;
821e0756
LP
229 unsigned rqueue_size;
230 size_t rqueue_allocated;
de1c301e
LP
231
232 sd_bus_message **wqueue;
233 unsigned wqueue_size;
234 size_t windex;
821e0756 235 size_t wqueue_allocated;
de1c301e 236
693eb9a2 237 uint64_t cookie;
de1c301e
LP
238
239 char *unique_name;
219728b3 240 uint64_t unique_id;
de1c301e 241
392d5b37 242 struct bus_match_node match_callbacks;
e3017af9 243 Prioq *reply_callbacks_prioq;
c9fe4af7 244 OrderedHashmap *reply_callbacks;
de1c301e 245 LIST_HEAD(struct filter_callback, filter_callbacks);
29ddb38f
LP
246
247 Hashmap *nodes;
29ddb38f
LP
248 Hashmap *vtable_methods;
249 Hashmap *vtable_properties;
de1c301e 250
3cb46740 251 union sockaddr_union sockaddr;
de1c301e
LP
252 socklen_t sockaddr_size;
253
a7893c6b 254 char *machine;
ee502e0c 255 pid_t nspid;
6629161f 256
98178d39 257 sd_id128_t server_id;
de1c301e
LP
258
259 char *address;
260 unsigned address_index;
261
262 int last_connect_error;
263
2181a7f5
LP
264 enum bus_auth auth;
265 size_t auth_rbegin;
de1c301e
LP
266 struct iovec auth_iovec[3];
267 unsigned auth_index;
2181a7f5 268 char *auth_buffer;
e3017af9 269 usec_t auth_timeout;
2571ead1
LP
270
271 struct ucred ucred;
c4e6556c 272 char *label;
18ac4643
LP
273 gid_t *groups;
274 size_t n_groups;
2c93b4ef 275
5b12334d
LP
276 uint64_t creds_mask;
277
2c93b4ef
LP
278 int *fds;
279 unsigned n_fds;
2fd9ae2e
LP
280
281 char *exec_path;
282 char **exec_argv;
9d373862 283
7286037f 284 unsigned iteration_counter;
fd8d62d9 285
45fbe937
LP
286 /* We do locking around the memfd cache, since we want to
287 * allow people to process a sd_bus_message in a different
288 * thread then it was generated on and free it there. Since
289 * adding something to the memfd cache might happen when a
290 * message is released, we hence need to protect this bit with
291 * a mutex. */
292 pthread_mutex_t memfd_cache_mutex;
bc7fd8cd
LP
293 struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
294 unsigned n_memfd_cache;
d5a2b9a6
LP
295
296 pid_t original_pid;
264ad849 297
40ca29a1
LP
298 sd_event_source *input_io_event_source;
299 sd_event_source *output_io_event_source;
300 sd_event_source *time_event_source;
abc5fe72 301 sd_event_source *quit_event_source;
8a5cd31e 302 sd_event_source *inotify_event_source;
40ca29a1 303 sd_event *event;
1e05d493 304 int event_priority;
affff0b6 305
19befb2d
LP
306 sd_bus_message *current_message;
307 sd_bus_slot *current_slot;
caa82984
LP
308 sd_bus_message_handler_t current_handler;
309 void *current_userdata;
76b54375
LP
310
311 sd_bus **default_bus_ptr;
312 pid_t tid;
8a0e0ed9 313
751bc6ac 314 char *cgroup_root;
5972fe95 315
455971c1 316 char *description;
b28ff39f 317
8f8f05a9 318 sd_bus_track *track_queue;
19befb2d
LP
319
320 LIST_HEAD(sd_bus_slot, slots);
232f3677 321 LIST_HEAD(sd_bus_track, tracks);
8a5cd31e
LP
322
323 int *inotify_watches;
324 size_t n_inotify_watches;
40ca29a1 325};
e3017af9 326
036d61b3 327/* For method calls we time-out at 25s, like in the D-Bus reference implementation */
e3017af9 328#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
25220239 329
036d61b3
LP
330/* For the authentication phase we grant 90s, to provide extra room during boot, when RNGs and such are not filled up
331 * with enough entropy yet and might delay the boot */
332#define BUS_AUTH_TIMEOUT ((usec_t) DEFAULT_TIMEOUT_USEC)
333
5ddda46f
LP
334#define BUS_WQUEUE_MAX (192*1024)
335#define BUS_RQUEUE_MAX (192*1024)
25220239
LP
336
337#define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
338#define BUS_AUTH_SIZE_MAX (64*1024)
ac89bf1d 339
ed205a6b
LP
340#define BUS_CONTAINER_DEPTH 128
341
ac89bf1d
LP
342/* Defined by the specification as maximum size of an array in
343 * bytes */
344#define BUS_ARRAY_MAX_SIZE 67108864
345
2c93b4ef
LP
346#define BUS_FDS_MAX 1024
347
2fd9ae2e
LP
348#define BUS_EXEC_ARGV_MAX 256
349
0ce036ce
LP
350bool interface_name_is_valid(const char *p) _pure_;
351bool service_name_is_valid(const char *p) _pure_;
f5d8989c 352char* service_name_startswith(const char *a, const char *b);
0ce036ce
LP
353bool member_name_is_valid(const char *p) _pure_;
354bool object_path_is_valid(const char *p) _pure_;
355char *object_path_startswith(const char *a, const char *b) _pure_;
6693860f 356
0ce036ce
LP
357bool namespace_complex_pattern(const char *pattern, const char *value) _pure_;
358bool path_complex_pattern(const char *pattern, const char *value) _pure_;
392d5b37 359
0ce036ce
LP
360bool namespace_simple_pattern(const char *pattern, const char *value) _pure_;
361bool path_simple_pattern(const char *pattern, const char *value) _pure_;
392d5b37 362
0ce036ce
LP
363int bus_message_type_from_string(const char *s, uint8_t *u) _pure_;
364const char *bus_message_type_to_string(uint8_t u) _pure_;
392d5b37 365
6693860f 366#define error_name_is_valid interface_name_is_valid
20902f3e
LP
367
368int bus_ensure_running(sd_bus *bus);
a7e3212d
LP
369int bus_start_running(sd_bus *bus);
370int bus_next_address(sd_bus *bus);
d5a2b9a6 371
7adc46fc 372int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
777d7a61 373
7adc46fc 374int bus_rqueue_make_room(sd_bus *bus);
7d22c717 375
d5a2b9a6 376bool bus_pid_changed(sd_bus *bus);
92e189e5 377
0f8bd8de
LP
378char *bus_address_escape(const char *v);
379
8a5cd31e
LP
380int bus_attach_io_events(sd_bus *b);
381int bus_attach_inotify_event(sd_bus *b);
382
383void bus_close_inotify_fd(sd_bus *b);
384void bus_close_io_fds(sd_bus *b);
385
92e189e5
LP
386#define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \
387 for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
388 _slash && !(_slash[(_slash) == (prefix)] = 0); \
389 _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
8ce2afd6
LP
390
391/* If we are invoking callbacks of a bus object, ensure unreffing the
392 * bus from the callback doesn't destroy the object we are working
393 * on */
394#define BUS_DONT_DESTROY(bus) \
4afd3348 395 _cleanup_(sd_bus_unrefp) _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)
09365592
LP
396
397int bus_set_address_system(sd_bus *bus);
398int bus_set_address_user(sd_bus *bus);
399int bus_set_address_system_remote(sd_bus *b, const char *host);
de33fc62 400int bus_set_address_system_machine(sd_bus *b, const char *machine);
19befb2d 401
fe3f22d1 402int bus_get_root_path(sd_bus *bus);
a095315b
KS
403
404int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
d4d00020 405
759e02e7
LP
406#define bus_assert_return(expr, r, error) \
407 do { \
34c38d2a 408 if (!assert_log(expr, #expr)) \
759e02e7
LP
409 return sd_bus_error_set_errno(error, r); \
410 } while (false)
98c5bbc8
LP
411
412void bus_enter_closing(sd_bus *bus);