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