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