]>
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" |
049af8ad | 31 | #include "mountpoint-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 | ||
031651a5 | 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) { |
b1a4981a | 562 | _cleanup_(sd_bus_close_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) { |
b1a4981a | 595 | _cleanup_(sd_bus_close_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 | ||
102b0214 YW |
631 | int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value) { |
632 | assert(name); | |
633 | ||
634 | if (expected_value && !streq_ptr(expected_value, value)) | |
635 | return 0; | |
636 | ||
637 | if (only_value) | |
638 | puts(value); | |
639 | else | |
640 | printf("%s=%s\n", name, value); | |
641 | ||
642 | return 0; | |
643 | } | |
644 | ||
645 | int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) { | |
eda19357 YW |
646 | va_list ap; |
647 | int r; | |
648 | ||
649 | assert(name); | |
650 | assert(fmt); | |
651 | ||
652 | if (expected_value) { | |
653 | _cleanup_free_ char *s = NULL; | |
654 | ||
655 | va_start(ap, fmt); | |
656 | r = vasprintf(&s, fmt, ap); | |
657 | va_end(ap); | |
658 | if (r < 0) | |
659 | return -ENOMEM; | |
660 | ||
661 | if (streq_ptr(expected_value, s)) { | |
662 | if (only_value) | |
663 | puts(s); | |
664 | else | |
665 | printf("%s=%s\n", name, s); | |
666 | } | |
667 | ||
668 | return 0; | |
669 | } | |
670 | ||
671 | if (!only_value) | |
672 | printf("%s=", name); | |
673 | va_start(ap, fmt); | |
674 | vprintf(fmt, ap); | |
675 | va_end(ap); | |
676 | puts(""); | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
681 | static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { | |
a1da8583 TG |
682 | char type; |
683 | const char *contents; | |
9f6eb1cd | 684 | int r; |
a1da8583 TG |
685 | |
686 | assert(name); | |
07636114 | 687 | assert(m); |
a1da8583 | 688 | |
07636114 | 689 | r = sd_bus_message_peek_type(m, &type, &contents); |
9f6eb1cd KS |
690 | if (r < 0) |
691 | return r; | |
a1da8583 TG |
692 | |
693 | switch (type) { | |
694 | ||
695 | case SD_BUS_TYPE_STRING: { | |
696 | const char *s; | |
9f6eb1cd | 697 | |
07636114 | 698 | r = sd_bus_message_read_basic(m, type, &s); |
9f6eb1cd KS |
699 | if (r < 0) |
700 | return r; | |
a1da8583 | 701 | |
5993d46a ZJS |
702 | if (all || !isempty(s)) { |
703 | bool good; | |
704 | ||
705 | /* This property has a single value, so we need to take | |
706 | * care not to print a new line, everything else is OK. */ | |
707 | good = !strchr(s, '\n'); | |
102b0214 | 708 | bus_print_property_value(name, expected_value, value, good ? s : "[unprintable]"); |
5993d46a | 709 | } |
a1da8583 TG |
710 | |
711 | return 1; | |
712 | } | |
713 | ||
714 | case SD_BUS_TYPE_BOOLEAN: { | |
c2fa048c | 715 | int b; |
a1da8583 | 716 | |
07636114 | 717 | r = sd_bus_message_read_basic(m, type, &b); |
9f6eb1cd KS |
718 | if (r < 0) |
719 | return r; | |
720 | ||
eda19357 YW |
721 | if (expected_value && parse_boolean(expected_value) != b) |
722 | return 1; | |
a1da8583 | 723 | |
102b0214 | 724 | bus_print_property_value(name, NULL, value, yes_no(b)); |
a1da8583 TG |
725 | return 1; |
726 | } | |
727 | ||
728 | case SD_BUS_TYPE_UINT64: { | |
729 | uint64_t u; | |
730 | ||
07636114 | 731 | r = sd_bus_message_read_basic(m, type, &u); |
9f6eb1cd KS |
732 | if (r < 0) |
733 | return r; | |
a1da8583 TG |
734 | |
735 | /* Yes, heuristics! But we can change this check | |
736 | * should it turn out to not be sufficient */ | |
737 | ||
ead0adb1 YW |
738 | if (endswith(name, "Timestamp") || |
739 | STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) { | |
4d9685be ZJS |
740 | char timestamp[FORMAT_TIMESTAMP_MAX]; |
741 | const char *t; | |
a1da8583 TG |
742 | |
743 | t = format_timestamp(timestamp, sizeof(timestamp), u); | |
744 | if (t || all) | |
102b0214 | 745 | bus_print_property_value(name, expected_value, value, strempty(t)); |
a1da8583 TG |
746 | |
747 | } else if (strstr(name, "USec")) { | |
748 | char timespan[FORMAT_TIMESPAN_MAX]; | |
749 | ||
eda19357 | 750 | (void) format_timespan(timespan, sizeof(timespan), u, 0); |
102b0214 | 751 | bus_print_property_value(name, expected_value, value, timespan); |
eda19357 | 752 | |
73186d53 DH |
753 | } else if (streq(name, "RestrictNamespaces")) { |
754 | _cleanup_free_ char *s = NULL; | |
ae978b9f | 755 | const char *result; |
73186d53 DH |
756 | |
757 | if ((u & NAMESPACE_FLAGS_ALL) == 0) | |
758 | result = "yes"; | |
759 | else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL) | |
760 | result = "no"; | |
761 | else { | |
86c2a9f1 | 762 | r = namespace_flags_to_string(u, &s); |
73186d53 DH |
763 | if (r < 0) |
764 | return r; | |
765 | ||
766 | result = s; | |
767 | } | |
768 | ||
102b0214 | 769 | bus_print_property_value(name, expected_value, value, result); |
21771f33 YW |
770 | |
771 | } else if (streq(name, "MountFlags")) { | |
ae978b9f | 772 | const char *result; |
21771f33 YW |
773 | |
774 | result = mount_propagation_flags_to_string(u); | |
775 | if (!result) | |
776 | return -EINVAL; | |
777 | ||
102b0214 | 778 | bus_print_property_value(name, expected_value, value, result); |
21771f33 | 779 | |
52610b02 YW |
780 | } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) { |
781 | _cleanup_free_ char *s = NULL; | |
782 | ||
783 | r = capability_set_to_string_alloc(u, &s); | |
784 | if (r < 0) | |
785 | return r; | |
786 | ||
102b0214 | 787 | bus_print_property_value(name, expected_value, value, s); |
52610b02 | 788 | |
21771f33 YW |
789 | } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) || |
790 | (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) || | |
791 | (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) || | |
792 | (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) || | |
793 | (endswith(name, "NSec") && u == (uint64_t) -1)) | |
794 | ||
102b0214 | 795 | bus_print_property_value(name, expected_value, value, "[not set]"); |
21771f33 YW |
796 | |
797 | else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) || | |
798 | (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) || | |
799 | (startswith(name, "Limit") && u == (uint64_t) -1) || | |
800 | (startswith(name, "DefaultLimit") && u == (uint64_t) -1)) | |
801 | ||
102b0214 | 802 | bus_print_property_value(name, expected_value, value, "infinity"); |
56c6b690 YW |
803 | else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == (uint64_t) -1) |
804 | bus_print_property_value(name, expected_value, value, "[no data]"); | |
21771f33 | 805 | else |
102b0214 | 806 | bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u); |
a1da8583 TG |
807 | |
808 | return 1; | |
809 | } | |
810 | ||
d7161865 DM |
811 | case SD_BUS_TYPE_INT64: { |
812 | int64_t i; | |
813 | ||
07636114 | 814 | r = sd_bus_message_read_basic(m, type, &i); |
d7161865 DM |
815 | if (r < 0) |
816 | return r; | |
817 | ||
102b0214 | 818 | bus_print_property_valuef(name, expected_value, value, "%"PRIi64, i); |
d7161865 DM |
819 | return 1; |
820 | } | |
821 | ||
a1da8583 TG |
822 | case SD_BUS_TYPE_UINT32: { |
823 | uint32_t u; | |
824 | ||
07636114 | 825 | r = sd_bus_message_read_basic(m, type, &u); |
9f6eb1cd KS |
826 | if (r < 0) |
827 | return r; | |
a1da8583 TG |
828 | |
829 | if (strstr(name, "UMask") || strstr(name, "Mode")) | |
102b0214 | 830 | bus_print_property_valuef(name, expected_value, value, "%04o", u); |
eda19357 | 831 | |
c533658a ZJS |
832 | else if (streq(name, "UID")) { |
833 | if (u == UID_INVALID) | |
102b0214 | 834 | bus_print_property_value(name, expected_value, value, "[not set]"); |
c533658a | 835 | else |
102b0214 | 836 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
c533658a ZJS |
837 | } else if (streq(name, "GID")) { |
838 | if (u == GID_INVALID) | |
102b0214 | 839 | bus_print_property_value(name, expected_value, value, "[not set]"); |
c533658a | 840 | else |
102b0214 | 841 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
c533658a | 842 | } else |
102b0214 | 843 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
a1da8583 TG |
844 | |
845 | return 1; | |
846 | } | |
847 | ||
848 | case SD_BUS_TYPE_INT32: { | |
849 | int32_t i; | |
850 | ||
07636114 | 851 | r = sd_bus_message_read_basic(m, type, &i); |
9f6eb1cd KS |
852 | if (r < 0) |
853 | return r; | |
a1da8583 | 854 | |
102b0214 | 855 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); |
a1da8583 TG |
856 | return 1; |
857 | } | |
858 | ||
859 | case SD_BUS_TYPE_DOUBLE: { | |
860 | double d; | |
861 | ||
07636114 | 862 | r = sd_bus_message_read_basic(m, type, &d); |
9f6eb1cd KS |
863 | if (r < 0) |
864 | return r; | |
a1da8583 | 865 | |
102b0214 | 866 | bus_print_property_valuef(name, expected_value, value, "%g", d); |
a1da8583 TG |
867 | return 1; |
868 | } | |
869 | ||
870 | case SD_BUS_TYPE_ARRAY: | |
a1da8583 | 871 | if (streq(contents, "s")) { |
261afec5 MAP |
872 | bool first = true; |
873 | const char *str; | |
a1da8583 | 874 | |
07636114 | 875 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents); |
9f6eb1cd KS |
876 | if (r < 0) |
877 | return r; | |
878 | ||
07636114 | 879 | while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) { |
5993d46a ZJS |
880 | bool good; |
881 | ||
4f9a9105 | 882 | if (first && !value) |
261afec5 MAP |
883 | printf("%s=", name); |
884 | ||
ed88a900 | 885 | /* This property has multiple space-separated values, so |
ead6bd25 | 886 | * neither spaces nor newlines can be allowed in a value. */ |
5993d46a ZJS |
887 | good = str[strcspn(str, " \n")] == '\0'; |
888 | ||
889 | printf("%s%s", first ? "" : " ", good ? str : "[unprintable]"); | |
261afec5 MAP |
890 | |
891 | first = false; | |
892 | } | |
9f6eb1cd KS |
893 | if (r < 0) |
894 | return r; | |
a1da8583 | 895 | |
4f9a9105 | 896 | if (first && all && !value) |
a1da8583 | 897 | printf("%s=", name); |
261afec5 | 898 | if (!first || all) |
a1da8583 | 899 | puts(""); |
a1da8583 | 900 | |
07636114 | 901 | r = sd_bus_message_exit_container(m); |
9f6eb1cd KS |
902 | if (r < 0) |
903 | return r; | |
a1da8583 TG |
904 | |
905 | return 1; | |
906 | ||
907 | } else if (streq(contents, "y")) { | |
908 | const uint8_t *u; | |
909 | size_t n; | |
910 | ||
07636114 | 911 | r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n); |
9f6eb1cd KS |
912 | if (r < 0) |
913 | return r; | |
914 | ||
a1da8583 | 915 | if (all || n > 0) { |
14cb109d | 916 | unsigned i; |
a1da8583 | 917 | |
4f9a9105 ZJS |
918 | if (!value) |
919 | printf("%s=", name); | |
a1da8583 TG |
920 | |
921 | for (i = 0; i < n; i++) | |
922 | printf("%02x", u[i]); | |
923 | ||
924 | puts(""); | |
925 | } | |
926 | ||
927 | return 1; | |
928 | ||
929 | } else if (streq(contents, "u")) { | |
930 | uint32_t *u; | |
931 | size_t n; | |
932 | ||
07636114 | 933 | r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n); |
9f6eb1cd KS |
934 | if (r < 0) |
935 | return r; | |
936 | ||
a1da8583 | 937 | if (all || n > 0) { |
14cb109d | 938 | unsigned i; |
a1da8583 | 939 | |
4f9a9105 ZJS |
940 | if (!value) |
941 | printf("%s=", name); | |
a1da8583 TG |
942 | |
943 | for (i = 0; i < n; i++) | |
944 | printf("%08x", u[i]); | |
945 | ||
946 | puts(""); | |
947 | } | |
948 | ||
949 | return 1; | |
950 | } | |
951 | ||
952 | break; | |
953 | } | |
954 | ||
955 | return 0; | |
956 | } | |
d21ed1ea | 957 | |
07636114 YW |
958 | int bus_message_print_all_properties( |
959 | sd_bus_message *m, | |
960 | bus_message_print_t func, | |
961 | char **filter, | |
962 | bool value, | |
963 | bool all, | |
964 | Set **found_properties) { | |
ffc06c35 | 965 | |
07636114 | 966 | int r; |
9f6eb1cd | 967 | |
07636114 | 968 | assert(m); |
ffc06c35 | 969 | |
07636114 | 970 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
ffc06c35 KS |
971 | if (r < 0) |
972 | return r; | |
973 | ||
07636114 | 974 | while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { |
eda19357 YW |
975 | _cleanup_free_ char *name_with_equal = NULL; |
976 | const char *name, *contents, *expected_value = NULL; | |
ffc06c35 | 977 | |
07636114 | 978 | r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name); |
ffc06c35 KS |
979 | if (r < 0) |
980 | return r; | |
981 | ||
07636114 YW |
982 | if (found_properties) { |
983 | r = set_ensure_allocated(found_properties, &string_hash_ops); | |
984 | if (r < 0) | |
985 | return log_oom(); | |
986 | ||
987 | r = set_put(*found_properties, name); | |
0ceff906 | 988 | if (r < 0 && r != -EEXIST) |
07636114 YW |
989 | return log_oom(); |
990 | } | |
991 | ||
eda19357 YW |
992 | name_with_equal = strappend(name, "="); |
993 | if (!name_with_equal) | |
994 | return log_oom(); | |
995 | ||
996 | if (!filter || strv_find(filter, name) || | |
997 | (expected_value = strv_find_startswith(filter, name_with_equal))) { | |
07636114 | 998 | r = sd_bus_message_peek_type(m, NULL, &contents); |
9f6eb1cd KS |
999 | if (r < 0) |
1000 | return r; | |
1001 | ||
07636114 | 1002 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); |
9f6eb1cd KS |
1003 | if (r < 0) |
1004 | return r; | |
ffc06c35 | 1005 | |
07636114 | 1006 | if (func) |
eda19357 | 1007 | r = func(name, expected_value, m, value, all); |
07636114 | 1008 | if (!func || r == 0) |
eda19357 | 1009 | r = bus_print_property(name, expected_value, m, value, all); |
9f6eb1cd KS |
1010 | if (r < 0) |
1011 | return r; | |
27e72d6b | 1012 | if (r == 0) { |
eda19357 | 1013 | if (all && !expected_value) |
27e72d6b SP |
1014 | printf("%s=[unprintable]\n", name); |
1015 | /* skip what we didn't read */ | |
07636114 | 1016 | r = sd_bus_message_skip(m, contents); |
27e72d6b SP |
1017 | if (r < 0) |
1018 | return r; | |
1019 | } | |
9f6eb1cd | 1020 | |
07636114 | 1021 | r = sd_bus_message_exit_container(m); |
9f6eb1cd KS |
1022 | if (r < 0) |
1023 | return r; | |
1024 | } else { | |
07636114 | 1025 | r = sd_bus_message_skip(m, "v"); |
9f6eb1cd KS |
1026 | if (r < 0) |
1027 | return r; | |
1028 | } | |
1029 | ||
07636114 | 1030 | r = sd_bus_message_exit_container(m); |
ffc06c35 KS |
1031 | if (r < 0) |
1032 | return r; | |
9f6eb1cd KS |
1033 | } |
1034 | if (r < 0) | |
1035 | return r; | |
ffc06c35 | 1036 | |
07636114 | 1037 | r = sd_bus_message_exit_container(m); |
9f6eb1cd KS |
1038 | if (r < 0) |
1039 | return r; | |
ffc06c35 | 1040 | |
9f6eb1cd KS |
1041 | return 0; |
1042 | } | |
ffc06c35 | 1043 | |
07636114 YW |
1044 | int bus_print_all_properties( |
1045 | sd_bus *bus, | |
1046 | const char *dest, | |
1047 | const char *path, | |
1048 | bus_message_print_t func, | |
1049 | char **filter, | |
1050 | bool value, | |
1051 | bool all, | |
1052 | Set **found_properties) { | |
1053 | ||
1054 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1055 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1056 | int r; | |
1057 | ||
1058 | assert(bus); | |
1059 | assert(path); | |
1060 | ||
1061 | r = sd_bus_call_method(bus, | |
1062 | dest, | |
1063 | path, | |
1064 | "org.freedesktop.DBus.Properties", | |
1065 | "GetAll", | |
1066 | &error, | |
1067 | &reply, | |
1068 | "s", ""); | |
1069 | if (r < 0) | |
1070 | return r; | |
1071 | ||
1072 | return bus_message_print_all_properties(reply, func, filter, value, all, found_properties); | |
1073 | } | |
1074 | ||
9f6eb1cd KS |
1075 | int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1076 | sd_id128_t *p = userdata; | |
1077 | const void *v; | |
1078 | size_t n; | |
1079 | int r; | |
ffc06c35 | 1080 | |
9f6eb1cd KS |
1081 | r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n); |
1082 | if (r < 0) | |
1083 | return r; | |
ffc06c35 | 1084 | |
9f6eb1cd KS |
1085 | if (n == 0) |
1086 | *p = SD_ID128_NULL; | |
1087 | else if (n == 16) | |
1088 | memcpy((*p).bytes, v, n); | |
1089 | else | |
1090 | return -EINVAL; | |
ffc06c35 | 1091 | |
9f6eb1cd KS |
1092 | return 0; |
1093 | } | |
ffc06c35 | 1094 | |
a7e4861c | 1095 | static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) { |
9f6eb1cd KS |
1096 | char type; |
1097 | int r; | |
ffc06c35 | 1098 | |
9f6eb1cd KS |
1099 | r = sd_bus_message_peek_type(m, &type, NULL); |
1100 | if (r < 0) | |
1101 | return r; | |
ffc06c35 | 1102 | |
9f6eb1cd | 1103 | switch (type) { |
8b3b6f58 | 1104 | |
9f6eb1cd | 1105 | case SD_BUS_TYPE_STRING: { |
f37f8a61 | 1106 | const char **p = userdata; |
8b3b6f58 | 1107 | const char *s; |
ffc06c35 | 1108 | |
9f6eb1cd KS |
1109 | r = sd_bus_message_read_basic(m, type, &s); |
1110 | if (r < 0) | |
8b3b6f58 | 1111 | return r; |
ffc06c35 | 1112 | |
9f6eb1cd | 1113 | if (isempty(s)) |
0b83b8a4 | 1114 | s = NULL; |
ffc06c35 | 1115 | |
a7e4861c | 1116 | if (flags & BUS_MAP_STRDUP) |
f37f8a61 YW |
1117 | return free_and_strdup((char **) userdata, s); |
1118 | ||
1119 | *p = s; | |
1120 | return 0; | |
9f6eb1cd | 1121 | } |
ffc06c35 | 1122 | |
9f6eb1cd | 1123 | case SD_BUS_TYPE_ARRAY: { |
7d6884b6 TA |
1124 | _cleanup_strv_free_ char **l = NULL; |
1125 | char ***p = userdata; | |
ffc06c35 | 1126 | |
9f6eb1cd KS |
1127 | r = bus_message_read_strv_extend(m, &l); |
1128 | if (r < 0) | |
8b3b6f58 | 1129 | return r; |
ffc06c35 | 1130 | |
130d3d22 | 1131 | return strv_free_and_replace(*p, l); |
9f6eb1cd | 1132 | } |
ffc06c35 | 1133 | |
9f6eb1cd | 1134 | case SD_BUS_TYPE_BOOLEAN: { |
a7e4861c | 1135 | int b; |
ffc06c35 | 1136 | |
9f6eb1cd KS |
1137 | r = sd_bus_message_read_basic(m, type, &b); |
1138 | if (r < 0) | |
8b3b6f58 | 1139 | return r; |
ffc06c35 | 1140 | |
a7e4861c | 1141 | if (flags & BUS_MAP_BOOLEAN_AS_BOOL) |
5d904a6a | 1142 | *(bool*) userdata = b; |
a7e4861c | 1143 | else |
5d904a6a | 1144 | *(int*) userdata = b; |
a7e4861c | 1145 | |
8b3b6f58 | 1146 | return 0; |
9f6eb1cd | 1147 | } |
ffc06c35 | 1148 | |
bdf97b8a | 1149 | case SD_BUS_TYPE_INT32: |
9f6eb1cd | 1150 | case SD_BUS_TYPE_UINT32: { |
bdf97b8a | 1151 | uint32_t u, *p = userdata; |
9f6eb1cd KS |
1152 | |
1153 | r = sd_bus_message_read_basic(m, type, &u); | |
1154 | if (r < 0) | |
8b3b6f58 | 1155 | return r; |
ffc06c35 | 1156 | |
9f6eb1cd | 1157 | *p = u; |
8b3b6f58 | 1158 | return 0; |
9f6eb1cd KS |
1159 | } |
1160 | ||
bdf97b8a | 1161 | case SD_BUS_TYPE_INT64: |
9f6eb1cd | 1162 | case SD_BUS_TYPE_UINT64: { |
bdf97b8a | 1163 | uint64_t t, *p = userdata; |
9f6eb1cd KS |
1164 | |
1165 | r = sd_bus_message_read_basic(m, type, &t); | |
1166 | if (r < 0) | |
8b3b6f58 | 1167 | return r; |
ffc06c35 | 1168 | |
9f6eb1cd | 1169 | *p = t; |
8b3b6f58 | 1170 | return 0; |
9f6eb1cd KS |
1171 | } |
1172 | ||
52e045f9 | 1173 | case SD_BUS_TYPE_DOUBLE: { |
8b3b6f58 | 1174 | double d, *p = userdata; |
52e045f9 SS |
1175 | |
1176 | r = sd_bus_message_read_basic(m, type, &d); | |
1177 | if (r < 0) | |
8b3b6f58 | 1178 | return r; |
52e045f9 SS |
1179 | |
1180 | *p = d; | |
8b3b6f58 LP |
1181 | return 0; |
1182 | }} | |
52e045f9 | 1183 | |
8b3b6f58 | 1184 | return -EOPNOTSUPP; |
9f6eb1cd KS |
1185 | } |
1186 | ||
fe506d56 LP |
1187 | int bus_message_map_all_properties( |
1188 | sd_bus_message *m, | |
1189 | const struct bus_properties_map *map, | |
a7e4861c | 1190 | unsigned flags, |
f9e0eefc | 1191 | sd_bus_error *error, |
fe506d56 LP |
1192 | void *userdata) { |
1193 | ||
9f6eb1cd KS |
1194 | int r; |
1195 | ||
aae2b488 | 1196 | assert(m); |
9f6eb1cd KS |
1197 | assert(map); |
1198 | ||
9f6eb1cd KS |
1199 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
1200 | if (r < 0) | |
1201 | return r; | |
1202 | ||
1203 | while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { | |
1204 | const struct bus_properties_map *prop; | |
1205 | const char *member; | |
1206 | const char *contents; | |
1207 | void *v; | |
1208 | unsigned i; | |
1209 | ||
1210 | r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member); | |
ffc06c35 KS |
1211 | if (r < 0) |
1212 | return r; | |
1213 | ||
9f6eb1cd KS |
1214 | for (i = 0, prop = NULL; map[i].member; i++) |
1215 | if (streq(map[i].member, member)) { | |
1216 | prop = &map[i]; | |
1217 | break; | |
1218 | } | |
1219 | ||
1220 | if (prop) { | |
1221 | r = sd_bus_message_peek_type(m, NULL, &contents); | |
1222 | if (r < 0) | |
1223 | return r; | |
1224 | ||
1225 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); | |
1226 | if (r < 0) | |
1227 | return r; | |
1228 | ||
1229 | v = (uint8_t *)userdata + prop->offset; | |
27e72d6b | 1230 | if (map[i].set) |
f9e0eefc | 1231 | r = prop->set(sd_bus_message_get_bus(m), member, m, error, v); |
9f6eb1cd | 1232 | else |
a7e4861c | 1233 | r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v); |
2b49a470 TA |
1234 | if (r < 0) |
1235 | return r; | |
9f6eb1cd KS |
1236 | |
1237 | r = sd_bus_message_exit_container(m); | |
1238 | if (r < 0) | |
1239 | return r; | |
1240 | } else { | |
1241 | r = sd_bus_message_skip(m, "v"); | |
1242 | if (r < 0) | |
6c1508b8 | 1243 | return r; |
9f6eb1cd KS |
1244 | } |
1245 | ||
ffc06c35 KS |
1246 | r = sd_bus_message_exit_container(m); |
1247 | if (r < 0) | |
1248 | return r; | |
1249 | } | |
fe506d56 LP |
1250 | if (r < 0) |
1251 | return r; | |
ffc06c35 | 1252 | |
aae2b488 DH |
1253 | return sd_bus_message_exit_container(m); |
1254 | } | |
1255 | ||
fe506d56 LP |
1256 | int bus_map_all_properties( |
1257 | sd_bus *bus, | |
1258 | const char *destination, | |
1259 | const char *path, | |
1260 | const struct bus_properties_map *map, | |
a7e4861c | 1261 | unsigned flags, |
f9e0eefc | 1262 | sd_bus_error *error, |
f37f8a61 | 1263 | sd_bus_message **reply, |
fe506d56 LP |
1264 | void *userdata) { |
1265 | ||
4afd3348 | 1266 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
aae2b488 DH |
1267 | int r; |
1268 | ||
1269 | assert(bus); | |
1270 | assert(destination); | |
1271 | assert(path); | |
1272 | assert(map); | |
a7e4861c | 1273 | assert(reply || (flags & BUS_MAP_STRDUP)); |
aae2b488 DH |
1274 | |
1275 | r = sd_bus_call_method( | |
1276 | bus, | |
1277 | destination, | |
1278 | path, | |
1279 | "org.freedesktop.DBus.Properties", | |
1280 | "GetAll", | |
f9e0eefc | 1281 | error, |
aae2b488 DH |
1282 | &m, |
1283 | "s", ""); | |
1284 | if (r < 0) | |
1285 | return r; | |
1286 | ||
a7e4861c | 1287 | r = bus_message_map_all_properties(m, map, flags, error, userdata); |
f37f8a61 YW |
1288 | if (r < 0) |
1289 | return r; | |
1290 | ||
1291 | if (reply) | |
1292 | *reply = sd_bus_message_ref(m); | |
1293 | ||
1294 | return r; | |
ffc06c35 KS |
1295 | } |
1296 | ||
38303498 | 1297 | int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) { |
b1a4981a | 1298 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
d21ed1ea LP |
1299 | int r; |
1300 | ||
1301 | assert(transport >= 0); | |
1302 | assert(transport < _BUS_TRANSPORT_MAX); | |
38303498 | 1303 | assert(ret); |
d21ed1ea LP |
1304 | |
1305 | assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); | |
15411c0c | 1306 | assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP); |
d21ed1ea LP |
1307 | |
1308 | switch (transport) { | |
1309 | ||
1310 | case BUS_TRANSPORT_LOCAL: | |
1311 | if (user) | |
38303498 | 1312 | r = sd_bus_default_user(&bus); |
fb507898 YW |
1313 | else { |
1314 | if (sd_booted() <= 0) { | |
1315 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ | |
1316 | log_error("System has not been booted with systemd as init system (PID 1). Can't operate."); | |
d21ed1ea | 1317 | |
fb507898 YW |
1318 | return -EHOSTDOWN; |
1319 | } | |
1320 | r = sd_bus_default_system(&bus); | |
1321 | } | |
d21ed1ea LP |
1322 | break; |
1323 | ||
1324 | case BUS_TRANSPORT_REMOTE: | |
38303498 | 1325 | r = sd_bus_open_system_remote(&bus, host); |
41dd15e4 LP |
1326 | break; |
1327 | ||
de33fc62 | 1328 | case BUS_TRANSPORT_MACHINE: |
38303498 | 1329 | r = sd_bus_open_system_machine(&bus, host); |
41dd15e4 LP |
1330 | break; |
1331 | ||
1332 | default: | |
1333 | assert_not_reached("Hmm, unknown transport type."); | |
1334 | } | |
38303498 LP |
1335 | if (r < 0) |
1336 | return r; | |
41dd15e4 | 1337 | |
38303498 LP |
1338 | r = sd_bus_set_exit_on_disconnect(bus, true); |
1339 | if (r < 0) | |
1340 | return r; | |
1341 | ||
1cc6c93a | 1342 | *ret = TAKE_PTR(bus); |
38303498 LP |
1343 | |
1344 | return 0; | |
41dd15e4 LP |
1345 | } |
1346 | ||
266f3e26 | 1347 | int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) { |
41dd15e4 LP |
1348 | int r; |
1349 | ||
1350 | assert(transport >= 0); | |
1351 | assert(transport < _BUS_TRANSPORT_MAX); | |
1352 | assert(bus); | |
1353 | ||
1354 | assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); | |
15411c0c | 1355 | assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP); |
41dd15e4 LP |
1356 | |
1357 | switch (transport) { | |
1358 | ||
1359 | case BUS_TRANSPORT_LOCAL: | |
1360 | if (user) | |
266f3e26 | 1361 | r = bus_connect_user_systemd(bus); |
fb507898 | 1362 | else { |
baaa35ad | 1363 | if (sd_booted() <= 0) |
fb507898 | 1364 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ |
baaa35ad ZJS |
1365 | return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), |
1366 | "System has not been booted with systemd as init system (PID 1). Can't operate."); | |
fb507898 YW |
1367 | r = bus_connect_system_systemd(bus); |
1368 | } | |
41dd15e4 LP |
1369 | break; |
1370 | ||
1371 | case BUS_TRANSPORT_REMOTE: | |
3db729cb | 1372 | r = sd_bus_open_system_remote(bus, host); |
d21ed1ea LP |
1373 | break; |
1374 | ||
de33fc62 LP |
1375 | case BUS_TRANSPORT_MACHINE: |
1376 | r = sd_bus_open_system_machine(bus, host); | |
d21ed1ea LP |
1377 | break; |
1378 | ||
1379 | default: | |
1380 | assert_not_reached("Hmm, unknown transport type."); | |
1381 | } | |
1382 | ||
1383 | return r; | |
1384 | } | |
e6504030 LP |
1385 | |
1386 | int bus_property_get_bool( | |
1387 | sd_bus *bus, | |
1388 | const char *path, | |
1389 | const char *interface, | |
1390 | const char *property, | |
1391 | sd_bus_message *reply, | |
ebcf1f97 LP |
1392 | void *userdata, |
1393 | sd_bus_error *error) { | |
e6504030 LP |
1394 | |
1395 | int b = *(bool*) userdata; | |
1396 | ||
1397 | return sd_bus_message_append_basic(reply, 'b', &b); | |
1398 | } | |
1399 | ||
43ce15ac JK |
1400 | int bus_property_set_bool( |
1401 | sd_bus *bus, | |
1402 | const char *path, | |
1403 | const char *interface, | |
1404 | const char *property, | |
1405 | sd_bus_message *value, | |
1406 | void *userdata, | |
1407 | sd_bus_error *error) { | |
1408 | ||
1409 | int b, r; | |
1410 | ||
1411 | r = sd_bus_message_read(value, "b", &b); | |
1412 | if (r < 0) | |
1413 | return r; | |
1414 | ||
5d904a6a | 1415 | *(bool*) userdata = b; |
43ce15ac JK |
1416 | return 0; |
1417 | } | |
1418 | ||
766c94ad LP |
1419 | int bus_property_get_id128( |
1420 | sd_bus *bus, | |
1421 | const char *path, | |
1422 | const char *interface, | |
1423 | const char *property, | |
1424 | sd_bus_message *reply, | |
1425 | void *userdata, | |
1426 | sd_bus_error *error) { | |
1427 | ||
1428 | sd_id128_t *id = userdata; | |
1429 | ||
1430 | if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */ | |
1431 | return sd_bus_message_append(reply, "ay", 0); | |
1432 | else | |
4b58153d | 1433 | return sd_bus_message_append_array(reply, 'y', id->bytes, 16); |
766c94ad LP |
1434 | } |
1435 | ||
718db961 LP |
1436 | #if __SIZEOF_SIZE_T__ != 8 |
1437 | int bus_property_get_size( | |
e6504030 LP |
1438 | sd_bus *bus, |
1439 | const char *path, | |
1440 | const char *interface, | |
1441 | const char *property, | |
1442 | sd_bus_message *reply, | |
ebcf1f97 LP |
1443 | void *userdata, |
1444 | sd_bus_error *error) { | |
e6504030 | 1445 | |
718db961 | 1446 | uint64_t sz = *(size_t*) userdata; |
e6504030 | 1447 | |
718db961 | 1448 | return sd_bus_message_append_basic(reply, 't', &sz); |
e6504030 | 1449 | } |
718db961 LP |
1450 | #endif |
1451 | ||
1452 | #if __SIZEOF_LONG__ != 8 | |
1453 | int bus_property_get_long( | |
1454 | sd_bus *bus, | |
1455 | const char *path, | |
1456 | const char *interface, | |
1457 | const char *property, | |
1458 | sd_bus_message *reply, | |
ebcf1f97 LP |
1459 | void *userdata, |
1460 | sd_bus_error *error) { | |
718db961 LP |
1461 | |
1462 | int64_t l = *(long*) userdata; | |
1463 | ||
1464 | return sd_bus_message_append_basic(reply, 'x', &l); | |
1465 | } | |
1466 | ||
1467 | int bus_property_get_ulong( | |
1468 | sd_bus *bus, | |
1469 | const char *path, | |
1470 | const char *interface, | |
1471 | const char *property, | |
1472 | sd_bus_message *reply, | |
ebcf1f97 LP |
1473 | void *userdata, |
1474 | sd_bus_error *error) { | |
718db961 LP |
1475 | |
1476 | uint64_t ul = *(unsigned long*) userdata; | |
1477 | ||
1478 | return sd_bus_message_append_basic(reply, 't', &ul); | |
1479 | } | |
1480 | #endif | |
5b30bef8 LP |
1481 | |
1482 | int bus_log_parse_error(int r) { | |
23bbb0de | 1483 | return log_error_errno(r, "Failed to parse bus message: %m"); |
5b30bef8 | 1484 | } |
f459b602 MAP |
1485 | |
1486 | int bus_log_create_error(int r) { | |
23bbb0de | 1487 | return log_error_errno(r, "Failed to create bus message: %m"); |
f459b602 MAP |
1488 | } |
1489 | ||
98a4c30b DH |
1490 | /** |
1491 | * bus_path_encode_unique() - encode unique object path | |
1492 | * @b: bus connection or NULL | |
1493 | * @prefix: object path prefix | |
1494 | * @sender_id: unique-name of client, or NULL | |
1495 | * @external_id: external ID to be chosen by client, or NULL | |
1496 | * @ret_path: storage for encoded object path pointer | |
1497 | * | |
1498 | * Whenever we provide a bus API that allows clients to create and manage | |
1499 | * server-side objects, we need to provide a unique name for these objects. If | |
1500 | * we let the server choose the name, we suffer from a race condition: If a | |
1501 | * client creates an object asynchronously, it cannot destroy that object until | |
1502 | * it received the method reply. It cannot know the name of the new object, | |
1503 | * thus, it cannot destroy it. Furthermore, it enforces a round-trip. | |
1504 | * | |
1505 | * Therefore, many APIs allow the client to choose the unique name for newly | |
1506 | * created objects. There're two problems to solve, though: | |
1507 | * 1) Object names are usually defined via dbus object paths, which are | |
1508 | * usually globally namespaced. Therefore, multiple clients must be able | |
1509 | * to choose unique object names without interference. | |
1510 | * 2) If multiple libraries share the same bus connection, they must be | |
1511 | * able to choose unique object names without interference. | |
1512 | * The first problem is solved easily by prefixing a name with the | |
1513 | * unique-bus-name of a connection. The server side must enforce this and | |
1514 | * reject any other name. The second problem is solved by providing unique | |
1515 | * suffixes from within sd-bus. | |
1516 | * | |
1517 | * This helper allows clients to create unique object-paths. It uses the | |
1518 | * template '/prefix/sender_id/external_id' and returns the new path in | |
1519 | * @ret_path (must be freed by the caller). | |
1520 | * If @sender_id is NULL, the unique-name of @b is used. If @external_id is | |
1521 | * NULL, this function allocates a unique suffix via @b (by requesting a new | |
1522 | * cookie). If both @sender_id and @external_id are given, @b can be passed as | |
1523 | * NULL. | |
1524 | * | |
1525 | * Returns: 0 on success, negative error code on failure. | |
1526 | */ | |
1527 | int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) { | |
1528 | _cleanup_free_ char *sender_label = NULL, *external_label = NULL; | |
1529 | char external_buf[DECIMAL_STR_MAX(uint64_t)], *p; | |
1530 | int r; | |
1531 | ||
1532 | assert_return(b || (sender_id && external_id), -EINVAL); | |
1533 | assert_return(object_path_is_valid(prefix), -EINVAL); | |
1534 | assert_return(ret_path, -EINVAL); | |
1535 | ||
1536 | if (!sender_id) { | |
1537 | r = sd_bus_get_unique_name(b, &sender_id); | |
1538 | if (r < 0) | |
1539 | return r; | |
1540 | } | |
1541 | ||
1542 | if (!external_id) { | |
1543 | xsprintf(external_buf, "%"PRIu64, ++b->cookie); | |
1544 | external_id = external_buf; | |
1545 | } | |
1546 | ||
1547 | sender_label = bus_label_escape(sender_id); | |
1548 | if (!sender_label) | |
1549 | return -ENOMEM; | |
1550 | ||
1551 | external_label = bus_label_escape(external_id); | |
1552 | if (!external_label) | |
1553 | return -ENOMEM; | |
1554 | ||
605405c6 | 1555 | p = strjoin(prefix, "/", sender_label, "/", external_label); |
98a4c30b DH |
1556 | if (!p) |
1557 | return -ENOMEM; | |
1558 | ||
1559 | *ret_path = p; | |
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | /** | |
1564 | * bus_path_decode_unique() - decode unique object path | |
1565 | * @path: object path to decode | |
1566 | * @prefix: object path prefix | |
1567 | * @ret_sender: output parameter for sender-id label | |
1568 | * @ret_external: output parameter for external-id label | |
1569 | * | |
1570 | * This does the reverse of bus_path_encode_unique() (see its description for | |
1571 | * details). Both trailing labels, sender-id and external-id, are unescaped and | |
1572 | * returned in the given output parameters (the caller must free them). | |
1573 | * | |
1574 | * Note that this function returns 0 if the path does not match the template | |
1575 | * (see bus_path_encode_unique()), 1 if it matched. | |
1576 | * | |
1577 | * Returns: Negative error code on failure, 0 if the given object path does not | |
1578 | * match the template (return parameters are set to NULL), 1 if it was | |
1579 | * parsed successfully (return parameters contain allocated labels). | |
1580 | */ | |
1581 | int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) { | |
1582 | const char *p, *q; | |
1583 | char *sender, *external; | |
1584 | ||
1585 | assert(object_path_is_valid(path)); | |
1586 | assert(object_path_is_valid(prefix)); | |
1587 | assert(ret_sender); | |
1588 | assert(ret_external); | |
1589 | ||
1590 | p = object_path_startswith(path, prefix); | |
1591 | if (!p) { | |
1592 | *ret_sender = NULL; | |
1593 | *ret_external = NULL; | |
1594 | return 0; | |
1595 | } | |
1596 | ||
1597 | q = strchr(p, '/'); | |
1598 | if (!q) { | |
1599 | *ret_sender = NULL; | |
1600 | *ret_external = NULL; | |
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | sender = bus_label_unescape_n(p, q - p); | |
1605 | external = bus_label_unescape(q + 1); | |
1606 | if (!sender || !external) { | |
1607 | free(sender); | |
1608 | free(external); | |
1609 | return -ENOMEM; | |
1610 | } | |
1611 | ||
1612 | *ret_sender = sender; | |
1613 | *ret_external = external; | |
1614 | return 1; | |
1615 | } | |
057171ef | 1616 | |
c9d031c3 EV |
1617 | int bus_property_get_rlimit( |
1618 | sd_bus *bus, | |
1619 | const char *path, | |
1620 | const char *interface, | |
1621 | const char *property, | |
1622 | sd_bus_message *reply, | |
1623 | void *userdata, | |
1624 | sd_bus_error *error) { | |
1625 | ||
6550c24c | 1626 | const char *is_soft; |
c9d031c3 EV |
1627 | struct rlimit *rl; |
1628 | uint64_t u; | |
1629 | rlim_t x; | |
1630 | ||
1631 | assert(bus); | |
1632 | assert(reply); | |
1633 | assert(userdata); | |
1634 | ||
147f6858 | 1635 | is_soft = endswith(property, "Soft"); |
6550c24c | 1636 | |
c9d031c3 EV |
1637 | rl = *(struct rlimit**) userdata; |
1638 | if (rl) | |
147f6858 | 1639 | x = is_soft ? rl->rlim_cur : rl->rlim_max; |
c9d031c3 EV |
1640 | else { |
1641 | struct rlimit buf = {}; | |
6550c24c | 1642 | const char *s, *p; |
c9d031c3 | 1643 | int z; |
147f6858 | 1644 | |
6550c24c | 1645 | /* Chop off "Soft" suffix */ |
147f6858 | 1646 | s = is_soft ? strndupa(property, is_soft - property) : property; |
c9d031c3 | 1647 | |
6550c24c LP |
1648 | /* Skip over any prefix, such as "Default" */ |
1649 | assert_se(p = strstr(s, "Limit")); | |
1650 | ||
1651 | z = rlimit_from_string(p + 5); | |
c9d031c3 EV |
1652 | assert(z >= 0); |
1653 | ||
6550c24c | 1654 | (void) getrlimit(z, &buf); |
147f6858 | 1655 | x = is_soft ? buf.rlim_cur : buf.rlim_max; |
c9d031c3 EV |
1656 | } |
1657 | ||
6550c24c LP |
1658 | /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all |
1659 | * archs */ | |
c9d031c3 EV |
1660 | u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x; |
1661 | ||
1662 | return sd_bus_message_append(reply, "t", u); | |
1663 | } | |
984794ba LP |
1664 | |
1665 | int bus_track_add_name_many(sd_bus_track *t, char **l) { | |
1666 | int r = 0; | |
1667 | char **i; | |
1668 | ||
1669 | assert(t); | |
1670 | ||
1671 | /* Continues adding after failure, and returns the first failure. */ | |
1672 | ||
1673 | STRV_FOREACH(i, l) { | |
1674 | int k; | |
1675 | ||
1676 | k = sd_bus_track_add_name(t, *i); | |
1677 | if (k < 0 && r >= 0) | |
1678 | r = k; | |
1679 | } | |
1680 | ||
1681 | return r; | |
1682 | } | |
d7afd945 | 1683 | |
0ddf50ff | 1684 | int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) { |
b1a4981a | 1685 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
d7afd945 LP |
1686 | const char *e; |
1687 | int r; | |
1688 | ||
1689 | assert(ret); | |
1690 | ||
1691 | /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */ | |
1692 | ||
1693 | r = sd_bus_new(&bus); | |
1694 | if (r < 0) | |
1695 | return r; | |
1696 | ||
0ddf50ff YW |
1697 | if (description) { |
1698 | r = sd_bus_set_description(bus, description); | |
1699 | if (r < 0) | |
1700 | return r; | |
1701 | } | |
1702 | ||
d7afd945 LP |
1703 | e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); |
1704 | if (!e) | |
1705 | e = DEFAULT_SYSTEM_BUS_ADDRESS; | |
1706 | ||
1707 | r = sd_bus_set_address(bus, e); | |
1708 | if (r < 0) | |
1709 | return r; | |
1710 | ||
1711 | r = sd_bus_set_bus_client(bus, true); | |
1712 | if (r < 0) | |
1713 | return r; | |
1714 | ||
1715 | r = sd_bus_set_trusted(bus, true); | |
1716 | if (r < 0) | |
1717 | return r; | |
1718 | ||
1719 | r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS); | |
1720 | if (r < 0) | |
1721 | return r; | |
1722 | ||
1723 | r = sd_bus_set_watch_bind(bus, true); | |
1724 | if (r < 0) | |
1725 | return r; | |
1726 | ||
1727 | r = sd_bus_set_connected_signal(bus, true); | |
1728 | if (r < 0) | |
1729 | return r; | |
1730 | ||
1731 | r = sd_bus_start(bus); | |
1732 | if (r < 0) | |
1733 | return r; | |
1734 | ||
1cc6c93a | 1735 | *ret = TAKE_PTR(bus); |
d7afd945 LP |
1736 | |
1737 | return 0; | |
1738 | } | |
906cb2eb | 1739 | |
19017acb LP |
1740 | int bus_reply_pair_array(sd_bus_message *m, char **l) { |
1741 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1742 | char **k, **v; | |
1743 | int r; | |
1744 | ||
1745 | assert(m); | |
1746 | ||
1747 | /* Reply to the specified message with a message containing a dictionary put together from the specified | |
1748 | * strv */ | |
1749 | ||
1750 | r = sd_bus_message_new_method_return(m, &reply); | |
1751 | if (r < 0) | |
1752 | return r; | |
1753 | ||
1754 | r = sd_bus_message_open_container(reply, 'a', "{ss}"); | |
1755 | if (r < 0) | |
1756 | return r; | |
1757 | ||
1758 | STRV_FOREACH_PAIR(k, v, l) { | |
1759 | r = sd_bus_message_append(reply, "{ss}", *k, *v); | |
1760 | if (r < 0) | |
1761 | return r; | |
1762 | } | |
1763 | ||
1764 | r = sd_bus_message_close_container(reply); | |
1765 | if (r < 0) | |
1766 | return r; | |
1767 | ||
1768 | return sd_bus_send(NULL, reply, NULL); | |
1769 | } |