]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
40ca29a1 | 2 | |
a8fbdf54 TA |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
5 | #include <inttypes.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <string.h> | |
9 | #include <sys/ioctl.h> | |
10 | #include <sys/resource.h> | |
0c842e0a | 11 | #include <sys/socket.h> |
a8fbdf54 | 12 | #include <unistd.h> |
0c842e0a | 13 | |
a8fbdf54 | 14 | #include "sd-bus-protocol.h" |
4f5dd394 | 15 | #include "sd-bus.h" |
ebd011d9 LP |
16 | #include "sd-daemon.h" |
17 | #include "sd-event.h" | |
a8fbdf54 | 18 | #include "sd-id128.h" |
d53d9474 | 19 | |
b5efdb8a | 20 | #include "alloc-util.h" |
d53d9474 LP |
21 | #include "bus-internal.h" |
22 | #include "bus-label.h" | |
23 | #include "bus-message.h" | |
3ffd4af2 | 24 | #include "bus-util.h" |
52610b02 | 25 | #include "cap-list.h" |
21771f33 | 26 | #include "cgroup-util.h" |
40ca29a1 | 27 | #include "def.h" |
4f5dd394 | 28 | #include "escape.h" |
3ffd4af2 | 29 | #include "fd-util.h" |
2bba9a57 | 30 | #include "missing.h" |
21771f33 | 31 | #include "mount-util.h" |
73186d53 | 32 | #include "nsflags.h" |
6bedfcbb | 33 | #include "parse-util.h" |
ee104e11 | 34 | #include "proc-cmdline.h" |
78f22b97 | 35 | #include "rlimit-util.h" |
15a5e950 | 36 | #include "stdio-util.h" |
d53d9474 | 37 | #include "strv.h" |
ee104e11 | 38 | #include "user-util.h" |
40ca29a1 | 39 | |
19070062 | 40 | static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
40ca29a1 LP |
41 | sd_event *e = userdata; |
42 | ||
40ca29a1 LP |
43 | assert(m); |
44 | assert(e); | |
45 | ||
19070062 | 46 | sd_bus_close(sd_bus_message_get_bus(m)); |
6203e07a | 47 | sd_event_exit(e, 0); |
b27adf35 | 48 | |
40ca29a1 LP |
49 | return 1; |
50 | } | |
51 | ||
6203e07a | 52 | int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) { |
75152a4d | 53 | const char *match; |
11846aa7 | 54 | const char *unique; |
40ca29a1 LP |
55 | int r; |
56 | ||
57 | assert(e); | |
58 | assert(bus); | |
59 | assert(name); | |
60 | ||
6203e07a LP |
61 | /* We unregister the name here and then wait for the |
62 | * NameOwnerChanged signal for this event to arrive before we | |
63 | * quit. We do this in order to make sure that any queued | |
64 | * requests are still processed before we really exit. */ | |
65 | ||
11846aa7 | 66 | r = sd_bus_get_unique_name(bus, &unique); |
40ca29a1 LP |
67 | if (r < 0) |
68 | return r; | |
69 | ||
75152a4d LP |
70 | match = strjoina( |
71 | "sender='org.freedesktop.DBus'," | |
72 | "type='signal'," | |
73 | "interface='org.freedesktop.DBus'," | |
74 | "member='NameOwnerChanged'," | |
75 | "path='/org/freedesktop/DBus'," | |
76 | "arg0='", name, "',", | |
77 | "arg1='", unique, "',", | |
78 | "arg2=''"); | |
79 | ||
80 | r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e); | |
40ca29a1 LP |
81 | if (r < 0) |
82 | return r; | |
83 | ||
75152a4d | 84 | r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL); |
40ca29a1 LP |
85 | if (r < 0) |
86 | return r; | |
87 | ||
40ca29a1 LP |
88 | return 0; |
89 | } | |
90 | ||
37224a5f LP |
91 | int bus_event_loop_with_idle( |
92 | sd_event *e, | |
93 | sd_bus *bus, | |
94 | const char *name, | |
95 | usec_t timeout, | |
96 | check_idle_t check_idle, | |
97 | void *userdata) { | |
40ca29a1 | 98 | bool exiting = false; |
6203e07a | 99 | int r, code; |
40ca29a1 LP |
100 | |
101 | assert(e); | |
102 | assert(bus); | |
103 | assert(name); | |
104 | ||
105 | for (;;) { | |
37224a5f LP |
106 | bool idle; |
107 | ||
40ca29a1 LP |
108 | r = sd_event_get_state(e); |
109 | if (r < 0) | |
110 | return r; | |
40ca29a1 LP |
111 | if (r == SD_EVENT_FINISHED) |
112 | break; | |
113 | ||
37224a5f LP |
114 | if (check_idle) |
115 | idle = check_idle(userdata); | |
116 | else | |
117 | idle = true; | |
118 | ||
119 | r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout); | |
40ca29a1 LP |
120 | if (r < 0) |
121 | return r; | |
122 | ||
a8ba6cd1 | 123 | if (r == 0 && !exiting && idle) { |
b27adf35 LP |
124 | |
125 | r = sd_bus_try_close(bus); | |
126 | if (r == -EBUSY) | |
127 | continue; | |
128 | ||
430e21c2 LP |
129 | /* Fallback for dbus1 connections: we |
130 | * unregister the name and wait for the | |
131 | * response to come through for it */ | |
15411c0c | 132 | if (r == -EOPNOTSUPP) { |
430e21c2 LP |
133 | |
134 | /* Inform the service manager that we | |
135 | * are going down, so that it will | |
136 | * queue all further start requests, | |
137 | * instead of assuming we are already | |
138 | * running. */ | |
139 | sd_notify(false, "STOPPING=1"); | |
b27adf35 LP |
140 | |
141 | r = bus_async_unregister_and_exit(e, bus, name); | |
142 | if (r < 0) | |
143 | return r; | |
144 | ||
145 | exiting = true; | |
146 | continue; | |
147 | } | |
148 | ||
40ca29a1 LP |
149 | if (r < 0) |
150 | return r; | |
151 | ||
b27adf35 LP |
152 | sd_event_exit(e, 0); |
153 | break; | |
40ca29a1 LP |
154 | } |
155 | } | |
156 | ||
6203e07a LP |
157 | r = sd_event_get_exit_code(e, &code); |
158 | if (r < 0) | |
159 | return r; | |
160 | ||
161 | return code; | |
40ca29a1 LP |
162 | } |
163 | ||
5fd38859 | 164 | int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) { |
4afd3348 | 165 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL; |
5fd38859 DH |
166 | int r, has_owner = 0; |
167 | ||
168 | assert(c); | |
169 | assert(name); | |
170 | ||
171 | r = sd_bus_call_method(c, | |
172 | "org.freedesktop.DBus", | |
173 | "/org/freedesktop/dbus", | |
174 | "org.freedesktop.DBus", | |
175 | "NameHasOwner", | |
176 | error, | |
177 | &rep, | |
178 | "s", | |
179 | name); | |
180 | if (r < 0) | |
181 | return r; | |
182 | ||
183 | r = sd_bus_message_read_basic(rep, 'b', &has_owner); | |
184 | if (r < 0) | |
185 | return sd_bus_error_set_errno(error, r); | |
186 | ||
187 | return has_owner; | |
188 | } | |
189 | ||
c529695e | 190 | static int check_good_user(sd_bus_message *m, uid_t good_user) { |
4afd3348 | 191 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
c529695e LP |
192 | uid_t sender_uid; |
193 | int r; | |
194 | ||
195 | assert(m); | |
196 | ||
197 | if (good_user == UID_INVALID) | |
198 | return 0; | |
199 | ||
200 | r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds); | |
201 | if (r < 0) | |
202 | return r; | |
203 | ||
0f514420 LP |
204 | /* Don't trust augmented credentials for authorization */ |
205 | assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM); | |
206 | ||
c529695e LP |
207 | r = sd_bus_creds_get_euid(creds, &sender_uid); |
208 | if (r < 0) | |
209 | return r; | |
210 | ||
211 | return sender_uid == good_user; | |
212 | } | |
213 | ||
ceb24229 | 214 | int bus_test_polkit( |
f3885791 | 215 | sd_bus_message *call, |
def9a7aa | 216 | int capability, |
40ca29a1 | 217 | const char *action, |
403ed0e5 | 218 | const char **details, |
c529695e | 219 | uid_t good_user, |
40ca29a1 LP |
220 | bool *_challenge, |
221 | sd_bus_error *e) { | |
222 | ||
40ca29a1 LP |
223 | int r; |
224 | ||
f3885791 | 225 | assert(call); |
40ca29a1 LP |
226 | assert(action); |
227 | ||
ceb24229 LP |
228 | /* Tests non-interactively! */ |
229 | ||
c529695e LP |
230 | r = check_good_user(call, good_user); |
231 | if (r != 0) | |
232 | return r; | |
233 | ||
f3885791 | 234 | r = sd_bus_query_sender_privilege(call, capability); |
5b12334d LP |
235 | if (r < 0) |
236 | return r; | |
f3885791 | 237 | else if (r > 0) |
40ca29a1 | 238 | return 1; |
349cc4a5 | 239 | #if ENABLE_POLKIT |
40ca29a1 | 240 | else { |
4afd3348 LP |
241 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL; |
242 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ceb24229 | 243 | int authorized = false, challenge = false; |
403ed0e5 | 244 | const char *sender, **k, **v; |
5b12334d | 245 | |
f3885791 | 246 | sender = sd_bus_message_get_sender(call); |
5b12334d LP |
247 | if (!sender) |
248 | return -EBADMSG; | |
40ca29a1 | 249 | |
403ed0e5 | 250 | r = sd_bus_message_new_method_call( |
f3885791 | 251 | call->bus, |
403ed0e5 | 252 | &request, |
40ca29a1 LP |
253 | "org.freedesktop.PolicyKit1", |
254 | "/org/freedesktop/PolicyKit1/Authority", | |
255 | "org.freedesktop.PolicyKit1.Authority", | |
403ed0e5 MC |
256 | "CheckAuthorization"); |
257 | if (r < 0) | |
258 | return r; | |
259 | ||
260 | r = sd_bus_message_append( | |
261 | request, | |
262 | "(sa{sv})s", | |
40ca29a1 | 263 | "system-bus-name", 1, "name", "s", sender, |
403ed0e5 MC |
264 | action); |
265 | if (r < 0) | |
266 | return r; | |
267 | ||
268 | r = sd_bus_message_open_container(request, 'a', "{ss}"); | |
269 | if (r < 0) | |
270 | return r; | |
40ca29a1 | 271 | |
403ed0e5 MC |
272 | STRV_FOREACH_PAIR(k, v, details) { |
273 | r = sd_bus_message_append(request, "{ss}", *k, *v); | |
274 | if (r < 0) | |
275 | return r; | |
276 | } | |
277 | ||
278 | r = sd_bus_message_close_container(request); | |
279 | if (r < 0) | |
280 | return r; | |
281 | ||
282 | r = sd_bus_message_append(request, "us", 0, NULL); | |
283 | if (r < 0) | |
284 | return r; | |
285 | ||
286 | r = sd_bus_call(call->bus, request, 0, e, &reply); | |
40ca29a1 LP |
287 | if (r < 0) { |
288 | /* Treat no PK available as access denied */ | |
289 | if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) { | |
290 | sd_bus_error_free(e); | |
291 | return -EACCES; | |
292 | } | |
293 | ||
294 | return r; | |
295 | } | |
296 | ||
313333b4 | 297 | r = sd_bus_message_enter_container(reply, 'r', "bba{ss}"); |
2b49a470 TA |
298 | if (r < 0) |
299 | return r; | |
300 | ||
301 | r = sd_bus_message_read(reply, "bb", &authorized, &challenge); | |
302 | if (r < 0) | |
303 | return r; | |
40ca29a1 LP |
304 | |
305 | if (authorized) | |
306 | return 1; | |
307 | ||
308 | if (_challenge) { | |
309 | *_challenge = challenge; | |
310 | return 0; | |
311 | } | |
312 | } | |
313 | #endif | |
314 | ||
315 | return -EACCES; | |
316 | } | |
317 | ||
349cc4a5 | 318 | #if ENABLE_POLKIT |
40ca29a1 LP |
319 | |
320 | typedef struct AsyncPolkitQuery { | |
321 | sd_bus_message *request, *reply; | |
322 | sd_bus_message_handler_t callback; | |
323 | void *userdata; | |
19befb2d | 324 | sd_bus_slot *slot; |
ebcf1f97 | 325 | Hashmap *registry; |
40ca29a1 LP |
326 | } AsyncPolkitQuery; |
327 | ||
19befb2d | 328 | static void async_polkit_query_free(AsyncPolkitQuery *q) { |
ebcf1f97 LP |
329 | |
330 | if (!q) | |
331 | return; | |
332 | ||
19befb2d | 333 | sd_bus_slot_unref(q->slot); |
ebcf1f97 LP |
334 | |
335 | if (q->registry && q->request) | |
336 | hashmap_remove(q->registry, q->request); | |
337 | ||
338 | sd_bus_message_unref(q->request); | |
339 | sd_bus_message_unref(q->reply); | |
340 | ||
341 | free(q); | |
342 | } | |
343 | ||
19070062 | 344 | static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) { |
4afd3348 | 345 | _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL; |
ebcf1f97 | 346 | AsyncPolkitQuery *q = userdata; |
40ca29a1 LP |
347 | int r; |
348 | ||
40ca29a1 LP |
349 | assert(reply); |
350 | assert(q); | |
351 | ||
19befb2d | 352 | q->slot = sd_bus_slot_unref(q->slot); |
40ca29a1 | 353 | q->reply = sd_bus_message_ref(reply); |
40ca29a1 | 354 | |
ebcf1f97 LP |
355 | r = sd_bus_message_rewind(q->request, true); |
356 | if (r < 0) { | |
357 | r = sd_bus_reply_method_errno(q->request, r, NULL); | |
358 | goto finish; | |
359 | } | |
40ca29a1 | 360 | |
19070062 | 361 | r = q->callback(q->request, q->userdata, &error_buffer); |
ebcf1f97 | 362 | r = bus_maybe_reply_error(q->request, r, &error_buffer); |
40ca29a1 | 363 | |
ebcf1f97 | 364 | finish: |
19befb2d LP |
365 | async_polkit_query_free(q); |
366 | ||
ebcf1f97 | 367 | return r; |
40ca29a1 LP |
368 | } |
369 | ||
370 | #endif | |
371 | ||
372 | int bus_verify_polkit_async( | |
f3885791 | 373 | sd_bus_message *call, |
def9a7aa | 374 | int capability, |
40ca29a1 | 375 | const char *action, |
403ed0e5 | 376 | const char **details, |
40ca29a1 | 377 | bool interactive, |
c529695e | 378 | uid_t good_user, |
f3885791 LP |
379 | Hashmap **registry, |
380 | sd_bus_error *error) { | |
40ca29a1 | 381 | |
349cc4a5 | 382 | #if ENABLE_POLKIT |
4afd3348 | 383 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL; |
40ca29a1 | 384 | AsyncPolkitQuery *q; |
403ed0e5 | 385 | const char *sender, **k, **v; |
f3885791 LP |
386 | sd_bus_message_handler_t callback; |
387 | void *userdata; | |
b911eb15 | 388 | int c; |
5b12334d | 389 | #endif |
40ca29a1 LP |
390 | int r; |
391 | ||
f3885791 | 392 | assert(call); |
40ca29a1 | 393 | assert(action); |
f3885791 | 394 | assert(registry); |
40ca29a1 | 395 | |
c529695e LP |
396 | r = check_good_user(call, good_user); |
397 | if (r != 0) | |
398 | return r; | |
399 | ||
349cc4a5 | 400 | #if ENABLE_POLKIT |
f3885791 | 401 | q = hashmap_get(*registry, call); |
40ca29a1 | 402 | if (q) { |
102d8f81 | 403 | int authorized, challenge; |
40ca29a1 LP |
404 | |
405 | /* This is the second invocation of this function, and | |
406 | * there's already a response from polkit, let's | |
407 | * process it */ | |
408 | assert(q->reply); | |
409 | ||
410 | if (sd_bus_message_is_method_error(q->reply, NULL)) { | |
411 | const sd_bus_error *e; | |
412 | ||
ebcf1f97 | 413 | /* Copy error from polkit reply */ |
40ca29a1 LP |
414 | e = sd_bus_message_get_error(q->reply); |
415 | sd_bus_error_copy(error, e); | |
40ca29a1 | 416 | |
ebcf1f97 LP |
417 | /* Treat no PK available as access denied */ |
418 | if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) | |
419 | return -EACCES; | |
420 | ||
5958d089 | 421 | return -sd_bus_error_get_errno(e); |
40ca29a1 LP |
422 | } |
423 | ||
424 | r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}"); | |
425 | if (r >= 0) | |
426 | r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge); | |
427 | ||
40ca29a1 LP |
428 | if (r < 0) |
429 | return r; | |
430 | ||
431 | if (authorized) | |
432 | return 1; | |
433 | ||
f2288cc6 LP |
434 | if (challenge) |
435 | return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required."); | |
436 | ||
40ca29a1 LP |
437 | return -EACCES; |
438 | } | |
439 | #endif | |
440 | ||
f3885791 | 441 | r = sd_bus_query_sender_privilege(call, capability); |
5b12334d LP |
442 | if (r < 0) |
443 | return r; | |
f3885791 | 444 | else if (r > 0) |
40ca29a1 | 445 | return 1; |
5b12334d | 446 | |
349cc4a5 | 447 | #if ENABLE_POLKIT |
f3885791 LP |
448 | if (sd_bus_get_current_message(call->bus) != call) |
449 | return -EINVAL; | |
450 | ||
451 | callback = sd_bus_get_current_handler(call->bus); | |
452 | if (!callback) | |
453 | return -EINVAL; | |
454 | ||
455 | userdata = sd_bus_get_current_userdata(call->bus); | |
456 | ||
457 | sender = sd_bus_message_get_sender(call); | |
5b12334d LP |
458 | if (!sender) |
459 | return -EBADMSG; | |
40ca29a1 | 460 | |
b911eb15 LP |
461 | c = sd_bus_message_get_allow_interactive_authorization(call); |
462 | if (c < 0) | |
463 | return c; | |
464 | if (c > 0) | |
465 | interactive = true; | |
466 | ||
d5099efc | 467 | r = hashmap_ensure_allocated(registry, NULL); |
40ca29a1 LP |
468 | if (r < 0) |
469 | return r; | |
470 | ||
471 | r = sd_bus_message_new_method_call( | |
f3885791 | 472 | call->bus, |
151b9b96 | 473 | &pk, |
40ca29a1 LP |
474 | "org.freedesktop.PolicyKit1", |
475 | "/org/freedesktop/PolicyKit1/Authority", | |
476 | "org.freedesktop.PolicyKit1.Authority", | |
151b9b96 | 477 | "CheckAuthorization"); |
40ca29a1 LP |
478 | if (r < 0) |
479 | return r; | |
480 | ||
481 | r = sd_bus_message_append( | |
482 | pk, | |
403ed0e5 | 483 | "(sa{sv})s", |
40ca29a1 | 484 | "system-bus-name", 1, "name", "s", sender, |
403ed0e5 MC |
485 | action); |
486 | if (r < 0) | |
487 | return r; | |
488 | ||
489 | r = sd_bus_message_open_container(pk, 'a', "{ss}"); | |
490 | if (r < 0) | |
491 | return r; | |
492 | ||
493 | STRV_FOREACH_PAIR(k, v, details) { | |
494 | r = sd_bus_message_append(pk, "{ss}", *k, *v); | |
495 | if (r < 0) | |
496 | return r; | |
497 | } | |
498 | ||
499 | r = sd_bus_message_close_container(pk); | |
500 | if (r < 0) | |
501 | return r; | |
502 | ||
503 | r = sd_bus_message_append(pk, "us", !!interactive, NULL); | |
40ca29a1 LP |
504 | if (r < 0) |
505 | return r; | |
506 | ||
507 | q = new0(AsyncPolkitQuery, 1); | |
508 | if (!q) | |
509 | return -ENOMEM; | |
510 | ||
f3885791 | 511 | q->request = sd_bus_message_ref(call); |
40ca29a1 LP |
512 | q->callback = callback; |
513 | q->userdata = userdata; | |
514 | ||
f3885791 | 515 | r = hashmap_put(*registry, call, q); |
40ca29a1 | 516 | if (r < 0) { |
19befb2d | 517 | async_polkit_query_free(q); |
40ca29a1 LP |
518 | return r; |
519 | } | |
520 | ||
ebcf1f97 LP |
521 | q->registry = *registry; |
522 | ||
f3885791 | 523 | r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0); |
ebcf1f97 | 524 | if (r < 0) { |
19befb2d | 525 | async_polkit_query_free(q); |
40ca29a1 | 526 | return r; |
ebcf1f97 | 527 | } |
40ca29a1 LP |
528 | |
529 | return 0; | |
530 | #endif | |
531 | ||
532 | return -EACCES; | |
533 | } | |
534 | ||
36e34057 | 535 | void bus_verify_polkit_async_registry_free(Hashmap *registry) { |
349cc4a5 | 536 | #if ENABLE_POLKIT |
224b0e7a | 537 | hashmap_free_with_destructor(registry, async_polkit_query_free); |
40ca29a1 LP |
538 | #endif |
539 | } | |
0c842e0a | 540 | |
718db961 | 541 | int bus_check_peercred(sd_bus *c) { |
0c842e0a | 542 | struct ucred ucred; |
3e641e36 | 543 | int fd, r; |
0c842e0a TG |
544 | |
545 | assert(c); | |
546 | ||
547 | fd = sd_bus_get_fd(c); | |
0f8bd8de LP |
548 | if (fd < 0) |
549 | return fd; | |
0c842e0a | 550 | |
3e641e36 LP |
551 | r = getpeercred(fd, &ucred); |
552 | if (r < 0) | |
553 | return r; | |
0c842e0a TG |
554 | |
555 | if (ucred.uid != 0 && ucred.uid != geteuid()) | |
556 | return -EPERM; | |
557 | ||
558 | return 1; | |
559 | } | |
560 | ||
266f3e26 | 561 | int bus_connect_system_systemd(sd_bus **_bus) { |
4afd3348 | 562 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
0c842e0a | 563 | int r; |
0c842e0a TG |
564 | |
565 | assert(_bus); | |
566 | ||
0f8bd8de | 567 | if (geteuid() != 0) |
266f3e26 | 568 | return sd_bus_default_system(_bus); |
a1da8583 | 569 | |
a132bef0 ZJS |
570 | /* If we are root then let's talk directly to the system |
571 | * instance, instead of going via the bus */ | |
a6aa8912 LP |
572 | |
573 | r = sd_bus_new(&bus); | |
0f8bd8de LP |
574 | if (r < 0) |
575 | return r; | |
a1da8583 | 576 | |
a6aa8912 LP |
577 | r = sd_bus_set_address(bus, "unix:path=/run/systemd/private"); |
578 | if (r < 0) | |
579 | return r; | |
580 | ||
581 | r = sd_bus_start(bus); | |
582 | if (r < 0) | |
266f3e26 | 583 | return sd_bus_default_system(_bus); |
a6aa8912 | 584 | |
0f8bd8de | 585 | r = bus_check_peercred(bus); |
a1da8583 TG |
586 | if (r < 0) |
587 | return r; | |
588 | ||
1cc6c93a | 589 | *_bus = TAKE_PTR(bus); |
0f8bd8de | 590 | |
a1da8583 TG |
591 | return 0; |
592 | } | |
593 | ||
266f3e26 | 594 | int bus_connect_user_systemd(sd_bus **_bus) { |
4afd3348 | 595 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
a6aa8912 | 596 | _cleanup_free_ char *ee = NULL; |
41dd15e4 LP |
597 | const char *e; |
598 | int r; | |
599 | ||
41dd15e4 LP |
600 | assert(_bus); |
601 | ||
602 | e = secure_getenv("XDG_RUNTIME_DIR"); | |
537220d9 | 603 | if (!e) |
266f3e26 | 604 | return sd_bus_default_user(_bus); |
537220d9 | 605 | |
a6aa8912 LP |
606 | ee = bus_address_escape(e); |
607 | if (!ee) | |
537220d9 | 608 | return -ENOMEM; |
41dd15e4 LP |
609 | |
610 | r = sd_bus_new(&bus); | |
611 | if (r < 0) | |
612 | return r; | |
613 | ||
605405c6 | 614 | bus->address = strjoin("unix:path=", ee, "/systemd/private"); |
a6aa8912 LP |
615 | if (!bus->address) |
616 | return -ENOMEM; | |
41dd15e4 LP |
617 | |
618 | r = sd_bus_start(bus); | |
619 | if (r < 0) | |
266f3e26 | 620 | return sd_bus_default_user(_bus); |
41dd15e4 LP |
621 | |
622 | r = bus_check_peercred(bus); | |
623 | if (r < 0) | |
624 | return r; | |
625 | ||
1cc6c93a | 626 | *_bus = TAKE_PTR(bus); |
41dd15e4 LP |
627 | |
628 | return 0; | |
629 | } | |
630 | ||
4f9a9105 ZJS |
631 | #define print_property(name, fmt, ...) \ |
632 | do { \ | |
633 | if (value) \ | |
634 | printf(fmt "\n", __VA_ARGS__); \ | |
635 | else \ | |
636 | printf("%s=" fmt "\n", name, __VA_ARGS__); \ | |
508f63b4 | 637 | } while (0) |
4f9a9105 | 638 | |
07636114 | 639 | int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all) { |
a1da8583 TG |
640 | char type; |
641 | const char *contents; | |
9f6eb1cd | 642 | int r; |
a1da8583 TG |
643 | |
644 | assert(name); | |
07636114 | 645 | assert(m); |
a1da8583 | 646 | |
07636114 | 647 | r = sd_bus_message_peek_type(m, &type, &contents); |
9f6eb1cd KS |
648 | if (r < 0) |
649 | return r; | |
a1da8583 TG |
650 | |
651 | switch (type) { | |
652 | ||
653 | case SD_BUS_TYPE_STRING: { | |
654 | const char *s; | |
9f6eb1cd | 655 | |
07636114 | 656 | r = sd_bus_message_read_basic(m, type, &s); |
9f6eb1cd KS |
657 | if (r < 0) |
658 | return r; | |
a1da8583 | 659 | |
5993d46a ZJS |
660 | if (all || !isempty(s)) { |
661 | bool good; | |
662 | ||
663 | /* This property has a single value, so we need to take | |
664 | * care not to print a new line, everything else is OK. */ | |
665 | good = !strchr(s, '\n'); | |
666 | print_property(name, "%s", good ? s : "[unprintable]"); | |
667 | } | |
a1da8583 TG |
668 | |
669 | return 1; | |
670 | } | |
671 | ||
672 | case SD_BUS_TYPE_BOOLEAN: { | |
c2fa048c | 673 | int b; |
a1da8583 | 674 | |
07636114 | 675 | r = sd_bus_message_read_basic(m, type, &b); |
9f6eb1cd KS |
676 | if (r < 0) |
677 | return r; | |
678 | ||
4f9a9105 | 679 | print_property(name, "%s", yes_no(b)); |
a1da8583 TG |
680 | |
681 | return 1; | |
682 | } | |
683 | ||
684 | case SD_BUS_TYPE_UINT64: { | |
685 | uint64_t u; | |
686 | ||
07636114 | 687 | r = sd_bus_message_read_basic(m, type, &u); |
9f6eb1cd KS |
688 | if (r < 0) |
689 | return r; | |
a1da8583 TG |
690 | |
691 | /* Yes, heuristics! But we can change this check | |
692 | * should it turn out to not be sufficient */ | |
693 | ||
ead0adb1 YW |
694 | if (endswith(name, "Timestamp") || |
695 | STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) { | |
4d9685be ZJS |
696 | char timestamp[FORMAT_TIMESTAMP_MAX]; |
697 | const char *t; | |
a1da8583 TG |
698 | |
699 | t = format_timestamp(timestamp, sizeof(timestamp), u); | |
700 | if (t || all) | |
4f9a9105 | 701 | print_property(name, "%s", strempty(t)); |
a1da8583 TG |
702 | |
703 | } else if (strstr(name, "USec")) { | |
704 | char timespan[FORMAT_TIMESPAN_MAX]; | |
705 | ||
4f9a9105 | 706 | print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0)); |
73186d53 DH |
707 | } else if (streq(name, "RestrictNamespaces")) { |
708 | _cleanup_free_ char *s = NULL; | |
ae978b9f | 709 | const char *result; |
73186d53 DH |
710 | |
711 | if ((u & NAMESPACE_FLAGS_ALL) == 0) | |
712 | result = "yes"; | |
713 | else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL) | |
714 | result = "no"; | |
715 | else { | |
86c2a9f1 | 716 | r = namespace_flags_to_string(u, &s); |
73186d53 DH |
717 | if (r < 0) |
718 | return r; | |
719 | ||
720 | result = s; | |
721 | } | |
722 | ||
723 | print_property(name, "%s", result); | |
21771f33 YW |
724 | |
725 | } else if (streq(name, "MountFlags")) { | |
ae978b9f | 726 | const char *result; |
21771f33 YW |
727 | |
728 | result = mount_propagation_flags_to_string(u); | |
729 | if (!result) | |
730 | return -EINVAL; | |
731 | ||
732 | print_property(name, "%s", result); | |
733 | ||
52610b02 YW |
734 | } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) { |
735 | _cleanup_free_ char *s = NULL; | |
736 | ||
737 | r = capability_set_to_string_alloc(u, &s); | |
738 | if (r < 0) | |
739 | return r; | |
740 | ||
741 | print_property(name, "%s", s); | |
742 | ||
21771f33 YW |
743 | } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) || |
744 | (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) || | |
745 | (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) || | |
746 | (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) || | |
747 | (endswith(name, "NSec") && u == (uint64_t) -1)) | |
748 | ||
749 | print_property(name, "%s", "[not set]"); | |
750 | ||
751 | else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) || | |
752 | (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) || | |
753 | (startswith(name, "Limit") && u == (uint64_t) -1) || | |
754 | (startswith(name, "DefaultLimit") && u == (uint64_t) -1)) | |
755 | ||
756 | print_property(name, "%s", "infinity"); | |
757 | else | |
4f9a9105 | 758 | print_property(name, "%"PRIu64, u); |
a1da8583 TG |
759 | |
760 | return 1; | |
761 | } | |
762 | ||
d7161865 DM |
763 | case SD_BUS_TYPE_INT64: { |
764 | int64_t i; | |
765 | ||
07636114 | 766 | r = sd_bus_message_read_basic(m, type, &i); |
d7161865 DM |
767 | if (r < 0) |
768 | return r; | |
769 | ||
4f9a9105 | 770 | print_property(name, "%"PRIi64, i); |
d7161865 DM |
771 | |
772 | return 1; | |
773 | } | |
774 | ||
a1da8583 TG |
775 | case SD_BUS_TYPE_UINT32: { |
776 | uint32_t u; | |
777 | ||
07636114 | 778 | r = sd_bus_message_read_basic(m, type, &u); |
9f6eb1cd KS |
779 | if (r < 0) |
780 | return r; | |
a1da8583 TG |
781 | |
782 | if (strstr(name, "UMask") || strstr(name, "Mode")) | |
4f9a9105 | 783 | print_property(name, "%04o", u); |
c533658a ZJS |
784 | else if (streq(name, "UID")) { |
785 | if (u == UID_INVALID) | |
786 | print_property(name, "%s", "[not set]"); | |
787 | else | |
788 | print_property(name, "%"PRIu32, u); | |
789 | } else if (streq(name, "GID")) { | |
790 | if (u == GID_INVALID) | |
791 | print_property(name, "%s", "[not set]"); | |
792 | else | |
793 | print_property(name, "%"PRIu32, u); | |
794 | } else | |
4f9a9105 | 795 | print_property(name, "%"PRIu32, u); |
a1da8583 TG |
796 | |
797 | return 1; | |
798 | } | |
799 | ||
800 | case SD_BUS_TYPE_INT32: { | |
801 | int32_t i; | |
802 | ||
07636114 | 803 | r = sd_bus_message_read_basic(m, type, &i); |
9f6eb1cd KS |
804 | if (r < 0) |
805 | return r; | |
a1da8583 | 806 | |
4f9a9105 | 807 | print_property(name, "%"PRIi32, i); |
a1da8583 TG |
808 | return 1; |
809 | } | |
810 | ||
811 | case SD_BUS_TYPE_DOUBLE: { | |
812 | double d; | |
813 | ||
07636114 | 814 | r = sd_bus_message_read_basic(m, type, &d); |
9f6eb1cd KS |
815 | if (r < 0) |
816 | return r; | |
a1da8583 | 817 | |
4f9a9105 | 818 | print_property(name, "%g", d); |
a1da8583 TG |
819 | return 1; |
820 | } | |
821 | ||
822 | case SD_BUS_TYPE_ARRAY: | |
a1da8583 | 823 | if (streq(contents, "s")) { |
261afec5 MAP |
824 | bool first = true; |
825 | const char *str; | |
a1da8583 | 826 | |
07636114 | 827 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents); |
9f6eb1cd KS |
828 | if (r < 0) |
829 | return r; | |
830 | ||
07636114 | 831 | while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) { |
5993d46a ZJS |
832 | bool good; |
833 | ||
4f9a9105 | 834 | if (first && !value) |
261afec5 MAP |
835 | printf("%s=", name); |
836 | ||
ed88a900 | 837 | /* This property has multiple space-separated values, so |
ead6bd25 | 838 | * neither spaces nor newlines can be allowed in a value. */ |
5993d46a ZJS |
839 | good = str[strcspn(str, " \n")] == '\0'; |
840 | ||
841 | printf("%s%s", first ? "" : " ", good ? str : "[unprintable]"); | |
261afec5 MAP |
842 | |
843 | first = false; | |
844 | } | |
9f6eb1cd KS |
845 | if (r < 0) |
846 | return r; | |
a1da8583 | 847 | |
4f9a9105 | 848 | if (first && all && !value) |
a1da8583 | 849 | printf("%s=", name); |
261afec5 | 850 | if (!first || all) |
a1da8583 | 851 | puts(""); |
a1da8583 | 852 | |
07636114 | 853 | r = sd_bus_message_exit_container(m); |
9f6eb1cd KS |
854 | if (r < 0) |
855 | return r; | |
a1da8583 TG |
856 | |
857 | return 1; | |
858 | ||
859 | } else if (streq(contents, "y")) { | |
860 | const uint8_t *u; | |
861 | size_t n; | |
862 | ||
07636114 | 863 | r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n); |
9f6eb1cd KS |
864 | if (r < 0) |
865 | return r; | |
866 | ||
a1da8583 TG |
867 | if (all || n > 0) { |
868 | unsigned int i; | |
869 | ||
4f9a9105 ZJS |
870 | if (!value) |
871 | printf("%s=", name); | |
a1da8583 TG |
872 | |
873 | for (i = 0; i < n; i++) | |
874 | printf("%02x", u[i]); | |
875 | ||
876 | puts(""); | |
877 | } | |
878 | ||
879 | return 1; | |
880 | ||
881 | } else if (streq(contents, "u")) { | |
882 | uint32_t *u; | |
883 | size_t n; | |
884 | ||
07636114 | 885 | r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n); |
9f6eb1cd KS |
886 | if (r < 0) |
887 | return r; | |
888 | ||
a1da8583 TG |
889 | if (all || n > 0) { |
890 | unsigned int i; | |
891 | ||
4f9a9105 ZJS |
892 | if (!value) |
893 | printf("%s=", name); | |
a1da8583 TG |
894 | |
895 | for (i = 0; i < n; i++) | |
896 | printf("%08x", u[i]); | |
897 | ||
898 | puts(""); | |
899 | } | |
900 | ||
901 | return 1; | |
902 | } | |
903 | ||
904 | break; | |
905 | } | |
906 | ||
907 | return 0; | |
908 | } | |
d21ed1ea | 909 | |
07636114 YW |
910 | int bus_message_print_all_properties( |
911 | sd_bus_message *m, | |
912 | bus_message_print_t func, | |
913 | char **filter, | |
914 | bool value, | |
915 | bool all, | |
916 | Set **found_properties) { | |
ffc06c35 | 917 | |
07636114 | 918 | int r; |
9f6eb1cd | 919 | |
07636114 | 920 | assert(m); |
ffc06c35 | 921 | |
07636114 | 922 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
ffc06c35 KS |
923 | if (r < 0) |
924 | return r; | |
925 | ||
07636114 | 926 | while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { |
ffc06c35 | 927 | const char *name; |
ffc06c35 | 928 | const char *contents; |
ffc06c35 | 929 | |
07636114 | 930 | r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name); |
ffc06c35 KS |
931 | if (r < 0) |
932 | return r; | |
933 | ||
07636114 YW |
934 | if (found_properties) { |
935 | r = set_ensure_allocated(found_properties, &string_hash_ops); | |
936 | if (r < 0) | |
937 | return log_oom(); | |
938 | ||
939 | r = set_put(*found_properties, name); | |
940 | if (r < 0 && r != EEXIST) | |
941 | return log_oom(); | |
942 | } | |
943 | ||
9f6eb1cd | 944 | if (!filter || strv_find(filter, name)) { |
07636114 | 945 | r = sd_bus_message_peek_type(m, NULL, &contents); |
9f6eb1cd KS |
946 | if (r < 0) |
947 | return r; | |
948 | ||
07636114 | 949 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); |
9f6eb1cd KS |
950 | if (r < 0) |
951 | return r; | |
ffc06c35 | 952 | |
07636114 YW |
953 | if (func) |
954 | r = func(name, m, value, all); | |
955 | if (!func || r == 0) | |
956 | r = bus_print_property(name, m, value, all); | |
9f6eb1cd KS |
957 | if (r < 0) |
958 | return r; | |
27e72d6b SP |
959 | if (r == 0) { |
960 | if (all) | |
961 | printf("%s=[unprintable]\n", name); | |
962 | /* skip what we didn't read */ | |
07636114 | 963 | r = sd_bus_message_skip(m, contents); |
27e72d6b SP |
964 | if (r < 0) |
965 | return r; | |
966 | } | |
9f6eb1cd | 967 | |
07636114 | 968 | r = sd_bus_message_exit_container(m); |
9f6eb1cd KS |
969 | if (r < 0) |
970 | return r; | |
971 | } else { | |
07636114 | 972 | r = sd_bus_message_skip(m, "v"); |
9f6eb1cd KS |
973 | if (r < 0) |
974 | return r; | |
975 | } | |
976 | ||
07636114 | 977 | r = sd_bus_message_exit_container(m); |
ffc06c35 KS |
978 | if (r < 0) |
979 | return r; | |
9f6eb1cd KS |
980 | } |
981 | if (r < 0) | |
982 | return r; | |
ffc06c35 | 983 | |
07636114 | 984 | r = sd_bus_message_exit_container(m); |
9f6eb1cd KS |
985 | if (r < 0) |
986 | return r; | |
ffc06c35 | 987 | |
9f6eb1cd KS |
988 | return 0; |
989 | } | |
ffc06c35 | 990 | |
07636114 YW |
991 | int bus_print_all_properties( |
992 | sd_bus *bus, | |
993 | const char *dest, | |
994 | const char *path, | |
995 | bus_message_print_t func, | |
996 | char **filter, | |
997 | bool value, | |
998 | bool all, | |
999 | Set **found_properties) { | |
1000 | ||
1001 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1002 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1003 | int r; | |
1004 | ||
1005 | assert(bus); | |
1006 | assert(path); | |
1007 | ||
1008 | r = sd_bus_call_method(bus, | |
1009 | dest, | |
1010 | path, | |
1011 | "org.freedesktop.DBus.Properties", | |
1012 | "GetAll", | |
1013 | &error, | |
1014 | &reply, | |
1015 | "s", ""); | |
1016 | if (r < 0) | |
1017 | return r; | |
1018 | ||
1019 | return bus_message_print_all_properties(reply, func, filter, value, all, found_properties); | |
1020 | } | |
1021 | ||
9f6eb1cd KS |
1022 | int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1023 | sd_id128_t *p = userdata; | |
1024 | const void *v; | |
1025 | size_t n; | |
1026 | int r; | |
ffc06c35 | 1027 | |
9f6eb1cd KS |
1028 | r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n); |
1029 | if (r < 0) | |
1030 | return r; | |
ffc06c35 | 1031 | |
9f6eb1cd KS |
1032 | if (n == 0) |
1033 | *p = SD_ID128_NULL; | |
1034 | else if (n == 16) | |
1035 | memcpy((*p).bytes, v, n); | |
1036 | else | |
1037 | return -EINVAL; | |
ffc06c35 | 1038 | |
9f6eb1cd KS |
1039 | return 0; |
1040 | } | |
ffc06c35 | 1041 | |
a7e4861c | 1042 | static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) { |
9f6eb1cd KS |
1043 | char type; |
1044 | int r; | |
ffc06c35 | 1045 | |
9f6eb1cd KS |
1046 | r = sd_bus_message_peek_type(m, &type, NULL); |
1047 | if (r < 0) | |
1048 | return r; | |
ffc06c35 | 1049 | |
9f6eb1cd | 1050 | switch (type) { |
8b3b6f58 | 1051 | |
9f6eb1cd | 1052 | case SD_BUS_TYPE_STRING: { |
f37f8a61 | 1053 | const char **p = userdata; |
8b3b6f58 | 1054 | const char *s; |
ffc06c35 | 1055 | |
9f6eb1cd KS |
1056 | r = sd_bus_message_read_basic(m, type, &s); |
1057 | if (r < 0) | |
8b3b6f58 | 1058 | return r; |
ffc06c35 | 1059 | |
9f6eb1cd | 1060 | if (isempty(s)) |
0b83b8a4 | 1061 | s = NULL; |
ffc06c35 | 1062 | |
a7e4861c | 1063 | if (flags & BUS_MAP_STRDUP) |
f37f8a61 YW |
1064 | return free_and_strdup((char **) userdata, s); |
1065 | ||
1066 | *p = s; | |
1067 | return 0; | |
9f6eb1cd | 1068 | } |
ffc06c35 | 1069 | |
9f6eb1cd | 1070 | case SD_BUS_TYPE_ARRAY: { |
7d6884b6 TA |
1071 | _cleanup_strv_free_ char **l = NULL; |
1072 | char ***p = userdata; | |
ffc06c35 | 1073 | |
9f6eb1cd KS |
1074 | r = bus_message_read_strv_extend(m, &l); |
1075 | if (r < 0) | |
8b3b6f58 | 1076 | return r; |
ffc06c35 | 1077 | |
130d3d22 | 1078 | return strv_free_and_replace(*p, l); |
9f6eb1cd | 1079 | } |
ffc06c35 | 1080 | |
9f6eb1cd | 1081 | case SD_BUS_TYPE_BOOLEAN: { |
a7e4861c | 1082 | int b; |
ffc06c35 | 1083 | |
9f6eb1cd KS |
1084 | r = sd_bus_message_read_basic(m, type, &b); |
1085 | if (r < 0) | |
8b3b6f58 | 1086 | return r; |
ffc06c35 | 1087 | |
a7e4861c | 1088 | if (flags & BUS_MAP_BOOLEAN_AS_BOOL) |
5d904a6a | 1089 | *(bool*) userdata = b; |
a7e4861c | 1090 | else |
5d904a6a | 1091 | *(int*) userdata = b; |
a7e4861c | 1092 | |
8b3b6f58 | 1093 | return 0; |
9f6eb1cd | 1094 | } |
ffc06c35 | 1095 | |
bdf97b8a | 1096 | case SD_BUS_TYPE_INT32: |
9f6eb1cd | 1097 | case SD_BUS_TYPE_UINT32: { |
bdf97b8a | 1098 | uint32_t u, *p = userdata; |
9f6eb1cd KS |
1099 | |
1100 | r = sd_bus_message_read_basic(m, type, &u); | |
1101 | if (r < 0) | |
8b3b6f58 | 1102 | return r; |
ffc06c35 | 1103 | |
9f6eb1cd | 1104 | *p = u; |
8b3b6f58 | 1105 | return 0; |
9f6eb1cd KS |
1106 | } |
1107 | ||
bdf97b8a | 1108 | case SD_BUS_TYPE_INT64: |
9f6eb1cd | 1109 | case SD_BUS_TYPE_UINT64: { |
bdf97b8a | 1110 | uint64_t t, *p = userdata; |
9f6eb1cd KS |
1111 | |
1112 | r = sd_bus_message_read_basic(m, type, &t); | |
1113 | if (r < 0) | |
8b3b6f58 | 1114 | return r; |
ffc06c35 | 1115 | |
9f6eb1cd | 1116 | *p = t; |
8b3b6f58 | 1117 | return 0; |
9f6eb1cd KS |
1118 | } |
1119 | ||
52e045f9 | 1120 | case SD_BUS_TYPE_DOUBLE: { |
8b3b6f58 | 1121 | double d, *p = userdata; |
52e045f9 SS |
1122 | |
1123 | r = sd_bus_message_read_basic(m, type, &d); | |
1124 | if (r < 0) | |
8b3b6f58 | 1125 | return r; |
52e045f9 SS |
1126 | |
1127 | *p = d; | |
8b3b6f58 LP |
1128 | return 0; |
1129 | }} | |
52e045f9 | 1130 | |
8b3b6f58 | 1131 | return -EOPNOTSUPP; |
9f6eb1cd KS |
1132 | } |
1133 | ||
fe506d56 LP |
1134 | int bus_message_map_all_properties( |
1135 | sd_bus_message *m, | |
1136 | const struct bus_properties_map *map, | |
a7e4861c | 1137 | unsigned flags, |
f9e0eefc | 1138 | sd_bus_error *error, |
fe506d56 LP |
1139 | void *userdata) { |
1140 | ||
9f6eb1cd KS |
1141 | int r; |
1142 | ||
aae2b488 | 1143 | assert(m); |
9f6eb1cd KS |
1144 | assert(map); |
1145 | ||
9f6eb1cd KS |
1146 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
1147 | if (r < 0) | |
1148 | return r; | |
1149 | ||
1150 | while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { | |
1151 | const struct bus_properties_map *prop; | |
1152 | const char *member; | |
1153 | const char *contents; | |
1154 | void *v; | |
1155 | unsigned i; | |
1156 | ||
1157 | r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member); | |
ffc06c35 KS |
1158 | if (r < 0) |
1159 | return r; | |
1160 | ||
9f6eb1cd KS |
1161 | for (i = 0, prop = NULL; map[i].member; i++) |
1162 | if (streq(map[i].member, member)) { | |
1163 | prop = &map[i]; | |
1164 | break; | |
1165 | } | |
1166 | ||
1167 | if (prop) { | |
1168 | r = sd_bus_message_peek_type(m, NULL, &contents); | |
1169 | if (r < 0) | |
1170 | return r; | |
1171 | ||
1172 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); | |
1173 | if (r < 0) | |
1174 | return r; | |
1175 | ||
1176 | v = (uint8_t *)userdata + prop->offset; | |
27e72d6b | 1177 | if (map[i].set) |
f9e0eefc | 1178 | r = prop->set(sd_bus_message_get_bus(m), member, m, error, v); |
9f6eb1cd | 1179 | else |
a7e4861c | 1180 | r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v); |
2b49a470 TA |
1181 | if (r < 0) |
1182 | return r; | |
9f6eb1cd KS |
1183 | |
1184 | r = sd_bus_message_exit_container(m); | |
1185 | if (r < 0) | |
1186 | return r; | |
1187 | } else { | |
1188 | r = sd_bus_message_skip(m, "v"); | |
1189 | if (r < 0) | |
6c1508b8 | 1190 | return r; |
9f6eb1cd KS |
1191 | } |
1192 | ||
ffc06c35 KS |
1193 | r = sd_bus_message_exit_container(m); |
1194 | if (r < 0) | |
1195 | return r; | |
1196 | } | |
fe506d56 LP |
1197 | if (r < 0) |
1198 | return r; | |
ffc06c35 | 1199 | |
aae2b488 DH |
1200 | return sd_bus_message_exit_container(m); |
1201 | } | |
1202 | ||
fe506d56 LP |
1203 | int bus_message_map_properties_changed( |
1204 | sd_bus_message *m, | |
1205 | const struct bus_properties_map *map, | |
a7e4861c | 1206 | unsigned flags, |
f9e0eefc | 1207 | sd_bus_error *error, |
fe506d56 LP |
1208 | void *userdata) { |
1209 | ||
aae2b488 DH |
1210 | const char *member; |
1211 | int r, invalidated, i; | |
1212 | ||
aae2b488 DH |
1213 | assert(m); |
1214 | assert(map); | |
1215 | ||
a7e4861c | 1216 | r = bus_message_map_all_properties(m, map, flags, error, userdata); |
aae2b488 DH |
1217 | if (r < 0) |
1218 | return r; | |
1219 | ||
1220 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s"); | |
1221 | if (r < 0) | |
1222 | return r; | |
1223 | ||
1224 | invalidated = 0; | |
1225 | while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0) | |
1226 | for (i = 0; map[i].member; i++) | |
1227 | if (streq(map[i].member, member)) { | |
1228 | ++invalidated; | |
1229 | break; | |
1230 | } | |
fe506d56 LP |
1231 | if (r < 0) |
1232 | return r; | |
aae2b488 DH |
1233 | |
1234 | r = sd_bus_message_exit_container(m); | |
1235 | if (r < 0) | |
1236 | return r; | |
1237 | ||
1238 | return invalidated; | |
1239 | } | |
1240 | ||
fe506d56 LP |
1241 | int bus_map_all_properties( |
1242 | sd_bus *bus, | |
1243 | const char *destination, | |
1244 | const char *path, | |
1245 | const struct bus_properties_map *map, | |
a7e4861c | 1246 | unsigned flags, |
f9e0eefc | 1247 | sd_bus_error *error, |
f37f8a61 | 1248 | sd_bus_message **reply, |
fe506d56 LP |
1249 | void *userdata) { |
1250 | ||
4afd3348 | 1251 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
aae2b488 DH |
1252 | int r; |
1253 | ||
1254 | assert(bus); | |
1255 | assert(destination); | |
1256 | assert(path); | |
1257 | assert(map); | |
a7e4861c | 1258 | assert(reply || (flags & BUS_MAP_STRDUP)); |
aae2b488 DH |
1259 | |
1260 | r = sd_bus_call_method( | |
1261 | bus, | |
1262 | destination, | |
1263 | path, | |
1264 | "org.freedesktop.DBus.Properties", | |
1265 | "GetAll", | |
f9e0eefc | 1266 | error, |
aae2b488 DH |
1267 | &m, |
1268 | "s", ""); | |
1269 | if (r < 0) | |
1270 | return r; | |
1271 | ||
a7e4861c | 1272 | r = bus_message_map_all_properties(m, map, flags, error, userdata); |
f37f8a61 YW |
1273 | if (r < 0) |
1274 | return r; | |
1275 | ||
1276 | if (reply) | |
1277 | *reply = sd_bus_message_ref(m); | |
1278 | ||
1279 | return r; | |
ffc06c35 KS |
1280 | } |
1281 | ||
38303498 LP |
1282 | int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) { |
1283 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
d21ed1ea LP |
1284 | int r; |
1285 | ||
1286 | assert(transport >= 0); | |
1287 | assert(transport < _BUS_TRANSPORT_MAX); | |
38303498 | 1288 | assert(ret); |
d21ed1ea LP |
1289 | |
1290 | assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); | |
15411c0c | 1291 | assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP); |
d21ed1ea LP |
1292 | |
1293 | switch (transport) { | |
1294 | ||
1295 | case BUS_TRANSPORT_LOCAL: | |
1296 | if (user) | |
38303498 | 1297 | r = sd_bus_default_user(&bus); |
fb507898 YW |
1298 | else { |
1299 | if (sd_booted() <= 0) { | |
1300 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ | |
1301 | log_error("System has not been booted with systemd as init system (PID 1). Can't operate."); | |
d21ed1ea | 1302 | |
fb507898 YW |
1303 | return -EHOSTDOWN; |
1304 | } | |
1305 | r = sd_bus_default_system(&bus); | |
1306 | } | |
d21ed1ea LP |
1307 | break; |
1308 | ||
1309 | case BUS_TRANSPORT_REMOTE: | |
38303498 | 1310 | r = sd_bus_open_system_remote(&bus, host); |
41dd15e4 LP |
1311 | break; |
1312 | ||
de33fc62 | 1313 | case BUS_TRANSPORT_MACHINE: |
38303498 | 1314 | r = sd_bus_open_system_machine(&bus, host); |
41dd15e4 LP |
1315 | break; |
1316 | ||
1317 | default: | |
1318 | assert_not_reached("Hmm, unknown transport type."); | |
1319 | } | |
38303498 LP |
1320 | if (r < 0) |
1321 | return r; | |
41dd15e4 | 1322 | |
38303498 LP |
1323 | r = sd_bus_set_exit_on_disconnect(bus, true); |
1324 | if (r < 0) | |
1325 | return r; | |
1326 | ||
1cc6c93a | 1327 | *ret = TAKE_PTR(bus); |
38303498 LP |
1328 | |
1329 | return 0; | |
41dd15e4 LP |
1330 | } |
1331 | ||
266f3e26 | 1332 | int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) { |
41dd15e4 LP |
1333 | int r; |
1334 | ||
1335 | assert(transport >= 0); | |
1336 | assert(transport < _BUS_TRANSPORT_MAX); | |
1337 | assert(bus); | |
1338 | ||
1339 | assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); | |
15411c0c | 1340 | assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP); |
41dd15e4 LP |
1341 | |
1342 | switch (transport) { | |
1343 | ||
1344 | case BUS_TRANSPORT_LOCAL: | |
1345 | if (user) | |
266f3e26 | 1346 | r = bus_connect_user_systemd(bus); |
fb507898 YW |
1347 | else { |
1348 | if (sd_booted() <= 0) { | |
1349 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ | |
1350 | log_error("System has not been booted with systemd as init system (PID 1). Can't operate."); | |
41dd15e4 | 1351 | |
fb507898 YW |
1352 | return -EHOSTDOWN; |
1353 | } | |
1354 | r = bus_connect_system_systemd(bus); | |
1355 | } | |
41dd15e4 LP |
1356 | break; |
1357 | ||
1358 | case BUS_TRANSPORT_REMOTE: | |
3db729cb | 1359 | r = sd_bus_open_system_remote(bus, host); |
d21ed1ea LP |
1360 | break; |
1361 | ||
de33fc62 LP |
1362 | case BUS_TRANSPORT_MACHINE: |
1363 | r = sd_bus_open_system_machine(bus, host); | |
d21ed1ea LP |
1364 | break; |
1365 | ||
1366 | default: | |
1367 | assert_not_reached("Hmm, unknown transport type."); | |
1368 | } | |
1369 | ||
1370 | return r; | |
1371 | } | |
e6504030 LP |
1372 | |
1373 | int bus_property_get_bool( | |
1374 | sd_bus *bus, | |
1375 | const char *path, | |
1376 | const char *interface, | |
1377 | const char *property, | |
1378 | sd_bus_message *reply, | |
ebcf1f97 LP |
1379 | void *userdata, |
1380 | sd_bus_error *error) { | |
e6504030 LP |
1381 | |
1382 | int b = *(bool*) userdata; | |
1383 | ||
1384 | return sd_bus_message_append_basic(reply, 'b', &b); | |
1385 | } | |
1386 | ||
43ce15ac JK |
1387 | int bus_property_set_bool( |
1388 | sd_bus *bus, | |
1389 | const char *path, | |
1390 | const char *interface, | |
1391 | const char *property, | |
1392 | sd_bus_message *value, | |
1393 | void *userdata, | |
1394 | sd_bus_error *error) { | |
1395 | ||
1396 | int b, r; | |
1397 | ||
1398 | r = sd_bus_message_read(value, "b", &b); | |
1399 | if (r < 0) | |
1400 | return r; | |
1401 | ||
5d904a6a | 1402 | *(bool*) userdata = b; |
43ce15ac JK |
1403 | return 0; |
1404 | } | |
1405 | ||
766c94ad LP |
1406 | int bus_property_get_id128( |
1407 | sd_bus *bus, | |
1408 | const char *path, | |
1409 | const char *interface, | |
1410 | const char *property, | |
1411 | sd_bus_message *reply, | |
1412 | void *userdata, | |
1413 | sd_bus_error *error) { | |
1414 | ||
1415 | sd_id128_t *id = userdata; | |
1416 | ||
1417 | if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */ | |
1418 | return sd_bus_message_append(reply, "ay", 0); | |
1419 | else | |
4b58153d | 1420 | return sd_bus_message_append_array(reply, 'y', id->bytes, 16); |
766c94ad LP |
1421 | } |
1422 | ||
718db961 LP |
1423 | #if __SIZEOF_SIZE_T__ != 8 |
1424 | int bus_property_get_size( | |
e6504030 LP |
1425 | sd_bus *bus, |
1426 | const char *path, | |
1427 | const char *interface, | |
1428 | const char *property, | |
1429 | sd_bus_message *reply, | |
ebcf1f97 LP |
1430 | void *userdata, |
1431 | sd_bus_error *error) { | |
e6504030 | 1432 | |
718db961 | 1433 | uint64_t sz = *(size_t*) userdata; |
e6504030 | 1434 | |
718db961 | 1435 | return sd_bus_message_append_basic(reply, 't', &sz); |
e6504030 | 1436 | } |
718db961 LP |
1437 | #endif |
1438 | ||
1439 | #if __SIZEOF_LONG__ != 8 | |
1440 | int bus_property_get_long( | |
1441 | sd_bus *bus, | |
1442 | const char *path, | |
1443 | const char *interface, | |
1444 | const char *property, | |
1445 | sd_bus_message *reply, | |
ebcf1f97 LP |
1446 | void *userdata, |
1447 | sd_bus_error *error) { | |
718db961 LP |
1448 | |
1449 | int64_t l = *(long*) userdata; | |
1450 | ||
1451 | return sd_bus_message_append_basic(reply, 'x', &l); | |
1452 | } | |
1453 | ||
1454 | int bus_property_get_ulong( | |
1455 | sd_bus *bus, | |
1456 | const char *path, | |
1457 | const char *interface, | |
1458 | const char *property, | |
1459 | sd_bus_message *reply, | |
ebcf1f97 LP |
1460 | void *userdata, |
1461 | sd_bus_error *error) { | |
718db961 LP |
1462 | |
1463 | uint64_t ul = *(unsigned long*) userdata; | |
1464 | ||
1465 | return sd_bus_message_append_basic(reply, 't', &ul); | |
1466 | } | |
1467 | #endif | |
5b30bef8 LP |
1468 | |
1469 | int bus_log_parse_error(int r) { | |
23bbb0de | 1470 | return log_error_errno(r, "Failed to parse bus message: %m"); |
5b30bef8 | 1471 | } |
f459b602 MAP |
1472 | |
1473 | int bus_log_create_error(int r) { | |
23bbb0de | 1474 | return log_error_errno(r, "Failed to create bus message: %m"); |
f459b602 MAP |
1475 | } |
1476 | ||
98a4c30b DH |
1477 | /** |
1478 | * bus_path_encode_unique() - encode unique object path | |
1479 | * @b: bus connection or NULL | |
1480 | * @prefix: object path prefix | |
1481 | * @sender_id: unique-name of client, or NULL | |
1482 | * @external_id: external ID to be chosen by client, or NULL | |
1483 | * @ret_path: storage for encoded object path pointer | |
1484 | * | |
1485 | * Whenever we provide a bus API that allows clients to create and manage | |
1486 | * server-side objects, we need to provide a unique name for these objects. If | |
1487 | * we let the server choose the name, we suffer from a race condition: If a | |
1488 | * client creates an object asynchronously, it cannot destroy that object until | |
1489 | * it received the method reply. It cannot know the name of the new object, | |
1490 | * thus, it cannot destroy it. Furthermore, it enforces a round-trip. | |
1491 | * | |
1492 | * Therefore, many APIs allow the client to choose the unique name for newly | |
1493 | * created objects. There're two problems to solve, though: | |
1494 | * 1) Object names are usually defined via dbus object paths, which are | |
1495 | * usually globally namespaced. Therefore, multiple clients must be able | |
1496 | * to choose unique object names without interference. | |
1497 | * 2) If multiple libraries share the same bus connection, they must be | |
1498 | * able to choose unique object names without interference. | |
1499 | * The first problem is solved easily by prefixing a name with the | |
1500 | * unique-bus-name of a connection. The server side must enforce this and | |
1501 | * reject any other name. The second problem is solved by providing unique | |
1502 | * suffixes from within sd-bus. | |
1503 | * | |
1504 | * This helper allows clients to create unique object-paths. It uses the | |
1505 | * template '/prefix/sender_id/external_id' and returns the new path in | |
1506 | * @ret_path (must be freed by the caller). | |
1507 | * If @sender_id is NULL, the unique-name of @b is used. If @external_id is | |
1508 | * NULL, this function allocates a unique suffix via @b (by requesting a new | |
1509 | * cookie). If both @sender_id and @external_id are given, @b can be passed as | |
1510 | * NULL. | |
1511 | * | |
1512 | * Returns: 0 on success, negative error code on failure. | |
1513 | */ | |
1514 | int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) { | |
1515 | _cleanup_free_ char *sender_label = NULL, *external_label = NULL; | |
1516 | char external_buf[DECIMAL_STR_MAX(uint64_t)], *p; | |
1517 | int r; | |
1518 | ||
1519 | assert_return(b || (sender_id && external_id), -EINVAL); | |
1520 | assert_return(object_path_is_valid(prefix), -EINVAL); | |
1521 | assert_return(ret_path, -EINVAL); | |
1522 | ||
1523 | if (!sender_id) { | |
1524 | r = sd_bus_get_unique_name(b, &sender_id); | |
1525 | if (r < 0) | |
1526 | return r; | |
1527 | } | |
1528 | ||
1529 | if (!external_id) { | |
1530 | xsprintf(external_buf, "%"PRIu64, ++b->cookie); | |
1531 | external_id = external_buf; | |
1532 | } | |
1533 | ||
1534 | sender_label = bus_label_escape(sender_id); | |
1535 | if (!sender_label) | |
1536 | return -ENOMEM; | |
1537 | ||
1538 | external_label = bus_label_escape(external_id); | |
1539 | if (!external_label) | |
1540 | return -ENOMEM; | |
1541 | ||
605405c6 | 1542 | p = strjoin(prefix, "/", sender_label, "/", external_label); |
98a4c30b DH |
1543 | if (!p) |
1544 | return -ENOMEM; | |
1545 | ||
1546 | *ret_path = p; | |
1547 | return 0; | |
1548 | } | |
1549 | ||
1550 | /** | |
1551 | * bus_path_decode_unique() - decode unique object path | |
1552 | * @path: object path to decode | |
1553 | * @prefix: object path prefix | |
1554 | * @ret_sender: output parameter for sender-id label | |
1555 | * @ret_external: output parameter for external-id label | |
1556 | * | |
1557 | * This does the reverse of bus_path_encode_unique() (see its description for | |
1558 | * details). Both trailing labels, sender-id and external-id, are unescaped and | |
1559 | * returned in the given output parameters (the caller must free them). | |
1560 | * | |
1561 | * Note that this function returns 0 if the path does not match the template | |
1562 | * (see bus_path_encode_unique()), 1 if it matched. | |
1563 | * | |
1564 | * Returns: Negative error code on failure, 0 if the given object path does not | |
1565 | * match the template (return parameters are set to NULL), 1 if it was | |
1566 | * parsed successfully (return parameters contain allocated labels). | |
1567 | */ | |
1568 | int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) { | |
1569 | const char *p, *q; | |
1570 | char *sender, *external; | |
1571 | ||
1572 | assert(object_path_is_valid(path)); | |
1573 | assert(object_path_is_valid(prefix)); | |
1574 | assert(ret_sender); | |
1575 | assert(ret_external); | |
1576 | ||
1577 | p = object_path_startswith(path, prefix); | |
1578 | if (!p) { | |
1579 | *ret_sender = NULL; | |
1580 | *ret_external = NULL; | |
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | q = strchr(p, '/'); | |
1585 | if (!q) { | |
1586 | *ret_sender = NULL; | |
1587 | *ret_external = NULL; | |
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | sender = bus_label_unescape_n(p, q - p); | |
1592 | external = bus_label_unescape(q + 1); | |
1593 | if (!sender || !external) { | |
1594 | free(sender); | |
1595 | free(external); | |
1596 | return -ENOMEM; | |
1597 | } | |
1598 | ||
1599 | *ret_sender = sender; | |
1600 | *ret_external = external; | |
1601 | return 1; | |
1602 | } | |
057171ef | 1603 | |
c9d031c3 EV |
1604 | int bus_property_get_rlimit( |
1605 | sd_bus *bus, | |
1606 | const char *path, | |
1607 | const char *interface, | |
1608 | const char *property, | |
1609 | sd_bus_message *reply, | |
1610 | void *userdata, | |
1611 | sd_bus_error *error) { | |
1612 | ||
6550c24c | 1613 | const char *is_soft; |
c9d031c3 EV |
1614 | struct rlimit *rl; |
1615 | uint64_t u; | |
1616 | rlim_t x; | |
1617 | ||
1618 | assert(bus); | |
1619 | assert(reply); | |
1620 | assert(userdata); | |
1621 | ||
147f6858 | 1622 | is_soft = endswith(property, "Soft"); |
6550c24c | 1623 | |
c9d031c3 EV |
1624 | rl = *(struct rlimit**) userdata; |
1625 | if (rl) | |
147f6858 | 1626 | x = is_soft ? rl->rlim_cur : rl->rlim_max; |
c9d031c3 EV |
1627 | else { |
1628 | struct rlimit buf = {}; | |
6550c24c | 1629 | const char *s, *p; |
c9d031c3 | 1630 | int z; |
147f6858 | 1631 | |
6550c24c | 1632 | /* Chop off "Soft" suffix */ |
147f6858 | 1633 | s = is_soft ? strndupa(property, is_soft - property) : property; |
c9d031c3 | 1634 | |
6550c24c LP |
1635 | /* Skip over any prefix, such as "Default" */ |
1636 | assert_se(p = strstr(s, "Limit")); | |
1637 | ||
1638 | z = rlimit_from_string(p + 5); | |
c9d031c3 EV |
1639 | assert(z >= 0); |
1640 | ||
6550c24c | 1641 | (void) getrlimit(z, &buf); |
147f6858 | 1642 | x = is_soft ? buf.rlim_cur : buf.rlim_max; |
c9d031c3 EV |
1643 | } |
1644 | ||
6550c24c LP |
1645 | /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all |
1646 | * archs */ | |
c9d031c3 EV |
1647 | u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x; |
1648 | ||
1649 | return sd_bus_message_append(reply, "t", u); | |
1650 | } | |
984794ba LP |
1651 | |
1652 | int bus_track_add_name_many(sd_bus_track *t, char **l) { | |
1653 | int r = 0; | |
1654 | char **i; | |
1655 | ||
1656 | assert(t); | |
1657 | ||
1658 | /* Continues adding after failure, and returns the first failure. */ | |
1659 | ||
1660 | STRV_FOREACH(i, l) { | |
1661 | int k; | |
1662 | ||
1663 | k = sd_bus_track_add_name(t, *i); | |
1664 | if (k < 0 && r >= 0) | |
1665 | r = k; | |
1666 | } | |
1667 | ||
1668 | return r; | |
1669 | } | |
d7afd945 | 1670 | |
0ddf50ff | 1671 | int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) { |
d7afd945 LP |
1672 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
1673 | const char *e; | |
1674 | int r; | |
1675 | ||
1676 | assert(ret); | |
1677 | ||
1678 | /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */ | |
1679 | ||
1680 | r = sd_bus_new(&bus); | |
1681 | if (r < 0) | |
1682 | return r; | |
1683 | ||
0ddf50ff YW |
1684 | if (description) { |
1685 | r = sd_bus_set_description(bus, description); | |
1686 | if (r < 0) | |
1687 | return r; | |
1688 | } | |
1689 | ||
d7afd945 LP |
1690 | e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); |
1691 | if (!e) | |
1692 | e = DEFAULT_SYSTEM_BUS_ADDRESS; | |
1693 | ||
1694 | r = sd_bus_set_address(bus, e); | |
1695 | if (r < 0) | |
1696 | return r; | |
1697 | ||
1698 | r = sd_bus_set_bus_client(bus, true); | |
1699 | if (r < 0) | |
1700 | return r; | |
1701 | ||
1702 | r = sd_bus_set_trusted(bus, true); | |
1703 | if (r < 0) | |
1704 | return r; | |
1705 | ||
1706 | r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS); | |
1707 | if (r < 0) | |
1708 | return r; | |
1709 | ||
1710 | r = sd_bus_set_watch_bind(bus, true); | |
1711 | if (r < 0) | |
1712 | return r; | |
1713 | ||
1714 | r = sd_bus_set_connected_signal(bus, true); | |
1715 | if (r < 0) | |
1716 | return r; | |
1717 | ||
1718 | r = sd_bus_start(bus); | |
1719 | if (r < 0) | |
1720 | return r; | |
1721 | ||
1cc6c93a | 1722 | *ret = TAKE_PTR(bus); |
d7afd945 LP |
1723 | |
1724 | return 0; | |
1725 | } | |
906cb2eb YW |
1726 | |
1727 | struct request_name_data { | |
200bc75d ZJS |
1728 | unsigned n_ref; |
1729 | ||
906cb2eb YW |
1730 | const char *name; |
1731 | uint64_t flags; | |
1732 | void *userdata; | |
1733 | }; | |
1734 | ||
200bc75d ZJS |
1735 | static void request_name_destroy_callback(void *userdata) { |
1736 | struct request_name_data *data = userdata; | |
1737 | ||
1738 | assert(data); | |
1739 | assert(data->n_ref > 0); | |
1740 | ||
1741 | log_info("%s n_ref=%u", __func__, data->n_ref); | |
1742 | ||
1743 | data->n_ref--; | |
1744 | if (data->n_ref == 0) | |
1745 | free(data); | |
1746 | } | |
1747 | ||
906cb2eb | 1748 | static int reload_dbus_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
200bc75d | 1749 | struct request_name_data *data = userdata; |
906cb2eb YW |
1750 | const sd_bus_error *e; |
1751 | int r; | |
1752 | ||
906cb2eb YW |
1753 | assert(data); |
1754 | assert(data->name); | |
200bc75d | 1755 | assert(data->n_ref > 0); |
906cb2eb YW |
1756 | |
1757 | e = sd_bus_message_get_error(m); | |
1758 | if (e) { | |
1759 | log_error_errno(sd_bus_error_get_errno(e), "Failed to reload DBus configuration: %s", e->message); | |
1760 | return 1; | |
1761 | } | |
1762 | ||
1763 | /* Here, use the default request name handler to avoid an infinite loop of reloading and requesting. */ | |
1764 | r = sd_bus_request_name_async(sd_bus_message_get_bus(m), NULL, data->name, data->flags, NULL, data->userdata); | |
1765 | if (r < 0) | |
1766 | log_error_errno(r, "Failed to request name: %m"); | |
1767 | ||
1768 | return 1; | |
1769 | } | |
1770 | ||
1771 | static int request_name_handler_may_reload_dbus(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
200bc75d | 1772 | struct request_name_data *data = userdata; |
906cb2eb YW |
1773 | uint32_t ret; |
1774 | int r; | |
1775 | ||
1776 | assert(m); | |
200bc75d | 1777 | assert(data); |
906cb2eb YW |
1778 | |
1779 | if (sd_bus_message_is_method_error(m, NULL)) { | |
1780 | const sd_bus_error *e = sd_bus_message_get_error(m); | |
200bc75d | 1781 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL; |
906cb2eb YW |
1782 | |
1783 | if (!sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED)) { | |
1784 | log_debug_errno(sd_bus_error_get_errno(e), | |
1785 | "Unable to request name, failing connection: %s", | |
1786 | e->message); | |
1787 | ||
1788 | bus_enter_closing(sd_bus_message_get_bus(m)); | |
1789 | return 1; | |
1790 | } | |
1791 | ||
1792 | log_debug_errno(sd_bus_error_get_errno(e), | |
200bc75d | 1793 | "Unable to request name, will retry after reloading DBus configuration: %s", |
906cb2eb YW |
1794 | e->message); |
1795 | ||
1796 | /* If systemd-timesyncd.service enables DynamicUser= and dbus.service | |
1797 | * started before the dynamic user is realized, then the DBus policy | |
1798 | * about timesyncd has not been enabled yet. So, let's try to reload | |
200bc75d | 1799 | * DBus configuration, and after that request the name again. Note that it |
906cb2eb YW |
1800 | * seems that no privileges are necessary to call the following method. */ |
1801 | ||
1802 | r = sd_bus_call_method_async( | |
1803 | sd_bus_message_get_bus(m), | |
200bc75d | 1804 | &slot, |
906cb2eb YW |
1805 | "org.freedesktop.DBus", |
1806 | "/org/freedesktop/DBus", | |
1807 | "org.freedesktop.DBus", | |
1808 | "ReloadConfig", | |
1809 | reload_dbus_handler, | |
200bc75d | 1810 | data, NULL); |
906cb2eb YW |
1811 | if (r < 0) { |
1812 | log_error_errno(r, "Failed to reload DBus configuration: %m"); | |
1813 | bus_enter_closing(sd_bus_message_get_bus(m)); | |
1814 | return 1; | |
1815 | } | |
1816 | ||
200bc75d ZJS |
1817 | data->n_ref ++; |
1818 | assert_se(sd_bus_slot_set_destroy_callback(slot, request_name_destroy_callback) >= 0); | |
1819 | ||
1820 | r = sd_bus_slot_set_floating(slot, true); | |
1821 | if (r < 0) | |
1822 | return r; | |
1823 | ||
906cb2eb YW |
1824 | return 1; |
1825 | } | |
1826 | ||
1827 | r = sd_bus_message_read(m, "u", &ret); | |
1828 | if (r < 0) | |
1829 | return r; | |
1830 | ||
1831 | switch (ret) { | |
1832 | ||
1833 | case BUS_NAME_ALREADY_OWNER: | |
1834 | log_debug("Already owner of requested service name, ignoring."); | |
1835 | return 1; | |
1836 | ||
1837 | case BUS_NAME_IN_QUEUE: | |
1838 | log_debug("In queue for requested service name."); | |
1839 | return 1; | |
1840 | ||
1841 | case BUS_NAME_PRIMARY_OWNER: | |
1842 | log_debug("Successfully acquired requested service name."); | |
1843 | return 1; | |
1844 | ||
1845 | case BUS_NAME_EXISTS: | |
1846 | log_debug("Requested service name already owned, failing connection."); | |
1847 | bus_enter_closing(sd_bus_message_get_bus(m)); | |
1848 | return 1; | |
1849 | } | |
1850 | ||
1851 | log_debug("Unexpected response from RequestName(), failing connection."); | |
1852 | bus_enter_closing(sd_bus_message_get_bus(m)); | |
1853 | return 1; | |
1854 | } | |
1855 | ||
1856 | int bus_request_name_async_may_reload_dbus(sd_bus *bus, sd_bus_slot **ret_slot, const char *name, uint64_t flags, void *userdata) { | |
200bc75d ZJS |
1857 | _cleanup_free_ struct request_name_data *data = NULL; |
1858 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL; | |
1859 | int r; | |
906cb2eb | 1860 | |
83b6c1a6 | 1861 | data = new(struct request_name_data, 1); |
906cb2eb YW |
1862 | if (!data) |
1863 | return -ENOMEM; | |
1864 | ||
83b6c1a6 | 1865 | *data = (struct request_name_data) { |
200bc75d | 1866 | .n_ref = 1, |
83b6c1a6 ZJS |
1867 | .name = name, |
1868 | .flags = flags, | |
1869 | .userdata = userdata, | |
1870 | }; | |
906cb2eb | 1871 | |
200bc75d ZJS |
1872 | r = sd_bus_request_name_async(bus, &slot, name, flags, request_name_handler_may_reload_dbus, data); |
1873 | if (r < 0) | |
1874 | return r; | |
1875 | ||
1876 | assert_se(sd_bus_slot_set_destroy_callback(slot, request_name_destroy_callback) >= 0); | |
1877 | TAKE_PTR(data); | |
1878 | ||
1879 | if (ret_slot) | |
1880 | *ret_slot = TAKE_PTR(slot); | |
1881 | else { | |
1882 | r = sd_bus_slot_set_floating(slot, true); | |
1883 | if (r < 0) | |
1884 | return r; | |
1885 | } | |
1886 | ||
1887 | return 0; | |
906cb2eb | 1888 | } |
19017acb LP |
1889 | |
1890 | int bus_reply_pair_array(sd_bus_message *m, char **l) { | |
1891 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1892 | char **k, **v; | |
1893 | int r; | |
1894 | ||
1895 | assert(m); | |
1896 | ||
1897 | /* Reply to the specified message with a message containing a dictionary put together from the specified | |
1898 | * strv */ | |
1899 | ||
1900 | r = sd_bus_message_new_method_return(m, &reply); | |
1901 | if (r < 0) | |
1902 | return r; | |
1903 | ||
1904 | r = sd_bus_message_open_container(reply, 'a', "{ss}"); | |
1905 | if (r < 0) | |
1906 | return r; | |
1907 | ||
1908 | STRV_FOREACH_PAIR(k, v, l) { | |
1909 | r = sd_bus_message_append(reply, "{ss}", *k, *v); | |
1910 | if (r < 0) | |
1911 | return r; | |
1912 | } | |
1913 | ||
1914 | r = sd_bus_message_close_container(reply); | |
1915 | if (r < 0) | |
1916 | return r; | |
1917 | ||
1918 | return sd_bus_send(NULL, reply, NULL); | |
1919 | } |