]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bus-util.c
Merge pull request #21209 from yuwata/veth-peer-mtu
[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
f5fbe71d 104 r = sd_event_run(e, exiting || !idle ? UINT64_MAX : 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
1b630835
LP
252int bus_connect_transport(
253 BusTransport transport,
254 const char *host,
255 bool user,
256 sd_bus **ret) {
257
b1a4981a 258 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
d21ed1ea
LP
259 int r;
260
261 assert(transport >= 0);
262 assert(transport < _BUS_TRANSPORT_MAX);
38303498 263 assert(ret);
d21ed1ea
LP
264
265 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1b630835 266 assert_return(transport != BUS_TRANSPORT_REMOTE || !user, -EOPNOTSUPP);
d21ed1ea
LP
267
268 switch (transport) {
269
270 case BUS_TRANSPORT_LOCAL:
271 if (user)
38303498 272 r = sd_bus_default_user(&bus);
fb507898 273 else {
d7a0f1f4 274 if (sd_booted() <= 0)
fb507898 275 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
d7a0f1f4
FS
276 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
277 "System has not been booted with systemd as init system (PID 1). Can't operate.");
fb507898
YW
278 r = sd_bus_default_system(&bus);
279 }
d21ed1ea
LP
280 break;
281
282 case BUS_TRANSPORT_REMOTE:
38303498 283 r = sd_bus_open_system_remote(&bus, host);
41dd15e4
LP
284 break;
285
de33fc62 286 case BUS_TRANSPORT_MACHINE:
1b630835
LP
287 if (user)
288 r = sd_bus_open_user_machine(&bus, host);
289 else
290 r = sd_bus_open_system_machine(&bus, host);
41dd15e4
LP
291 break;
292
293 default:
04499a70 294 assert_not_reached();
41dd15e4 295 }
38303498
LP
296 if (r < 0)
297 return r;
41dd15e4 298
38303498
LP
299 r = sd_bus_set_exit_on_disconnect(bus, true);
300 if (r < 0)
301 return r;
302
1cc6c93a 303 *ret = TAKE_PTR(bus);
38303498 304 return 0;
41dd15e4
LP
305}
306
266f3e26 307int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
41dd15e4
LP
308 int r;
309
310 assert(transport >= 0);
311 assert(transport < _BUS_TRANSPORT_MAX);
312 assert(bus);
313
314 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
15411c0c 315 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
41dd15e4
LP
316
317 switch (transport) {
318
319 case BUS_TRANSPORT_LOCAL:
320 if (user)
266f3e26 321 r = bus_connect_user_systemd(bus);
fb507898 322 else {
baaa35ad 323 if (sd_booted() <= 0)
fb507898 324 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
baaa35ad
ZJS
325 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
326 "System has not been booted with systemd as init system (PID 1). Can't operate.");
fb507898
YW
327 r = bus_connect_system_systemd(bus);
328 }
41dd15e4
LP
329 break;
330
331 case BUS_TRANSPORT_REMOTE:
3db729cb 332 r = sd_bus_open_system_remote(bus, host);
d21ed1ea
LP
333 break;
334
de33fc62
LP
335 case BUS_TRANSPORT_MACHINE:
336 r = sd_bus_open_system_machine(bus, host);
d21ed1ea
LP
337 break;
338
339 default:
04499a70 340 assert_not_reached();
d21ed1ea
LP
341 }
342
343 return r;
344}
e6504030 345
98a4c30b
DH
346/**
347 * bus_path_encode_unique() - encode unique object path
348 * @b: bus connection or NULL
349 * @prefix: object path prefix
350 * @sender_id: unique-name of client, or NULL
351 * @external_id: external ID to be chosen by client, or NULL
352 * @ret_path: storage for encoded object path pointer
353 *
354 * Whenever we provide a bus API that allows clients to create and manage
355 * server-side objects, we need to provide a unique name for these objects. If
356 * we let the server choose the name, we suffer from a race condition: If a
357 * client creates an object asynchronously, it cannot destroy that object until
358 * it received the method reply. It cannot know the name of the new object,
359 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
360 *
361 * Therefore, many APIs allow the client to choose the unique name for newly
362 * created objects. There're two problems to solve, though:
363 * 1) Object names are usually defined via dbus object paths, which are
364 * usually globally namespaced. Therefore, multiple clients must be able
365 * to choose unique object names without interference.
366 * 2) If multiple libraries share the same bus connection, they must be
367 * able to choose unique object names without interference.
368 * The first problem is solved easily by prefixing a name with the
369 * unique-bus-name of a connection. The server side must enforce this and
370 * reject any other name. The second problem is solved by providing unique
371 * suffixes from within sd-bus.
372 *
373 * This helper allows clients to create unique object-paths. It uses the
374 * template '/prefix/sender_id/external_id' and returns the new path in
375 * @ret_path (must be freed by the caller).
376 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
377 * NULL, this function allocates a unique suffix via @b (by requesting a new
378 * cookie). If both @sender_id and @external_id are given, @b can be passed as
379 * NULL.
380 *
381 * Returns: 0 on success, negative error code on failure.
382 */
383int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
384 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
385 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
386 int r;
387
388 assert_return(b || (sender_id && external_id), -EINVAL);
5453a4b1 389 assert_return(sd_bus_object_path_is_valid(prefix), -EINVAL);
98a4c30b
DH
390 assert_return(ret_path, -EINVAL);
391
392 if (!sender_id) {
393 r = sd_bus_get_unique_name(b, &sender_id);
394 if (r < 0)
395 return r;
396 }
397
398 if (!external_id) {
399 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
400 external_id = external_buf;
401 }
402
403 sender_label = bus_label_escape(sender_id);
404 if (!sender_label)
405 return -ENOMEM;
406
407 external_label = bus_label_escape(external_id);
408 if (!external_label)
409 return -ENOMEM;
410
657ee2d8 411 p = path_join(prefix, sender_label, external_label);
98a4c30b
DH
412 if (!p)
413 return -ENOMEM;
414
415 *ret_path = p;
416 return 0;
417}
418
419/**
420 * bus_path_decode_unique() - decode unique object path
421 * @path: object path to decode
422 * @prefix: object path prefix
423 * @ret_sender: output parameter for sender-id label
424 * @ret_external: output parameter for external-id label
425 *
426 * This does the reverse of bus_path_encode_unique() (see its description for
427 * details). Both trailing labels, sender-id and external-id, are unescaped and
428 * returned in the given output parameters (the caller must free them).
429 *
430 * Note that this function returns 0 if the path does not match the template
431 * (see bus_path_encode_unique()), 1 if it matched.
432 *
433 * Returns: Negative error code on failure, 0 if the given object path does not
434 * match the template (return parameters are set to NULL), 1 if it was
435 * parsed successfully (return parameters contain allocated labels).
436 */
437int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
438 const char *p, *q;
439 char *sender, *external;
440
5453a4b1
ZJS
441 assert(sd_bus_object_path_is_valid(path));
442 assert(sd_bus_object_path_is_valid(prefix));
98a4c30b
DH
443 assert(ret_sender);
444 assert(ret_external);
445
446 p = object_path_startswith(path, prefix);
447 if (!p) {
448 *ret_sender = NULL;
449 *ret_external = NULL;
450 return 0;
451 }
452
453 q = strchr(p, '/');
454 if (!q) {
455 *ret_sender = NULL;
456 *ret_external = NULL;
457 return 0;
458 }
459
460 sender = bus_label_unescape_n(p, q - p);
461 external = bus_label_unescape(q + 1);
462 if (!sender || !external) {
463 free(sender);
464 free(external);
465 return -ENOMEM;
466 }
467
468 *ret_sender = sender;
469 *ret_external = external;
470 return 1;
471}
057171ef 472
984794ba
LP
473int bus_track_add_name_many(sd_bus_track *t, char **l) {
474 int r = 0;
475 char **i;
476
477 assert(t);
478
479 /* Continues adding after failure, and returns the first failure. */
480
481 STRV_FOREACH(i, l) {
482 int k;
483
484 k = sd_bus_track_add_name(t, *i);
485 if (k < 0 && r >= 0)
486 r = k;
487 }
488
489 return r;
490}
d7afd945 491
0ddf50ff 492int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
b1a4981a 493 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
d7afd945
LP
494 const char *e;
495 int r;
496
497 assert(ret);
498
61252bae
ZJS
499 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
500 * turned on. */
d7afd945
LP
501
502 r = sd_bus_new(&bus);
503 if (r < 0)
504 return r;
505
0ddf50ff
YW
506 if (description) {
507 r = sd_bus_set_description(bus, description);
508 if (r < 0)
509 return r;
510 }
511
d7afd945
LP
512 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
513 if (!e)
514 e = DEFAULT_SYSTEM_BUS_ADDRESS;
515
516 r = sd_bus_set_address(bus, e);
517 if (r < 0)
518 return r;
519
520 r = sd_bus_set_bus_client(bus, true);
521 if (r < 0)
522 return r;
523
d7afd945
LP
524 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
525 if (r < 0)
526 return r;
527
528 r = sd_bus_set_watch_bind(bus, true);
529 if (r < 0)
530 return r;
531
532 r = sd_bus_set_connected_signal(bus, true);
533 if (r < 0)
534 return r;
535
536 r = sd_bus_start(bus);
537 if (r < 0)
538 return r;
539
1cc6c93a 540 *ret = TAKE_PTR(bus);
d7afd945
LP
541
542 return 0;
543}
906cb2eb 544
19017acb
LP
545int bus_reply_pair_array(sd_bus_message *m, char **l) {
546 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
547 char **k, **v;
548 int r;
549
550 assert(m);
551
61252bae
ZJS
552 /* Reply to the specified message with a message containing a dictionary put together from the
553 * specified strv */
19017acb
LP
554
555 r = sd_bus_message_new_method_return(m, &reply);
556 if (r < 0)
557 return r;
558
559 r = sd_bus_message_open_container(reply, 'a', "{ss}");
560 if (r < 0)
561 return r;
562
563 STRV_FOREACH_PAIR(k, v, l) {
564 r = sd_bus_message_append(reply, "{ss}", *k, *v);
565 if (r < 0)
566 return r;
567 }
568
569 r = sd_bus_message_close_container(reply);
570 if (r < 0)
571 return r;
572
573 return sd_bus_send(NULL, reply, NULL);
574}
2a66c2a1
LP
575
576static void bus_message_unref_wrapper(void *m) {
577 sd_bus_message_unref(m);
578}
579
580const struct hash_ops bus_message_hash_ops = {
581 .hash = trivial_hash_func,
582 .compare = trivial_compare_func,
583 .free_value = bus_message_unref_wrapper,
584};