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