]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
40ca29a1 | 2 | |
a8fbdf54 | 3 | #include <fcntl.h> |
69a283c5 | 4 | #include <malloc.h> |
a8fbdf54 | 5 | #include <stdlib.h> |
0c842e0a | 6 | #include <sys/socket.h> |
69a283c5 | 7 | #include <sys/stat.h> |
a8fbdf54 | 8 | #include <unistd.h> |
0c842e0a | 9 | |
4f5dd394 | 10 | #include "sd-bus.h" |
ebd011d9 LP |
11 | #include "sd-daemon.h" |
12 | #include "sd-event.h" | |
a8fbdf54 | 13 | #include "sd-id128.h" |
d53d9474 | 14 | |
d9ccf6b3 | 15 | #include "alloc-util.h" |
73d3ac8e | 16 | #include "bus-common-errors.h" |
d53d9474 LP |
17 | #include "bus-internal.h" |
18 | #include "bus-label.h" | |
3ffd4af2 | 19 | #include "bus-util.h" |
ad963c3f LP |
20 | #include "capsule-util.h" |
21 | #include "chase.h" | |
f359b307 | 22 | #include "daemon-util.h" |
afc55a5e | 23 | #include "env-util.h" |
69a283c5 | 24 | #include "errno-util.h" |
fb22861d | 25 | #include "fd-util.h" |
ad963c3f | 26 | #include "format-util.h" |
93a1f792 | 27 | #include "log.h" |
a87a9625 | 28 | #include "memfd-util.h" |
2485b7e2 | 29 | #include "memstream-util.h" |
657ee2d8 | 30 | #include "path-util.h" |
69a283c5 | 31 | #include "pidref.h" |
269e4d2d | 32 | #include "socket-util.h" |
15a5e950 | 33 | #include "stdio-util.h" |
d8a77d55 | 34 | #include "string-table.h" |
69a283c5 | 35 | #include "string-util.h" |
d9ccf6b3 | 36 | #include "strv.h" |
69a283c5 | 37 | #include "time-util.h" |
ad963c3f | 38 | #include "uid-classification.h" |
40ca29a1 | 39 | |
9d5f05ae | 40 | static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
99534007 | 41 | sd_event *e = ASSERT_PTR(userdata); |
40ca29a1 | 42 | |
40ca29a1 | 43 | assert(m); |
40ca29a1 | 44 | |
19070062 | 45 | sd_bus_close(sd_bus_message_get_bus(m)); |
6203e07a | 46 | sd_event_exit(e, 0); |
b27adf35 | 47 | |
40ca29a1 LP |
48 | return 1; |
49 | } | |
50 | ||
fb293b3c ZJS |
51 | int bus_log_address_error(int r, BusTransport transport) { |
52 | bool hint = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM; | |
53 | ||
237fbb67 | 54 | return log_error_errno(r, |
fb293b3c ZJS |
55 | 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)" : |
56 | "Failed to set bus address: %m"); | |
237fbb67 ZJS |
57 | } |
58 | ||
c577fe65 | 59 | int bus_log_connect_full(int log_level, int r, BusTransport transport, RuntimeScope scope) { |
10a7340a ZJS |
60 | bool hint_vars = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM, |
61 | hint_addr = transport == BUS_TRANSPORT_LOCAL && ERRNO_IS_PRIVILEGE(r); | |
62 | ||
c577fe65 YW |
63 | return log_full_errno(log_level, r, |
64 | hint_vars ? "Failed to connect to %s scope bus via %s transport: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" : | |
65 | hint_addr ? "Failed to connect to %s scope bus via %s transport: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)" : | |
66 | "Failed to connect to %s scope bus via %s transport: %m", runtime_scope_to_string(scope), bus_transport_to_string(transport)); | |
237fbb67 ZJS |
67 | } |
68 | ||
69a283c5 DDM |
69 | int bus_log_connect_error(int r, BusTransport transport, RuntimeScope scope) { |
70 | return bus_log_connect_full(LOG_ERR, r, transport, scope); | |
71 | } | |
72 | ||
6203e07a | 73 | int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) { |
75152a4d | 74 | const char *match; |
11846aa7 | 75 | const char *unique; |
40ca29a1 LP |
76 | int r; |
77 | ||
78 | assert(e); | |
79 | assert(bus); | |
80 | assert(name); | |
81 | ||
6203e07a LP |
82 | /* We unregister the name here and then wait for the |
83 | * NameOwnerChanged signal for this event to arrive before we | |
84 | * quit. We do this in order to make sure that any queued | |
85 | * requests are still processed before we really exit. */ | |
86 | ||
11846aa7 | 87 | r = sd_bus_get_unique_name(bus, &unique); |
40ca29a1 LP |
88 | if (r < 0) |
89 | return r; | |
90 | ||
75152a4d LP |
91 | match = strjoina( |
92 | "sender='org.freedesktop.DBus'," | |
93 | "type='signal'," | |
94 | "interface='org.freedesktop.DBus'," | |
95 | "member='NameOwnerChanged'," | |
96 | "path='/org/freedesktop/DBus'," | |
97 | "arg0='", name, "',", | |
98 | "arg1='", unique, "',", | |
99 | "arg2=''"); | |
100 | ||
101 | r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e); | |
40ca29a1 LP |
102 | if (r < 0) |
103 | return r; | |
104 | ||
75152a4d | 105 | r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL); |
40ca29a1 LP |
106 | if (r < 0) |
107 | return r; | |
108 | ||
40ca29a1 LP |
109 | return 0; |
110 | } | |
111 | ||
afc55a5e LP |
112 | static bool idle_allowed(void) { |
113 | static int allowed = -1; | |
114 | ||
115 | if (allowed >= 0) | |
116 | return allowed; | |
117 | ||
118 | allowed = secure_getenv_bool("SYSTEMD_EXIT_ON_IDLE"); | |
119 | if (allowed < 0 && allowed != -ENXIO) | |
120 | log_debug_errno(allowed, "Failed to parse $SYSTEMD_EXIT_ON_IDLE, ignoring: %m"); | |
121 | ||
122 | return allowed != 0; | |
123 | } | |
124 | ||
37224a5f LP |
125 | int bus_event_loop_with_idle( |
126 | sd_event *e, | |
127 | sd_bus *bus, | |
128 | const char *name, | |
129 | usec_t timeout, | |
130 | check_idle_t check_idle, | |
131 | void *userdata) { | |
d94027ad | 132 | |
40ca29a1 | 133 | bool exiting = false; |
6203e07a | 134 | int r, code; |
40ca29a1 LP |
135 | |
136 | assert(e); | |
137 | assert(bus); | |
138 | assert(name); | |
139 | ||
140 | for (;;) { | |
37224a5f LP |
141 | bool idle; |
142 | ||
40ca29a1 LP |
143 | r = sd_event_get_state(e); |
144 | if (r < 0) | |
145 | return r; | |
40ca29a1 LP |
146 | if (r == SD_EVENT_FINISHED) |
147 | break; | |
148 | ||
afc55a5e | 149 | if (!idle_allowed() || sd_bus_pending_method_calls(bus) > 0) |
75fb956f LP |
150 | idle = false; |
151 | else if (check_idle) | |
37224a5f LP |
152 | idle = check_idle(userdata); |
153 | else | |
154 | idle = true; | |
155 | ||
f5fbe71d | 156 | r = sd_event_run(e, exiting || !idle ? UINT64_MAX : timeout); |
40ca29a1 LP |
157 | if (r < 0) |
158 | return r; | |
159 | ||
a8ba6cd1 | 160 | if (r == 0 && !exiting && idle) { |
e72d5955 LP |
161 | log_debug("Idle for %s, exiting.", FORMAT_TIMESPAN(timeout, 1)); |
162 | ||
99cde098 | 163 | /* Inform the service manager that we are going down, so that it will queue all |
f359b307 | 164 | * further start requests, instead of assuming we are still running. */ |
792a8bfb | 165 | (void) sd_notify(false, NOTIFY_STOPPING_MESSAGE); |
b27adf35 | 166 | |
99cde098 | 167 | r = bus_async_unregister_and_exit(e, bus, name); |
40ca29a1 LP |
168 | if (r < 0) |
169 | return r; | |
170 | ||
99cde098 | 171 | exiting = true; |
40ca29a1 LP |
172 | } |
173 | } | |
174 | ||
6203e07a LP |
175 | r = sd_event_get_exit_code(e, &code); |
176 | if (r < 0) | |
177 | return r; | |
178 | ||
179 | return code; | |
40ca29a1 LP |
180 | } |
181 | ||
9d5f05ae | 182 | int bus_name_has_owner(sd_bus *bus, const char *name, sd_bus_error *reterr_error) { |
4afd3348 | 183 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL; |
5fd38859 DH |
184 | int r, has_owner = 0; |
185 | ||
9d5f05ae | 186 | assert(bus); |
5fd38859 DH |
187 | assert(name); |
188 | ||
9d5f05ae | 189 | r = sd_bus_call_method(bus, |
5fd38859 DH |
190 | "org.freedesktop.DBus", |
191 | "/org/freedesktop/dbus", | |
192 | "org.freedesktop.DBus", | |
193 | "NameHasOwner", | |
9d5f05ae | 194 | reterr_error, |
5fd38859 DH |
195 | &rep, |
196 | "s", | |
197 | name); | |
198 | if (r < 0) | |
199 | return r; | |
200 | ||
201 | r = sd_bus_message_read_basic(rep, 'b', &has_owner); | |
202 | if (r < 0) | |
9d5f05ae | 203 | return sd_bus_error_set_errno(reterr_error, r); |
5fd38859 DH |
204 | |
205 | return has_owner; | |
206 | } | |
207 | ||
73d3ac8e ZJS |
208 | bool bus_error_is_unknown_service(const sd_bus_error *error) { |
209 | return sd_bus_error_has_names(error, | |
210 | SD_BUS_ERROR_SERVICE_UNKNOWN, | |
211 | SD_BUS_ERROR_NAME_HAS_NO_OWNER, | |
212 | BUS_ERROR_NO_SUCH_UNIT); | |
213 | } | |
214 | ||
ac004f4c YW |
215 | bool bus_error_is_connection(const sd_bus_error *error) { |
216 | return sd_bus_error_has_names(error, | |
217 | SD_BUS_ERROR_NO_REPLY, | |
218 | SD_BUS_ERROR_DISCONNECTED, | |
219 | SD_BUS_ERROR_TIMED_OUT); | |
220 | } | |
221 | ||
9d5f05ae | 222 | int bus_check_peercred(sd_bus *bus) { |
0c842e0a | 223 | struct ucred ucred; |
3e641e36 | 224 | int fd, r; |
0c842e0a | 225 | |
9d5f05ae | 226 | assert(bus); |
0c842e0a | 227 | |
9d5f05ae | 228 | fd = sd_bus_get_fd(bus); |
0f8bd8de LP |
229 | if (fd < 0) |
230 | return fd; | |
0c842e0a | 231 | |
3e641e36 LP |
232 | r = getpeercred(fd, &ucred); |
233 | if (r < 0) | |
234 | return r; | |
0c842e0a TG |
235 | |
236 | if (ucred.uid != 0 && ucred.uid != geteuid()) | |
237 | return -EPERM; | |
238 | ||
239 | return 1; | |
240 | } | |
241 | ||
9d5f05ae | 242 | int bus_connect_system_systemd(sd_bus **ret) { |
b1a4981a | 243 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
0c842e0a | 244 | int r; |
0c842e0a | 245 | |
9d5f05ae | 246 | assert(ret); |
0c842e0a | 247 | |
a6aa8912 | 248 | r = sd_bus_new(&bus); |
0f8bd8de LP |
249 | if (r < 0) |
250 | return r; | |
a1da8583 | 251 | |
a6aa8912 LP |
252 | r = sd_bus_set_address(bus, "unix:path=/run/systemd/private"); |
253 | if (r < 0) | |
254 | return r; | |
255 | ||
256 | r = sd_bus_start(bus); | |
257 | if (r < 0) | |
a339495b | 258 | return r; |
a6aa8912 | 259 | |
0f8bd8de | 260 | r = bus_check_peercred(bus); |
a1da8583 TG |
261 | if (r < 0) |
262 | return r; | |
263 | ||
9d5f05ae | 264 | *ret = TAKE_PTR(bus); |
a1da8583 TG |
265 | return 0; |
266 | } | |
267 | ||
9d5f05ae | 268 | int bus_connect_user_systemd(sd_bus **ret) { |
b1a4981a | 269 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
a6aa8912 | 270 | _cleanup_free_ char *ee = NULL; |
41dd15e4 LP |
271 | const char *e; |
272 | int r; | |
273 | ||
9d5f05ae | 274 | assert(ret); |
41dd15e4 LP |
275 | |
276 | e = secure_getenv("XDG_RUNTIME_DIR"); | |
537220d9 | 277 | if (!e) |
c5698fe9 | 278 | return -ENOMEDIUM; |
537220d9 | 279 | |
a6aa8912 LP |
280 | ee = bus_address_escape(e); |
281 | if (!ee) | |
537220d9 | 282 | return -ENOMEM; |
41dd15e4 LP |
283 | |
284 | r = sd_bus_new(&bus); | |
285 | if (r < 0) | |
286 | return r; | |
287 | ||
605405c6 | 288 | bus->address = strjoin("unix:path=", ee, "/systemd/private"); |
a6aa8912 LP |
289 | if (!bus->address) |
290 | return -ENOMEM; | |
41dd15e4 LP |
291 | |
292 | r = sd_bus_start(bus); | |
293 | if (r < 0) | |
a339495b | 294 | return r; |
41dd15e4 LP |
295 | |
296 | r = bus_check_peercred(bus); | |
297 | if (r < 0) | |
298 | return r; | |
299 | ||
9d5f05ae | 300 | *ret = TAKE_PTR(bus); |
41dd15e4 LP |
301 | return 0; |
302 | } | |
303 | ||
ad963c3f LP |
304 | static int pin_capsule_socket(const char *capsule, const char *suffix, uid_t *ret_uid, gid_t *ret_gid) { |
305 | _cleanup_close_ int inode_fd = -EBADF; | |
306 | _cleanup_free_ char *p = NULL; | |
307 | struct stat st; | |
308 | int r; | |
309 | ||
310 | assert(capsule); | |
311 | assert(suffix); | |
8e34fdb8 MY |
312 | assert(ret_uid); |
313 | assert(ret_gid); | |
ad963c3f LP |
314 | |
315 | p = path_join("/run/capsules", capsule, suffix); | |
316 | if (!p) | |
317 | return -ENOMEM; | |
318 | ||
319 | /* We enter territory owned by the user, hence let's be paranoid about symlinks and ownership */ | |
320 | r = chase(p, /* root= */ NULL, CHASE_SAFE|CHASE_PROHIBIT_SYMLINKS, /* ret_path= */ NULL, &inode_fd); | |
321 | if (r < 0) | |
322 | return r; | |
323 | ||
324 | if (fstat(inode_fd, &st) < 0) | |
8e34fdb8 | 325 | return negative_errno(); |
ad963c3f LP |
326 | |
327 | /* Paranoid safety check */ | |
328 | if (uid_is_system(st.st_uid) || gid_is_system(st.st_gid)) | |
329 | return -EPERM; | |
330 | ||
331 | *ret_uid = st.st_uid; | |
332 | *ret_gid = st.st_gid; | |
333 | ||
334 | return TAKE_FD(inode_fd); | |
335 | } | |
336 | ||
8e34fdb8 | 337 | static int bus_set_address_capsule(sd_bus *bus, const char *capsule, const char *suffix, int *ret_pin_fd) { |
ad963c3f LP |
338 | _cleanup_close_ int inode_fd = -EBADF; |
339 | _cleanup_free_ char *pp = NULL; | |
340 | uid_t uid; | |
341 | gid_t gid; | |
342 | int r; | |
343 | ||
8e34fdb8 | 344 | assert(bus); |
ad963c3f | 345 | assert(capsule); |
8e34fdb8 MY |
346 | assert(suffix); |
347 | assert(ret_pin_fd); | |
348 | ||
349 | /* Connects to a capsule's user bus. We need to do so under the capsule's UID/GID, otherwise | |
350 | * the service manager might refuse our connection. Hence fake it. */ | |
ad963c3f LP |
351 | |
352 | r = capsule_name_is_valid(capsule); | |
353 | if (r < 0) | |
354 | return r; | |
355 | if (r == 0) | |
356 | return -EINVAL; | |
357 | ||
8e34fdb8 | 358 | inode_fd = pin_capsule_socket(capsule, suffix, &uid, &gid); |
ad963c3f LP |
359 | if (inode_fd < 0) |
360 | return inode_fd; | |
361 | ||
362 | pp = bus_address_escape(FORMAT_PROC_FD_PATH(inode_fd)); | |
363 | if (!pp) | |
364 | return -ENOMEM; | |
365 | ||
ad963c3f LP |
366 | if (asprintf(&bus->address, "unix:path=%s,uid=" UID_FMT ",gid=" GID_FMT, pp, uid, gid) < 0) |
367 | return -ENOMEM; | |
368 | ||
8e34fdb8 | 369 | *ret_pin_fd = TAKE_FD(inode_fd); /* This fd must be kept pinned until the connection has been established */ |
ad963c3f LP |
370 | return 0; |
371 | } | |
372 | ||
373 | int bus_set_address_capsule_bus(sd_bus *bus, const char *capsule, int *ret_pin_fd) { | |
8e34fdb8 MY |
374 | return bus_set_address_capsule(bus, capsule, "bus", ret_pin_fd); |
375 | } | |
376 | ||
9d5f05ae | 377 | int bus_connect_capsule_systemd(const char *capsule, sd_bus **ret) { |
8e34fdb8 | 378 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
ad963c3f | 379 | _cleanup_close_ int inode_fd = -EBADF; |
ad963c3f LP |
380 | int r; |
381 | ||
ad963c3f | 382 | assert(capsule); |
9d5f05ae | 383 | assert(ret); |
ad963c3f | 384 | |
8e34fdb8 | 385 | r = sd_bus_new(&bus); |
ad963c3f LP |
386 | if (r < 0) |
387 | return r; | |
ad963c3f | 388 | |
8e34fdb8 MY |
389 | r = bus_set_address_capsule(bus, capsule, "systemd/private", &inode_fd); |
390 | if (r < 0) | |
391 | return r; | |
ad963c3f | 392 | |
8e34fdb8 MY |
393 | r = sd_bus_start(bus); |
394 | if (r < 0) | |
395 | return r; | |
ad963c3f | 396 | |
9d5f05ae | 397 | *ret = TAKE_PTR(bus); |
ad963c3f LP |
398 | return 0; |
399 | } | |
400 | ||
9d5f05ae | 401 | int bus_connect_capsule_bus(const char *capsule, sd_bus **ret) { |
ad963c3f LP |
402 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
403 | _cleanup_close_ int inode_fd = -EBADF; | |
404 | int r; | |
405 | ||
406 | assert(capsule); | |
9d5f05ae | 407 | assert(ret); |
ad963c3f LP |
408 | |
409 | r = sd_bus_new(&bus); | |
410 | if (r < 0) | |
411 | return r; | |
412 | ||
413 | r = bus_set_address_capsule_bus(bus, capsule, &inode_fd); | |
414 | if (r < 0) | |
415 | return r; | |
416 | ||
417 | r = sd_bus_set_bus_client(bus, true); | |
418 | if (r < 0) | |
419 | return r; | |
420 | ||
421 | r = sd_bus_start(bus); | |
422 | if (r < 0) | |
423 | return r; | |
424 | ||
9d5f05ae | 425 | *ret = TAKE_PTR(bus); |
ad963c3f LP |
426 | return 0; |
427 | } | |
428 | ||
1b630835 LP |
429 | int bus_connect_transport( |
430 | BusTransport transport, | |
431 | const char *host, | |
4870133b | 432 | RuntimeScope runtime_scope, |
1b630835 LP |
433 | sd_bus **ret) { |
434 | ||
b1a4981a | 435 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
d21ed1ea LP |
436 | int r; |
437 | ||
438 | assert(transport >= 0); | |
439 | assert(transport < _BUS_TRANSPORT_MAX); | |
38303498 | 440 | assert(ret); |
d21ed1ea | 441 | |
d21ed1ea LP |
442 | switch (transport) { |
443 | ||
444 | case BUS_TRANSPORT_LOCAL: | |
ad963c3f | 445 | assert_return(!host, -EINVAL); |
4870133b LP |
446 | |
447 | switch (runtime_scope) { | |
448 | ||
449 | case RUNTIME_SCOPE_USER: | |
38303498 | 450 | r = sd_bus_default_user(&bus); |
4870133b LP |
451 | break; |
452 | ||
453 | case RUNTIME_SCOPE_SYSTEM: | |
d7a0f1f4 | 454 | if (sd_booted() <= 0) |
fb507898 | 455 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ |
d7a0f1f4 FS |
456 | return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), |
457 | "System has not been booted with systemd as init system (PID 1). Can't operate."); | |
c240f293 | 458 | |
fb507898 | 459 | r = sd_bus_default_system(&bus); |
4870133b LP |
460 | break; |
461 | ||
462 | default: | |
463 | assert_not_reached(); | |
fb507898 | 464 | } |
d21ed1ea LP |
465 | break; |
466 | ||
467 | case BUS_TRANSPORT_REMOTE: | |
ad963c3f LP |
468 | assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP); |
469 | ||
38303498 | 470 | r = sd_bus_open_system_remote(&bus, host); |
41dd15e4 LP |
471 | break; |
472 | ||
de33fc62 | 473 | case BUS_TRANSPORT_MACHINE: |
4870133b LP |
474 | switch (runtime_scope) { |
475 | ||
476 | case RUNTIME_SCOPE_USER: | |
1b630835 | 477 | r = sd_bus_open_user_machine(&bus, host); |
4870133b LP |
478 | break; |
479 | ||
480 | case RUNTIME_SCOPE_SYSTEM: | |
1b630835 | 481 | r = sd_bus_open_system_machine(&bus, host); |
4870133b LP |
482 | break; |
483 | ||
484 | default: | |
485 | assert_not_reached(); | |
486 | } | |
487 | ||
41dd15e4 LP |
488 | break; |
489 | ||
ad963c3f LP |
490 | case BUS_TRANSPORT_CAPSULE: |
491 | assert_return(runtime_scope == RUNTIME_SCOPE_USER, -EINVAL); | |
492 | ||
493 | r = bus_connect_capsule_bus(host, &bus); | |
494 | break; | |
495 | ||
41dd15e4 | 496 | default: |
04499a70 | 497 | assert_not_reached(); |
41dd15e4 | 498 | } |
38303498 LP |
499 | if (r < 0) |
500 | return r; | |
41dd15e4 | 501 | |
38303498 LP |
502 | r = sd_bus_set_exit_on_disconnect(bus, true); |
503 | if (r < 0) | |
504 | return r; | |
505 | ||
1cc6c93a | 506 | *ret = TAKE_PTR(bus); |
38303498 | 507 | return 0; |
41dd15e4 LP |
508 | } |
509 | ||
ad963c3f LP |
510 | int bus_connect_transport_systemd( |
511 | BusTransport transport, | |
512 | const char *host, | |
513 | RuntimeScope runtime_scope, | |
9d5f05ae | 514 | sd_bus **ret) { |
ad963c3f | 515 | |
d0316b7a DDM |
516 | int r; |
517 | ||
41dd15e4 LP |
518 | assert(transport >= 0); |
519 | assert(transport < _BUS_TRANSPORT_MAX); | |
9d5f05ae | 520 | assert(ret); |
41dd15e4 LP |
521 | |
522 | switch (transport) { | |
523 | ||
524 | case BUS_TRANSPORT_LOCAL: | |
ad963c3f LP |
525 | assert_return(!host, -EINVAL); |
526 | ||
4870133b LP |
527 | switch (runtime_scope) { |
528 | ||
529 | case RUNTIME_SCOPE_USER: | |
9d5f05ae | 530 | r = bus_connect_user_systemd(ret); |
d0316b7a DDM |
531 | /* We used to always fall back to the user session bus if we couldn't connect to the |
532 | * private manager bus. To keep compat with existing code that was setting | |
533 | * DBUS_SESSION_BUS_ADDRESS without setting XDG_RUNTIME_DIR, connect to the user | |
534 | * session bus if DBUS_SESSION_BUS_ADDRESS is set and XDG_RUNTIME_DIR isn't. */ | |
c240f293 MY |
535 | if (r == -ENOMEDIUM && secure_getenv("DBUS_SESSION_BUS_ADDRESS")) { |
536 | log_debug_errno(r, "$XDG_RUNTIME_DIR not set, unable to connect to private bus. Falling back to session bus."); | |
9d5f05ae | 537 | r = sd_bus_default_user(ret); |
c240f293 | 538 | } |
d0316b7a DDM |
539 | |
540 | return r; | |
73e91092 | 541 | |
4870133b LP |
542 | case RUNTIME_SCOPE_SYSTEM: |
543 | if (sd_booted() <= 0) | |
544 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ | |
545 | return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), | |
546 | "System has not been booted with systemd as init system (PID 1). Can't operate."); | |
4870133b | 547 | |
8c9c8e30 MY |
548 | /* If we are root then let's talk directly to the system instance, instead of |
549 | * going via the bus. */ | |
a178ffdf | 550 | if (geteuid() == 0) |
9d5f05ae | 551 | return bus_connect_system_systemd(ret); |
a178ffdf | 552 | |
9d5f05ae | 553 | return sd_bus_default_system(ret); |
8c9c8e30 | 554 | |
4870133b LP |
555 | default: |
556 | assert_not_reached(); | |
557 | } | |
558 | ||
559 | break; | |
41dd15e4 LP |
560 | |
561 | case BUS_TRANSPORT_REMOTE: | |
ad963c3f | 562 | assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP); |
9d5f05ae | 563 | return sd_bus_open_system_remote(ret, host); |
d21ed1ea | 564 | |
de33fc62 | 565 | case BUS_TRANSPORT_MACHINE: |
ad963c3f | 566 | assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP); |
9d5f05ae | 567 | return sd_bus_open_system_machine(ret, host); |
ad963c3f LP |
568 | |
569 | case BUS_TRANSPORT_CAPSULE: | |
570 | assert_return(runtime_scope == RUNTIME_SCOPE_USER, -EINVAL); | |
9d5f05ae | 571 | return bus_connect_capsule_systemd(host, ret); |
d21ed1ea LP |
572 | |
573 | default: | |
04499a70 | 574 | assert_not_reached(); |
d21ed1ea | 575 | } |
d21ed1ea | 576 | } |
e6504030 | 577 | |
98a4c30b DH |
578 | /** |
579 | * bus_path_encode_unique() - encode unique object path | |
580 | * @b: bus connection or NULL | |
581 | * @prefix: object path prefix | |
582 | * @sender_id: unique-name of client, or NULL | |
583 | * @external_id: external ID to be chosen by client, or NULL | |
584 | * @ret_path: storage for encoded object path pointer | |
585 | * | |
586 | * Whenever we provide a bus API that allows clients to create and manage | |
587 | * server-side objects, we need to provide a unique name for these objects. If | |
588 | * we let the server choose the name, we suffer from a race condition: If a | |
589 | * client creates an object asynchronously, it cannot destroy that object until | |
590 | * it received the method reply. It cannot know the name of the new object, | |
591 | * thus, it cannot destroy it. Furthermore, it enforces a round-trip. | |
592 | * | |
593 | * Therefore, many APIs allow the client to choose the unique name for newly | |
594 | * created objects. There're two problems to solve, though: | |
595 | * 1) Object names are usually defined via dbus object paths, which are | |
596 | * usually globally namespaced. Therefore, multiple clients must be able | |
597 | * to choose unique object names without interference. | |
598 | * 2) If multiple libraries share the same bus connection, they must be | |
599 | * able to choose unique object names without interference. | |
600 | * The first problem is solved easily by prefixing a name with the | |
601 | * unique-bus-name of a connection. The server side must enforce this and | |
602 | * reject any other name. The second problem is solved by providing unique | |
603 | * suffixes from within sd-bus. | |
604 | * | |
605 | * This helper allows clients to create unique object-paths. It uses the | |
606 | * template '/prefix/sender_id/external_id' and returns the new path in | |
607 | * @ret_path (must be freed by the caller). | |
608 | * If @sender_id is NULL, the unique-name of @b is used. If @external_id is | |
609 | * NULL, this function allocates a unique suffix via @b (by requesting a new | |
610 | * cookie). If both @sender_id and @external_id are given, @b can be passed as | |
611 | * NULL. | |
612 | * | |
613 | * Returns: 0 on success, negative error code on failure. | |
614 | */ | |
615 | int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) { | |
616 | _cleanup_free_ char *sender_label = NULL, *external_label = NULL; | |
617 | char external_buf[DECIMAL_STR_MAX(uint64_t)], *p; | |
618 | int r; | |
619 | ||
620 | assert_return(b || (sender_id && external_id), -EINVAL); | |
5453a4b1 | 621 | assert_return(sd_bus_object_path_is_valid(prefix), -EINVAL); |
98a4c30b DH |
622 | assert_return(ret_path, -EINVAL); |
623 | ||
624 | if (!sender_id) { | |
625 | r = sd_bus_get_unique_name(b, &sender_id); | |
626 | if (r < 0) | |
627 | return r; | |
628 | } | |
629 | ||
630 | if (!external_id) { | |
631 | xsprintf(external_buf, "%"PRIu64, ++b->cookie); | |
632 | external_id = external_buf; | |
633 | } | |
634 | ||
635 | sender_label = bus_label_escape(sender_id); | |
636 | if (!sender_label) | |
637 | return -ENOMEM; | |
638 | ||
639 | external_label = bus_label_escape(external_id); | |
640 | if (!external_label) | |
641 | return -ENOMEM; | |
642 | ||
657ee2d8 | 643 | p = path_join(prefix, sender_label, external_label); |
98a4c30b DH |
644 | if (!p) |
645 | return -ENOMEM; | |
646 | ||
647 | *ret_path = p; | |
648 | return 0; | |
649 | } | |
650 | ||
651 | /** | |
652 | * bus_path_decode_unique() - decode unique object path | |
653 | * @path: object path to decode | |
654 | * @prefix: object path prefix | |
655 | * @ret_sender: output parameter for sender-id label | |
656 | * @ret_external: output parameter for external-id label | |
657 | * | |
658 | * This does the reverse of bus_path_encode_unique() (see its description for | |
659 | * details). Both trailing labels, sender-id and external-id, are unescaped and | |
660 | * returned in the given output parameters (the caller must free them). | |
661 | * | |
662 | * Note that this function returns 0 if the path does not match the template | |
663 | * (see bus_path_encode_unique()), 1 if it matched. | |
664 | * | |
665 | * Returns: Negative error code on failure, 0 if the given object path does not | |
666 | * match the template (return parameters are set to NULL), 1 if it was | |
667 | * parsed successfully (return parameters contain allocated labels). | |
668 | */ | |
669 | int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) { | |
670 | const char *p, *q; | |
671 | char *sender, *external; | |
672 | ||
5453a4b1 ZJS |
673 | assert(sd_bus_object_path_is_valid(path)); |
674 | assert(sd_bus_object_path_is_valid(prefix)); | |
98a4c30b DH |
675 | assert(ret_sender); |
676 | assert(ret_external); | |
677 | ||
678 | p = object_path_startswith(path, prefix); | |
679 | if (!p) { | |
680 | *ret_sender = NULL; | |
681 | *ret_external = NULL; | |
682 | return 0; | |
683 | } | |
684 | ||
685 | q = strchr(p, '/'); | |
686 | if (!q) { | |
687 | *ret_sender = NULL; | |
688 | *ret_external = NULL; | |
689 | return 0; | |
690 | } | |
691 | ||
692 | sender = bus_label_unescape_n(p, q - p); | |
693 | external = bus_label_unescape(q + 1); | |
694 | if (!sender || !external) { | |
695 | free(sender); | |
696 | free(external); | |
697 | return -ENOMEM; | |
698 | } | |
699 | ||
700 | *ret_sender = sender; | |
701 | *ret_external = external; | |
702 | return 1; | |
703 | } | |
057171ef | 704 | |
1446e3c3 | 705 | int bus_track_add_name_many(sd_bus_track *t, char * const *l) { |
984794ba | 706 | int r = 0; |
984794ba LP |
707 | |
708 | assert(t); | |
709 | ||
710 | /* Continues adding after failure, and returns the first failure. */ | |
711 | ||
809c3a84 ZJS |
712 | STRV_FOREACH(i, l) |
713 | RET_GATHER(r, sd_bus_track_add_name(t, *i)); | |
984794ba LP |
714 | return r; |
715 | } | |
d7afd945 | 716 | |
8402ca04 RP |
717 | int bus_track_to_strv(sd_bus_track *t, char ***ret) { |
718 | _cleanup_strv_free_ char **subscribed = NULL; | |
33eeea41 | 719 | int r; |
8402ca04 RP |
720 | |
721 | assert(ret); | |
722 | ||
723 | for (const char *n = sd_bus_track_first(t); n; n = sd_bus_track_next(t)) { | |
33eeea41 MY |
724 | int c = sd_bus_track_count_name(t, n); |
725 | assert(c >= 0); | |
8402ca04 | 726 | |
33eeea41 | 727 | for (int j = 0; j < c; j++) { |
8402ca04 RP |
728 | r = strv_extend(&subscribed, n); |
729 | if (r < 0) | |
730 | return r; | |
731 | } | |
732 | } | |
733 | ||
734 | *ret = TAKE_PTR(subscribed); | |
33eeea41 | 735 | return 0; |
8402ca04 RP |
736 | } |
737 | ||
0ddf50ff | 738 | int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) { |
b1a4981a | 739 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
d7afd945 LP |
740 | const char *e; |
741 | int r; | |
742 | ||
743 | assert(ret); | |
744 | ||
61252bae ZJS |
745 | /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal |
746 | * turned on. */ | |
d7afd945 LP |
747 | |
748 | r = sd_bus_new(&bus); | |
749 | if (r < 0) | |
750 | return r; | |
751 | ||
0ddf50ff YW |
752 | if (description) { |
753 | r = sd_bus_set_description(bus, description); | |
754 | if (r < 0) | |
755 | return r; | |
756 | } | |
757 | ||
d7afd945 LP |
758 | e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); |
759 | if (!e) | |
760 | e = DEFAULT_SYSTEM_BUS_ADDRESS; | |
761 | ||
762 | r = sd_bus_set_address(bus, e); | |
763 | if (r < 0) | |
764 | return r; | |
765 | ||
766 | r = sd_bus_set_bus_client(bus, true); | |
767 | if (r < 0) | |
768 | return r; | |
769 | ||
d7afd945 LP |
770 | r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS); |
771 | if (r < 0) | |
772 | return r; | |
773 | ||
774 | r = sd_bus_set_watch_bind(bus, true); | |
775 | if (r < 0) | |
776 | return r; | |
777 | ||
778 | r = sd_bus_set_connected_signal(bus, true); | |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
782 | r = sd_bus_start(bus); | |
783 | if (r < 0) | |
784 | return r; | |
785 | ||
1cc6c93a | 786 | *ret = TAKE_PTR(bus); |
d7afd945 LP |
787 | |
788 | return 0; | |
789 | } | |
906cb2eb | 790 | |
9d5f05ae | 791 | int bus_reply_pair_array(sd_bus_message *m, char * const *l) { |
19017acb | 792 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
19017acb LP |
793 | int r; |
794 | ||
795 | assert(m); | |
796 | ||
61252bae ZJS |
797 | /* Reply to the specified message with a message containing a dictionary put together from the |
798 | * specified strv */ | |
19017acb LP |
799 | |
800 | r = sd_bus_message_new_method_return(m, &reply); | |
801 | if (r < 0) | |
802 | return r; | |
803 | ||
804 | r = sd_bus_message_open_container(reply, 'a', "{ss}"); | |
805 | if (r < 0) | |
806 | return r; | |
807 | ||
808 | STRV_FOREACH_PAIR(k, v, l) { | |
809 | r = sd_bus_message_append(reply, "{ss}", *k, *v); | |
810 | if (r < 0) | |
811 | return r; | |
812 | } | |
813 | ||
814 | r = sd_bus_message_close_container(reply); | |
815 | if (r < 0) | |
816 | return r; | |
817 | ||
51cc3825 | 818 | return sd_bus_message_send(reply); |
19017acb | 819 | } |
2a66c2a1 | 820 | |
9d5f05ae | 821 | static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
2485b7e2 YW |
822 | _cleanup_(memstream_done) MemStream m = {}; |
823 | _cleanup_free_ char *dump = NULL; | |
fb22861d LB |
824 | _cleanup_close_ int fd = -EBADF; |
825 | size_t dump_size; | |
2485b7e2 | 826 | FILE *f; |
fb22861d LB |
827 | int r; |
828 | ||
829 | assert(message); | |
830 | ||
2485b7e2 YW |
831 | f = memstream_init(&m); |
832 | if (!f) | |
fb22861d LB |
833 | return -ENOMEM; |
834 | ||
2485b7e2 | 835 | r = RET_NERRNO(malloc_info(/* options= */ 0, f)); |
fb22861d LB |
836 | if (r < 0) |
837 | return r; | |
838 | ||
2485b7e2 YW |
839 | r = memstream_finalize(&m, &dump, &dump_size); |
840 | if (r < 0) | |
841 | return r; | |
f392dfb5 | 842 | |
a87a9625 | 843 | fd = memfd_new_and_seal("malloc-info", dump, dump_size); |
fb22861d LB |
844 | if (fd < 0) |
845 | return fd; | |
846 | ||
847 | r = sd_bus_reply_method_return(message, "h", fd); | |
848 | if (r < 0) | |
849 | return r; | |
850 | ||
851 | return 1; /* Stop further processing */ | |
852 | } | |
853 | ||
854 | /* The default install callback will fail and disconnect the bus if it cannot register the match, but this | |
855 | * is only a debug method, we definitely don't want to fail in case there's some permission issue. */ | |
9d5f05ae | 856 | static int dummy_install_callback(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
fb22861d LB |
857 | return 1; |
858 | } | |
859 | ||
860 | int bus_register_malloc_status(sd_bus *bus, const char *destination) { | |
861 | const char *match; | |
862 | int r; | |
863 | ||
864 | assert(bus); | |
865 | assert(!isempty(destination)); | |
866 | ||
867 | match = strjoina("type='method_call'," | |
868 | "interface='org.freedesktop.MemoryAllocation1'," | |
869 | "path='/org/freedesktop/MemoryAllocation1'," | |
870 | "destination='", destination, "',", | |
871 | "member='GetMallocInfo'"); | |
872 | ||
873 | r = sd_bus_add_match_async(bus, NULL, match, method_dump_memory_state_by_fd, dummy_install_callback, NULL); | |
874 | if (r < 0) | |
875 | return log_debug_errno(r, "Failed to subscribe to GetMallocInfo() calls on MemoryAllocation1 interface: %m"); | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
1b78be0b LP |
880 | int bus_creds_get_pidref( |
881 | sd_bus_creds *c, | |
882 | PidRef *ret) { | |
883 | ||
884 | int pidfd = -EBADF; | |
885 | pid_t pid; | |
886 | int r; | |
887 | ||
888 | assert(c); | |
889 | assert(ret); | |
890 | ||
891 | r = sd_bus_creds_get_pid(c, &pid); | |
892 | if (r < 0) | |
893 | return r; | |
894 | ||
895 | r = sd_bus_creds_get_pidfd_dup(c, &pidfd); | |
896 | if (r < 0 && r != -ENODATA) | |
897 | return r; | |
898 | ||
899 | *ret = (PidRef) { | |
900 | .pid = pid, | |
901 | .fd = pidfd, | |
902 | }; | |
903 | ||
904 | return 0; | |
905 | } | |
906 | ||
907 | int bus_query_sender_pidref( | |
908 | sd_bus_message *m, | |
909 | PidRef *ret) { | |
910 | ||
911 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
912 | int r; | |
913 | ||
914 | assert(m); | |
915 | assert(ret); | |
916 | ||
917 | r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); | |
918 | if (r < 0) | |
919 | return r; | |
920 | ||
921 | return bus_creds_get_pidref(creds, ret); | |
922 | } | |
8157cc0e | 923 | |
a9a8d2e1 MY |
924 | int bus_get_instance_id(sd_bus *bus, sd_id128_t *ret) { |
925 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
926 | int r; | |
927 | ||
928 | assert(bus); | |
929 | assert(ret); | |
930 | ||
931 | r = sd_bus_call_method(bus, | |
932 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetId", | |
31a1e15c | 933 | /* reterr_error = */ NULL, &reply, NULL); |
a9a8d2e1 MY |
934 | if (r < 0) |
935 | return r; | |
936 | ||
937 | const char *id; | |
938 | ||
939 | r = sd_bus_message_read_basic(reply, 's', &id); | |
940 | if (r < 0) | |
941 | return r; | |
942 | ||
943 | return sd_id128_from_string(id, ret); | |
944 | } | |
945 | ||
d8a77d55 DDM |
946 | static const char* const bus_transport_table[] = { |
947 | [BUS_TRANSPORT_LOCAL] = "local", | |
948 | [BUS_TRANSPORT_REMOTE] = "remote", | |
949 | [BUS_TRANSPORT_MACHINE] = "machine", | |
950 | [BUS_TRANSPORT_CAPSULE] = "capsule", | |
951 | }; | |
952 | ||
953 | DEFINE_STRING_TABLE_LOOKUP_TO_STRING(bus_transport, BusTransport); |