]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bus-util.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / shared / bus-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
40ca29a1 2
a8fbdf54
TA
3#include <errno.h>
4#include <fcntl.h>
5#include <inttypes.h>
a8fbdf54 6#include <stdlib.h>
a8fbdf54
TA
7#include <sys/ioctl.h>
8#include <sys/resource.h>
0c842e0a 9#include <sys/socket.h>
a8fbdf54 10#include <unistd.h>
0c842e0a 11
4f5dd394 12#include "sd-bus.h"
ebd011d9
LP
13#include "sd-daemon.h"
14#include "sd-event.h"
a8fbdf54 15#include "sd-id128.h"
d53d9474 16
73d3ac8e 17#include "bus-common-errors.h"
d53d9474
LP
18#include "bus-internal.h"
19#include "bus-label.h"
3ffd4af2 20#include "bus-util.h"
657ee2d8 21#include "path-util.h"
269e4d2d 22#include "socket-util.h"
15a5e950 23#include "stdio-util.h"
40ca29a1 24
19070062 25static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
99534007 26 sd_event *e = ASSERT_PTR(userdata);
40ca29a1 27
40ca29a1 28 assert(m);
40ca29a1 29
19070062 30 sd_bus_close(sd_bus_message_get_bus(m));
6203e07a 31 sd_event_exit(e, 0);
b27adf35 32
40ca29a1
LP
33 return 1;
34}
35
fb293b3c
ZJS
36int bus_log_address_error(int r, BusTransport transport) {
37 bool hint = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM;
38
237fbb67 39 return log_error_errno(r,
fb293b3c
ZJS
40 hint ? "Failed to set bus address: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" :
41 "Failed to set bus address: %m");
237fbb67
ZJS
42}
43
10a7340a
ZJS
44int bus_log_connect_error(int r, BusTransport transport) {
45 bool hint_vars = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM,
46 hint_addr = transport == BUS_TRANSPORT_LOCAL && ERRNO_IS_PRIVILEGE(r);
47
237fbb67 48 return log_error_errno(r,
10a7340a
ZJS
49 r == hint_vars ? "Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" :
50 r == hint_addr ? "Failed to connect to bus: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)" :
51 "Failed to connect to bus: %m");
237fbb67
ZJS
52}
53
6203e07a 54int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
75152a4d 55 const char *match;
11846aa7 56 const char *unique;
40ca29a1
LP
57 int r;
58
59 assert(e);
60 assert(bus);
61 assert(name);
62
6203e07a
LP
63 /* We unregister the name here and then wait for the
64 * NameOwnerChanged signal for this event to arrive before we
65 * quit. We do this in order to make sure that any queued
66 * requests are still processed before we really exit. */
67
11846aa7 68 r = sd_bus_get_unique_name(bus, &unique);
40ca29a1
LP
69 if (r < 0)
70 return r;
71
75152a4d
LP
72 match = strjoina(
73 "sender='org.freedesktop.DBus',"
74 "type='signal',"
75 "interface='org.freedesktop.DBus',"
76 "member='NameOwnerChanged',"
77 "path='/org/freedesktop/DBus',"
78 "arg0='", name, "',",
79 "arg1='", unique, "',",
80 "arg2=''");
81
82 r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e);
40ca29a1
LP
83 if (r < 0)
84 return r;
85
75152a4d 86 r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL);
40ca29a1
LP
87 if (r < 0)
88 return r;
89
40ca29a1
LP
90 return 0;
91}
92
37224a5f
LP
93int bus_event_loop_with_idle(
94 sd_event *e,
95 sd_bus *bus,
96 const char *name,
97 usec_t timeout,
98 check_idle_t check_idle,
99 void *userdata) {
40ca29a1 100 bool exiting = false;
6203e07a 101 int r, code;
40ca29a1
LP
102
103 assert(e);
104 assert(bus);
105 assert(name);
106
107 for (;;) {
37224a5f
LP
108 bool idle;
109
40ca29a1
LP
110 r = sd_event_get_state(e);
111 if (r < 0)
112 return r;
40ca29a1
LP
113 if (r == SD_EVENT_FINISHED)
114 break;
115
37224a5f
LP
116 if (check_idle)
117 idle = check_idle(userdata);
118 else
119 idle = true;
120
f5fbe71d 121 r = sd_event_run(e, exiting || !idle ? UINT64_MAX : timeout);
40ca29a1
LP
122 if (r < 0)
123 return r;
124
a8ba6cd1 125 if (r == 0 && !exiting && idle) {
99cde098
ZJS
126 /* Inform the service manager that we are going down, so that it will queue all
127 * further start requests, instead of assuming we are already running. */
128 sd_notify(false, "STOPPING=1");
b27adf35 129
99cde098 130 r = bus_async_unregister_and_exit(e, bus, name);
40ca29a1
LP
131 if (r < 0)
132 return r;
133
99cde098
ZJS
134 exiting = true;
135 continue;
40ca29a1
LP
136 }
137 }
138
6203e07a
LP
139 r = sd_event_get_exit_code(e, &code);
140 if (r < 0)
141 return r;
142
143 return code;
40ca29a1
LP
144}
145
5fd38859 146int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
4afd3348 147 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
5fd38859
DH
148 int r, has_owner = 0;
149
150 assert(c);
151 assert(name);
152
153 r = sd_bus_call_method(c,
154 "org.freedesktop.DBus",
155 "/org/freedesktop/dbus",
156 "org.freedesktop.DBus",
157 "NameHasOwner",
158 error,
159 &rep,
160 "s",
161 name);
162 if (r < 0)
163 return r;
164
165 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
166 if (r < 0)
167 return sd_bus_error_set_errno(error, r);
168
169 return has_owner;
170}
171
73d3ac8e
ZJS
172bool bus_error_is_unknown_service(const sd_bus_error *error) {
173 return sd_bus_error_has_names(error,
174 SD_BUS_ERROR_SERVICE_UNKNOWN,
175 SD_BUS_ERROR_NAME_HAS_NO_OWNER,
176 BUS_ERROR_NO_SUCH_UNIT);
177}
178
718db961 179int bus_check_peercred(sd_bus *c) {
0c842e0a 180 struct ucred ucred;
3e641e36 181 int fd, r;
0c842e0a
TG
182
183 assert(c);
184
185 fd = sd_bus_get_fd(c);
0f8bd8de
LP
186 if (fd < 0)
187 return fd;
0c842e0a 188
3e641e36
LP
189 r = getpeercred(fd, &ucred);
190 if (r < 0)
191 return r;
0c842e0a
TG
192
193 if (ucred.uid != 0 && ucred.uid != geteuid())
194 return -EPERM;
195
196 return 1;
197}
198
c90821aa 199int bus_connect_system_systemd(sd_bus **ret_bus) {
b1a4981a 200 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
0c842e0a 201 int r;
0c842e0a 202
c90821aa 203 assert(ret_bus);
0c842e0a 204
0f8bd8de 205 if (geteuid() != 0)
c90821aa 206 return sd_bus_default_system(ret_bus);
a1da8583 207
a132bef0
ZJS
208 /* If we are root then let's talk directly to the system
209 * instance, instead of going via the bus */
a6aa8912
LP
210
211 r = sd_bus_new(&bus);
0f8bd8de
LP
212 if (r < 0)
213 return r;
a1da8583 214
a6aa8912
LP
215 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
216 if (r < 0)
217 return r;
218
219 r = sd_bus_start(bus);
220 if (r < 0)
c90821aa 221 return sd_bus_default_system(ret_bus);
a6aa8912 222
0f8bd8de 223 r = bus_check_peercred(bus);
a1da8583
TG
224 if (r < 0)
225 return r;
226
c90821aa 227 *ret_bus = TAKE_PTR(bus);
a1da8583
TG
228 return 0;
229}
230
c90821aa 231int bus_connect_user_systemd(sd_bus **ret_bus) {
b1a4981a 232 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
a6aa8912 233 _cleanup_free_ char *ee = NULL;
41dd15e4
LP
234 const char *e;
235 int r;
236
c90821aa 237 assert(ret_bus);
41dd15e4
LP
238
239 e = secure_getenv("XDG_RUNTIME_DIR");
537220d9 240 if (!e)
c90821aa 241 return sd_bus_default_user(ret_bus);
537220d9 242
a6aa8912
LP
243 ee = bus_address_escape(e);
244 if (!ee)
537220d9 245 return -ENOMEM;
41dd15e4
LP
246
247 r = sd_bus_new(&bus);
248 if (r < 0)
249 return r;
250
605405c6 251 bus->address = strjoin("unix:path=", ee, "/systemd/private");
a6aa8912
LP
252 if (!bus->address)
253 return -ENOMEM;
41dd15e4
LP
254
255 r = sd_bus_start(bus);
256 if (r < 0)
c90821aa 257 return sd_bus_default_user(ret_bus);
41dd15e4
LP
258
259 r = bus_check_peercred(bus);
260 if (r < 0)
261 return r;
262
c90821aa 263 *ret_bus = TAKE_PTR(bus);
41dd15e4
LP
264 return 0;
265}
266
1b630835
LP
267int bus_connect_transport(
268 BusTransport transport,
269 const char *host,
270 bool user,
271 sd_bus **ret) {
272
b1a4981a 273 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
d21ed1ea
LP
274 int r;
275
276 assert(transport >= 0);
277 assert(transport < _BUS_TRANSPORT_MAX);
38303498 278 assert(ret);
d21ed1ea
LP
279
280 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1b630835 281 assert_return(transport != BUS_TRANSPORT_REMOTE || !user, -EOPNOTSUPP);
d21ed1ea
LP
282
283 switch (transport) {
284
285 case BUS_TRANSPORT_LOCAL:
286 if (user)
38303498 287 r = sd_bus_default_user(&bus);
fb507898 288 else {
d7a0f1f4 289 if (sd_booted() <= 0)
fb507898 290 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
d7a0f1f4
FS
291 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
292 "System has not been booted with systemd as init system (PID 1). Can't operate.");
fb507898
YW
293 r = sd_bus_default_system(&bus);
294 }
d21ed1ea
LP
295 break;
296
297 case BUS_TRANSPORT_REMOTE:
38303498 298 r = sd_bus_open_system_remote(&bus, host);
41dd15e4
LP
299 break;
300
de33fc62 301 case BUS_TRANSPORT_MACHINE:
1b630835
LP
302 if (user)
303 r = sd_bus_open_user_machine(&bus, host);
304 else
305 r = sd_bus_open_system_machine(&bus, host);
41dd15e4
LP
306 break;
307
308 default:
04499a70 309 assert_not_reached();
41dd15e4 310 }
38303498
LP
311 if (r < 0)
312 return r;
41dd15e4 313
38303498
LP
314 r = sd_bus_set_exit_on_disconnect(bus, true);
315 if (r < 0)
316 return r;
317
1cc6c93a 318 *ret = TAKE_PTR(bus);
38303498 319 return 0;
41dd15e4
LP
320}
321
266f3e26 322int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
41dd15e4
LP
323 assert(transport >= 0);
324 assert(transport < _BUS_TRANSPORT_MAX);
325 assert(bus);
326
327 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
15411c0c 328 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
41dd15e4
LP
329
330 switch (transport) {
331
332 case BUS_TRANSPORT_LOCAL:
333 if (user)
73e91092
ZJS
334 return bus_connect_user_systemd(bus);
335
336 if (sd_booted() <= 0)
337 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
338 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
339 "System has not been booted with systemd as init system (PID 1). Can't operate.");
340 return bus_connect_system_systemd(bus);
41dd15e4
LP
341
342 case BUS_TRANSPORT_REMOTE:
73e91092 343 return sd_bus_open_system_remote(bus, host);
d21ed1ea 344
de33fc62 345 case BUS_TRANSPORT_MACHINE:
73e91092 346 return sd_bus_open_system_machine(bus, host);
d21ed1ea
LP
347
348 default:
04499a70 349 assert_not_reached();
d21ed1ea 350 }
d21ed1ea 351}
e6504030 352
98a4c30b
DH
353/**
354 * bus_path_encode_unique() - encode unique object path
355 * @b: bus connection or NULL
356 * @prefix: object path prefix
357 * @sender_id: unique-name of client, or NULL
358 * @external_id: external ID to be chosen by client, or NULL
359 * @ret_path: storage for encoded object path pointer
360 *
361 * Whenever we provide a bus API that allows clients to create and manage
362 * server-side objects, we need to provide a unique name for these objects. If
363 * we let the server choose the name, we suffer from a race condition: If a
364 * client creates an object asynchronously, it cannot destroy that object until
365 * it received the method reply. It cannot know the name of the new object,
366 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
367 *
368 * Therefore, many APIs allow the client to choose the unique name for newly
369 * created objects. There're two problems to solve, though:
370 * 1) Object names are usually defined via dbus object paths, which are
371 * usually globally namespaced. Therefore, multiple clients must be able
372 * to choose unique object names without interference.
373 * 2) If multiple libraries share the same bus connection, they must be
374 * able to choose unique object names without interference.
375 * The first problem is solved easily by prefixing a name with the
376 * unique-bus-name of a connection. The server side must enforce this and
377 * reject any other name. The second problem is solved by providing unique
378 * suffixes from within sd-bus.
379 *
380 * This helper allows clients to create unique object-paths. It uses the
381 * template '/prefix/sender_id/external_id' and returns the new path in
382 * @ret_path (must be freed by the caller).
383 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
384 * NULL, this function allocates a unique suffix via @b (by requesting a new
385 * cookie). If both @sender_id and @external_id are given, @b can be passed as
386 * NULL.
387 *
388 * Returns: 0 on success, negative error code on failure.
389 */
390int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
391 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
392 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
393 int r;
394
395 assert_return(b || (sender_id && external_id), -EINVAL);
5453a4b1 396 assert_return(sd_bus_object_path_is_valid(prefix), -EINVAL);
98a4c30b
DH
397 assert_return(ret_path, -EINVAL);
398
399 if (!sender_id) {
400 r = sd_bus_get_unique_name(b, &sender_id);
401 if (r < 0)
402 return r;
403 }
404
405 if (!external_id) {
406 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
407 external_id = external_buf;
408 }
409
410 sender_label = bus_label_escape(sender_id);
411 if (!sender_label)
412 return -ENOMEM;
413
414 external_label = bus_label_escape(external_id);
415 if (!external_label)
416 return -ENOMEM;
417
657ee2d8 418 p = path_join(prefix, sender_label, external_label);
98a4c30b
DH
419 if (!p)
420 return -ENOMEM;
421
422 *ret_path = p;
423 return 0;
424}
425
426/**
427 * bus_path_decode_unique() - decode unique object path
428 * @path: object path to decode
429 * @prefix: object path prefix
430 * @ret_sender: output parameter for sender-id label
431 * @ret_external: output parameter for external-id label
432 *
433 * This does the reverse of bus_path_encode_unique() (see its description for
434 * details). Both trailing labels, sender-id and external-id, are unescaped and
435 * returned in the given output parameters (the caller must free them).
436 *
437 * Note that this function returns 0 if the path does not match the template
438 * (see bus_path_encode_unique()), 1 if it matched.
439 *
440 * Returns: Negative error code on failure, 0 if the given object path does not
441 * match the template (return parameters are set to NULL), 1 if it was
442 * parsed successfully (return parameters contain allocated labels).
443 */
444int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
445 const char *p, *q;
446 char *sender, *external;
447
5453a4b1
ZJS
448 assert(sd_bus_object_path_is_valid(path));
449 assert(sd_bus_object_path_is_valid(prefix));
98a4c30b
DH
450 assert(ret_sender);
451 assert(ret_external);
452
453 p = object_path_startswith(path, prefix);
454 if (!p) {
455 *ret_sender = NULL;
456 *ret_external = NULL;
457 return 0;
458 }
459
460 q = strchr(p, '/');
461 if (!q) {
462 *ret_sender = NULL;
463 *ret_external = NULL;
464 return 0;
465 }
466
467 sender = bus_label_unescape_n(p, q - p);
468 external = bus_label_unescape(q + 1);
469 if (!sender || !external) {
470 free(sender);
471 free(external);
472 return -ENOMEM;
473 }
474
475 *ret_sender = sender;
476 *ret_external = external;
477 return 1;
478}
057171ef 479
984794ba
LP
480int bus_track_add_name_many(sd_bus_track *t, char **l) {
481 int r = 0;
984794ba
LP
482
483 assert(t);
484
485 /* Continues adding after failure, and returns the first failure. */
486
487 STRV_FOREACH(i, l) {
488 int k;
489
490 k = sd_bus_track_add_name(t, *i);
491 if (k < 0 && r >= 0)
492 r = k;
493 }
494
495 return r;
496}
d7afd945 497
0ddf50ff 498int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
b1a4981a 499 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
d7afd945
LP
500 const char *e;
501 int r;
502
503 assert(ret);
504
61252bae
ZJS
505 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
506 * turned on. */
d7afd945
LP
507
508 r = sd_bus_new(&bus);
509 if (r < 0)
510 return r;
511
0ddf50ff
YW
512 if (description) {
513 r = sd_bus_set_description(bus, description);
514 if (r < 0)
515 return r;
516 }
517
d7afd945
LP
518 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
519 if (!e)
520 e = DEFAULT_SYSTEM_BUS_ADDRESS;
521
522 r = sd_bus_set_address(bus, e);
523 if (r < 0)
524 return r;
525
526 r = sd_bus_set_bus_client(bus, true);
527 if (r < 0)
528 return r;
529
d7afd945
LP
530 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
531 if (r < 0)
532 return r;
533
534 r = sd_bus_set_watch_bind(bus, true);
535 if (r < 0)
536 return r;
537
538 r = sd_bus_set_connected_signal(bus, true);
539 if (r < 0)
540 return r;
541
542 r = sd_bus_start(bus);
543 if (r < 0)
544 return r;
545
1cc6c93a 546 *ret = TAKE_PTR(bus);
d7afd945
LP
547
548 return 0;
549}
906cb2eb 550
19017acb
LP
551int bus_reply_pair_array(sd_bus_message *m, char **l) {
552 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
19017acb
LP
553 int r;
554
555 assert(m);
556
61252bae
ZJS
557 /* Reply to the specified message with a message containing a dictionary put together from the
558 * specified strv */
19017acb
LP
559
560 r = sd_bus_message_new_method_return(m, &reply);
561 if (r < 0)
562 return r;
563
564 r = sd_bus_message_open_container(reply, 'a', "{ss}");
565 if (r < 0)
566 return r;
567
568 STRV_FOREACH_PAIR(k, v, l) {
569 r = sd_bus_message_append(reply, "{ss}", *k, *v);
570 if (r < 0)
571 return r;
572 }
573
574 r = sd_bus_message_close_container(reply);
575 if (r < 0)
576 return r;
577
578 return sd_bus_send(NULL, reply, NULL);
579}
2a66c2a1
LP
580
581static void bus_message_unref_wrapper(void *m) {
582 sd_bus_message_unref(m);
583}
584
585const struct hash_ops bus_message_hash_ops = {
586 .hash = trivial_hash_func,
587 .compare = trivial_compare_func,
588 .free_value = bus_message_unref_wrapper,
589};