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