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