]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | #pragma once | |
3 | ||
4 | #include "sd-id128.h" | |
5 | ||
6 | #include "bus-forward.h" | |
7 | #include "bus-kernel.h" | |
8 | #include "bus-match.h" | |
9 | #include "constants.h" | |
10 | #include "list.h" | |
11 | #include "runtime-scope.h" | |
12 | #include "socket-util.h" | |
13 | ||
14 | /* Note that we use the new /run prefix here (instead of /var/run) since we require them to be aliases and | |
15 | * that way we become independent of /var being mounted */ | |
16 | #define DEFAULT_SYSTEM_BUS_ADDRESS "unix:path=/run/dbus/system_bus_socket" | |
17 | #define DEFAULT_USER_BUS_ADDRESS_FMT "unix:path=%s/bus" | |
18 | ||
19 | typedef struct BusReplyCallback { | |
20 | sd_bus_message_handler_t callback; | |
21 | usec_t timeout_usec; /* this is a relative timeout until we reach the BUS_HELLO state, and an absolute one right after */ | |
22 | uint64_t cookie; | |
23 | unsigned prioq_idx; | |
24 | } BusReplyCallback; | |
25 | ||
26 | typedef struct BusFilterCallback { | |
27 | sd_bus_message_handler_t callback; | |
28 | ||
29 | unsigned last_iteration; | |
30 | ||
31 | LIST_FIELDS(BusFilterCallback, callbacks); | |
32 | } BusFilterCallback; | |
33 | ||
34 | typedef struct BusNode { | |
35 | char *path; | |
36 | BusNode *parent; | |
37 | LIST_HEAD(BusNode, child); | |
38 | LIST_FIELDS(BusNode, siblings); | |
39 | ||
40 | LIST_HEAD(BusNodeCallback, callbacks); | |
41 | LIST_HEAD(BusNodeVTable, vtables); | |
42 | LIST_HEAD(BusNodeEnumerator, enumerators); | |
43 | LIST_HEAD(BusNodeObjectManager, object_managers); | |
44 | } BusNode; | |
45 | ||
46 | typedef struct BusNodeCallback { | |
47 | BusNode *node; | |
48 | ||
49 | bool is_fallback; | |
50 | unsigned last_iteration; | |
51 | ||
52 | sd_bus_message_handler_t callback; | |
53 | ||
54 | LIST_FIELDS(BusNodeCallback, callbacks); | |
55 | } BusNodeCallback; | |
56 | ||
57 | typedef struct BusNodeEnumerator { | |
58 | BusNode *node; | |
59 | ||
60 | sd_bus_node_enumerator_t callback; | |
61 | ||
62 | unsigned last_iteration; | |
63 | ||
64 | LIST_FIELDS(BusNodeEnumerator, enumerators); | |
65 | } BusNodeEnumerator; | |
66 | ||
67 | typedef struct BusNodeObjectManager { | |
68 | BusNode *node; | |
69 | ||
70 | LIST_FIELDS(BusNodeObjectManager, object_managers); | |
71 | } BusNodeObjectManager; | |
72 | ||
73 | typedef struct BusNodeVTable { | |
74 | BusNode *node; | |
75 | ||
76 | bool is_fallback; | |
77 | unsigned last_iteration; | |
78 | ||
79 | char *interface; | |
80 | const sd_bus_vtable *vtable; | |
81 | sd_bus_object_find_t find; | |
82 | ||
83 | LIST_FIELDS(BusNodeVTable, vtables); | |
84 | } BusNodeVTable; | |
85 | ||
86 | typedef struct BusVTableMember { | |
87 | const char *path; | |
88 | const char *interface; | |
89 | const char *member; | |
90 | BusNodeVTable *parent; | |
91 | unsigned last_iteration; | |
92 | const sd_bus_vtable *vtable; | |
93 | } BusVTableMember; | |
94 | ||
95 | typedef enum BusSlotType { | |
96 | BUS_REPLY_CALLBACK, | |
97 | BUS_FILTER_CALLBACK, | |
98 | BUS_MATCH_CALLBACK, | |
99 | BUS_NODE_CALLBACK, | |
100 | BUS_NODE_ENUMERATOR, | |
101 | BUS_NODE_VTABLE, | |
102 | BUS_NODE_OBJECT_MANAGER, | |
103 | _BUS_SLOT_INVALID = -EINVAL, | |
104 | } BusSlotType; | |
105 | ||
106 | typedef struct sd_bus_slot { | |
107 | unsigned n_ref; | |
108 | BusSlotType type:8; | |
109 | ||
110 | /* Slots can be "floating" or not. If they are not floating (the usual case) then they reference the | |
111 | * bus object they are associated with. This means the bus object stays allocated at least as long as | |
112 | * there is a slot around associated with it. If it is floating, then the slot's lifecycle is bound | |
113 | * to the lifecycle of the bus: it will be disconnected from the bus when the bus is destroyed, and | |
114 | * it keeping the slot reffed hence won't mean the bus stays reffed too. Internally this means the | |
115 | * reference direction is reversed: floating slots objects are referenced by the bus object, and not | |
116 | * vice versa. */ | |
117 | bool floating; | |
118 | bool match_added; | |
119 | ||
120 | sd_bus *bus; | |
121 | void *userdata; | |
122 | sd_bus_destroy_t destroy_callback; | |
123 | ||
124 | char *description; | |
125 | ||
126 | LIST_FIELDS(sd_bus_slot, slots); | |
127 | ||
128 | union { | |
129 | BusReplyCallback reply_callback; | |
130 | BusFilterCallback filter_callback; | |
131 | BusMatchCallback match_callback; | |
132 | BusNodeCallback node_callback; | |
133 | BusNodeEnumerator node_enumerator; | |
134 | BusNodeObjectManager node_object_manager; | |
135 | BusNodeVTable node_vtable; | |
136 | }; | |
137 | } sd_bus_slot; | |
138 | ||
139 | typedef enum BusState { | |
140 | BUS_UNSET, | |
141 | BUS_WATCH_BIND, /* waiting for the socket to appear via inotify */ | |
142 | BUS_OPENING, /* the kernel's connect() is still not ready */ | |
143 | BUS_AUTHENTICATING, /* we are currently in the "SASL" authorization phase of dbus */ | |
144 | BUS_HELLO, /* we are waiting for the Hello() response */ | |
145 | BUS_RUNNING, | |
146 | BUS_CLOSING, | |
147 | BUS_CLOSED, | |
148 | _BUS_STATE_MAX, | |
149 | } BusState; | |
150 | ||
151 | static inline bool BUS_IS_OPEN(BusState state) { | |
152 | return state > BUS_UNSET && state < BUS_CLOSING; | |
153 | } | |
154 | ||
155 | typedef enum BusAuth { | |
156 | _BUS_AUTH_INVALID, | |
157 | BUS_AUTH_EXTERNAL, | |
158 | BUS_AUTH_ANONYMOUS | |
159 | } BusAuth; | |
160 | ||
161 | typedef struct sd_bus { | |
162 | unsigned n_ref; | |
163 | ||
164 | BusState state; | |
165 | int input_fd, output_fd; | |
166 | int inotify_fd; | |
167 | int message_version; | |
168 | int message_endian; | |
169 | ||
170 | bool can_fds; | |
171 | bool bus_client; | |
172 | bool ucred_valid; | |
173 | bool is_server; | |
174 | bool anonymous_auth; | |
175 | bool prefer_readv; | |
176 | bool prefer_writev; | |
177 | bool match_callbacks_modified; | |
178 | bool filter_callbacks_modified; | |
179 | bool nodes_modified; | |
180 | bool trusted; | |
181 | bool manual_peer_interface; | |
182 | bool allow_interactive_authorization; | |
183 | bool exit_on_disconnect; | |
184 | bool exited; | |
185 | bool exit_triggered; | |
186 | bool is_local; | |
187 | bool watch_bind; | |
188 | bool is_monitor; | |
189 | bool accept_fd; | |
190 | bool attach_timestamp; | |
191 | bool connected_signal; | |
192 | bool close_on_exit; | |
193 | ||
194 | RuntimeScope runtime_scope; | |
195 | ||
196 | int use_memfd; | |
197 | ||
198 | void *rbuffer; | |
199 | size_t rbuffer_size; | |
200 | ||
201 | sd_bus_message **rqueue; | |
202 | size_t rqueue_size; | |
203 | ||
204 | sd_bus_message **wqueue; | |
205 | size_t wqueue_size; | |
206 | size_t windex; | |
207 | ||
208 | uint64_t cookie; | |
209 | uint64_t read_counter; /* A counter for each incoming msg */ | |
210 | ||
211 | char *unique_name; | |
212 | uint64_t unique_id; | |
213 | ||
214 | BusMatchNode match_callbacks; | |
215 | Prioq *reply_callbacks_prioq; | |
216 | OrderedHashmap *reply_callbacks; | |
217 | LIST_HEAD(BusFilterCallback, filter_callbacks); | |
218 | ||
219 | Hashmap *nodes; | |
220 | Set *vtable_methods; | |
221 | Set *vtable_properties; | |
222 | ||
223 | union sockaddr_union sockaddr; | |
224 | socklen_t sockaddr_size; | |
225 | ||
226 | pid_t nspid; | |
227 | char *machine; | |
228 | ||
229 | sd_id128_t server_id; | |
230 | ||
231 | char *address; | |
232 | unsigned address_index; | |
233 | ||
234 | uid_t connect_as_uid; | |
235 | gid_t connect_as_gid; | |
236 | ||
237 | int last_connect_error; | |
238 | ||
239 | BusAuth auth; | |
240 | unsigned auth_index; | |
241 | struct iovec auth_iovec[3]; | |
242 | size_t auth_rbegin; | |
243 | char *auth_buffer; | |
244 | usec_t auth_timeout; | |
245 | ||
246 | struct ucred ucred; | |
247 | char *label; | |
248 | gid_t *groups; | |
249 | size_t n_groups; | |
250 | union sockaddr_union sockaddr_peer; | |
251 | socklen_t sockaddr_size_peer; | |
252 | int pidfd; | |
253 | ||
254 | uint64_t creds_mask; | |
255 | ||
256 | int *fds; | |
257 | size_t n_fds; | |
258 | ||
259 | char *exec_path; | |
260 | char **exec_argv; | |
261 | ||
262 | /* We do locking around the memfd cache, since we want to | |
263 | * allow people to process a sd_bus_message in a different | |
264 | * thread then it was generated on and free it there. Since | |
265 | * adding something to the memfd cache might happen when a | |
266 | * message is released, we hence need to protect this bit with | |
267 | * a mutex. */ | |
268 | pthread_mutex_t memfd_cache_mutex; | |
269 | struct memfd_cache memfd_cache[MEMFD_CACHE_MAX]; | |
270 | unsigned n_memfd_cache; | |
271 | ||
272 | uint64_t origin_id; | |
273 | pid_t busexec_pid; | |
274 | ||
275 | unsigned iteration_counter; | |
276 | ||
277 | sd_event_source *input_io_event_source; | |
278 | sd_event_source *output_io_event_source; | |
279 | sd_event_source *time_event_source; | |
280 | sd_event_source *quit_event_source; | |
281 | sd_event_source *inotify_event_source; | |
282 | sd_event *event; | |
283 | int event_priority; | |
284 | ||
285 | pid_t tid; | |
286 | ||
287 | sd_bus_message *current_message; | |
288 | sd_bus_slot *current_slot; | |
289 | sd_bus_message_handler_t current_handler; | |
290 | void *current_userdata; | |
291 | ||
292 | sd_bus **default_bus_ptr; | |
293 | ||
294 | char *description; | |
295 | char *patch_sender; | |
296 | ||
297 | sd_bus_track *track_queue; | |
298 | ||
299 | LIST_HEAD(sd_bus_slot, slots); | |
300 | LIST_HEAD(sd_bus_track, tracks); | |
301 | ||
302 | int *inotify_watches; | |
303 | size_t n_inotify_watches; | |
304 | ||
305 | /* zero means use value specified by $SYSTEMD_BUS_TIMEOUT= environment variable or built-in default */ | |
306 | usec_t method_call_timeout; | |
307 | } sd_bus; | |
308 | ||
309 | /* For method calls we timeout at 25s, like in the D-Bus reference implementation */ | |
310 | #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) | |
311 | ||
312 | /* For the authentication phase we grant 90s, to provide extra room during boot, when RNGs and such are not filled up | |
313 | * with enough entropy yet and might delay the boot */ | |
314 | #define BUS_AUTH_TIMEOUT ((usec_t) DEFAULT_TIMEOUT_USEC) | |
315 | ||
316 | #define BUS_WQUEUE_MAX (384*1024) | |
317 | #define BUS_RQUEUE_MAX (384*1024) | |
318 | ||
319 | #define BUS_MESSAGE_SIZE_MAX (128*1024*1024) | |
320 | #define BUS_AUTH_SIZE_MAX (64*1024) | |
321 | /* Note that the D-Bus specification states that bus paths shall have no size limit. We enforce here one | |
322 | * anyway, since truly unbounded strings are a security problem. The limit we pick is relatively large however, | |
323 | * to not clash unnecessarily with real-life applications. */ | |
324 | #define BUS_PATH_SIZE_MAX (64*1024) | |
325 | ||
326 | #define BUS_CONTAINER_DEPTH 128 | |
327 | ||
328 | /* Defined by the specification as maximum size of an array in bytes */ | |
329 | #define BUS_ARRAY_MAX_SIZE 67108864 | |
330 | ||
331 | #define BUS_FDS_MAX 1024 | |
332 | ||
333 | #define BUS_EXEC_ARGV_MAX 256 | |
334 | ||
335 | bool interface_name_is_valid(const char *p) _pure_; | |
336 | bool service_name_is_valid(const char *p) _pure_; | |
337 | bool member_name_is_valid(const char *p) _pure_; | |
338 | bool object_path_is_valid(const char *p) _pure_; | |
339 | ||
340 | char* object_path_startswith(const char *a, const char *b) _pure_; | |
341 | ||
342 | bool namespace_complex_pattern(const char *pattern, const char *value) _pure_; | |
343 | bool path_complex_pattern(const char *pattern, const char *value) _pure_; | |
344 | ||
345 | bool namespace_simple_pattern(const char *pattern, const char *value) _pure_; | |
346 | bool path_simple_pattern(const char *pattern, const char *value) _pure_; | |
347 | ||
348 | int bus_message_type_from_string(const char *s, uint8_t *u); | |
349 | const char* bus_message_type_to_string(uint8_t u) _pure_; | |
350 | ||
351 | #define error_name_is_valid interface_name_is_valid | |
352 | ||
353 | sd_bus *bus_resolve(sd_bus *bus); | |
354 | ||
355 | int bus_ensure_running(sd_bus *bus); | |
356 | int bus_start_running(sd_bus *bus); | |
357 | int bus_next_address(sd_bus *bus); | |
358 | ||
359 | int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m); | |
360 | ||
361 | int bus_rqueue_make_room(sd_bus *bus); | |
362 | ||
363 | bool bus_origin_changed(sd_bus *bus); | |
364 | ||
365 | char* bus_address_escape(const char *v); | |
366 | ||
367 | int bus_attach_io_events(sd_bus *b); | |
368 | int bus_attach_inotify_event(sd_bus *b); | |
369 | ||
370 | void bus_close_inotify_fd(sd_bus *b); | |
371 | void bus_close_io_fds(sd_bus *b); | |
372 | ||
373 | int bus_add_match_full( | |
374 | sd_bus *bus, | |
375 | sd_bus_slot **slot, | |
376 | bool asynchronous, | |
377 | const char *match, | |
378 | sd_bus_message_handler_t callback, | |
379 | sd_bus_message_handler_t install_callback, | |
380 | void *userdata, | |
381 | uint64_t timeout_usec); | |
382 | ||
383 | #define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \ | |
384 | for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \ | |
385 | _slash && ((_slash[(_slash) == (prefix)] = 0), true); \ | |
386 | _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/')) | |
387 | ||
388 | /* If we are invoking callbacks of a bus object, ensure unreffing the | |
389 | * bus from the callback doesn't destroy the object we are working on */ | |
390 | #define BUS_DONT_DESTROY(bus) \ | |
391 | _cleanup_(sd_bus_unrefp) _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus) | |
392 | ||
393 | int bus_set_address_system(sd_bus *bus); | |
394 | int bus_set_address_user(sd_bus *bus); | |
395 | int bus_set_address_system_remote(sd_bus *b, const char *host); | |
396 | int bus_set_address_machine(sd_bus *b, RuntimeScope runtime_scope, const char *machine); | |
397 | ||
398 | int bus_maybe_reply_error(sd_bus_message *m, int r, const sd_bus_error *e); | |
399 | ||
400 | #define bus_assert_return(expr, r, error) \ | |
401 | do { \ | |
402 | if (!assert_log(expr, #expr)) \ | |
403 | return sd_bus_error_set_errno(error, r); \ | |
404 | } while (false) | |
405 | ||
406 | void bus_enter_closing(sd_bus *bus); | |
407 | ||
408 | void bus_set_state(sd_bus *bus, BusState state); |