| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-print-properties.h" |
| 7 |
#include "capability-list.h" |
| 8 |
#include "cgroup-util.h" |
| 9 |
#include "escape.h" |
| 10 |
#include "log.h" |
| 11 |
#include "mountpoint-util.h" |
| 12 |
#include "nsflags.h" |
| 13 |
#include "parse-util.h" |
| 14 |
#include "set.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "strv.h" |
| 17 |
#include "time-util.h" |
| 18 |
|
| 19 |
bool bus_property_is_timestamp(const char *name) { |
| 20 |
assert(name); |
| 21 |
|
| 22 |
/* Trust me, this naming convention is ironclad. Except for these three. Okay four. Well... */ |
| 23 |
return endswith(name, "Timestamp") || |
| 24 |
STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec"); |
| 25 |
} |
| 26 |
|
| 27 |
int bus_print_property_value(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *value) { |
| 28 |
assert(name); |
| 29 |
|
| 30 |
if (expected_value && !streq_ptr(expected_value, value)) |
| 31 |
return 0; |
| 32 |
|
| 33 |
if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) && isempty(value)) |
| 34 |
return 0; |
| 35 |
|
| 36 |
if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE)) |
| 37 |
puts(strempty(value)); |
| 38 |
else |
| 39 |
printf("%s=%s\n", name, strempty(value)); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int bus_print_property_valuef(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *fmt, ...) { |
| 45 |
_cleanup_free_ char *s = NULL; |
| 46 |
va_list ap; |
| 47 |
int r; |
| 48 |
|
| 49 |
assert(name); |
| 50 |
assert(fmt); |
| 51 |
|
| 52 |
va_start(ap, fmt); |
| 53 |
r = vasprintf(&s, fmt, ap); |
| 54 |
va_end(ap); |
| 55 |
if (r < 0) |
| 56 |
return -ENOMEM; |
| 57 |
|
| 58 |
return bus_print_property_value(name, expected_value, flags, s); |
| 59 |
} |
| 60 |
|
| 61 |
static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags) { |
| 62 |
char type; |
| 63 |
const char *contents; |
| 64 |
int r; |
| 65 |
|
| 66 |
assert(name); |
| 67 |
assert(m); |
| 68 |
|
| 69 |
r = sd_bus_message_peek_type(m, &type, &contents); |
| 70 |
if (r < 0) |
| 71 |
return r; |
| 72 |
|
| 73 |
switch (type) { |
| 74 |
|
| 75 |
case SD_BUS_TYPE_STRING: { |
| 76 |
const char *s; |
| 77 |
|
| 78 |
r = sd_bus_message_read_basic(m, type, &s); |
| 79 |
if (r < 0) |
| 80 |
return r; |
| 81 |
|
| 82 |
if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) || !isempty(s)) { |
| 83 |
bool good; |
| 84 |
|
| 85 |
/* This property has a single value, so we need to take |
| 86 |
* care not to print a new line, everything else is OK. */ |
| 87 |
good = !strchr(s, '\n'); |
| 88 |
bus_print_property_value(name, expected_value, flags, good ? s : "[unprintable]"); |
| 89 |
} |
| 90 |
|
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
case SD_BUS_TYPE_BOOLEAN: { |
| 95 |
int b; |
| 96 |
|
| 97 |
r = sd_bus_message_read_basic(m, type, &b); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
|
| 101 |
if (expected_value && parse_boolean(expected_value) != b) |
| 102 |
return 1; |
| 103 |
|
| 104 |
bus_print_property_value(name, NULL, flags, yes_no(b)); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
case SD_BUS_TYPE_UINT64: { |
| 109 |
uint64_t u; |
| 110 |
|
| 111 |
r = sd_bus_message_read_basic(m, type, &u); |
| 112 |
if (r < 0) |
| 113 |
return r; |
| 114 |
|
| 115 |
if (bus_property_is_timestamp(name)) |
| 116 |
bus_print_property_value(name, expected_value, flags, FORMAT_TIMESTAMP(u)); |
| 117 |
|
| 118 |
/* Managed OOM pressure default implies "unset" and use the default set in oomd.conf. Without |
| 119 |
* this condition, we will print "infinity" which implies there is no limit on memory |
| 120 |
* pressure duration and is incorrect. */ |
| 121 |
else if (streq(name, "ManagedOOMMemoryPressureDurationUSec") && u == USEC_INFINITY) |
| 122 |
bus_print_property_value(name, expected_value, flags, "[not set]"); |
| 123 |
|
| 124 |
else if (strstr(name, "USec")) |
| 125 |
bus_print_property_value(name, expected_value, flags, FORMAT_TIMESPAN(u, 0)); |
| 126 |
|
| 127 |
else if (streq(name, "CoredumpFilter")) |
| 128 |
bus_print_property_valuef(name, expected_value, flags, "0x%"PRIx64, u); |
| 129 |
|
| 130 |
else if (streq(name, "RestrictNamespaces")) { |
| 131 |
_cleanup_free_ char *s = NULL; |
| 132 |
const char *result; |
| 133 |
|
| 134 |
if ((u & NAMESPACE_FLAGS_ALL) == 0) |
| 135 |
result = "yes"; |
| 136 |
else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL)) |
| 137 |
result = "no"; |
| 138 |
else { |
| 139 |
r = namespace_flags_to_string(u, &s); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
|
| 143 |
result = s; |
| 144 |
} |
| 145 |
|
| 146 |
bus_print_property_value(name, expected_value, flags, result); |
| 147 |
|
| 148 |
} else if (streq(name, "MountFlags")) { |
| 149 |
const char *result; |
| 150 |
|
| 151 |
result = mount_propagation_flag_to_string(u); |
| 152 |
if (!result) |
| 153 |
return -EINVAL; |
| 154 |
|
| 155 |
bus_print_property_value(name, expected_value, flags, result); |
| 156 |
|
| 157 |
} else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) { |
| 158 |
_cleanup_free_ char *s = NULL; |
| 159 |
|
| 160 |
r = capability_set_to_string(u, &s); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
bus_print_property_value(name, expected_value, flags, s); |
| 165 |
|
| 166 |
} else if (STR_IN_SET(name, "CPUWeight", "StartupCPUWeight") && u == CGROUP_WEIGHT_IDLE) |
| 167 |
bus_print_property_value(name, expected_value, flags, "idle"); |
| 168 |
|
| 169 |
else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) || |
| 170 |
(STR_IN_SET(name, "MemoryCurrent", "MemoryAvailable", "TasksCurrent") && u == UINT64_MAX) || |
| 171 |
(startswith(name, "Memory") && ENDSWITH_SET(name, "Current", "Peak") && u == CGROUP_LIMIT_MAX) || |
| 172 |
(startswith(name, "IO") && ENDSWITH_SET(name, "Bytes", "Operations") && u == UINT64_MAX) || |
| 173 |
(endswith(name, "NSec") && u == UINT64_MAX)) |
| 174 |
|
| 175 |
bus_print_property_value(name, expected_value, flags, "[not set]"); |
| 176 |
|
| 177 |
else if ((ENDSWITH_SET(name, "MemoryLow", "MemoryMin", |
| 178 |
"MemoryHigh", "MemoryMax", |
| 179 |
"MemorySwapMax", "MemoryZSwapMax", "MemoryLimit") && |
| 180 |
u == CGROUP_LIMIT_MAX) || |
| 181 |
(endswith(name, "TasksMax") && u == UINT64_MAX) || |
| 182 |
(startswith(name, "Limit") && u == UINT64_MAX) || |
| 183 |
(startswith(name, "DefaultLimit") && u == UINT64_MAX)) |
| 184 |
|
| 185 |
bus_print_property_value(name, expected_value, flags, "infinity"); |
| 186 |
else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == UINT64_MAX) |
| 187 |
bus_print_property_value(name, expected_value, flags, "[no data]"); |
| 188 |
else |
| 189 |
bus_print_property_valuef(name, expected_value, flags, "%"PRIu64, u); |
| 190 |
|
| 191 |
return 1; |
| 192 |
} |
| 193 |
|
| 194 |
case SD_BUS_TYPE_INT64: { |
| 195 |
int64_t i; |
| 196 |
|
| 197 |
r = sd_bus_message_read_basic(m, type, &i); |
| 198 |
if (r < 0) |
| 199 |
return r; |
| 200 |
|
| 201 |
bus_print_property_valuef(name, expected_value, flags, "%"PRIi64, i); |
| 202 |
return 1; |
| 203 |
} |
| 204 |
|
| 205 |
case SD_BUS_TYPE_UINT32: { |
| 206 |
uint32_t u; |
| 207 |
|
| 208 |
r = sd_bus_message_read_basic(m, type, &u); |
| 209 |
if (r < 0) |
| 210 |
return r; |
| 211 |
|
| 212 |
if (strstr(name, "UMask") || strstr(name, "Mode")) |
| 213 |
bus_print_property_valuef(name, expected_value, flags, "%04o", u); |
| 214 |
|
| 215 |
else if (streq(name, "UID")) { |
| 216 |
if (u == UID_INVALID) |
| 217 |
bus_print_property_value(name, expected_value, flags, "[not set]"); |
| 218 |
else |
| 219 |
bus_print_property_valuef(name, expected_value, flags, "%"PRIu32, u); |
| 220 |
} else if (streq(name, "GID")) { |
| 221 |
if (u == GID_INVALID) |
| 222 |
bus_print_property_value(name, expected_value, flags, "[not set]"); |
| 223 |
else |
| 224 |
bus_print_property_valuef(name, expected_value, flags, "%"PRIu32, u); |
| 225 |
} else |
| 226 |
bus_print_property_valuef(name, expected_value, flags, "%"PRIu32, u); |
| 227 |
|
| 228 |
return 1; |
| 229 |
} |
| 230 |
|
| 231 |
case SD_BUS_TYPE_INT32: { |
| 232 |
int32_t i; |
| 233 |
|
| 234 |
r = sd_bus_message_read_basic(m, type, &i); |
| 235 |
if (r < 0) |
| 236 |
return r; |
| 237 |
|
| 238 |
bus_print_property_valuef(name, expected_value, flags, "%"PRIi32, i); |
| 239 |
return 1; |
| 240 |
} |
| 241 |
|
| 242 |
case SD_BUS_TYPE_DOUBLE: { |
| 243 |
double d; |
| 244 |
|
| 245 |
r = sd_bus_message_read_basic(m, type, &d); |
| 246 |
if (r < 0) |
| 247 |
return r; |
| 248 |
|
| 249 |
bus_print_property_valuef(name, expected_value, flags, "%g", d); |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
case SD_BUS_TYPE_ARRAY: |
| 254 |
if (streq(contents, "s")) { |
| 255 |
bool first = true; |
| 256 |
const char *str; |
| 257 |
|
| 258 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) { |
| 263 |
_cleanup_free_ char *e = NULL; |
| 264 |
|
| 265 |
e = shell_maybe_quote(str, 0); |
| 266 |
if (!e) |
| 267 |
return -ENOMEM; |
| 268 |
|
| 269 |
if (first) { |
| 270 |
if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE)) |
| 271 |
printf("%s=", name); |
| 272 |
first = false; |
| 273 |
} else |
| 274 |
fputs(" ", stdout); |
| 275 |
|
| 276 |
fputs(e, stdout); |
| 277 |
} |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
if (first && FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) && !FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE)) |
| 282 |
printf("%s=", name); |
| 283 |
if (!first || FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY)) |
| 284 |
puts(""); |
| 285 |
|
| 286 |
r = sd_bus_message_exit_container(m); |
| 287 |
if (r < 0) |
| 288 |
return r; |
| 289 |
|
| 290 |
return 1; |
| 291 |
|
| 292 |
} else if (streq(contents, "y")) { |
| 293 |
const uint8_t *u; |
| 294 |
size_t n; |
| 295 |
|
| 296 |
r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n); |
| 297 |
if (r < 0) |
| 298 |
return r; |
| 299 |
|
| 300 |
if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) || n > 0) { |
| 301 |
unsigned i; |
| 302 |
|
| 303 |
if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE)) |
| 304 |
printf("%s=", name); |
| 305 |
|
| 306 |
for (i = 0; i < n; i++) |
| 307 |
printf("%02x", u[i]); |
| 308 |
|
| 309 |
puts(""); |
| 310 |
} |
| 311 |
|
| 312 |
return 1; |
| 313 |
|
| 314 |
} else if (streq(contents, "u")) { |
| 315 |
uint32_t *u; |
| 316 |
size_t n; |
| 317 |
|
| 318 |
r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n); |
| 319 |
if (r < 0) |
| 320 |
return r; |
| 321 |
|
| 322 |
if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) || n > 0) { |
| 323 |
unsigned i; |
| 324 |
|
| 325 |
if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE)) |
| 326 |
printf("%s=", name); |
| 327 |
|
| 328 |
for (i = 0; i < n; i++) |
| 329 |
printf("%08x", u[i]); |
| 330 |
|
| 331 |
puts(""); |
| 332 |
} |
| 333 |
|
| 334 |
return 1; |
| 335 |
} |
| 336 |
|
| 337 |
break; |
| 338 |
} |
| 339 |
|
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
int bus_message_print_all_properties( |
| 344 |
sd_bus_message *m, |
| 345 |
bus_message_print_t func, |
| 346 |
char **filter, |
| 347 |
BusPrintPropertyFlags flags, |
| 348 |
Set **found_properties) { |
| 349 |
|
| 350 |
int r; |
| 351 |
|
| 352 |
assert(m); |
| 353 |
|
| 354 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
| 355 |
if (r < 0) |
| 356 |
return r; |
| 357 |
|
| 358 |
while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { |
| 359 |
_cleanup_free_ char *name_with_equal = NULL; |
| 360 |
const char *name, *contents, *expected_value = NULL; |
| 361 |
|
| 362 |
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name); |
| 363 |
if (r < 0) |
| 364 |
return r; |
| 365 |
|
| 366 |
if (found_properties) { |
| 367 |
r = set_ensure_put(found_properties, &string_hash_ops, name); |
| 368 |
if (r < 0) |
| 369 |
return log_oom(); |
| 370 |
} |
| 371 |
|
| 372 |
name_with_equal = strjoin(name, "="); |
| 373 |
if (!name_with_equal) |
| 374 |
return log_oom(); |
| 375 |
|
| 376 |
if (!filter || |
| 377 |
strv_contains(filter, name) || |
| 378 |
(expected_value = strv_find_startswith(filter, name_with_equal))) { |
| 379 |
|
| 380 |
r = sd_bus_message_peek_type(m, NULL, &contents); |
| 381 |
if (r < 0) |
| 382 |
return r; |
| 383 |
|
| 384 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); |
| 385 |
if (r < 0) |
| 386 |
return r; |
| 387 |
|
| 388 |
if (func) |
| 389 |
r = func(name, expected_value, m, flags); |
| 390 |
if (!func || r == 0) |
| 391 |
r = bus_print_property(name, expected_value, m, flags); |
| 392 |
if (r < 0) |
| 393 |
return r; |
| 394 |
if (r == 0) { |
| 395 |
if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) && !expected_value) |
| 396 |
printf("%s=[unprintable]\n", name); |
| 397 |
/* skip what we didn't read */ |
| 398 |
r = sd_bus_message_skip(m, contents); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
} |
| 402 |
|
| 403 |
r = sd_bus_message_exit_container(m); |
| 404 |
if (r < 0) |
| 405 |
return r; |
| 406 |
} else { |
| 407 |
r = sd_bus_message_skip(m, "v"); |
| 408 |
if (r < 0) |
| 409 |
return r; |
| 410 |
} |
| 411 |
|
| 412 |
r = sd_bus_message_exit_container(m); |
| 413 |
if (r < 0) |
| 414 |
return r; |
| 415 |
} |
| 416 |
if (r < 0) |
| 417 |
return r; |
| 418 |
|
| 419 |
r = sd_bus_message_exit_container(m); |
| 420 |
if (r < 0) |
| 421 |
return r; |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
int bus_print_all_properties( |
| 427 |
sd_bus *bus, |
| 428 |
const char *dest, |
| 429 |
const char *path, |
| 430 |
bus_message_print_t func, |
| 431 |
char **filter, |
| 432 |
BusPrintPropertyFlags flags, |
| 433 |
sd_bus_error *reterr_error) { |
| 434 |
|
| 435 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 436 |
int r; |
| 437 |
|
| 438 |
assert(bus); |
| 439 |
assert(path); |
| 440 |
|
| 441 |
r = sd_bus_call_method(bus, |
| 442 |
dest, |
| 443 |
path, |
| 444 |
"org.freedesktop.DBus.Properties", |
| 445 |
"GetAll", |
| 446 |
reterr_error, |
| 447 |
&reply, |
| 448 |
"s", ""); |
| 449 |
if (r < 0) |
| 450 |
return r; |
| 451 |
|
| 452 |
return bus_message_print_all_properties(reply, func, filter, flags, NULL); |
| 453 |
} |