]>
Commit | Line | Data |
---|---|---|
de1c301e LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <endian.h> | |
23 | #include <assert.h> | |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <netdb.h> | |
27 | #include <sys/poll.h> | |
28 | #include <byteswap.h> | |
63edf05e | 29 | #include <sys/mman.h> |
45fbe937 | 30 | #include <pthread.h> |
de1c301e LP |
31 | |
32 | #include "util.h" | |
33 | #include "macro.h" | |
2fd9ae2e | 34 | #include "strv.h" |
0a72c2bd | 35 | #include "set.h" |
6c03089c | 36 | #include "missing.h" |
ab9001a1 | 37 | #include "def.h" |
af08d2f9 | 38 | #include "cgroup-util.h" |
f01de965 | 39 | #include "bus-label.h" |
de1c301e LP |
40 | |
41 | #include "sd-bus.h" | |
42 | #include "bus-internal.h" | |
43 | #include "bus-message.h" | |
44 | #include "bus-type.h" | |
a7e3212d | 45 | #include "bus-socket.h" |
6629161f | 46 | #include "bus-kernel.h" |
392d5b37 | 47 | #include "bus-control.h" |
29ddb38f LP |
48 | #include "bus-introspect.h" |
49 | #include "bus-signature.h" | |
992c052c | 50 | #include "bus-objects.h" |
40ca29a1 | 51 | #include "bus-util.h" |
a7893c6b | 52 | #include "bus-container.h" |
0461f8cd | 53 | #include "bus-protocol.h" |
8f8f05a9 | 54 | #include "bus-track.h" |
de1c301e | 55 | |
e3017af9 | 56 | static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec); |
1e05d493 LP |
57 | static int attach_io_events(sd_bus *b); |
58 | static void detach_io_events(sd_bus *b); | |
e3017af9 | 59 | |
f54514f3 LP |
60 | static void bus_close_fds(sd_bus *b) { |
61 | assert(b); | |
62 | ||
1e05d493 LP |
63 | detach_io_events(b); |
64 | ||
f54514f3 | 65 | if (b->input_fd >= 0) |
03e334a1 | 66 | safe_close(b->input_fd); |
f54514f3 LP |
67 | |
68 | if (b->output_fd >= 0 && b->output_fd != b->input_fd) | |
03e334a1 | 69 | safe_close(b->output_fd); |
f54514f3 LP |
70 | |
71 | b->input_fd = b->output_fd = -1; | |
72 | } | |
73 | ||
29ddb38f LP |
74 | static void bus_node_destroy(sd_bus *b, struct node *n) { |
75 | struct node_callback *c; | |
76 | struct node_vtable *v; | |
77 | struct node_enumerator *e; | |
78 | ||
79 | assert(b); | |
80 | ||
81 | if (!n) | |
82 | return; | |
83 | ||
84 | while (n->child) | |
85 | bus_node_destroy(b, n->child); | |
86 | ||
87 | while ((c = n->callbacks)) { | |
71fda00f | 88 | LIST_REMOVE(callbacks, n->callbacks, c); |
29ddb38f LP |
89 | free(c); |
90 | } | |
91 | ||
92 | while ((v = n->vtables)) { | |
71fda00f | 93 | LIST_REMOVE(vtables, n->vtables, v); |
29ddb38f LP |
94 | free(v->interface); |
95 | free(v); | |
96 | } | |
97 | ||
98 | while ((e = n->enumerators)) { | |
71fda00f | 99 | LIST_REMOVE(enumerators, n->enumerators, e); |
29ddb38f LP |
100 | free(e); |
101 | } | |
102 | ||
103 | if (n->parent) | |
71fda00f | 104 | LIST_REMOVE(siblings, n->parent->child, n); |
29ddb38f LP |
105 | |
106 | assert_se(hashmap_remove(b->nodes, n->path) == n); | |
107 | free(n->path); | |
108 | free(n); | |
109 | } | |
110 | ||
0e586eae | 111 | static void bus_reset_queues(sd_bus *b) { |
0e586eae LP |
112 | assert(b); |
113 | ||
eb33a6f8 DH |
114 | /* NOTE: We _must_ decrement b->Xqueue_size before calling |
115 | * sd_bus_message_unref() for _each_ message. Otherwise the | |
116 | * self-reference checks in sd_bus_unref() will fire for each message. | |
117 | * We would thus recurse into sd_bus_message_unref() and trigger the | |
118 | * assert(m->n_ref > 0) */ | |
119 | ||
f389bf15 LP |
120 | while (b->rqueue_size > 0) |
121 | sd_bus_message_unref(b->rqueue[--b->rqueue_size]); | |
122 | ||
0e586eae | 123 | free(b->rqueue); |
f389bf15 LP |
124 | b->rqueue = NULL; |
125 | b->rqueue_allocated = 0; | |
0e586eae | 126 | |
f389bf15 LP |
127 | while (b->wqueue_size > 0) |
128 | sd_bus_message_unref(b->wqueue[--b->wqueue_size]); | |
0e586eae | 129 | |
f389bf15 LP |
130 | free(b->wqueue); |
131 | b->wqueue = NULL; | |
132 | b->wqueue_allocated = 0; | |
0e586eae LP |
133 | } |
134 | ||
de1c301e LP |
135 | static void bus_free(sd_bus *b) { |
136 | struct filter_callback *f; | |
29ddb38f | 137 | struct node *n; |
de1c301e LP |
138 | |
139 | assert(b); | |
140 | ||
8f8f05a9 LP |
141 | assert(!b->track_queue); |
142 | ||
40ca29a1 LP |
143 | sd_bus_detach_event(b); |
144 | ||
f4d140e9 LP |
145 | if (b->default_bus_ptr) |
146 | *b->default_bus_ptr = NULL; | |
147 | ||
f54514f3 | 148 | bus_close_fds(b); |
de1c301e | 149 | |
63edf05e LP |
150 | if (b->kdbus_buffer) |
151 | munmap(b->kdbus_buffer, KDBUS_POOL_SIZE); | |
152 | ||
de1c301e | 153 | free(b->rbuffer); |
89ffcd2a | 154 | free(b->unique_name); |
2181a7f5 | 155 | free(b->auth_buffer); |
89ffcd2a | 156 | free(b->address); |
e9a967f9 | 157 | free(b->kernel); |
a7893c6b | 158 | free(b->machine); |
8a0e0ed9 | 159 | free(b->fake_label); |
751bc6ac | 160 | free(b->cgroup_root); |
5972fe95 | 161 | free(b->connection_name); |
89ffcd2a | 162 | |
2fd9ae2e LP |
163 | free(b->exec_path); |
164 | strv_free(b->exec_argv); | |
165 | ||
2c93b4ef LP |
166 | close_many(b->fds, b->n_fds); |
167 | free(b->fds); | |
168 | ||
0e586eae | 169 | bus_reset_queues(b); |
de1c301e LP |
170 | |
171 | hashmap_free_free(b->reply_callbacks); | |
e3017af9 | 172 | prioq_free(b->reply_callbacks_prioq); |
de1c301e LP |
173 | |
174 | while ((f = b->filter_callbacks)) { | |
71fda00f | 175 | LIST_REMOVE(callbacks, b->filter_callbacks, f); |
de1c301e LP |
176 | free(f); |
177 | } | |
178 | ||
392d5b37 LP |
179 | bus_match_free(&b->match_callbacks); |
180 | ||
29ddb38f LP |
181 | hashmap_free_free(b->vtable_methods); |
182 | hashmap_free_free(b->vtable_properties); | |
183 | ||
184 | while ((n = hashmap_first(b->nodes))) | |
185 | bus_node_destroy(b, n); | |
186 | ||
187 | hashmap_free(b->nodes); | |
188 | ||
bc7fd8cd LP |
189 | bus_kernel_flush_memfd(b); |
190 | ||
45fbe937 LP |
191 | assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0); |
192 | ||
de1c301e LP |
193 | free(b); |
194 | } | |
195 | ||
d9f644e2 | 196 | _public_ int sd_bus_new(sd_bus **ret) { |
de1c301e LP |
197 | sd_bus *r; |
198 | ||
d6888822 | 199 | assert_return(ret, -EINVAL); |
021a1e78 | 200 | |
de1c301e LP |
201 | r = new0(sd_bus, 1); |
202 | if (!r) | |
021a1e78 | 203 | return -ENOMEM; |
de1c301e | 204 | |
e4ee6e5c | 205 | r->n_ref = REFCNT_INIT; |
e82c9509 | 206 | r->input_fd = r->output_fd = -1; |
de1c301e | 207 | r->message_version = 1; |
3310dfd5 | 208 | r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME; |
d21a7bb1 DM |
209 | r->hello_flags |= KDBUS_HELLO_ACCEPT_FD; |
210 | r->attach_flags |= KDBUS_ATTACH_NAMES; | |
d5a2b9a6 | 211 | r->original_pid = getpid(); |
de1c301e | 212 | |
45fbe937 LP |
213 | assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0); |
214 | ||
de1c301e LP |
215 | /* We guarantee that wqueue always has space for at least one |
216 | * entry */ | |
821e0756 | 217 | if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) { |
de1c301e | 218 | free(r); |
021a1e78 | 219 | return -ENOMEM; |
de1c301e LP |
220 | } |
221 | ||
021a1e78 LP |
222 | *ret = r; |
223 | return 0; | |
224 | } | |
225 | ||
d9f644e2 | 226 | _public_ int sd_bus_set_address(sd_bus *bus, const char *address) { |
021a1e78 LP |
227 | char *a; |
228 | ||
d6888822 LP |
229 | assert_return(bus, -EINVAL); |
230 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
231 | assert_return(address, -EINVAL); | |
232 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
021a1e78 LP |
233 | |
234 | a = strdup(address); | |
235 | if (!a) | |
236 | return -ENOMEM; | |
237 | ||
238 | free(bus->address); | |
239 | bus->address = a; | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
d9f644e2 | 244 | _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) { |
d6888822 LP |
245 | assert_return(bus, -EINVAL); |
246 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
247 | assert_return(input_fd >= 0, -EINVAL); | |
248 | assert_return(output_fd >= 0, -EINVAL); | |
249 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
021a1e78 | 250 | |
e82c9509 LP |
251 | bus->input_fd = input_fd; |
252 | bus->output_fd = output_fd; | |
021a1e78 LP |
253 | return 0; |
254 | } | |
255 | ||
d9f644e2 | 256 | _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) { |
2fd9ae2e LP |
257 | char *p, **a; |
258 | ||
d6888822 LP |
259 | assert_return(bus, -EINVAL); |
260 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
261 | assert_return(path, -EINVAL); | |
262 | assert_return(!strv_isempty(argv), -EINVAL); | |
263 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
2fd9ae2e LP |
264 | |
265 | p = strdup(path); | |
266 | if (!p) | |
267 | return -ENOMEM; | |
268 | ||
269 | a = strv_copy(argv); | |
270 | if (!a) { | |
271 | free(p); | |
272 | return -ENOMEM; | |
273 | } | |
274 | ||
275 | free(bus->exec_path); | |
276 | strv_free(bus->exec_argv); | |
277 | ||
278 | bus->exec_path = p; | |
279 | bus->exec_argv = a; | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
d9f644e2 | 284 | _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) { |
d6888822 LP |
285 | assert_return(bus, -EINVAL); |
286 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
287 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
021a1e78 | 288 | |
94bbf1ba | 289 | bus->bus_client = !!b; |
021a1e78 LP |
290 | return 0; |
291 | } | |
292 | ||
09365592 LP |
293 | _public_ int sd_bus_set_monitor(sd_bus *bus, int b) { |
294 | assert_return(bus, -EINVAL); | |
295 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
296 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
297 | ||
298 | SET_FLAG(bus->hello_flags, KDBUS_HELLO_MONITOR, b); | |
299 | return 0; | |
300 | } | |
301 | ||
d9f644e2 | 302 | _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) { |
d6888822 LP |
303 | assert_return(bus, -EINVAL); |
304 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
305 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
021a1e78 | 306 | |
264ad849 LP |
307 | SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b); |
308 | return 0; | |
309 | } | |
310 | ||
4fc31988 | 311 | _public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) { |
16be4368 KS |
312 | assert_return(bus, -EINVAL); |
313 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
314 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
315 | ||
d21a7bb1 | 316 | SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b); |
16be4368 KS |
317 | return 0; |
318 | } | |
319 | ||
4fc31988 | 320 | _public_ int sd_bus_negotiate_creds(sd_bus *bus, uint64_t mask) { |
16be4368 | 321 | assert_return(bus, -EINVAL); |
95c4fe82 | 322 | assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL); |
16be4368 KS |
323 | assert_return(bus->state == BUS_UNSET, -EPERM); |
324 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
325 | ||
49b832c5 | 326 | /* The well knowns we need unconditionally, so that matches can work */ |
3310dfd5 | 327 | bus->creds_mask = mask | SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME; |
49b832c5 | 328 | |
ae439c9f | 329 | return kdbus_translate_attach_flags(bus->creds_mask, &bus->attach_flags); |
021a1e78 | 330 | } |
de1c301e | 331 | |
d9f644e2 | 332 | _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) { |
d6888822 LP |
333 | assert_return(bus, -EINVAL); |
334 | assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL); | |
335 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
336 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
2181a7f5 LP |
337 | |
338 | bus->is_server = !!b; | |
98178d39 | 339 | bus->server_id = server_id; |
2181a7f5 LP |
340 | return 0; |
341 | } | |
342 | ||
d9f644e2 | 343 | _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) { |
d6888822 LP |
344 | assert_return(bus, -EINVAL); |
345 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
346 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
2181a7f5 LP |
347 | |
348 | bus->anonymous_auth = !!b; | |
349 | return 0; | |
350 | } | |
351 | ||
adacb957 LP |
352 | _public_ int sd_bus_set_trusted(sd_bus *bus, int b) { |
353 | assert_return(bus, -EINVAL); | |
354 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
355 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
356 | ||
357 | bus->trusted = !!b; | |
358 | return 0; | |
359 | } | |
360 | ||
5972fe95 LP |
361 | _public_ int sd_bus_set_name(sd_bus *bus, const char *name) { |
362 | char *n; | |
363 | ||
364 | assert_return(bus, -EINVAL); | |
365 | assert_return(name, -EINVAL); | |
366 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
367 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
368 | ||
369 | n = strdup(name); | |
370 | if (!n) | |
371 | return -ENOMEM; | |
372 | ||
373 | free(bus->connection_name); | |
374 | bus->connection_name = n; | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
ebcf1f97 | 379 | static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) { |
de1c301e LP |
380 | const char *s; |
381 | int r; | |
382 | ||
383 | assert(bus); | |
c99c09a2 | 384 | assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING); |
de1c301e LP |
385 | assert(reply); |
386 | ||
40ca29a1 | 387 | r = sd_bus_message_get_errno(reply); |
eb01ba5d LP |
388 | if (r < 0) |
389 | return r; | |
40ca29a1 LP |
390 | if (r > 0) |
391 | return -r; | |
eb01ba5d | 392 | |
de1c301e LP |
393 | r = sd_bus_message_read(reply, "s", &s); |
394 | if (r < 0) | |
395 | return r; | |
396 | ||
dafb7591 LP |
397 | if (!service_name_is_valid(s) || s[0] != ':') |
398 | return -EBADMSG; | |
399 | ||
de1c301e LP |
400 | bus->unique_name = strdup(s); |
401 | if (!bus->unique_name) | |
402 | return -ENOMEM; | |
403 | ||
c99c09a2 LP |
404 | if (bus->state == BUS_HELLO) |
405 | bus->state = BUS_RUNNING; | |
dafb7591 | 406 | |
de1c301e LP |
407 | return 1; |
408 | } | |
409 | ||
410 | static int bus_send_hello(sd_bus *bus) { | |
411 | _cleanup_bus_message_unref_ sd_bus_message *m = NULL; | |
412 | int r; | |
413 | ||
414 | assert(bus); | |
415 | ||
6629161f | 416 | if (!bus->bus_client || bus->is_kernel) |
021a1e78 LP |
417 | return 0; |
418 | ||
de1c301e LP |
419 | r = sd_bus_message_new_method_call( |
420 | bus, | |
151b9b96 | 421 | &m, |
de1c301e | 422 | "org.freedesktop.DBus", |
a7639e37 | 423 | "/org/freedesktop/DBus", |
de1c301e | 424 | "org.freedesktop.DBus", |
151b9b96 | 425 | "Hello"); |
de1c301e LP |
426 | if (r < 0) |
427 | return r; | |
428 | ||
693eb9a2 | 429 | return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_cookie); |
de1c301e LP |
430 | } |
431 | ||
a7e3212d | 432 | int bus_start_running(sd_bus *bus) { |
de1c301e LP |
433 | assert(bus); |
434 | ||
f08838da | 435 | if (bus->bus_client && !bus->is_kernel) { |
de1c301e | 436 | bus->state = BUS_HELLO; |
e3017af9 | 437 | return 1; |
de1c301e LP |
438 | } |
439 | ||
440 | bus->state = BUS_RUNNING; | |
e3017af9 | 441 | return 1; |
de1c301e LP |
442 | } |
443 | ||
444 | static int parse_address_key(const char **p, const char *key, char **value) { | |
821e0756 | 445 | size_t l, n = 0, allocated = 0; |
de1c301e LP |
446 | const char *a; |
447 | char *r = NULL; | |
448 | ||
449 | assert(p); | |
450 | assert(*p); | |
de1c301e LP |
451 | assert(value); |
452 | ||
2fd9ae2e LP |
453 | if (key) { |
454 | l = strlen(key); | |
455 | if (strncmp(*p, key, l) != 0) | |
456 | return 0; | |
de1c301e | 457 | |
2fd9ae2e LP |
458 | if ((*p)[l] != '=') |
459 | return 0; | |
de1c301e | 460 | |
2fd9ae2e LP |
461 | if (*value) |
462 | return -EINVAL; | |
de1c301e | 463 | |
2fd9ae2e LP |
464 | a = *p + l + 1; |
465 | } else | |
466 | a = *p; | |
467 | ||
468 | while (*a != ';' && *a != ',' && *a != 0) { | |
821e0756 | 469 | char c; |
de1c301e LP |
470 | |
471 | if (*a == '%') { | |
472 | int x, y; | |
473 | ||
474 | x = unhexchar(a[1]); | |
475 | if (x < 0) { | |
476 | free(r); | |
477 | return x; | |
478 | } | |
479 | ||
480 | y = unhexchar(a[2]); | |
481 | if (y < 0) { | |
482 | free(r); | |
483 | return y; | |
484 | } | |
485 | ||
de1c301e | 486 | c = (char) ((x << 4) | y); |
89ffcd2a LP |
487 | a += 3; |
488 | } else { | |
de1c301e | 489 | c = *a; |
89ffcd2a LP |
490 | a++; |
491 | } | |
de1c301e | 492 | |
821e0756 | 493 | if (!GREEDY_REALLOC(r, allocated, n + 2)) |
de1c301e | 494 | return -ENOMEM; |
de1c301e | 495 | |
de1c301e LP |
496 | r[n++] = c; |
497 | } | |
498 | ||
89ffcd2a LP |
499 | if (!r) { |
500 | r = strdup(""); | |
501 | if (!r) | |
502 | return -ENOMEM; | |
503 | } else | |
504 | r[n] = 0; | |
505 | ||
506 | if (*a == ',') | |
507 | a++; | |
508 | ||
de1c301e | 509 | *p = a; |
2fd9ae2e LP |
510 | |
511 | free(*value); | |
de1c301e | 512 | *value = r; |
2fd9ae2e | 513 | |
de1c301e LP |
514 | return 1; |
515 | } | |
516 | ||
517 | static void skip_address_key(const char **p) { | |
518 | assert(p); | |
519 | assert(*p); | |
520 | ||
89ffcd2a LP |
521 | *p += strcspn(*p, ","); |
522 | ||
523 | if (**p == ',') | |
524 | (*p) ++; | |
de1c301e LP |
525 | } |
526 | ||
2fd9ae2e LP |
527 | static int parse_unix_address(sd_bus *b, const char **p, char **guid) { |
528 | _cleanup_free_ char *path = NULL, *abstract = NULL; | |
529 | size_t l; | |
de1c301e LP |
530 | int r; |
531 | ||
532 | assert(b); | |
2fd9ae2e LP |
533 | assert(p); |
534 | assert(*p); | |
535 | assert(guid); | |
de1c301e | 536 | |
2fd9ae2e LP |
537 | while (**p != 0 && **p != ';') { |
538 | r = parse_address_key(p, "guid", guid); | |
539 | if (r < 0) | |
540 | return r; | |
541 | else if (r > 0) | |
542 | continue; | |
de1c301e | 543 | |
2fd9ae2e LP |
544 | r = parse_address_key(p, "path", &path); |
545 | if (r < 0) | |
546 | return r; | |
547 | else if (r > 0) | |
548 | continue; | |
de1c301e | 549 | |
2fd9ae2e LP |
550 | r = parse_address_key(p, "abstract", &abstract); |
551 | if (r < 0) | |
552 | return r; | |
553 | else if (r > 0) | |
554 | continue; | |
de1c301e | 555 | |
2fd9ae2e LP |
556 | skip_address_key(p); |
557 | } | |
de1c301e | 558 | |
2fd9ae2e LP |
559 | if (!path && !abstract) |
560 | return -EINVAL; | |
de1c301e | 561 | |
2fd9ae2e LP |
562 | if (path && abstract) |
563 | return -EINVAL; | |
564 | ||
565 | if (path) { | |
566 | l = strlen(path); | |
567 | if (l > sizeof(b->sockaddr.un.sun_path)) | |
568 | return -E2BIG; | |
de1c301e | 569 | |
2fd9ae2e LP |
570 | b->sockaddr.un.sun_family = AF_UNIX; |
571 | strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path)); | |
572 | b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l; | |
573 | } else if (abstract) { | |
574 | l = strlen(abstract); | |
575 | if (l > sizeof(b->sockaddr.un.sun_path) - 1) | |
576 | return -E2BIG; | |
577 | ||
578 | b->sockaddr.un.sun_family = AF_UNIX; | |
579 | b->sockaddr.un.sun_path[0] = 0; | |
580 | strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1); | |
581 | b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l; | |
582 | } | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | static int parse_tcp_address(sd_bus *b, const char **p, char **guid) { | |
588 | _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL; | |
2fd9ae2e | 589 | int r; |
b92bea5d ZJS |
590 | struct addrinfo *result, hints = { |
591 | .ai_socktype = SOCK_STREAM, | |
592 | .ai_flags = AI_ADDRCONFIG, | |
593 | }; | |
2fd9ae2e LP |
594 | |
595 | assert(b); | |
596 | assert(p); | |
597 | assert(*p); | |
598 | assert(guid); | |
599 | ||
600 | while (**p != 0 && **p != ';') { | |
601 | r = parse_address_key(p, "guid", guid); | |
602 | if (r < 0) | |
603 | return r; | |
604 | else if (r > 0) | |
605 | continue; | |
606 | ||
607 | r = parse_address_key(p, "host", &host); | |
608 | if (r < 0) | |
609 | return r; | |
610 | else if (r > 0) | |
611 | continue; | |
612 | ||
613 | r = parse_address_key(p, "port", &port); | |
614 | if (r < 0) | |
615 | return r; | |
616 | else if (r > 0) | |
617 | continue; | |
618 | ||
619 | r = parse_address_key(p, "family", &family); | |
620 | if (r < 0) | |
621 | return r; | |
622 | else if (r > 0) | |
623 | continue; | |
624 | ||
625 | skip_address_key(p); | |
626 | } | |
627 | ||
628 | if (!host || !port) | |
629 | return -EINVAL; | |
630 | ||
2fd9ae2e LP |
631 | if (family) { |
632 | if (streq(family, "ipv4")) | |
633 | hints.ai_family = AF_INET; | |
634 | else if (streq(family, "ipv6")) | |
635 | hints.ai_family = AF_INET6; | |
636 | else | |
637 | return -EINVAL; | |
638 | } | |
639 | ||
640 | r = getaddrinfo(host, port, &hints, &result); | |
641 | if (r == EAI_SYSTEM) | |
642 | return -errno; | |
643 | else if (r != 0) | |
644 | return -EADDRNOTAVAIL; | |
645 | ||
646 | memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen); | |
647 | b->sockaddr_size = result->ai_addrlen; | |
648 | ||
649 | freeaddrinfo(result); | |
650 | ||
651 | return 0; | |
652 | } | |
653 | ||
654 | static int parse_exec_address(sd_bus *b, const char **p, char **guid) { | |
655 | char *path = NULL; | |
656 | unsigned n_argv = 0, j; | |
657 | char **argv = NULL; | |
821e0756 | 658 | size_t allocated = 0; |
2fd9ae2e LP |
659 | int r; |
660 | ||
661 | assert(b); | |
662 | assert(p); | |
663 | assert(*p); | |
664 | assert(guid); | |
665 | ||
666 | while (**p != 0 && **p != ';') { | |
667 | r = parse_address_key(p, "guid", guid); | |
668 | if (r < 0) | |
669 | goto fail; | |
670 | else if (r > 0) | |
671 | continue; | |
672 | ||
673 | r = parse_address_key(p, "path", &path); | |
674 | if (r < 0) | |
675 | goto fail; | |
676 | else if (r > 0) | |
677 | continue; | |
678 | ||
679 | if (startswith(*p, "argv")) { | |
680 | unsigned ul; | |
681 | ||
682 | errno = 0; | |
683 | ul = strtoul(*p + 4, (char**) p, 10); | |
8333c77e | 684 | if (errno > 0 || **p != '=' || ul > 256) { |
2fd9ae2e LP |
685 | r = -EINVAL; |
686 | goto fail; | |
687 | } | |
688 | ||
689 | (*p) ++; | |
690 | ||
691 | if (ul >= n_argv) { | |
821e0756 | 692 | if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) { |
2fd9ae2e LP |
693 | r = -ENOMEM; |
694 | goto fail; | |
695 | } | |
696 | ||
2fd9ae2e LP |
697 | n_argv = ul + 1; |
698 | } | |
699 | ||
700 | r = parse_address_key(p, NULL, argv + ul); | |
de1c301e | 701 | if (r < 0) |
2fd9ae2e | 702 | goto fail; |
de1c301e | 703 | |
2fd9ae2e | 704 | continue; |
de1c301e LP |
705 | } |
706 | ||
2fd9ae2e LP |
707 | skip_address_key(p); |
708 | } | |
de1c301e | 709 | |
5a0f6033 LP |
710 | if (!path) { |
711 | r = -EINVAL; | |
2fd9ae2e | 712 | goto fail; |
5a0f6033 | 713 | } |
de1c301e | 714 | |
2fd9ae2e LP |
715 | /* Make sure there are no holes in the array, with the |
716 | * exception of argv[0] */ | |
717 | for (j = 1; j < n_argv; j++) | |
718 | if (!argv[j]) { | |
719 | r = -EINVAL; | |
720 | goto fail; | |
721 | } | |
722 | ||
723 | if (argv && argv[0] == NULL) { | |
724 | argv[0] = strdup(path); | |
725 | if (!argv[0]) { | |
726 | r = -ENOMEM; | |
727 | goto fail; | |
728 | } | |
729 | } | |
de1c301e | 730 | |
2fd9ae2e LP |
731 | b->exec_path = path; |
732 | b->exec_argv = argv; | |
733 | return 0; | |
de1c301e | 734 | |
2fd9ae2e LP |
735 | fail: |
736 | for (j = 0; j < n_argv; j++) | |
737 | free(argv[j]); | |
738 | ||
739 | free(argv); | |
740 | free(path); | |
741 | return r; | |
742 | } | |
743 | ||
6629161f LP |
744 | static int parse_kernel_address(sd_bus *b, const char **p, char **guid) { |
745 | _cleanup_free_ char *path = NULL; | |
746 | int r; | |
747 | ||
748 | assert(b); | |
749 | assert(p); | |
750 | assert(*p); | |
751 | assert(guid); | |
752 | ||
753 | while (**p != 0 && **p != ';') { | |
754 | r = parse_address_key(p, "guid", guid); | |
755 | if (r < 0) | |
756 | return r; | |
757 | else if (r > 0) | |
758 | continue; | |
759 | ||
760 | r = parse_address_key(p, "path", &path); | |
761 | if (r < 0) | |
762 | return r; | |
763 | else if (r > 0) | |
764 | continue; | |
765 | ||
766 | skip_address_key(p); | |
767 | } | |
768 | ||
769 | if (!path) | |
770 | return -EINVAL; | |
771 | ||
772 | free(b->kernel); | |
773 | b->kernel = path; | |
774 | path = NULL; | |
775 | ||
776 | return 0; | |
777 | } | |
778 | ||
bc9fd78c | 779 | static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) { |
a7893c6b LP |
780 | _cleanup_free_ char *machine = NULL; |
781 | int r; | |
782 | ||
783 | assert(b); | |
784 | assert(p); | |
785 | assert(*p); | |
786 | assert(guid); | |
787 | ||
788 | while (**p != 0 && **p != ';') { | |
789 | r = parse_address_key(p, "guid", guid); | |
790 | if (r < 0) | |
791 | return r; | |
792 | else if (r > 0) | |
793 | continue; | |
794 | ||
795 | r = parse_address_key(p, "machine", &machine); | |
796 | if (r < 0) | |
797 | return r; | |
798 | else if (r > 0) | |
799 | continue; | |
800 | ||
801 | skip_address_key(p); | |
802 | } | |
803 | ||
804 | if (!machine) | |
805 | return -EINVAL; | |
806 | ||
b6741478 LP |
807 | if (!filename_is_safe(machine)) |
808 | return -EINVAL; | |
809 | ||
a7893c6b LP |
810 | free(b->machine); |
811 | b->machine = machine; | |
812 | machine = NULL; | |
813 | ||
814 | b->sockaddr.un.sun_family = AF_UNIX; | |
df1e0204 LP |
815 | strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path)); |
816 | b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen("/var/run/dbus/system_bus_socket"); | |
a7893c6b LP |
817 | |
818 | return 0; | |
819 | } | |
820 | ||
bc9fd78c LP |
821 | static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid) { |
822 | _cleanup_free_ char *machine = NULL; | |
823 | int r; | |
824 | ||
825 | assert(b); | |
826 | assert(p); | |
827 | assert(*p); | |
828 | assert(guid); | |
829 | ||
830 | while (**p != 0 && **p != ';') { | |
831 | r = parse_address_key(p, "guid", guid); | |
832 | if (r < 0) | |
833 | return r; | |
834 | else if (r > 0) | |
835 | continue; | |
836 | ||
837 | r = parse_address_key(p, "machine", &machine); | |
838 | if (r < 0) | |
839 | return r; | |
840 | else if (r > 0) | |
841 | continue; | |
842 | ||
843 | skip_address_key(p); | |
844 | } | |
845 | ||
846 | if (!machine) | |
847 | return -EINVAL; | |
848 | ||
849 | if (!filename_is_safe(machine)) | |
850 | return -EINVAL; | |
851 | ||
852 | free(b->machine); | |
853 | b->machine = machine; | |
854 | machine = NULL; | |
855 | ||
856 | free(b->kernel); | |
857 | b->kernel = strdup("/dev/kdbus/0-system/bus"); | |
858 | if (!b->kernel) | |
859 | return -ENOMEM; | |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
2fd9ae2e LP |
864 | static void bus_reset_parsed_address(sd_bus *b) { |
865 | assert(b); | |
866 | ||
867 | zero(b->sockaddr); | |
868 | b->sockaddr_size = 0; | |
869 | strv_free(b->exec_argv); | |
870 | free(b->exec_path); | |
871 | b->exec_path = NULL; | |
872 | b->exec_argv = NULL; | |
98178d39 | 873 | b->server_id = SD_ID128_NULL; |
6629161f LP |
874 | free(b->kernel); |
875 | b->kernel = NULL; | |
a7893c6b LP |
876 | free(b->machine); |
877 | b->machine = NULL; | |
2fd9ae2e LP |
878 | } |
879 | ||
880 | static int bus_parse_next_address(sd_bus *b) { | |
881 | _cleanup_free_ char *guid = NULL; | |
882 | const char *a; | |
883 | int r; | |
884 | ||
885 | assert(b); | |
886 | ||
887 | if (!b->address) | |
888 | return 0; | |
889 | if (b->address[b->address_index] == 0) | |
890 | return 0; | |
891 | ||
892 | bus_reset_parsed_address(b); | |
893 | ||
894 | a = b->address + b->address_index; | |
de1c301e | 895 | |
2fd9ae2e | 896 | while (*a != 0) { |
de1c301e | 897 | |
2fd9ae2e LP |
898 | if (*a == ';') { |
899 | a++; | |
900 | continue; | |
de1c301e LP |
901 | } |
902 | ||
2fd9ae2e LP |
903 | if (startswith(a, "unix:")) { |
904 | a += 5; | |
de1c301e | 905 | |
2fd9ae2e | 906 | r = parse_unix_address(b, &a, &guid); |
de1c301e LP |
907 | if (r < 0) |
908 | return r; | |
2fd9ae2e | 909 | break; |
de1c301e | 910 | |
2fd9ae2e | 911 | } else if (startswith(a, "tcp:")) { |
de1c301e | 912 | |
2fd9ae2e LP |
913 | a += 4; |
914 | r = parse_tcp_address(b, &a, &guid); | |
de1c301e LP |
915 | if (r < 0) |
916 | return r; | |
de1c301e | 917 | |
2fd9ae2e LP |
918 | break; |
919 | ||
920 | } else if (startswith(a, "unixexec:")) { | |
921 | ||
922 | a += 9; | |
923 | r = parse_exec_address(b, &a, &guid); | |
de1c301e LP |
924 | if (r < 0) |
925 | return r; | |
de1c301e | 926 | |
2fd9ae2e | 927 | break; |
de1c301e | 928 | |
6629161f LP |
929 | } else if (startswith(a, "kernel:")) { |
930 | ||
931 | a += 7; | |
932 | r = parse_kernel_address(b, &a, &guid); | |
933 | if (r < 0) | |
934 | return r; | |
935 | ||
a7893c6b | 936 | break; |
bc9fd78c LP |
937 | } else if (startswith(a, "x-container-unix:")) { |
938 | ||
939 | a += 17; | |
940 | r = parse_container_unix_address(b, &a, &guid); | |
941 | if (r < 0) | |
942 | return r; | |
943 | ||
944 | break; | |
945 | } else if (startswith(a, "x-container-kernel:")) { | |
a7893c6b | 946 | |
bc9fd78c LP |
947 | a += 19; |
948 | r = parse_container_kernel_address(b, &a, &guid); | |
a7893c6b LP |
949 | if (r < 0) |
950 | return r; | |
951 | ||
6629161f | 952 | break; |
de1c301e LP |
953 | } |
954 | ||
2fd9ae2e LP |
955 | a = strchr(a, ';'); |
956 | if (!a) | |
957 | return 0; | |
de1c301e LP |
958 | } |
959 | ||
960 | if (guid) { | |
98178d39 | 961 | r = sd_id128_from_string(guid, &b->server_id); |
de1c301e LP |
962 | if (r < 0) |
963 | return r; | |
964 | } | |
965 | ||
2fd9ae2e | 966 | b->address_index = a - b->address; |
de1c301e LP |
967 | return 1; |
968 | } | |
969 | ||
a7e3212d | 970 | static int bus_start_address(sd_bus *b) { |
2fd9ae2e LP |
971 | int r; |
972 | ||
973 | assert(b); | |
974 | ||
975 | for (;;) { | |
1e05d493 | 976 | bool skipped = false; |
2fd9ae2e | 977 | |
1e05d493 | 978 | bus_close_fds(b); |
a7e3212d | 979 | |
1e05d493 | 980 | if (b->exec_path) |
a7893c6b | 981 | r = bus_socket_exec(b); |
bc9fd78c LP |
982 | else if (b->machine && b->kernel) |
983 | r = bus_container_connect_kernel(b); | |
984 | else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC) | |
985 | r = bus_container_connect_socket(b); | |
1e05d493 | 986 | else if (b->kernel) |
a7893c6b | 987 | r = bus_kernel_connect(b); |
1e05d493 | 988 | else if (b->sockaddr.sa.sa_family != AF_UNSPEC) |
a7893c6b | 989 | r = bus_socket_connect(b); |
1e05d493 LP |
990 | else |
991 | skipped = true; | |
992 | ||
993 | if (!skipped) { | |
994 | if (r >= 0) { | |
995 | r = attach_io_events(b); | |
996 | if (r >= 0) | |
997 | return r; | |
998 | } | |
6629161f | 999 | |
2fd9ae2e LP |
1000 | b->last_connect_error = -r; |
1001 | } | |
1002 | ||
1003 | r = bus_parse_next_address(b); | |
1004 | if (r < 0) | |
1005 | return r; | |
1006 | if (r == 0) | |
1007 | return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED; | |
de1c301e LP |
1008 | } |
1009 | } | |
1010 | ||
a7e3212d LP |
1011 | int bus_next_address(sd_bus *b) { |
1012 | assert(b); | |
1013 | ||
1014 | bus_reset_parsed_address(b); | |
1015 | return bus_start_address(b); | |
1016 | } | |
1017 | ||
021a1e78 | 1018 | static int bus_start_fd(sd_bus *b) { |
6629161f | 1019 | struct stat st; |
021a1e78 LP |
1020 | int r; |
1021 | ||
1022 | assert(b); | |
e82c9509 LP |
1023 | assert(b->input_fd >= 0); |
1024 | assert(b->output_fd >= 0); | |
021a1e78 | 1025 | |
e82c9509 | 1026 | r = fd_nonblock(b->input_fd, true); |
021a1e78 LP |
1027 | if (r < 0) |
1028 | return r; | |
1029 | ||
e82c9509 | 1030 | r = fd_cloexec(b->input_fd, true); |
021a1e78 LP |
1031 | if (r < 0) |
1032 | return r; | |
1033 | ||
e82c9509 LP |
1034 | if (b->input_fd != b->output_fd) { |
1035 | r = fd_nonblock(b->output_fd, true); | |
1036 | if (r < 0) | |
1037 | return r; | |
1038 | ||
1039 | r = fd_cloexec(b->output_fd, true); | |
1040 | if (r < 0) | |
1041 | return r; | |
1042 | } | |
1043 | ||
6629161f LP |
1044 | if (fstat(b->input_fd, &st) < 0) |
1045 | return -errno; | |
1046 | ||
1047 | if (S_ISCHR(b->input_fd)) | |
1048 | return bus_kernel_take_fd(b); | |
1049 | else | |
1050 | return bus_socket_take_fd(b); | |
021a1e78 LP |
1051 | } |
1052 | ||
d9f644e2 | 1053 | _public_ int sd_bus_start(sd_bus *bus) { |
021a1e78 LP |
1054 | int r; |
1055 | ||
d6888822 LP |
1056 | assert_return(bus, -EINVAL); |
1057 | assert_return(bus->state == BUS_UNSET, -EPERM); | |
1058 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
021a1e78 LP |
1059 | |
1060 | bus->state = BUS_OPENING; | |
1061 | ||
2181a7f5 LP |
1062 | if (bus->is_server && bus->bus_client) |
1063 | return -EINVAL; | |
1064 | ||
e82c9509 | 1065 | if (bus->input_fd >= 0) |
021a1e78 | 1066 | r = bus_start_fd(bus); |
a7893c6b | 1067 | else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine) |
a7e3212d | 1068 | r = bus_start_address(bus); |
021a1e78 LP |
1069 | else |
1070 | return -EINVAL; | |
1071 | ||
1072 | if (r < 0) | |
1073 | return r; | |
1074 | ||
1075 | return bus_send_hello(bus); | |
1076 | } | |
1077 | ||
af08d2f9 LP |
1078 | _public_ int sd_bus_open(sd_bus **ret) { |
1079 | const char *e; | |
1080 | sd_bus *b; | |
1081 | int r; | |
1082 | ||
1083 | assert_return(ret, -EINVAL); | |
1084 | ||
1085 | /* Let's connect to the starter bus if it is set, and | |
1086 | * otherwise to the bus that is appropropriate for the scope | |
1087 | * we are running in */ | |
1088 | ||
1089 | e = secure_getenv("DBUS_STARTER_BUS_TYPE"); | |
1090 | if (e) { | |
1091 | if (streq(e, "system")) | |
1092 | return sd_bus_open_system(ret); | |
09365592 | 1093 | else if (STR_IN_SET(e, "session", "user")) |
af08d2f9 LP |
1094 | return sd_bus_open_user(ret); |
1095 | } | |
1096 | ||
1097 | e = secure_getenv("DBUS_STARTER_ADDRESS"); | |
1098 | if (!e) { | |
1099 | if (cg_pid_get_owner_uid(0, NULL) >= 0) | |
1100 | return sd_bus_open_user(ret); | |
1101 | else | |
1102 | return sd_bus_open_system(ret); | |
1103 | } | |
1104 | ||
1105 | r = sd_bus_new(&b); | |
1106 | if (r < 0) | |
1107 | return r; | |
1108 | ||
1109 | r = sd_bus_set_address(b, e); | |
1110 | if (r < 0) | |
1111 | goto fail; | |
1112 | ||
1113 | b->bus_client = true; | |
1114 | ||
1115 | /* We don't know whether the bus is trusted or not, so better | |
1116 | * be safe, and authenticate everything */ | |
1117 | b->trusted = false; | |
1118 | b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS; | |
1119 | ||
1120 | r = sd_bus_start(b); | |
1121 | if (r < 0) | |
1122 | goto fail; | |
1123 | ||
1124 | *ret = b; | |
1125 | return 0; | |
1126 | ||
1127 | fail: | |
1128 | bus_free(b); | |
1129 | return r; | |
1130 | } | |
1131 | ||
09365592 | 1132 | int bus_set_address_system(sd_bus *b) { |
de1c301e | 1133 | const char *e; |
09365592 LP |
1134 | assert(b); |
1135 | ||
1136 | e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); | |
1137 | if (e) | |
1138 | return sd_bus_set_address(b, e); | |
1139 | ||
1140 | return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_PATH); | |
1141 | } | |
1142 | ||
1143 | _public_ int sd_bus_open_system(sd_bus **ret) { | |
de1c301e LP |
1144 | sd_bus *b; |
1145 | int r; | |
1146 | ||
d6888822 | 1147 | assert_return(ret, -EINVAL); |
de1c301e | 1148 | |
021a1e78 LP |
1149 | r = sd_bus_new(&b); |
1150 | if (r < 0) | |
1151 | return r; | |
1152 | ||
09365592 | 1153 | r = bus_set_address_system(b); |
e3dd987c LP |
1154 | if (r < 0) |
1155 | goto fail; | |
de1c301e | 1156 | |
94bbf1ba | 1157 | b->bus_client = true; |
5972fe95 | 1158 | b->is_system = true; |
021a1e78 | 1159 | |
adacb957 LP |
1160 | /* Let's do per-method access control on the system bus. We |
1161 | * need the caller's UID and capability set for that. */ | |
1162 | b->trusted = false; | |
1163 | b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS; | |
1164 | ||
021a1e78 LP |
1165 | r = sd_bus_start(b); |
1166 | if (r < 0) | |
1167 | goto fail; | |
de1c301e LP |
1168 | |
1169 | *ret = b; | |
1170 | return 0; | |
021a1e78 LP |
1171 | |
1172 | fail: | |
1173 | bus_free(b); | |
1174 | return r; | |
de1c301e LP |
1175 | } |
1176 | ||
09365592 | 1177 | int bus_set_address_user(sd_bus *b) { |
de1c301e | 1178 | const char *e; |
de1c301e | 1179 | |
09365592 | 1180 | assert(b); |
021a1e78 | 1181 | |
6c03089c | 1182 | e = secure_getenv("DBUS_SESSION_BUS_ADDRESS"); |
09365592 LP |
1183 | if (e) |
1184 | return sd_bus_set_address(b, e); | |
1185 | ||
1186 | e = secure_getenv("XDG_RUNTIME_DIR"); | |
de1c301e | 1187 | if (e) { |
09365592 | 1188 | _cleanup_free_ char *ee = NULL; |
e3dd987c | 1189 | |
09365592 LP |
1190 | ee = bus_address_escape(e); |
1191 | if (!ee) | |
1192 | return -ENOMEM; | |
e3dd987c | 1193 | |
626851be | 1194 | #ifdef ENABLE_KDBUS |
09365592 | 1195 | asprintf(&b->address, KERNEL_USER_BUS_FMT ";" UNIX_USER_BUS_FMT, (unsigned long) getuid(), ee); |
626851be | 1196 | #else |
09365592 | 1197 | asprintf(&b->address, UNIX_USER_BUS_FMT, ee); |
626851be | 1198 | #endif |
09365592 | 1199 | } else { |
626851be | 1200 | #ifdef ENABLE_KDBUS |
09365592 | 1201 | asprintf(&b->address, KERNEL_USER_BUS_FMT, (unsigned long) getuid()); |
626851be | 1202 | #else |
09365592 | 1203 | return -ECONNREFUSED; |
626851be | 1204 | #endif |
de1c301e LP |
1205 | } |
1206 | ||
09365592 LP |
1207 | if (!b->address) |
1208 | return -ENOMEM; | |
1209 | ||
1210 | return 0; | |
1211 | } | |
1212 | ||
1213 | _public_ int sd_bus_open_user(sd_bus **ret) { | |
1214 | sd_bus *b; | |
1215 | int r; | |
1216 | ||
1217 | assert_return(ret, -EINVAL); | |
1218 | ||
1219 | r = sd_bus_new(&b); | |
1220 | if (r < 0) | |
1221 | return r; | |
1222 | ||
1223 | r = bus_set_address_user(b); | |
1224 | if (r < 0) | |
1225 | return r; | |
1226 | ||
94bbf1ba | 1227 | b->bus_client = true; |
5972fe95 | 1228 | b->is_user = true; |
de1c301e | 1229 | |
adacb957 LP |
1230 | /* We don't do any per-method access control on the user |
1231 | * bus. */ | |
1232 | b->trusted = true; | |
1233 | ||
021a1e78 | 1234 | r = sd_bus_start(b); |
2571ead1 LP |
1235 | if (r < 0) |
1236 | goto fail; | |
de1c301e LP |
1237 | |
1238 | *ret = b; | |
1239 | return 0; | |
2571ead1 LP |
1240 | |
1241 | fail: | |
021a1e78 | 1242 | bus_free(b); |
2571ead1 | 1243 | return r; |
de1c301e LP |
1244 | } |
1245 | ||
09365592 | 1246 | int bus_set_address_system_remote(sd_bus *b, const char *host) { |
0f8bd8de | 1247 | _cleanup_free_ char *e = NULL; |
0f8bd8de | 1248 | |
09365592 LP |
1249 | assert(b); |
1250 | assert(host); | |
0f8bd8de LP |
1251 | |
1252 | e = bus_address_escape(host); | |
1253 | if (!e) | |
1254 | return -ENOMEM; | |
1255 | ||
09365592 LP |
1256 | b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL); |
1257 | if (!b->address) | |
0f8bd8de | 1258 | return -ENOMEM; |
a7893c6b | 1259 | |
09365592 LP |
1260 | return 0; |
1261 | } | |
1262 | ||
1263 | _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) { | |
1264 | sd_bus *bus; | |
1265 | int r; | |
1266 | ||
1267 | assert_return(host, -EINVAL); | |
1268 | assert_return(ret, -EINVAL); | |
1269 | ||
a7893c6b | 1270 | r = sd_bus_new(&bus); |
09365592 | 1271 | if (r < 0) |
a7893c6b | 1272 | return r; |
a7893c6b | 1273 | |
09365592 LP |
1274 | r = bus_set_address_system_remote(bus, host); |
1275 | if (r < 0) | |
1276 | goto fail; | |
1277 | ||
a7893c6b | 1278 | bus->bus_client = true; |
09365592 | 1279 | bus->trusted = false; |
a7893c6b LP |
1280 | |
1281 | r = sd_bus_start(bus); | |
09365592 LP |
1282 | if (r < 0) |
1283 | goto fail; | |
a7893c6b LP |
1284 | |
1285 | *ret = bus; | |
1286 | return 0; | |
09365592 LP |
1287 | |
1288 | fail: | |
1289 | bus_free(bus); | |
1290 | return r; | |
a7893c6b LP |
1291 | } |
1292 | ||
09365592 | 1293 | int bus_set_address_system_container(sd_bus *b, const char *machine) { |
a7893c6b | 1294 | _cleanup_free_ char *e = NULL; |
a7893c6b | 1295 | |
09365592 LP |
1296 | assert(b); |
1297 | assert(machine); | |
a7893c6b LP |
1298 | |
1299 | e = bus_address_escape(machine); | |
1300 | if (!e) | |
1301 | return -ENOMEM; | |
1302 | ||
9e554864 | 1303 | #ifdef ENABLE_KDBUS |
09365592 | 1304 | b->address = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL); |
9e554864 | 1305 | #else |
09365592 | 1306 | b->address = strjoin("x-container-unix:machine=", e, NULL); |
9e554864 | 1307 | #endif |
09365592 | 1308 | if (!b->address) |
a7893c6b | 1309 | return -ENOMEM; |
0f8bd8de | 1310 | |
09365592 LP |
1311 | return 0; |
1312 | } | |
1313 | ||
1314 | _public_ int sd_bus_open_system_container(sd_bus **ret, const char *machine) { | |
1315 | sd_bus *bus; | |
1316 | int r; | |
1317 | ||
1318 | assert_return(machine, -EINVAL); | |
1319 | assert_return(ret, -EINVAL); | |
1320 | assert_return(filename_is_safe(machine), -EINVAL); | |
1321 | ||
0f8bd8de | 1322 | r = sd_bus_new(&bus); |
09365592 | 1323 | if (r < 0) |
0f8bd8de | 1324 | return r; |
0f8bd8de | 1325 | |
09365592 LP |
1326 | r = bus_set_address_system_container(bus, machine); |
1327 | if (r < 0) | |
1328 | goto fail; | |
1329 | ||
0f8bd8de | 1330 | bus->bus_client = true; |
09365592 | 1331 | bus->trusted = false; |
0f8bd8de LP |
1332 | |
1333 | r = sd_bus_start(bus); | |
09365592 LP |
1334 | if (r < 0) |
1335 | goto fail; | |
0f8bd8de LP |
1336 | |
1337 | *ret = bus; | |
1338 | return 0; | |
09365592 LP |
1339 | |
1340 | fail: | |
1341 | bus_free(bus); | |
1342 | return r; | |
0f8bd8de LP |
1343 | } |
1344 | ||
d9f644e2 | 1345 | _public_ void sd_bus_close(sd_bus *bus) { |
0e586eae | 1346 | |
de1c301e LP |
1347 | if (!bus) |
1348 | return; | |
d5a2b9a6 LP |
1349 | if (bus->state == BUS_CLOSED) |
1350 | return; | |
1351 | if (bus_pid_changed(bus)) | |
f54514f3 LP |
1352 | return; |
1353 | ||
1354 | bus->state = BUS_CLOSED; | |
e82c9509 | 1355 | |
40ca29a1 LP |
1356 | sd_bus_detach_event(bus); |
1357 | ||
0e586eae LP |
1358 | /* Drop all queued messages so that they drop references to |
1359 | * the bus object and the bus may be freed */ | |
1360 | bus_reset_queues(bus); | |
1361 | ||
f54514f3 LP |
1362 | if (!bus->is_kernel) |
1363 | bus_close_fds(bus); | |
1364 | ||
1365 | /* We'll leave the fd open in case this is a kernel bus, since | |
1366 | * there might still be memblocks around that reference this | |
cd6d5e1c | 1367 | * bus, and they might need to invoke the KDBUS_CMD_FREE |
1d0e3c98 | 1368 | * ioctl on the fd when they are freed. */ |
de1c301e LP |
1369 | } |
1370 | ||
718db961 LP |
1371 | static void bus_enter_closing(sd_bus *bus) { |
1372 | assert(bus); | |
1373 | ||
1374 | if (bus->state != BUS_OPENING && | |
1375 | bus->state != BUS_AUTHENTICATING && | |
1376 | bus->state != BUS_HELLO && | |
1377 | bus->state != BUS_RUNNING) | |
1378 | return; | |
1379 | ||
1380 | bus->state = BUS_CLOSING; | |
1381 | } | |
1382 | ||
d9f644e2 | 1383 | _public_ sd_bus *sd_bus_ref(sd_bus *bus) { |
9d6c7c82 | 1384 | assert_return(bus, NULL); |
de1c301e | 1385 | |
e4ee6e5c | 1386 | assert_se(REFCNT_INC(bus->n_ref) >= 2); |
de1c301e | 1387 | |
de1c301e LP |
1388 | return bus; |
1389 | } | |
1390 | ||
d9f644e2 | 1391 | _public_ sd_bus *sd_bus_unref(sd_bus *bus) { |
b7fc42e0 | 1392 | unsigned i; |
5b1bc83f LP |
1393 | |
1394 | if (!bus) | |
1395 | return NULL; | |
de1c301e | 1396 | |
374c3569 DH |
1397 | /* TODO/FIXME: It's naive to think REFCNT_GET() is thread-safe in any |
1398 | * way but exclusive REFCNT_DEC(). The current logic _must_ lock around | |
1399 | * REFCNT_GET() until REFCNT_DEC() or two threads might end up in | |
1400 | * parallel in bus_reset_queues(). But locking would totally break the | |
1401 | * recursion we introduce by bus_reset_queues()... | |
1402 | * (Imagine one thread in sd_bus_message_unref() setting n_ref to 0 and | |
1403 | * thus calling into sd_bus_unref(). If at the same time the real | |
1404 | * thread calls sd_bus_unref(), both end up with "q == true" and will | |
1405 | * call into bus_reset_queues(). | |
1406 | * If we require the main bus to be alive until all dispatch threads | |
1407 | * are done, there is no need to do ref-counts at all. So in both ways, | |
1408 | * the REFCNT thing is humbug.) | |
1409 | * | |
1410 | * On a second note: messages are *not* required to have ->bus set nor | |
1411 | * does it have to be _this_ bus that they're assigned to. This whole | |
1412 | * ref-cnt checking breaks apart if a message is not assigned to us. | |
1413 | * (which is _very_ easy to trigger with the current API). */ | |
1414 | ||
f389bf15 LP |
1415 | if (REFCNT_GET(bus->n_ref) == bus->rqueue_size + bus->wqueue_size + 1) { |
1416 | bool q = true; | |
b7fc42e0 | 1417 | |
f389bf15 LP |
1418 | for (i = 0; i < bus->rqueue_size; i++) |
1419 | if (bus->rqueue[i]->n_ref > 1) { | |
1420 | q = false; | |
1421 | break; | |
1422 | } | |
b7fc42e0 | 1423 | |
f389bf15 LP |
1424 | if (q) { |
1425 | for (i = 0; i < bus->wqueue_size; i++) | |
1426 | if (bus->wqueue[i]->n_ref > 1) { | |
1427 | q = false; | |
1428 | break; | |
1429 | } | |
1430 | } | |
b7fc42e0 | 1431 | |
f389bf15 LP |
1432 | /* We are the only holders on the messages, and the |
1433 | * messages are the only holders on us, so let's drop | |
1434 | * the messages and thus implicitly also kill our own | |
eb33a6f8 DH |
1435 | * last references. |
1436 | * bus_reset_queues() decrements the queue-size before | |
1437 | * calling into sd_bus_message_unref(). Thus, it | |
1438 | * protects us from recursion. */ | |
f389bf15 | 1439 | |
23abf5d5 LP |
1440 | if (q) |
1441 | bus_reset_queues(bus); | |
f389bf15 | 1442 | } |
de1c301e | 1443 | |
f389bf15 LP |
1444 | i = REFCNT_DEC(bus->n_ref); |
1445 | if (i > 0) | |
1446 | return NULL; | |
1447 | ||
1448 | bus_free(bus); | |
de1c301e LP |
1449 | return NULL; |
1450 | } | |
1451 | ||
d9f644e2 | 1452 | _public_ int sd_bus_is_open(sd_bus *bus) { |
d6888822 LP |
1453 | |
1454 | assert_return(bus, -EINVAL); | |
1455 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
e3017af9 | 1456 | |
f54514f3 | 1457 | return BUS_IS_OPEN(bus->state); |
e3017af9 LP |
1458 | } |
1459 | ||
d9f644e2 | 1460 | _public_ int sd_bus_can_send(sd_bus *bus, char type) { |
d728d708 LP |
1461 | int r; |
1462 | ||
d6888822 LP |
1463 | assert_return(bus, -EINVAL); |
1464 | assert_return(bus->state != BUS_UNSET, -ENOTCONN); | |
1465 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
de1c301e | 1466 | |
09365592 LP |
1467 | if (bus->hello_flags & KDBUS_HELLO_MONITOR) |
1468 | return 0; | |
1469 | ||
d728d708 | 1470 | if (type == SD_BUS_TYPE_UNIX_FD) { |
264ad849 | 1471 | if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) |
021a1e78 LP |
1472 | return 0; |
1473 | ||
20902f3e | 1474 | r = bus_ensure_running(bus); |
d728d708 LP |
1475 | if (r < 0) |
1476 | return r; | |
de1c301e | 1477 | |
d728d708 LP |
1478 | return bus->can_fds; |
1479 | } | |
1480 | ||
1481 | return bus_type_is_valid(type); | |
de1c301e LP |
1482 | } |
1483 | ||
d9f644e2 | 1484 | _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) { |
d728d708 | 1485 | int r; |
de1c301e | 1486 | |
d6888822 LP |
1487 | assert_return(bus, -EINVAL); |
1488 | assert_return(server_id, -EINVAL); | |
1489 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
de1c301e | 1490 | |
20902f3e | 1491 | r = bus_ensure_running(bus); |
d728d708 LP |
1492 | if (r < 0) |
1493 | return r; | |
de1c301e | 1494 | |
98178d39 | 1495 | *server_id = bus->server_id; |
d728d708 | 1496 | return 0; |
de1c301e LP |
1497 | } |
1498 | ||
3df7a7e6 | 1499 | static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) { |
7adc46fc | 1500 | assert(b); |
de1c301e LP |
1501 | assert(m); |
1502 | ||
aea93deb LP |
1503 | if (m->sealed) { |
1504 | /* If we copy the same message to multiple | |
693eb9a2 | 1505 | * destinations, avoid using the same cookie |
aea93deb | 1506 | * numbers. */ |
693eb9a2 | 1507 | b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m)); |
de1c301e | 1508 | return 0; |
aea93deb | 1509 | } |
de1c301e | 1510 | |
3df7a7e6 LP |
1511 | if (timeout == 0) |
1512 | timeout = BUS_DEFAULT_TIMEOUT; | |
1513 | ||
693eb9a2 | 1514 | return bus_message_seal(m, ++b->cookie, timeout); |
de1c301e LP |
1515 | } |
1516 | ||
e1c433c6 LP |
1517 | static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) { |
1518 | assert(b); | |
1519 | ||
6f285378 | 1520 | /* Do packet version and endianness already match? */ |
e1c433c6 LP |
1521 | if ((b->message_version == 0 || b->message_version == (*m)->header->version) && |
1522 | (b->message_endian == 0 || b->message_endian == (*m)->header->endian)) | |
1523 | return 0; | |
1524 | ||
1525 | /* No? Then remarshal! */ | |
1526 | return bus_message_remarshal(b, m); | |
1527 | } | |
1528 | ||
7adc46fc LP |
1529 | int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) { |
1530 | assert(b); | |
1531 | assert(m); | |
1532 | ||
7adc46fc LP |
1533 | /* The bus specification says the serial number cannot be 0, |
1534 | * hence let's fill something in for synthetic messages. Since | |
1535 | * synthetic messages might have a fake sender and we don't | |
1536 | * want to interfere with the real sender's serial numbers we | |
6f285378 | 1537 | * pick a fixed, artificial one. We use (uint32_t) -1 rather |
7adc46fc LP |
1538 | * than (uint64_t) -1 since dbus1 only had 32bit identifiers, |
1539 | * even though kdbus can do 64bit. */ | |
1540 | ||
3df7a7e6 | 1541 | return bus_message_seal(m, 0xFFFFFFFFULL, 0); |
7adc46fc LP |
1542 | } |
1543 | ||
069f5e61 | 1544 | static int bus_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call, size_t *idx) { |
2e3db52d LP |
1545 | int r; |
1546 | ||
718db961 | 1547 | assert(bus); |
2e3db52d | 1548 | assert(m); |
718db961 LP |
1549 | |
1550 | if (bus->is_kernel) | |
069f5e61 | 1551 | r = bus_kernel_write_message(bus, m, hint_sync_call); |
718db961 | 1552 | else |
2e3db52d LP |
1553 | r = bus_socket_write_message(bus, m, idx); |
1554 | ||
1555 | if (r <= 0) | |
1556 | return r; | |
1557 | ||
90911bb6 | 1558 | if (bus->is_kernel || *idx >= BUS_MESSAGE_SIZE(m)) |
42c4ebcb | 1559 | log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s", |
2e3db52d LP |
1560 | bus_message_type_to_string(m->header->type), |
1561 | strna(sd_bus_message_get_sender(m)), | |
1562 | strna(sd_bus_message_get_destination(m)), | |
1563 | strna(sd_bus_message_get_path(m)), | |
1564 | strna(sd_bus_message_get_interface(m)), | |
1565 | strna(sd_bus_message_get_member(m)), | |
42c4ebcb LP |
1566 | BUS_MESSAGE_COOKIE(m), |
1567 | m->reply_cookie, | |
2e3db52d LP |
1568 | strna(m->error.message)); |
1569 | ||
1570 | return r; | |
718db961 LP |
1571 | } |
1572 | ||
de1c301e | 1573 | static int dispatch_wqueue(sd_bus *bus) { |
e3017af9 | 1574 | int r, ret = 0; |
de1c301e LP |
1575 | |
1576 | assert(bus); | |
89ffcd2a | 1577 | assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); |
de1c301e | 1578 | |
de1c301e LP |
1579 | while (bus->wqueue_size > 0) { |
1580 | ||
069f5e61 | 1581 | r = bus_write_message(bus, bus->wqueue[0], false, &bus->windex); |
718db961 | 1582 | if (r < 0) |
de1c301e | 1583 | return r; |
718db961 | 1584 | else if (r == 0) |
e3017af9 LP |
1585 | /* Didn't do anything this time */ |
1586 | return ret; | |
6629161f | 1587 | else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) { |
de1c301e LP |
1588 | /* Fully written. Let's drop the entry from |
1589 | * the queue. | |
1590 | * | |
1591 | * This isn't particularly optimized, but | |
1592 | * well, this is supposed to be our worst-case | |
1593 | * buffer only, and the socket buffer is | |
1594 | * supposed to be our primary buffer, and if | |
1595 | * it got full, then all bets are off | |
1596 | * anyway. */ | |
1597 | ||
de1c301e | 1598 | bus->wqueue_size --; |
c4e6e242 | 1599 | sd_bus_message_unref(bus->wqueue[0]); |
de1c301e LP |
1600 | memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size); |
1601 | bus->windex = 0; | |
1602 | ||
e3017af9 | 1603 | ret = 1; |
de1c301e LP |
1604 | } |
1605 | } | |
1606 | ||
e3017af9 | 1607 | return ret; |
de1c301e LP |
1608 | } |
1609 | ||
766c5809 | 1610 | static int bus_read_message(sd_bus *bus, bool hint_priority, int64_t priority) { |
7d22c717 LP |
1611 | assert(bus); |
1612 | ||
1613 | if (bus->is_kernel) | |
766c5809 | 1614 | return bus_kernel_read_message(bus, hint_priority, priority); |
7d22c717 LP |
1615 | else |
1616 | return bus_socket_read_message(bus); | |
1617 | } | |
1618 | ||
7adc46fc | 1619 | int bus_rqueue_make_room(sd_bus *bus) { |
821e0756 | 1620 | assert(bus); |
7d22c717 | 1621 | |
821e0756 | 1622 | if (bus->rqueue_size >= BUS_RQUEUE_MAX) |
7d22c717 LP |
1623 | return -ENOBUFS; |
1624 | ||
821e0756 | 1625 | if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1)) |
7d22c717 LP |
1626 | return -ENOMEM; |
1627 | ||
7d22c717 LP |
1628 | return 0; |
1629 | } | |
1630 | ||
766c5809 | 1631 | static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) { |
e3017af9 | 1632 | int r, ret = 0; |
de1c301e LP |
1633 | |
1634 | assert(bus); | |
1635 | assert(m); | |
89ffcd2a | 1636 | assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); |
de1c301e | 1637 | |
766c5809 LP |
1638 | /* Note that the priority logic is only available on kdbus, |
1639 | * where the rqueue is unused. We check the rqueue here | |
1640 | * anyway, because it's simple... */ | |
1641 | ||
7d22c717 LP |
1642 | for (;;) { |
1643 | if (bus->rqueue_size > 0) { | |
1644 | /* Dispatch a queued message */ | |
de1c301e | 1645 | |
7d22c717 LP |
1646 | *m = bus->rqueue[0]; |
1647 | bus->rqueue_size --; | |
1648 | memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size); | |
1649 | return 1; | |
1650 | } | |
de1c301e | 1651 | |
7d22c717 | 1652 | /* Try to read a new message */ |
766c5809 | 1653 | r = bus_read_message(bus, hint_priority, priority); |
718db961 | 1654 | if (r < 0) |
e3017af9 | 1655 | return r; |
e3017af9 LP |
1656 | if (r == 0) |
1657 | return ret; | |
de1c301e | 1658 | |
2e8d788c | 1659 | ret = 1; |
7d22c717 | 1660 | } |
de1c301e LP |
1661 | } |
1662 | ||
069f5e61 | 1663 | static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) { |
e1c433c6 | 1664 | _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m); |
de1c301e LP |
1665 | int r; |
1666 | ||
d6888822 | 1667 | assert_return(bus, -EINVAL); |
d6888822 LP |
1668 | assert_return(m, -EINVAL); |
1669 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
09365592 | 1670 | assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS); |
021a1e78 | 1671 | |
a3d59cd1 LP |
1672 | if (!BUS_IS_OPEN(bus->state)) |
1673 | return -ENOTCONN; | |
1674 | ||
021a1e78 LP |
1675 | if (m->n_fds > 0) { |
1676 | r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD); | |
1677 | if (r < 0) | |
1678 | return r; | |
1679 | if (r == 0) | |
1680 | return -ENOTSUP; | |
1681 | } | |
de1c301e | 1682 | |
693eb9a2 | 1683 | /* If the cookie number isn't kept, then we know that no reply |
29f6aadd | 1684 | * is expected */ |
693eb9a2 | 1685 | if (!cookie && !m->sealed) |
0461f8cd | 1686 | m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED; |
29f6aadd | 1687 | |
3df7a7e6 | 1688 | r = bus_seal_message(bus, m, 0); |
de1c301e LP |
1689 | if (r < 0) |
1690 | return r; | |
1691 | ||
441d56a1 | 1692 | /* Remarshall if we have to. This will possibly unref the |
e1c433c6 LP |
1693 | * message and place a replacement in m */ |
1694 | r = bus_remarshal_message(bus, &m); | |
1695 | if (r < 0) | |
1696 | return r; | |
1697 | ||
5407f2de LP |
1698 | /* If this is a reply and no reply was requested, then let's |
1699 | * suppress this, if we can */ | |
693eb9a2 | 1700 | if (m->dont_send && !cookie) |
7a37d625 | 1701 | return 1; |
5407f2de | 1702 | |
89ffcd2a | 1703 | if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) { |
de1c301e LP |
1704 | size_t idx = 0; |
1705 | ||
069f5e61 | 1706 | r = bus_write_message(bus, m, hint_sync_call, &idx); |
32f46480 | 1707 | if (r < 0) { |
441d56a1 | 1708 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
32f46480 | 1709 | bus_enter_closing(bus); |
441d56a1 LP |
1710 | return -ECONNRESET; |
1711 | } | |
32f46480 | 1712 | |
de1c301e | 1713 | return r; |
32f46480 | 1714 | } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) { |
de1c301e LP |
1715 | /* Wasn't fully written. So let's remember how |
1716 | * much was written. Note that the first entry | |
1717 | * of the wqueue array is always allocated so | |
1718 | * that we always can remember how much was | |
1719 | * written. */ | |
1720 | bus->wqueue[0] = sd_bus_message_ref(m); | |
1721 | bus->wqueue_size = 1; | |
1722 | bus->windex = idx; | |
1723 | } | |
1724 | } else { | |
de1c301e LP |
1725 | /* Just append it to the queue. */ |
1726 | ||
25220239 | 1727 | if (bus->wqueue_size >= BUS_WQUEUE_MAX) |
de1c301e LP |
1728 | return -ENOBUFS; |
1729 | ||
821e0756 | 1730 | if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1)) |
de1c301e LP |
1731 | return -ENOMEM; |
1732 | ||
821e0756 | 1733 | bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m); |
de1c301e LP |
1734 | } |
1735 | ||
693eb9a2 LP |
1736 | if (cookie) |
1737 | *cookie = BUS_MESSAGE_COOKIE(m); | |
de1c301e | 1738 | |
7a37d625 | 1739 | return 1; |
de1c301e LP |
1740 | } |
1741 | ||
069f5e61 LP |
1742 | _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *cookie) { |
1743 | return bus_send_internal(bus, m, cookie, false); | |
1744 | } | |
1745 | ||
693eb9a2 | 1746 | _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) { |
911121a7 LP |
1747 | int r; |
1748 | ||
1749 | assert_return(bus, -EINVAL); | |
911121a7 LP |
1750 | assert_return(m, -EINVAL); |
1751 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
1752 | ||
a3d59cd1 LP |
1753 | if (!BUS_IS_OPEN(bus->state)) |
1754 | return -ENOTCONN; | |
1755 | ||
911121a7 LP |
1756 | if (!streq_ptr(m->destination, destination)) { |
1757 | ||
1758 | if (!destination) | |
1759 | return -EEXIST; | |
1760 | ||
1761 | r = sd_bus_message_set_destination(m, destination); | |
1762 | if (r < 0) | |
1763 | return r; | |
1764 | } | |
1765 | ||
693eb9a2 | 1766 | return sd_bus_send(bus, m, cookie); |
911121a7 LP |
1767 | } |
1768 | ||
de1c301e LP |
1769 | static usec_t calc_elapse(uint64_t usec) { |
1770 | if (usec == (uint64_t) -1) | |
1771 | return 0; | |
1772 | ||
de1c301e LP |
1773 | return now(CLOCK_MONOTONIC) + usec; |
1774 | } | |
1775 | ||
e3017af9 LP |
1776 | static int timeout_compare(const void *a, const void *b) { |
1777 | const struct reply_callback *x = a, *y = b; | |
1778 | ||
1779 | if (x->timeout != 0 && y->timeout == 0) | |
1780 | return -1; | |
1781 | ||
1782 | if (x->timeout == 0 && y->timeout != 0) | |
1783 | return 1; | |
1784 | ||
1785 | if (x->timeout < y->timeout) | |
1786 | return -1; | |
1787 | ||
1788 | if (x->timeout > y->timeout) | |
1789 | return 1; | |
1790 | ||
1791 | return 0; | |
1792 | } | |
1793 | ||
c49b30a2 | 1794 | _public_ int sd_bus_call_async( |
de1c301e | 1795 | sd_bus *bus, |
e1c433c6 | 1796 | sd_bus_message *_m, |
52f3ba91 | 1797 | sd_bus_message_handler_t callback, |
de1c301e LP |
1798 | void *userdata, |
1799 | uint64_t usec, | |
693eb9a2 | 1800 | uint64_t *cookie) { |
de1c301e | 1801 | |
e1c433c6 | 1802 | _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m); |
de1c301e LP |
1803 | struct reply_callback *c; |
1804 | int r; | |
1805 | ||
d6888822 | 1806 | assert_return(bus, -EINVAL); |
d6888822 | 1807 | assert_return(m, -EINVAL); |
40ca29a1 | 1808 | assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); |
0461f8cd | 1809 | assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL); |
d6888822 LP |
1810 | assert_return(callback, -EINVAL); |
1811 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
09365592 | 1812 | assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS); |
89ffcd2a | 1813 | |
a3d59cd1 LP |
1814 | if (!BUS_IS_OPEN(bus->state)) |
1815 | return -ENOTCONN; | |
1816 | ||
89ffcd2a LP |
1817 | r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func); |
1818 | if (r < 0) | |
1819 | return r; | |
de1c301e | 1820 | |
3df7a7e6 LP |
1821 | r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare); |
1822 | if (r < 0) | |
1823 | return r; | |
e3017af9 | 1824 | |
3df7a7e6 | 1825 | r = bus_seal_message(bus, m, usec); |
de1c301e LP |
1826 | if (r < 0) |
1827 | return r; | |
1828 | ||
e1c433c6 LP |
1829 | r = bus_remarshal_message(bus, &m); |
1830 | if (r < 0) | |
1831 | return r; | |
1832 | ||
eba8617e | 1833 | c = new0(struct reply_callback, 1); |
de1c301e LP |
1834 | if (!c) |
1835 | return -ENOMEM; | |
1836 | ||
1837 | c->callback = callback; | |
1838 | c->userdata = userdata; | |
693eb9a2 | 1839 | c->cookie = BUS_MESSAGE_COOKIE(m); |
3df7a7e6 | 1840 | c->timeout = calc_elapse(m->timeout); |
de1c301e | 1841 | |
693eb9a2 | 1842 | r = hashmap_put(bus->reply_callbacks, &c->cookie, c); |
de1c301e LP |
1843 | if (r < 0) { |
1844 | free(c); | |
1845 | return r; | |
1846 | } | |
1847 | ||
e3017af9 LP |
1848 | if (c->timeout != 0) { |
1849 | r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx); | |
1850 | if (r < 0) { | |
1851 | c->timeout = 0; | |
693eb9a2 | 1852 | sd_bus_call_async_cancel(bus, c->cookie); |
e3017af9 LP |
1853 | return r; |
1854 | } | |
1855 | } | |
1856 | ||
693eb9a2 | 1857 | r = sd_bus_send(bus, m, cookie); |
de1c301e | 1858 | if (r < 0) { |
693eb9a2 | 1859 | sd_bus_call_async_cancel(bus, c->cookie); |
de1c301e LP |
1860 | return r; |
1861 | } | |
1862 | ||
1863 | return r; | |
1864 | } | |
1865 | ||
693eb9a2 | 1866 | _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t cookie) { |
e3017af9 | 1867 | struct reply_callback *c; |
de1c301e | 1868 | |
d6888822 | 1869 | assert_return(bus, -EINVAL); |
693eb9a2 | 1870 | assert_return(cookie != 0, -EINVAL); |
d6888822 | 1871 | assert_return(!bus_pid_changed(bus), -ECHILD); |
de1c301e | 1872 | |
693eb9a2 | 1873 | c = hashmap_remove(bus->reply_callbacks, &cookie); |
de1c301e LP |
1874 | if (!c) |
1875 | return 0; | |
1876 | ||
e3017af9 LP |
1877 | if (c->timeout != 0) |
1878 | prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx); | |
1879 | ||
de1c301e LP |
1880 | free(c); |
1881 | return 1; | |
1882 | } | |
1883 | ||
20902f3e | 1884 | int bus_ensure_running(sd_bus *bus) { |
89ffcd2a LP |
1885 | int r; |
1886 | ||
1887 | assert(bus); | |
1888 | ||
718db961 | 1889 | if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING) |
021a1e78 | 1890 | return -ENOTCONN; |
d728d708 LP |
1891 | if (bus->state == BUS_RUNNING) |
1892 | return 1; | |
89ffcd2a LP |
1893 | |
1894 | for (;;) { | |
1895 | r = sd_bus_process(bus, NULL); | |
1896 | if (r < 0) | |
1897 | return r; | |
d728d708 LP |
1898 | if (bus->state == BUS_RUNNING) |
1899 | return 1; | |
e3017af9 LP |
1900 | if (r > 0) |
1901 | continue; | |
89ffcd2a LP |
1902 | |
1903 | r = sd_bus_wait(bus, (uint64_t) -1); | |
1904 | if (r < 0) | |
1905 | return r; | |
1906 | } | |
1907 | } | |
1908 | ||
97f82db3 | 1909 | _public_ int sd_bus_call( |
de1c301e | 1910 | sd_bus *bus, |
97f82db3 | 1911 | sd_bus_message *_m, |
de1c301e LP |
1912 | uint64_t usec, |
1913 | sd_bus_error *error, | |
1914 | sd_bus_message **reply) { | |
1915 | ||
97f82db3 | 1916 | _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m); |
de1c301e | 1917 | usec_t timeout; |
693eb9a2 | 1918 | uint64_t cookie; |
7d22c717 LP |
1919 | unsigned i; |
1920 | int r; | |
de1c301e | 1921 | |
97f82db3 | 1922 | assert_return(bus, -EINVAL); |
97f82db3 KS |
1923 | assert_return(m, -EINVAL); |
1924 | assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); | |
1925 | assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL); | |
1926 | assert_return(!bus_error_is_dirty(error), -EINVAL); | |
1927 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
09365592 | 1928 | assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS); |
97f82db3 | 1929 | |
a3d59cd1 LP |
1930 | if (!BUS_IS_OPEN(bus->state)) |
1931 | return -ENOTCONN; | |
1932 | ||
97f82db3 KS |
1933 | r = bus_ensure_running(bus); |
1934 | if (r < 0) | |
1935 | return r; | |
1936 | ||
a43b9ca3 | 1937 | i = bus->rqueue_size; |
97f82db3 KS |
1938 | |
1939 | r = bus_seal_message(bus, m, usec); | |
1940 | if (r < 0) | |
1941 | return r; | |
1942 | ||
1943 | r = bus_remarshal_message(bus, &m); | |
1944 | if (r < 0) | |
1945 | return r; | |
1946 | ||
069f5e61 | 1947 | r = bus_send_internal(bus, m, &cookie, true); |
de1c301e LP |
1948 | if (r < 0) |
1949 | return r; | |
1950 | ||
3df7a7e6 | 1951 | timeout = calc_elapse(m->timeout); |
de1c301e LP |
1952 | |
1953 | for (;;) { | |
1954 | usec_t left; | |
de1c301e | 1955 | |
7d22c717 LP |
1956 | while (i < bus->rqueue_size) { |
1957 | sd_bus_message *incoming = NULL; | |
1958 | ||
1959 | incoming = bus->rqueue[i]; | |
89ffcd2a | 1960 | |
693eb9a2 | 1961 | if (incoming->reply_cookie == cookie) { |
de1c301e LP |
1962 | /* Found a match! */ |
1963 | ||
7d22c717 LP |
1964 | memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1)); |
1965 | bus->rqueue_size--; | |
1966 | ||
40ca29a1 | 1967 | if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) { |
b7f247e0 | 1968 | |
2ce97e2b LP |
1969 | if (incoming->n_fds <= 0 || (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) { |
1970 | if (reply) | |
1971 | *reply = incoming; | |
1972 | else | |
1973 | sd_bus_message_unref(incoming); | |
1974 | ||
1975 | return 1; | |
1976 | } | |
1977 | ||
1978 | r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry."); | |
b7f247e0 | 1979 | |
a43b9ca3 | 1980 | } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) |
de1c301e | 1981 | r = sd_bus_error_copy(error, &incoming->error); |
a43b9ca3 LP |
1982 | else |
1983 | r = -EIO; | |
de1c301e LP |
1984 | |
1985 | sd_bus_message_unref(incoming); | |
a43b9ca3 | 1986 | return r; |
a8a07f89 | 1987 | |
693eb9a2 | 1988 | } else if (BUS_MESSAGE_COOKIE(incoming) == cookie && |
a8a07f89 LP |
1989 | bus->unique_name && |
1990 | incoming->sender && | |
1991 | streq(bus->unique_name, incoming->sender)) { | |
1992 | ||
7d22c717 LP |
1993 | memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1)); |
1994 | bus->rqueue_size--; | |
1995 | ||
a8a07f89 LP |
1996 | /* Our own message? Somebody is trying |
1997 | * to send its own client a message, | |
1998 | * let's not dead-lock, let's fail | |
1999 | * immediately. */ | |
2000 | ||
2001 | sd_bus_message_unref(incoming); | |
2002 | return -ELOOP; | |
de1c301e LP |
2003 | } |
2004 | ||
de1c301e | 2005 | /* Try to read more, right-away */ |
7d22c717 | 2006 | i++; |
de1c301e | 2007 | } |
7d22c717 | 2008 | |
766c5809 | 2009 | r = bus_read_message(bus, false, 0); |
32f46480 | 2010 | if (r < 0) { |
441d56a1 | 2011 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
32f46480 | 2012 | bus_enter_closing(bus); |
441d56a1 LP |
2013 | return -ECONNRESET; |
2014 | } | |
32f46480 | 2015 | |
a43b9ca3 | 2016 | return r; |
32f46480 | 2017 | } |
7d22c717 | 2018 | if (r > 0) |
e3017af9 | 2019 | continue; |
de1c301e LP |
2020 | |
2021 | if (timeout > 0) { | |
2022 | usec_t n; | |
2023 | ||
2024 | n = now(CLOCK_MONOTONIC); | |
2025 | if (n >= timeout) | |
2026 | return -ETIMEDOUT; | |
2027 | ||
2028 | left = timeout - n; | |
2029 | } else | |
2030 | left = (uint64_t) -1; | |
2031 | ||
e3017af9 | 2032 | r = bus_poll(bus, true, left); |
de1c301e LP |
2033 | if (r < 0) |
2034 | return r; | |
f8e11cda LP |
2035 | if (r == 0) |
2036 | return -ETIMEDOUT; | |
de1c301e LP |
2037 | |
2038 | r = dispatch_wqueue(bus); | |
32f46480 | 2039 | if (r < 0) { |
441d56a1 | 2040 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
32f46480 | 2041 | bus_enter_closing(bus); |
441d56a1 LP |
2042 | return -ECONNRESET; |
2043 | } | |
32f46480 | 2044 | |
de1c301e | 2045 | return r; |
32f46480 | 2046 | } |
de1c301e LP |
2047 | } |
2048 | } | |
2049 | ||
d9f644e2 | 2050 | _public_ int sd_bus_get_fd(sd_bus *bus) { |
d6888822 LP |
2051 | |
2052 | assert_return(bus, -EINVAL); | |
d6888822 LP |
2053 | assert_return(bus->input_fd == bus->output_fd, -EPERM); |
2054 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
de1c301e | 2055 | |
e82c9509 | 2056 | return bus->input_fd; |
de1c301e LP |
2057 | } |
2058 | ||
d9f644e2 | 2059 | _public_ int sd_bus_get_events(sd_bus *bus) { |
de1c301e LP |
2060 | int flags = 0; |
2061 | ||
d6888822 | 2062 | assert_return(bus, -EINVAL); |
d6888822 | 2063 | assert_return(!bus_pid_changed(bus), -ECHILD); |
de1c301e | 2064 | |
a3d59cd1 LP |
2065 | if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING) |
2066 | return -ENOTCONN; | |
2067 | ||
de1c301e LP |
2068 | if (bus->state == BUS_OPENING) |
2069 | flags |= POLLOUT; | |
89ffcd2a LP |
2070 | else if (bus->state == BUS_AUTHENTICATING) { |
2071 | ||
2181a7f5 | 2072 | if (bus_socket_auth_needs_write(bus)) |
89ffcd2a LP |
2073 | flags |= POLLOUT; |
2074 | ||
2075 | flags |= POLLIN; | |
2076 | ||
2077 | } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) { | |
de1c301e LP |
2078 | if (bus->rqueue_size <= 0) |
2079 | flags |= POLLIN; | |
2080 | if (bus->wqueue_size > 0) | |
2081 | flags |= POLLOUT; | |
2082 | } | |
2083 | ||
2084 | return flags; | |
2085 | } | |
2086 | ||
d9f644e2 | 2087 | _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) { |
e3017af9 LP |
2088 | struct reply_callback *c; |
2089 | ||
d6888822 LP |
2090 | assert_return(bus, -EINVAL); |
2091 | assert_return(timeout_usec, -EINVAL); | |
d6888822 | 2092 | assert_return(!bus_pid_changed(bus), -ECHILD); |
e3017af9 | 2093 | |
a3d59cd1 LP |
2094 | if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING) |
2095 | return -ENOTCONN; | |
2096 | ||
8f8f05a9 LP |
2097 | if (bus->track_queue) { |
2098 | *timeout_usec = 0; | |
2099 | return 1; | |
2100 | } | |
2101 | ||
718db961 LP |
2102 | if (bus->state == BUS_CLOSING) { |
2103 | *timeout_usec = 0; | |
2104 | return 1; | |
2105 | } | |
2106 | ||
e3017af9 LP |
2107 | if (bus->state == BUS_AUTHENTICATING) { |
2108 | *timeout_usec = bus->auth_timeout; | |
2109 | return 1; | |
2110 | } | |
2111 | ||
adee69fa LP |
2112 | if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) { |
2113 | *timeout_usec = (uint64_t) -1; | |
e3017af9 | 2114 | return 0; |
adee69fa | 2115 | } |
e3017af9 | 2116 | |
8efd6381 LP |
2117 | if (bus->rqueue_size > 0) { |
2118 | *timeout_usec = 0; | |
2119 | return 1; | |
2120 | } | |
2121 | ||
e3017af9 | 2122 | c = prioq_peek(bus->reply_callbacks_prioq); |
adee69fa LP |
2123 | if (!c) { |
2124 | *timeout_usec = (uint64_t) -1; | |
e3017af9 | 2125 | return 0; |
adee69fa | 2126 | } |
e3017af9 LP |
2127 | |
2128 | *timeout_usec = c->timeout; | |
2129 | return 1; | |
2130 | } | |
2131 | ||
2132 | static int process_timeout(sd_bus *bus) { | |
ebcf1f97 | 2133 | _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; |
eb01ba5d | 2134 | _cleanup_bus_message_unref_ sd_bus_message* m = NULL; |
e3017af9 LP |
2135 | struct reply_callback *c; |
2136 | usec_t n; | |
2137 | int r; | |
2138 | ||
2139 | assert(bus); | |
2140 | ||
2141 | c = prioq_peek(bus->reply_callbacks_prioq); | |
2142 | if (!c) | |
2143 | return 0; | |
2144 | ||
2145 | n = now(CLOCK_MONOTONIC); | |
2146 | if (c->timeout > n) | |
2147 | return 0; | |
2148 | ||
eb01ba5d LP |
2149 | r = bus_message_new_synthetic_error( |
2150 | bus, | |
693eb9a2 | 2151 | c->cookie, |
14c24659 | 2152 | &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"), |
eb01ba5d LP |
2153 | &m); |
2154 | if (r < 0) | |
2155 | return r; | |
2156 | ||
34a2c9e8 LP |
2157 | m->sender = "org.freedesktop.DBus"; |
2158 | ||
7adc46fc | 2159 | r = bus_seal_synthetic_message(bus, m); |
718db961 LP |
2160 | if (r < 0) |
2161 | return r; | |
2162 | ||
e3017af9 | 2163 | assert_se(prioq_pop(bus->reply_callbacks_prioq) == c); |
693eb9a2 | 2164 | hashmap_remove(bus->reply_callbacks, &c->cookie); |
e3017af9 | 2165 | |
718db961 LP |
2166 | bus->current = m; |
2167 | bus->iteration_counter ++; | |
2168 | ||
ebcf1f97 LP |
2169 | r = c->callback(bus, m, c->userdata, &error_buffer); |
2170 | r = bus_maybe_reply_error(m, r, &error_buffer); | |
e3017af9 LP |
2171 | free(c); |
2172 | ||
718db961 LP |
2173 | bus->current = NULL; |
2174 | ||
ebcf1f97 | 2175 | return r; |
e3017af9 LP |
2176 | } |
2177 | ||
9d373862 LP |
2178 | static int process_hello(sd_bus *bus, sd_bus_message *m) { |
2179 | assert(bus); | |
2180 | assert(m); | |
2181 | ||
2182 | if (bus->state != BUS_HELLO) | |
2183 | return 0; | |
2184 | ||
2185 | /* Let's make sure the first message on the bus is the HELLO | |
2186 | * reply. But note that we don't actually parse the message | |
2181a7f5 LP |
2187 | * here (we leave that to the usual handling), we just verify |
2188 | * we don't let any earlier msg through. */ | |
9d373862 | 2189 | |
40ca29a1 LP |
2190 | if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN && |
2191 | m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) | |
9d373862 LP |
2192 | return -EIO; |
2193 | ||
693eb9a2 | 2194 | if (m->reply_cookie != bus->hello_cookie) |
9d373862 LP |
2195 | return -EIO; |
2196 | ||
2197 | return 0; | |
2198 | } | |
2199 | ||
a652755d | 2200 | static int process_reply(sd_bus *bus, sd_bus_message *m) { |
2ce97e2b | 2201 | _cleanup_bus_message_unref_ sd_bus_message *synthetic_reply = NULL; |
ebcf1f97 | 2202 | _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; |
2ce97e2b | 2203 | _cleanup_free_ struct reply_callback *c = NULL; |
a652755d LP |
2204 | int r; |
2205 | ||
2206 | assert(bus); | |
2207 | assert(m); | |
2208 | ||
40ca29a1 LP |
2209 | if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN && |
2210 | m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) | |
a652755d LP |
2211 | return 0; |
2212 | ||
09365592 LP |
2213 | if (bus->is_kernel && (bus->hello_flags & KDBUS_HELLO_MONITOR)) |
2214 | return 0; | |
2215 | ||
2216 | if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name)) | |
2217 | return 0; | |
2218 | ||
693eb9a2 | 2219 | c = hashmap_remove(bus->reply_callbacks, &m->reply_cookie); |
a652755d LP |
2220 | if (!c) |
2221 | return 0; | |
2222 | ||
2223 | if (c->timeout != 0) | |
2224 | prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx); | |
2225 | ||
2ce97e2b LP |
2226 | if (m->n_fds > 0 && !(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) { |
2227 | ||
2228 | /* If the reply contained a file descriptor which we | |
2229 | * didn't want we pass an error instead. */ | |
2230 | ||
2231 | r = bus_message_new_synthetic_error( | |
2232 | bus, | |
2233 | m->reply_cookie, | |
2234 | &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"), | |
2235 | &synthetic_reply); | |
2236 | if (r < 0) | |
2237 | return r; | |
2238 | ||
2239 | r = bus_seal_synthetic_message(bus, synthetic_reply); | |
2240 | if (r < 0) | |
2241 | return r; | |
2242 | ||
2243 | m = synthetic_reply; | |
2244 | } else { | |
2245 | r = sd_bus_message_rewind(m, true); | |
2246 | if (r < 0) | |
2247 | return r; | |
2248 | } | |
88fe224c | 2249 | |
ebcf1f97 LP |
2250 | r = c->callback(bus, m, c->userdata, &error_buffer); |
2251 | r = bus_maybe_reply_error(m, r, &error_buffer); | |
a652755d | 2252 | |
ebcf1f97 | 2253 | return r; |
a652755d LP |
2254 | } |
2255 | ||
2256 | static int process_filter(sd_bus *bus, sd_bus_message *m) { | |
ebcf1f97 | 2257 | _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; |
a652755d LP |
2258 | struct filter_callback *l; |
2259 | int r; | |
2260 | ||
392d5b37 LP |
2261 | assert(bus); |
2262 | assert(m); | |
2263 | ||
7286037f LP |
2264 | do { |
2265 | bus->filter_callbacks_modified = false; | |
2266 | ||
2267 | LIST_FOREACH(callbacks, l, bus->filter_callbacks) { | |
2268 | ||
2269 | if (bus->filter_callbacks_modified) | |
2270 | break; | |
2271 | ||
2272 | /* Don't run this more than once per iteration */ | |
2273 | if (l->last_iteration == bus->iteration_counter) | |
2274 | continue; | |
2275 | ||
2276 | l->last_iteration = bus->iteration_counter; | |
2277 | ||
88fe224c LP |
2278 | r = sd_bus_message_rewind(m, true); |
2279 | if (r < 0) | |
2280 | return r; | |
2281 | ||
969987ea | 2282 | r = l->callback(bus, m, l->userdata, &error_buffer); |
ebcf1f97 | 2283 | r = bus_maybe_reply_error(m, r, &error_buffer); |
7286037f LP |
2284 | if (r != 0) |
2285 | return r; | |
2286 | ||
2287 | } | |
2288 | ||
2289 | } while (bus->filter_callbacks_modified); | |
a652755d LP |
2290 | |
2291 | return 0; | |
2292 | } | |
2293 | ||
392d5b37 | 2294 | static int process_match(sd_bus *bus, sd_bus_message *m) { |
7286037f LP |
2295 | int r; |
2296 | ||
392d5b37 LP |
2297 | assert(bus); |
2298 | assert(m); | |
2299 | ||
7286037f LP |
2300 | do { |
2301 | bus->match_callbacks_modified = false; | |
2302 | ||
eb01ba5d | 2303 | r = bus_match_run(bus, &bus->match_callbacks, m); |
7286037f LP |
2304 | if (r != 0) |
2305 | return r; | |
2306 | ||
2307 | } while (bus->match_callbacks_modified); | |
2308 | ||
2309 | return 0; | |
392d5b37 LP |
2310 | } |
2311 | ||
b9bf7e2b LP |
2312 | static int process_builtin(sd_bus *bus, sd_bus_message *m) { |
2313 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
2314 | int r; | |
2315 | ||
2316 | assert(bus); | |
2317 | assert(m); | |
2318 | ||
09365592 LP |
2319 | if (bus->hello_flags & KDBUS_HELLO_MONITOR) |
2320 | return 0; | |
2321 | ||
758bf0c7 LP |
2322 | if (bus->manual_peer_interface) |
2323 | return 0; | |
2324 | ||
40ca29a1 | 2325 | if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) |
b9bf7e2b LP |
2326 | return 0; |
2327 | ||
2328 | if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer")) | |
2329 | return 0; | |
2330 | ||
0461f8cd | 2331 | if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) |
b9bf7e2b LP |
2332 | return 1; |
2333 | ||
2334 | if (streq_ptr(m->member, "Ping")) | |
df2d202e | 2335 | r = sd_bus_message_new_method_return(m, &reply); |
b9bf7e2b LP |
2336 | else if (streq_ptr(m->member, "GetMachineId")) { |
2337 | sd_id128_t id; | |
2338 | char sid[33]; | |
2339 | ||
2340 | r = sd_id128_get_machine(&id); | |
2341 | if (r < 0) | |
2342 | return r; | |
2343 | ||
df2d202e | 2344 | r = sd_bus_message_new_method_return(m, &reply); |
b9bf7e2b LP |
2345 | if (r < 0) |
2346 | return r; | |
2347 | ||
2348 | r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid)); | |
2349 | } else { | |
29ddb38f | 2350 | r = sd_bus_message_new_method_errorf( |
df2d202e | 2351 | m, &reply, |
40ca29a1 | 2352 | SD_BUS_ERROR_UNKNOWN_METHOD, |
b9bf7e2b | 2353 | "Unknown method '%s' on interface '%s'.", m->member, m->interface); |
b9bf7e2b LP |
2354 | } |
2355 | ||
2356 | if (r < 0) | |
2357 | return r; | |
2358 | ||
2359 | r = sd_bus_send(bus, reply, NULL); | |
2360 | if (r < 0) | |
2361 | return r; | |
2362 | ||
2363 | return 1; | |
2364 | } | |
2365 | ||
2ce97e2b LP |
2366 | static int process_fd_check(sd_bus *bus, sd_bus_message *m) { |
2367 | assert(bus); | |
2368 | assert(m); | |
2369 | ||
2370 | /* If we got a message with a file descriptor which we didn't | |
2371 | * want to accept, then let's drop it. How can this even | |
2372 | * happen? For example, when the kernel queues a message into | |
2373 | * an activatable names's queue which allows fds, and then is | |
2374 | * delivered to us later even though we ourselves did not | |
2375 | * negotiate it. */ | |
2376 | ||
09365592 LP |
2377 | if (bus->hello_flags & KDBUS_HELLO_MONITOR) |
2378 | return 0; | |
2379 | ||
2ce97e2b LP |
2380 | if (m->n_fds <= 0) |
2381 | return 0; | |
2382 | ||
2383 | if (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD) | |
2384 | return 0; | |
2385 | ||
2386 | if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) | |
2387 | return 1; /* just eat it up */ | |
2388 | ||
2389 | return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry."); | |
2390 | } | |
2391 | ||
992c052c | 2392 | static int process_message(sd_bus *bus, sd_bus_message *m) { |
e3017af9 LP |
2393 | int r; |
2394 | ||
2395 | assert(bus); | |
992c052c | 2396 | assert(m); |
e3017af9 | 2397 | |
affff0b6 | 2398 | bus->current = m; |
992c052c | 2399 | bus->iteration_counter++; |
e3017af9 | 2400 | |
42c4ebcb | 2401 | log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s", |
4cdf0751 | 2402 | bus_message_type_to_string(m->header->type), |
40ca29a1 | 2403 | strna(sd_bus_message_get_sender(m)), |
4cdf0751 | 2404 | strna(sd_bus_message_get_destination(m)), |
40ca29a1 LP |
2405 | strna(sd_bus_message_get_path(m)), |
2406 | strna(sd_bus_message_get_interface(m)), | |
4cdf0751 | 2407 | strna(sd_bus_message_get_member(m)), |
42c4ebcb LP |
2408 | BUS_MESSAGE_COOKIE(m), |
2409 | m->reply_cookie, | |
4cdf0751 | 2410 | strna(m->error.message)); |
40ca29a1 | 2411 | |
992c052c LP |
2412 | r = process_hello(bus, m); |
2413 | if (r != 0) | |
affff0b6 | 2414 | goto finish; |
a652755d | 2415 | |
992c052c LP |
2416 | r = process_reply(bus, m); |
2417 | if (r != 0) | |
affff0b6 | 2418 | goto finish; |
e3017af9 | 2419 | |
2ce97e2b LP |
2420 | r = process_fd_check(bus, m); |
2421 | if (r != 0) | |
2422 | goto finish; | |
2423 | ||
992c052c LP |
2424 | r = process_filter(bus, m); |
2425 | if (r != 0) | |
affff0b6 | 2426 | goto finish; |
a652755d | 2427 | |
992c052c LP |
2428 | r = process_match(bus, m); |
2429 | if (r != 0) | |
affff0b6 | 2430 | goto finish; |
a652755d | 2431 | |
992c052c LP |
2432 | r = process_builtin(bus, m); |
2433 | if (r != 0) | |
affff0b6 LP |
2434 | goto finish; |
2435 | ||
2436 | r = bus_process_object(bus, m); | |
a652755d | 2437 | |
affff0b6 LP |
2438 | finish: |
2439 | bus->current = NULL; | |
2440 | return r; | |
29ddb38f | 2441 | } |
88fe224c | 2442 | |
8f8f05a9 LP |
2443 | static int dispatch_track(sd_bus *bus) { |
2444 | assert(bus); | |
2445 | ||
2446 | if (!bus->track_queue) | |
2447 | return 0; | |
2448 | ||
2449 | bus_track_dispatch(bus->track_queue); | |
2450 | return 1; | |
2451 | } | |
2452 | ||
766c5809 | 2453 | static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) { |
992c052c | 2454 | _cleanup_bus_message_unref_ sd_bus_message *m = NULL; |
29ddb38f | 2455 | int r; |
a652755d | 2456 | |
29ddb38f | 2457 | assert(bus); |
992c052c | 2458 | assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); |
7286037f | 2459 | |
992c052c LP |
2460 | r = process_timeout(bus); |
2461 | if (r != 0) | |
2462 | goto null_message; | |
7286037f | 2463 | |
992c052c LP |
2464 | r = dispatch_wqueue(bus); |
2465 | if (r != 0) | |
2466 | goto null_message; | |
7286037f | 2467 | |
8f8f05a9 LP |
2468 | r = dispatch_track(bus); |
2469 | if (r != 0) | |
2470 | goto null_message; | |
2471 | ||
766c5809 | 2472 | r = dispatch_rqueue(bus, hint_priority, priority, &m); |
992c052c LP |
2473 | if (r < 0) |
2474 | return r; | |
2475 | if (!m) | |
2476 | goto null_message; | |
7286037f | 2477 | |
992c052c LP |
2478 | r = process_message(bus, m); |
2479 | if (r != 0) | |
2480 | goto null_message; | |
7286037f | 2481 | |
992c052c LP |
2482 | if (ret) { |
2483 | r = sd_bus_message_rewind(m, true); | |
29ddb38f LP |
2484 | if (r < 0) |
2485 | return r; | |
e3017af9 | 2486 | |
992c052c LP |
2487 | *ret = m; |
2488 | m = NULL; | |
2489 | return 1; | |
2490 | } | |
a652755d | 2491 | |
40ca29a1 | 2492 | if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) { |
a652755d | 2493 | |
dc74ce9b LP |
2494 | log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s", |
2495 | strna(sd_bus_message_get_sender(m)), | |
2496 | strna(sd_bus_message_get_path(m)), | |
2497 | strna(sd_bus_message_get_interface(m)), | |
2498 | strna(sd_bus_message_get_member(m))); | |
2499 | ||
992c052c | 2500 | r = sd_bus_reply_method_errorf( |
df2d202e | 2501 | m, |
40ca29a1 | 2502 | SD_BUS_ERROR_UNKNOWN_OBJECT, |
992c052c | 2503 | "Unknown object '%s'.", m->path); |
29ddb38f LP |
2504 | if (r < 0) |
2505 | return r; | |
2506 | } | |
e3017af9 | 2507 | |
992c052c | 2508 | return 1; |
0a72c2bd | 2509 | |
992c052c LP |
2510 | null_message: |
2511 | if (r >= 0 && ret) | |
2512 | *ret = NULL; | |
0a72c2bd | 2513 | |
992c052c | 2514 | return r; |
29ddb38f | 2515 | } |
0a72c2bd | 2516 | |
718db961 LP |
2517 | static int process_closing(sd_bus *bus, sd_bus_message **ret) { |
2518 | _cleanup_bus_message_unref_ sd_bus_message *m = NULL; | |
2519 | struct reply_callback *c; | |
2520 | int r; | |
2521 | ||
2522 | assert(bus); | |
2523 | assert(bus->state == BUS_CLOSING); | |
2524 | ||
2525 | c = hashmap_first(bus->reply_callbacks); | |
2526 | if (c) { | |
ebcf1f97 LP |
2527 | _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; |
2528 | ||
718db961 LP |
2529 | /* First, fail all outstanding method calls */ |
2530 | r = bus_message_new_synthetic_error( | |
2531 | bus, | |
693eb9a2 | 2532 | c->cookie, |
14c24659 | 2533 | &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"), |
718db961 LP |
2534 | &m); |
2535 | if (r < 0) | |
2536 | return r; | |
2537 | ||
7adc46fc | 2538 | r = bus_seal_synthetic_message(bus, m); |
718db961 LP |
2539 | if (r < 0) |
2540 | return r; | |
2541 | ||
2542 | if (c->timeout != 0) | |
2543 | prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx); | |
2544 | ||
693eb9a2 | 2545 | hashmap_remove(bus->reply_callbacks, &c->cookie); |
718db961 LP |
2546 | |
2547 | bus->current = m; | |
2548 | bus->iteration_counter++; | |
2549 | ||
ebcf1f97 LP |
2550 | r = c->callback(bus, m, c->userdata, &error_buffer); |
2551 | r = bus_maybe_reply_error(m, r, &error_buffer); | |
718db961 LP |
2552 | free(c); |
2553 | ||
718db961 LP |
2554 | goto finish; |
2555 | } | |
2556 | ||
2557 | /* Then, synthesize a Disconnected message */ | |
2558 | r = sd_bus_message_new_signal( | |
2559 | bus, | |
151b9b96 | 2560 | &m, |
718db961 LP |
2561 | "/org/freedesktop/DBus/Local", |
2562 | "org.freedesktop.DBus.Local", | |
151b9b96 | 2563 | "Disconnected"); |
718db961 LP |
2564 | if (r < 0) |
2565 | return r; | |
2566 | ||
34a2c9e8 LP |
2567 | m->sender = "org.freedesktop.DBus.Local"; |
2568 | ||
7adc46fc | 2569 | r = bus_seal_synthetic_message(bus, m); |
718db961 LP |
2570 | if (r < 0) |
2571 | return r; | |
2572 | ||
2573 | sd_bus_close(bus); | |
2574 | ||
2575 | bus->current = m; | |
2576 | bus->iteration_counter++; | |
2577 | ||
2578 | r = process_filter(bus, m); | |
2579 | if (r != 0) | |
2580 | goto finish; | |
2581 | ||
2582 | r = process_match(bus, m); | |
2583 | if (r != 0) | |
2584 | goto finish; | |
2585 | ||
2586 | if (ret) { | |
2587 | *ret = m; | |
2588 | m = NULL; | |
2589 | } | |
2590 | ||
2591 | r = 1; | |
2592 | ||
2593 | finish: | |
2594 | bus->current = NULL; | |
2595 | return r; | |
2596 | } | |
2597 | ||
766c5809 | 2598 | static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) { |
8ce2afd6 | 2599 | BUS_DONT_DESTROY(bus); |
29ddb38f | 2600 | int r; |
0a72c2bd | 2601 | |
992c052c LP |
2602 | /* Returns 0 when we didn't do anything. This should cause the |
2603 | * caller to invoke sd_bus_wait() before returning the next | |
2604 | * time. Returns > 0 when we did something, which possibly | |
2605 | * means *ret is filled in with an unprocessed message. */ | |
0a72c2bd | 2606 | |
d6888822 LP |
2607 | assert_return(bus, -EINVAL); |
2608 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
0a72c2bd | 2609 | |
992c052c | 2610 | /* We don't allow recursively invoking sd_bus_process(). */ |
718db961 | 2611 | assert_return(!bus->current, -EBUSY); |
0a72c2bd | 2612 | |
992c052c | 2613 | switch (bus->state) { |
0a72c2bd | 2614 | |
992c052c | 2615 | case BUS_UNSET: |
992c052c | 2616 | return -ENOTCONN; |
0a72c2bd | 2617 | |
6d6f4904 LP |
2618 | case BUS_CLOSED: |
2619 | return -ECONNRESET; | |
2620 | ||
992c052c LP |
2621 | case BUS_OPENING: |
2622 | r = bus_socket_process_opening(bus); | |
441d56a1 | 2623 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
718db961 LP |
2624 | bus_enter_closing(bus); |
2625 | r = 1; | |
2626 | } else if (r < 0) | |
29ddb38f | 2627 | return r; |
992c052c LP |
2628 | if (ret) |
2629 | *ret = NULL; | |
a652755d LP |
2630 | return r; |
2631 | ||
992c052c | 2632 | case BUS_AUTHENTICATING: |
992c052c | 2633 | r = bus_socket_process_authenticating(bus); |
441d56a1 | 2634 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
718db961 LP |
2635 | bus_enter_closing(bus); |
2636 | r = 1; | |
2637 | } else if (r < 0) | |
29ddb38f | 2638 | return r; |
718db961 | 2639 | |
992c052c LP |
2640 | if (ret) |
2641 | *ret = NULL; | |
718db961 | 2642 | |
992c052c | 2643 | return r; |
a652755d | 2644 | |
992c052c LP |
2645 | case BUS_RUNNING: |
2646 | case BUS_HELLO: | |
766c5809 | 2647 | r = process_running(bus, hint_priority, priority, ret); |
441d56a1 | 2648 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
718db961 LP |
2649 | bus_enter_closing(bus); |
2650 | r = 1; | |
2651 | ||
2652 | if (ret) | |
2653 | *ret = NULL; | |
2654 | } | |
43a43f50 | 2655 | |
43a43f50 | 2656 | return r; |
718db961 LP |
2657 | |
2658 | case BUS_CLOSING: | |
2659 | return process_closing(bus, ret); | |
992c052c | 2660 | } |
43a43f50 | 2661 | |
992c052c | 2662 | assert_not_reached("Unknown state"); |
e3017af9 LP |
2663 | } |
2664 | ||
766c5809 LP |
2665 | _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { |
2666 | return bus_process_internal(bus, false, 0, ret); | |
2667 | } | |
2668 | ||
2669 | _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) { | |
2670 | return bus_process_internal(bus, true, priority, ret); | |
2671 | } | |
2672 | ||
992c052c LP |
2673 | static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) { |
2674 | struct pollfd p[2] = {}; | |
2675 | int r, e, n; | |
2676 | struct timespec ts; | |
8efd6381 | 2677 | usec_t m = (usec_t) -1; |
adcdb374 LP |
2678 | |
2679 | assert(bus); | |
718db961 LP |
2680 | |
2681 | if (bus->state == BUS_CLOSING) | |
2682 | return 1; | |
2683 | ||
a3d59cd1 LP |
2684 | if (!BUS_IS_OPEN(bus->state)) |
2685 | return -ENOTCONN; | |
adcdb374 | 2686 | |
992c052c LP |
2687 | e = sd_bus_get_events(bus); |
2688 | if (e < 0) | |
2689 | return e; | |
adcdb374 | 2690 | |
992c052c | 2691 | if (need_more) |
8efd6381 LP |
2692 | /* The caller really needs some more data, he doesn't |
2693 | * care about what's already read, or any timeouts | |
2694 | * except its own.*/ | |
992c052c | 2695 | e |= POLLIN; |
992c052c | 2696 | else { |
8efd6381 LP |
2697 | usec_t until; |
2698 | /* The caller wants to process if there's something to | |
2699 | * process, but doesn't care otherwise */ | |
2700 | ||
2701 | r = sd_bus_get_timeout(bus, &until); | |
2702 | if (r < 0) | |
2703 | return r; | |
2704 | if (r > 0) { | |
2705 | usec_t nw; | |
2706 | nw = now(CLOCK_MONOTONIC); | |
2707 | m = until > nw ? until - nw : 0; | |
2708 | } | |
992c052c | 2709 | } |
adcdb374 | 2710 | |
992c052c LP |
2711 | if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m)) |
2712 | m = timeout_usec; | |
adcdb374 | 2713 | |
992c052c LP |
2714 | p[0].fd = bus->input_fd; |
2715 | if (bus->output_fd == bus->input_fd) { | |
2716 | p[0].events = e; | |
2717 | n = 1; | |
2718 | } else { | |
2719 | p[0].events = e & POLLIN; | |
2720 | p[1].fd = bus->output_fd; | |
2721 | p[1].events = e & POLLOUT; | |
2722 | n = 2; | |
adcdb374 LP |
2723 | } |
2724 | ||
992c052c | 2725 | r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL); |
adcdb374 | 2726 | if (r < 0) |
992c052c | 2727 | return -errno; |
adcdb374 | 2728 | |
992c052c | 2729 | return r > 0 ? 1 : 0; |
adcdb374 LP |
2730 | } |
2731 | ||
d9f644e2 | 2732 | _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) { |
9db76355 | 2733 | |
d6888822 | 2734 | assert_return(bus, -EINVAL); |
d6888822 | 2735 | assert_return(!bus_pid_changed(bus), -ECHILD); |
9db76355 | 2736 | |
718db961 LP |
2737 | if (bus->state == BUS_CLOSING) |
2738 | return 0; | |
2739 | ||
a3d59cd1 LP |
2740 | if (!BUS_IS_OPEN(bus->state)) |
2741 | return -ENOTCONN; | |
718db961 | 2742 | |
992c052c LP |
2743 | if (bus->rqueue_size > 0) |
2744 | return 0; | |
9db76355 | 2745 | |
992c052c LP |
2746 | return bus_poll(bus, false, timeout_usec); |
2747 | } | |
9db76355 | 2748 | |
d9f644e2 | 2749 | _public_ int sd_bus_flush(sd_bus *bus) { |
992c052c | 2750 | int r; |
9db76355 | 2751 | |
d6888822 | 2752 | assert_return(bus, -EINVAL); |
d6888822 | 2753 | assert_return(!bus_pid_changed(bus), -ECHILD); |
9db76355 | 2754 | |
718db961 LP |
2755 | if (bus->state == BUS_CLOSING) |
2756 | return 0; | |
2757 | ||
a3d59cd1 LP |
2758 | if (!BUS_IS_OPEN(bus->state)) |
2759 | return -ENOTCONN; | |
718db961 | 2760 | |
992c052c LP |
2761 | r = bus_ensure_running(bus); |
2762 | if (r < 0) | |
2763 | return r; | |
9db76355 | 2764 | |
992c052c LP |
2765 | if (bus->wqueue_size <= 0) |
2766 | return 0; | |
9db76355 | 2767 | |
992c052c LP |
2768 | for (;;) { |
2769 | r = dispatch_wqueue(bus); | |
32f46480 | 2770 | if (r < 0) { |
441d56a1 | 2771 | if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { |
32f46480 | 2772 | bus_enter_closing(bus); |
441d56a1 LP |
2773 | return -ECONNRESET; |
2774 | } | |
32f46480 | 2775 | |
9db76355 | 2776 | return r; |
32f46480 | 2777 | } |
9db76355 | 2778 | |
992c052c LP |
2779 | if (bus->wqueue_size <= 0) |
2780 | return 0; | |
9db76355 | 2781 | |
992c052c | 2782 | r = bus_poll(bus, false, (uint64_t) -1); |
9db76355 LP |
2783 | if (r < 0) |
2784 | return r; | |
9db76355 | 2785 | } |
9db76355 LP |
2786 | } |
2787 | ||
d9f644e2 ZJS |
2788 | _public_ int sd_bus_add_filter(sd_bus *bus, |
2789 | sd_bus_message_handler_t callback, | |
2790 | void *userdata) { | |
2791 | ||
992c052c | 2792 | struct filter_callback *f; |
de1c301e | 2793 | |
d6888822 LP |
2794 | assert_return(bus, -EINVAL); |
2795 | assert_return(callback, -EINVAL); | |
2796 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
de1c301e | 2797 | |
992c052c LP |
2798 | f = new0(struct filter_callback, 1); |
2799 | if (!f) | |
29ddb38f | 2800 | return -ENOMEM; |
992c052c LP |
2801 | f->callback = callback; |
2802 | f->userdata = userdata; | |
29ddb38f | 2803 | |
992c052c | 2804 | bus->filter_callbacks_modified = true; |
71fda00f | 2805 | LIST_PREPEND(callbacks, bus->filter_callbacks, f); |
de1c301e LP |
2806 | return 0; |
2807 | } | |
a652755d | 2808 | |
d9f644e2 ZJS |
2809 | _public_ int sd_bus_remove_filter(sd_bus *bus, |
2810 | sd_bus_message_handler_t callback, | |
2811 | void *userdata) { | |
2812 | ||
992c052c | 2813 | struct filter_callback *f; |
a652755d | 2814 | |
d6888822 LP |
2815 | assert_return(bus, -EINVAL); |
2816 | assert_return(callback, -EINVAL); | |
2817 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
a652755d | 2818 | |
992c052c LP |
2819 | LIST_FOREACH(callbacks, f, bus->filter_callbacks) { |
2820 | if (f->callback == callback && f->userdata == userdata) { | |
2821 | bus->filter_callbacks_modified = true; | |
71fda00f | 2822 | LIST_REMOVE(callbacks, bus->filter_callbacks, f); |
992c052c LP |
2823 | free(f); |
2824 | return 1; | |
29ddb38f LP |
2825 | } |
2826 | } | |
2827 | ||
992c052c | 2828 | return 0; |
a652755d | 2829 | } |
392d5b37 | 2830 | |
d9f644e2 ZJS |
2831 | _public_ int sd_bus_add_match(sd_bus *bus, |
2832 | const char *match, | |
2833 | sd_bus_message_handler_t callback, | |
2834 | void *userdata) { | |
2835 | ||
992c052c LP |
2836 | struct bus_match_component *components = NULL; |
2837 | unsigned n_components = 0; | |
2838 | uint64_t cookie = 0; | |
2839 | int r = 0; | |
392d5b37 | 2840 | |
d6888822 LP |
2841 | assert_return(bus, -EINVAL); |
2842 | assert_return(match, -EINVAL); | |
2843 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
392d5b37 | 2844 | |
992c052c LP |
2845 | r = bus_match_parse(match, &components, &n_components); |
2846 | if (r < 0) | |
2847 | goto finish; | |
29ddb38f | 2848 | |
992c052c LP |
2849 | if (bus->bus_client) { |
2850 | cookie = ++bus->match_cookie; | |
29ddb38f | 2851 | |
992c052c LP |
2852 | r = bus_add_match_internal(bus, match, components, n_components, cookie); |
2853 | if (r < 0) | |
2854 | goto finish; | |
392d5b37 LP |
2855 | } |
2856 | ||
992c052c LP |
2857 | bus->match_callbacks_modified = true; |
2858 | r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL); | |
29ddb38f | 2859 | if (r < 0) { |
992c052c LP |
2860 | if (bus->bus_client) |
2861 | bus_remove_match_internal(bus, match, cookie); | |
29ddb38f | 2862 | } |
917b5dc7 | 2863 | |
992c052c LP |
2864 | finish: |
2865 | bus_match_parse_free(components, n_components); | |
2866 | return r; | |
917b5dc7 LP |
2867 | } |
2868 | ||
d9f644e2 ZJS |
2869 | _public_ int sd_bus_remove_match(sd_bus *bus, |
2870 | const char *match, | |
2871 | sd_bus_message_handler_t callback, | |
2872 | void *userdata) { | |
2873 | ||
992c052c LP |
2874 | struct bus_match_component *components = NULL; |
2875 | unsigned n_components = 0; | |
2876 | int r = 0, q = 0; | |
2877 | uint64_t cookie = 0; | |
917b5dc7 | 2878 | |
d6888822 LP |
2879 | assert_return(bus, -EINVAL); |
2880 | assert_return(match, -EINVAL); | |
2881 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
917b5dc7 | 2882 | |
992c052c | 2883 | r = bus_match_parse(match, &components, &n_components); |
29ddb38f LP |
2884 | if (r < 0) |
2885 | return r; | |
f10dda3b | 2886 | |
992c052c LP |
2887 | bus->match_callbacks_modified = true; |
2888 | r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie); | |
f10dda3b | 2889 | |
992c052c LP |
2890 | if (bus->bus_client) |
2891 | q = bus_remove_match_internal(bus, match, cookie); | |
f10dda3b | 2892 | |
992c052c | 2893 | bus_match_parse_free(components, n_components); |
f10dda3b | 2894 | |
992c052c | 2895 | return r < 0 ? r : q; |
f10dda3b LP |
2896 | } |
2897 | ||
992c052c LP |
2898 | bool bus_pid_changed(sd_bus *bus) { |
2899 | assert(bus); | |
f10dda3b | 2900 | |
992c052c LP |
2901 | /* We don't support people creating a bus connection and |
2902 | * keeping it around over a fork(). Let's complain. */ | |
d5a2b9a6 | 2903 | |
992c052c | 2904 | return bus->original_pid != getpid(); |
d5a2b9a6 | 2905 | } |
40ca29a1 LP |
2906 | |
2907 | static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
0927756b | 2908 | sd_bus *bus = userdata; |
40ca29a1 LP |
2909 | int r; |
2910 | ||
2911 | assert(bus); | |
2912 | ||
2913 | r = sd_bus_process(bus, NULL); | |
2914 | if (r < 0) | |
2915 | return r; | |
2916 | ||
2917 | return 1; | |
2918 | } | |
2919 | ||
2920 | static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { | |
0927756b | 2921 | sd_bus *bus = userdata; |
40ca29a1 LP |
2922 | int r; |
2923 | ||
2924 | assert(bus); | |
2925 | ||
2926 | r = sd_bus_process(bus, NULL); | |
2927 | if (r < 0) | |
2928 | return r; | |
2929 | ||
2930 | return 1; | |
2931 | } | |
2932 | ||
2933 | static int prepare_callback(sd_event_source *s, void *userdata) { | |
2934 | sd_bus *bus = userdata; | |
2935 | int r, e; | |
2936 | usec_t until; | |
2937 | ||
2938 | assert(s); | |
2939 | assert(bus); | |
2940 | ||
2941 | e = sd_bus_get_events(bus); | |
2942 | if (e < 0) | |
2943 | return e; | |
2944 | ||
2945 | if (bus->output_fd != bus->input_fd) { | |
2946 | ||
2947 | r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN); | |
2948 | if (r < 0) | |
2949 | return r; | |
2950 | ||
2951 | r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT); | |
2952 | if (r < 0) | |
2953 | return r; | |
2954 | } else { | |
2955 | r = sd_event_source_set_io_events(bus->input_io_event_source, e); | |
2956 | if (r < 0) | |
2957 | return r; | |
2958 | } | |
2959 | ||
2960 | r = sd_bus_get_timeout(bus, &until); | |
2961 | if (r < 0) | |
2962 | return r; | |
2963 | if (r > 0) { | |
2964 | int j; | |
2965 | ||
2966 | j = sd_event_source_set_time(bus->time_event_source, until); | |
2967 | if (j < 0) | |
2968 | return j; | |
2969 | } | |
2970 | ||
2971 | r = sd_event_source_set_enabled(bus->time_event_source, r > 0); | |
2972 | if (r < 0) | |
2973 | return r; | |
2974 | ||
2975 | return 1; | |
2976 | } | |
2977 | ||
abc5fe72 LP |
2978 | static int quit_callback(sd_event_source *event, void *userdata) { |
2979 | sd_bus *bus = userdata; | |
2980 | ||
2981 | assert(event); | |
2982 | ||
2983 | sd_bus_flush(bus); | |
2984 | ||
2985 | return 1; | |
2986 | } | |
2987 | ||
1e05d493 LP |
2988 | static int attach_io_events(sd_bus *bus) { |
2989 | int r; | |
2990 | ||
2991 | assert(bus); | |
2992 | ||
2993 | if (bus->input_fd < 0) | |
2994 | return 0; | |
2995 | ||
2996 | if (!bus->event) | |
2997 | return 0; | |
2998 | ||
2999 | if (!bus->input_io_event_source) { | |
151b9b96 | 3000 | r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus); |
1e05d493 LP |
3001 | if (r < 0) |
3002 | return r; | |
3003 | ||
3004 | r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback); | |
3005 | if (r < 0) | |
3006 | return r; | |
3007 | ||
3008 | r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority); | |
3009 | } else | |
3010 | r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd); | |
3011 | ||
3012 | if (r < 0) | |
3013 | return r; | |
3014 | ||
3015 | if (bus->output_fd != bus->input_fd) { | |
3016 | assert(bus->output_fd >= 0); | |
3017 | ||
3018 | if (!bus->output_io_event_source) { | |
151b9b96 | 3019 | r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus); |
1e05d493 LP |
3020 | if (r < 0) |
3021 | return r; | |
3022 | ||
3023 | r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority); | |
3024 | } else | |
3025 | r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd); | |
3026 | ||
3027 | if (r < 0) | |
3028 | return r; | |
3029 | } | |
3030 | ||
3031 | return 0; | |
3032 | } | |
3033 | ||
3034 | static void detach_io_events(sd_bus *bus) { | |
3035 | assert(bus); | |
3036 | ||
3037 | if (bus->input_io_event_source) { | |
3038 | sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF); | |
3039 | bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source); | |
3040 | } | |
3041 | ||
3042 | if (bus->output_io_event_source) { | |
3043 | sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF); | |
3044 | bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source); | |
3045 | } | |
3046 | } | |
3047 | ||
d9f644e2 | 3048 | _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) { |
40ca29a1 LP |
3049 | int r; |
3050 | ||
3051 | assert_return(bus, -EINVAL); | |
40ca29a1 LP |
3052 | assert_return(!bus->event, -EBUSY); |
3053 | ||
3054 | assert(!bus->input_io_event_source); | |
3055 | assert(!bus->output_io_event_source); | |
3056 | assert(!bus->time_event_source); | |
3057 | ||
76b54375 LP |
3058 | if (event) |
3059 | bus->event = sd_event_ref(event); | |
3060 | else { | |
3061 | r = sd_event_default(&bus->event); | |
3062 | if (r < 0) | |
3063 | return r; | |
3064 | } | |
40ca29a1 | 3065 | |
1e05d493 | 3066 | bus->event_priority = priority; |
40ca29a1 | 3067 | |
6a0f1f6d | 3068 | r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus); |
40ca29a1 LP |
3069 | if (r < 0) |
3070 | goto fail; | |
3071 | ||
3072 | r = sd_event_source_set_priority(bus->time_event_source, priority); | |
3073 | if (r < 0) | |
3074 | goto fail; | |
3075 | ||
151b9b96 | 3076 | r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus); |
abc5fe72 LP |
3077 | if (r < 0) |
3078 | goto fail; | |
3079 | ||
1e05d493 LP |
3080 | r = attach_io_events(bus); |
3081 | if (r < 0) | |
3082 | goto fail; | |
3083 | ||
40ca29a1 LP |
3084 | return 0; |
3085 | ||
3086 | fail: | |
3087 | sd_bus_detach_event(bus); | |
3088 | return r; | |
3089 | } | |
3090 | ||
d9f644e2 | 3091 | _public_ int sd_bus_detach_event(sd_bus *bus) { |
40ca29a1 | 3092 | assert_return(bus, -EINVAL); |
a82cafb9 LP |
3093 | |
3094 | if (!bus->event) | |
3095 | return 0; | |
40ca29a1 | 3096 | |
1e05d493 | 3097 | detach_io_events(bus); |
40ca29a1 | 3098 | |
86befb40 LP |
3099 | if (bus->time_event_source) { |
3100 | sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF); | |
40ca29a1 | 3101 | bus->time_event_source = sd_event_source_unref(bus->time_event_source); |
86befb40 | 3102 | } |
40ca29a1 | 3103 | |
86befb40 LP |
3104 | if (bus->quit_event_source) { |
3105 | sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF); | |
abc5fe72 | 3106 | bus->quit_event_source = sd_event_source_unref(bus->quit_event_source); |
86befb40 | 3107 | } |
abc5fe72 | 3108 | |
40ca29a1 LP |
3109 | if (bus->event) |
3110 | bus->event = sd_event_unref(bus->event); | |
3111 | ||
a82cafb9 | 3112 | return 1; |
40ca29a1 | 3113 | } |
affff0b6 | 3114 | |
2be44176 LP |
3115 | _public_ sd_event* sd_bus_get_event(sd_bus *bus) { |
3116 | assert_return(bus, NULL); | |
3117 | ||
3118 | return bus->event; | |
3119 | } | |
3120 | ||
76b54375 | 3121 | _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) { |
affff0b6 LP |
3122 | assert_return(bus, NULL); |
3123 | ||
3124 | return bus->current; | |
3125 | } | |
76b54375 LP |
3126 | |
3127 | static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) { | |
3128 | sd_bus *b = NULL; | |
3129 | int r; | |
3130 | ||
3131 | assert(bus_open); | |
3132 | assert(default_bus); | |
3133 | ||
3134 | if (!ret) | |
3135 | return !!*default_bus; | |
3136 | ||
3137 | if (*default_bus) { | |
3138 | *ret = sd_bus_ref(*default_bus); | |
3139 | return 0; | |
3140 | } | |
3141 | ||
3142 | r = bus_open(&b); | |
3143 | if (r < 0) | |
3144 | return r; | |
3145 | ||
3146 | b->default_bus_ptr = default_bus; | |
3147 | b->tid = gettid(); | |
3148 | *default_bus = b; | |
3149 | ||
3150 | *ret = b; | |
3151 | return 1; | |
3152 | } | |
3153 | ||
3154 | _public_ int sd_bus_default_system(sd_bus **ret) { | |
ec202eae | 3155 | static thread_local sd_bus *default_system_bus = NULL; |
76b54375 LP |
3156 | |
3157 | return bus_default(sd_bus_open_system, &default_system_bus, ret); | |
3158 | } | |
3159 | ||
3160 | _public_ int sd_bus_default_user(sd_bus **ret) { | |
ec202eae | 3161 | static thread_local sd_bus *default_user_bus = NULL; |
76b54375 LP |
3162 | |
3163 | return bus_default(sd_bus_open_user, &default_user_bus, ret); | |
3164 | } | |
3165 | ||
af08d2f9 LP |
3166 | _public_ int sd_bus_default(sd_bus **ret) { |
3167 | ||
3168 | const char *e; | |
3169 | ||
3170 | /* Let's try our best to reuse another cached connection. If | |
3171 | * the starter bus type is set, connect via our normal | |
3172 | * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that | |
3173 | * we can share the connection with the user/system default | |
3174 | * bus. */ | |
3175 | ||
3176 | e = secure_getenv("DBUS_STARTER_BUS_TYPE"); | |
3177 | if (e) { | |
3178 | if (streq(e, "system")) | |
3179 | return sd_bus_default_system(ret); | |
09365592 | 3180 | else if (STR_IN_SET(e, "user", "session")) |
af08d2f9 LP |
3181 | return sd_bus_default_user(ret); |
3182 | } | |
3183 | ||
3184 | /* No type is specified, so we have not other option than to | |
3185 | * use the starter address if it is set. */ | |
3186 | ||
3187 | e = secure_getenv("DBUS_STARTER_ADDRESS"); | |
3188 | if (e) { | |
3189 | static thread_local sd_bus *default_starter_bus = NULL; | |
3190 | ||
3191 | return bus_default(sd_bus_open, &default_starter_bus, ret); | |
3192 | } | |
3193 | ||
3194 | /* Finally, if nothing is set use the cached connection for | |
3195 | * the right scope */ | |
3196 | ||
3197 | if (cg_pid_get_owner_uid(0, NULL) >= 0) | |
3198 | return sd_bus_default_user(ret); | |
3199 | else | |
3200 | return sd_bus_default_system(ret); | |
3201 | } | |
3202 | ||
76b54375 LP |
3203 | _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { |
3204 | assert_return(b, -EINVAL); | |
3205 | assert_return(tid, -EINVAL); | |
3206 | assert_return(!bus_pid_changed(b), -ECHILD); | |
3207 | ||
3208 | if (b->tid != 0) { | |
3209 | *tid = b->tid; | |
3210 | return 0; | |
3211 | } | |
3212 | ||
3213 | if (b->event) | |
3214 | return sd_event_get_tid(b->event, tid); | |
3215 | ||
3216 | return -ENXIO; | |
3217 | } | |
28383ba1 | 3218 | |
a6278b88 LP |
3219 | _public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) { |
3220 | _cleanup_free_ char *e = NULL; | |
3221 | char *ret; | |
3222 | ||
3223 | assert_return(object_path_is_valid(prefix), -EINVAL); | |
3224 | assert_return(external_id, -EINVAL); | |
3225 | assert_return(ret_path, -EINVAL); | |
3226 | ||
3227 | e = bus_label_escape(external_id); | |
3228 | if (!e) | |
3229 | return -ENOMEM; | |
3230 | ||
3231 | ret = strjoin(prefix, "/", e, NULL); | |
3232 | if (!ret) | |
3233 | return -ENOMEM; | |
3234 | ||
3235 | *ret_path = ret; | |
3236 | return 0; | |
28383ba1 LP |
3237 | } |
3238 | ||
a6278b88 LP |
3239 | _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) { |
3240 | const char *e; | |
3241 | char *ret; | |
3242 | ||
3243 | assert_return(object_path_is_valid(path), -EINVAL); | |
3244 | assert_return(object_path_is_valid(prefix), -EINVAL); | |
3245 | assert_return(external_id, -EINVAL); | |
3246 | ||
3247 | e = object_path_startswith(path, prefix); | |
3248 | if (!e) { | |
3249 | *external_id = NULL; | |
3250 | return 0; | |
3251 | } | |
3252 | ||
3253 | ret = bus_label_unescape(e); | |
3254 | if (!ret) | |
3255 | return -ENOMEM; | |
3256 | ||
3257 | *external_id = ret; | |
3258 | return 1; | |
28383ba1 | 3259 | } |
5b12334d LP |
3260 | |
3261 | _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) { | |
3262 | sd_bus_creds *c; | |
3263 | pid_t pid = 0; | |
3264 | int r; | |
3265 | ||
3266 | assert_return(bus, -EINVAL); | |
95c4fe82 | 3267 | assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP); |
5b12334d | 3268 | assert_return(ret, -EINVAL); |
5b12334d | 3269 | assert_return(!bus_pid_changed(bus), -ECHILD); |
a3d59cd1 LP |
3270 | |
3271 | if (!bus->is_kernel) | |
3272 | return -ENOTSUP; | |
3273 | ||
3274 | if (!BUS_IS_OPEN(bus->state)) | |
3275 | return -ENOTCONN; | |
5b12334d LP |
3276 | |
3277 | if (!bus->ucred_valid && !isempty(bus->label)) | |
3278 | return -ENODATA; | |
3279 | ||
3280 | c = bus_creds_new(); | |
3281 | if (!c) | |
3282 | return -ENOMEM; | |
3283 | ||
3284 | if (bus->ucred_valid) { | |
3285 | pid = c->pid = bus->ucred.pid; | |
3286 | c->uid = bus->ucred.uid; | |
3287 | c->gid = bus->ucred.gid; | |
3288 | ||
3310dfd5 | 3289 | c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask; |
5b12334d LP |
3290 | } |
3291 | ||
3292 | if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) { | |
3293 | c->label = strdup(bus->label); | |
3294 | if (!c->label) { | |
3295 | sd_bus_creds_unref(c); | |
3296 | return -ENOMEM; | |
3297 | } | |
3298 | ||
3310dfd5 | 3299 | c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT; |
5b12334d LP |
3300 | } |
3301 | ||
3302 | r = bus_creds_add_more(c, mask, pid, 0); | |
3303 | if (r < 0) | |
3304 | return r; | |
3305 | ||
3306 | *ret = c; | |
3307 | return 0; | |
3308 | } | |
ae095f86 LP |
3309 | |
3310 | _public_ int sd_bus_try_close(sd_bus *bus) { | |
3311 | int r; | |
3312 | ||
3313 | assert_return(bus, -EINVAL); | |
ae095f86 | 3314 | assert_return(!bus_pid_changed(bus), -ECHILD); |
a3d59cd1 LP |
3315 | |
3316 | if (!bus->is_kernel) | |
3317 | return -ENOTSUP; | |
3318 | ||
3319 | if (!BUS_IS_OPEN(bus->state)) | |
3320 | return -ENOTCONN; | |
ae095f86 | 3321 | |
bd746b8b LP |
3322 | if (bus->rqueue_size > 0) |
3323 | return -EBUSY; | |
3324 | ||
14f862a5 LP |
3325 | if (bus->wqueue_size > 0) |
3326 | return -EBUSY; | |
3327 | ||
ae095f86 LP |
3328 | r = bus_kernel_try_close(bus); |
3329 | if (r < 0) | |
3330 | return r; | |
3331 | ||
3332 | sd_bus_close(bus); | |
3333 | return 0; | |
3334 | } | |
5972fe95 LP |
3335 | |
3336 | _public_ int sd_bus_get_name(sd_bus *bus, const char **name) { | |
3337 | assert_return(bus, -EINVAL); | |
3338 | assert_return(name, -EINVAL); | |
3339 | assert_return(!bus_pid_changed(bus), -ECHILD); | |
3340 | ||
3341 | *name = bus->connection_name; | |
3342 | return 0; | |
3343 | } |