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