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