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