| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/vm_sockets.h> |
| 4 |
#include <locale.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-id128.h" |
| 9 |
#include "sd-json.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "ansi-color.h" |
| 13 |
#include "build.h" |
| 14 |
#include "bus-common-errors.h" |
| 15 |
#include "bus-error.h" |
| 16 |
#include "bus-locator.h" |
| 17 |
#include "bus-map-properties.h" |
| 18 |
#include "bus-message-util.h" |
| 19 |
#include "bus-util.h" |
| 20 |
#include "errno-util.h" |
| 21 |
#include "format-table.h" |
| 22 |
#include "help-util.h" |
| 23 |
#include "hostname-setup.h" |
| 24 |
#include "hostname-util.h" |
| 25 |
#include "log.h" |
| 26 |
#include "main-func.h" |
| 27 |
#include "options.h" |
| 28 |
#include "os-util.h" |
| 29 |
#include "parse-argument.h" |
| 30 |
#include "polkit-agent.h" |
| 31 |
#include "runtime-scope.h" |
| 32 |
#include "string-util.h" |
| 33 |
#include "strv.h" |
| 34 |
#include "time-util.h" |
| 35 |
#include "verbs.h" |
| 36 |
|
| 37 |
static bool arg_ask_password = true; |
| 38 |
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
| 39 |
static const char *arg_host = NULL; |
| 40 |
static bool arg_transient = false; |
| 41 |
static bool arg_pretty = false; |
| 42 |
static bool arg_static = false; |
| 43 |
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; |
| 44 |
|
| 45 |
typedef struct StatusInfo { |
| 46 |
const char *hostname; |
| 47 |
const char *static_hostname; |
| 48 |
const char *pretty_hostname; |
| 49 |
const char *icon_name; |
| 50 |
const char *chassis; |
| 51 |
const char *chassis_asset_tag; |
| 52 |
const char *deployment; |
| 53 |
const char *location; |
| 54 |
char **tags; |
| 55 |
const char *kernel_name; |
| 56 |
const char *kernel_release; |
| 57 |
const char *os_pretty_name; |
| 58 |
const char *os_fancy_name; |
| 59 |
const char *os_cpe_name; |
| 60 |
usec_t os_support_end; |
| 61 |
const char *os_image_id; |
| 62 |
const char *os_image_version; |
| 63 |
const char *virtualization; |
| 64 |
const char *architecture; |
| 65 |
const char *home_url; |
| 66 |
const char *hardware_vendor; |
| 67 |
const char *hardware_model; |
| 68 |
const char *firmware_version; |
| 69 |
usec_t firmware_date; |
| 70 |
sd_id128_t machine_id; |
| 71 |
sd_id128_t boot_id; |
| 72 |
const char *hardware_serial; |
| 73 |
sd_id128_t product_uuid; |
| 74 |
uint32_t vsock_cid; |
| 75 |
const char *hardware_sku; |
| 76 |
const char *hardware_version; |
| 77 |
} StatusInfo; |
| 78 |
|
| 79 |
static const char* chassis_string_to_glyph(const char *chassis) { |
| 80 |
if (streq_ptr(chassis, "laptop")) |
| 81 |
return UTF8("💻"); /* Personal Computer */ |
| 82 |
if (streq_ptr(chassis, "desktop")) |
| 83 |
return UTF8("🖥️"); /* Desktop Computer */ |
| 84 |
if (streq_ptr(chassis, "server")) |
| 85 |
return UTF8("🖳"); /* Old Personal Computer */ |
| 86 |
if (streq_ptr(chassis, "tablet")) |
| 87 |
return UTF8("具"); /* Ideograph tool, implement; draw up, write, looks vaguely tabletty */ |
| 88 |
if (streq_ptr(chassis, "watch")) |
| 89 |
return UTF8("⌚"); /* Watch */ |
| 90 |
if (streq_ptr(chassis, "handset")) |
| 91 |
return UTF8("📱"); /* Mobile Phone */ |
| 92 |
if (streq_ptr(chassis, "vm")) |
| 93 |
return UTF8("💽"); /* Computer disk */ |
| 94 |
if (streq_ptr(chassis, "container")) |
| 95 |
return UTF8("📦"); /* Package */ |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
static const char *os_support_end_color(usec_t n, usec_t eol) { |
| 100 |
usec_t left; |
| 101 |
|
| 102 |
/* If the end of support is over, color output in red. If only a month is left, color output in |
| 103 |
* yellow. If more than a year is left, color green. In between just show in regular color. */ |
| 104 |
|
| 105 |
if (n >= eol) |
| 106 |
return ansi_highlight_red(); |
| 107 |
left = eol - n; |
| 108 |
if (left < USEC_PER_MONTH) |
| 109 |
return ansi_highlight_yellow(); |
| 110 |
if (left > USEC_PER_YEAR) |
| 111 |
return ansi_highlight_green(); |
| 112 |
|
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
static int print_status_info(StatusInfo *i) { |
| 117 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 118 |
TableCell *cell; |
| 119 |
int r; |
| 120 |
|
| 121 |
assert(i); |
| 122 |
|
| 123 |
table = table_new_vertical(); |
| 124 |
if (!table) |
| 125 |
return log_oom(); |
| 126 |
|
| 127 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 128 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 129 |
|
| 130 |
table_set_ersatz_string(table, TABLE_ERSATZ_UNSET); |
| 131 |
|
| 132 |
if (!isempty(i->hostname) && |
| 133 |
!streq_ptr(i->hostname, i->static_hostname)) { |
| 134 |
r = table_add_many(table, |
| 135 |
TABLE_FIELD, "Transient hostname", |
| 136 |
TABLE_STRING, i->hostname); |
| 137 |
if (r < 0) |
| 138 |
return table_log_add_error(r); |
| 139 |
} |
| 140 |
|
| 141 |
r = table_add_many(table, |
| 142 |
TABLE_FIELD, "Static hostname", |
| 143 |
TABLE_STRING, i->static_hostname); |
| 144 |
if (r < 0) |
| 145 |
return table_log_add_error(r); |
| 146 |
|
| 147 |
if (!isempty(i->pretty_hostname) && |
| 148 |
!streq_ptr(i->pretty_hostname, i->static_hostname)) { |
| 149 |
r = table_add_many(table, |
| 150 |
TABLE_FIELD, "Pretty hostname", |
| 151 |
TABLE_STRING, i->pretty_hostname); |
| 152 |
if (r < 0) |
| 153 |
return table_log_add_error(r); |
| 154 |
} |
| 155 |
|
| 156 |
if (!isempty(i->icon_name)) { |
| 157 |
r = table_add_many(table, |
| 158 |
TABLE_FIELD, "Icon name", |
| 159 |
TABLE_STRING, i->icon_name); |
| 160 |
if (r < 0) |
| 161 |
return table_log_add_error(r); |
| 162 |
} |
| 163 |
|
| 164 |
if (!isempty(i->chassis)) { |
| 165 |
/* Possibly add a pretty symbol. Let's not bother with non-unicode fallbacks, because this is |
| 166 |
* just a prettification and we can't really express this with ASCII anyway. */ |
| 167 |
const char *v = chassis_string_to_glyph(i->chassis); |
| 168 |
if (v) |
| 169 |
v = strjoina(i->chassis, " ", v); |
| 170 |
|
| 171 |
r = table_add_many(table, |
| 172 |
TABLE_FIELD, "Chassis", |
| 173 |
TABLE_STRING, v ?: i->chassis); |
| 174 |
if (r < 0) |
| 175 |
return table_log_add_error(r); |
| 176 |
} |
| 177 |
|
| 178 |
if (!isempty(i->chassis_asset_tag)) { |
| 179 |
r = table_add_many(table, |
| 180 |
TABLE_FIELD, "Chassis Asset Tag", |
| 181 |
TABLE_STRING, i->chassis_asset_tag); |
| 182 |
if (r < 0) |
| 183 |
return table_log_add_error(r); |
| 184 |
} |
| 185 |
|
| 186 |
if (!isempty(i->deployment)) { |
| 187 |
r = table_add_many(table, |
| 188 |
TABLE_FIELD, "Deployment", |
| 189 |
TABLE_STRING, i->deployment); |
| 190 |
if (r < 0) |
| 191 |
return table_log_add_error(r); |
| 192 |
} |
| 193 |
|
| 194 |
if (!isempty(i->location)) { |
| 195 |
r = table_add_many(table, |
| 196 |
TABLE_FIELD, "Location", |
| 197 |
TABLE_STRING, i->location); |
| 198 |
if (r < 0) |
| 199 |
return table_log_add_error(r); |
| 200 |
} |
| 201 |
|
| 202 |
if (!strv_isempty(i->tags)) { |
| 203 |
_cleanup_free_ char *j = strv_join(i->tags, ":"); |
| 204 |
if (!j) |
| 205 |
return log_oom(); |
| 206 |
|
| 207 |
r = table_add_many(table, |
| 208 |
TABLE_FIELD, "Tags", |
| 209 |
TABLE_STRING, j); |
| 210 |
if (r < 0) |
| 211 |
return table_log_add_error(r); |
| 212 |
} |
| 213 |
|
| 214 |
if (!sd_id128_is_null(i->machine_id)) { |
| 215 |
r = table_add_many(table, |
| 216 |
TABLE_FIELD, "Machine ID", |
| 217 |
TABLE_ID128, i->machine_id); |
| 218 |
if (r < 0) |
| 219 |
return table_log_add_error(r); |
| 220 |
} |
| 221 |
|
| 222 |
if (!sd_id128_is_null(i->boot_id)) { |
| 223 |
r = table_add_many(table, |
| 224 |
TABLE_FIELD, "Boot ID", |
| 225 |
TABLE_ID128, i->boot_id); |
| 226 |
if (r < 0) |
| 227 |
return table_log_add_error(r); |
| 228 |
} |
| 229 |
|
| 230 |
if (!sd_id128_is_null(i->product_uuid)) { |
| 231 |
r = table_add_many(table, |
| 232 |
TABLE_FIELD, "Product UUID", |
| 233 |
TABLE_UUID, i->product_uuid); |
| 234 |
if (r < 0) |
| 235 |
return table_log_add_error(r); |
| 236 |
} |
| 237 |
|
| 238 |
if (i->vsock_cid != VMADDR_CID_ANY) { |
| 239 |
r = table_add_many(table, |
| 240 |
TABLE_FIELD, "AF_VSOCK CID", |
| 241 |
TABLE_UINT32, i->vsock_cid); |
| 242 |
if (r < 0) |
| 243 |
return table_log_add_error(r); |
| 244 |
} |
| 245 |
|
| 246 |
if (!isempty(i->virtualization)) { |
| 247 |
r = table_add_many(table, |
| 248 |
TABLE_FIELD, "Virtualization", |
| 249 |
TABLE_STRING, i->virtualization); |
| 250 |
if (r < 0) |
| 251 |
return table_log_add_error(r); |
| 252 |
} |
| 253 |
|
| 254 |
if (use_fancy_name(i->os_fancy_name)) { |
| 255 |
r = table_add_many(table, |
| 256 |
TABLE_FIELD, "Operating System", |
| 257 |
TABLE_STRING_WITH_ANSI, i->os_fancy_name, |
| 258 |
TABLE_SET_URL, i->home_url); |
| 259 |
if (r < 0) |
| 260 |
return table_log_add_error(r); |
| 261 |
} else if (!isempty(i->os_pretty_name)) { |
| 262 |
r = table_add_many(table, |
| 263 |
TABLE_FIELD, "Operating System", |
| 264 |
TABLE_STRING, i->os_pretty_name, |
| 265 |
TABLE_SET_URL, i->home_url); |
| 266 |
if (r < 0) |
| 267 |
return table_log_add_error(r); |
| 268 |
} |
| 269 |
|
| 270 |
if (!isempty(i->os_cpe_name)) { |
| 271 |
r = table_add_many(table, |
| 272 |
TABLE_FIELD, "CPE OS Name", |
| 273 |
TABLE_STRING, i->os_cpe_name); |
| 274 |
if (r < 0) |
| 275 |
return table_log_add_error(r); |
| 276 |
} |
| 277 |
|
| 278 |
if (timestamp_is_set(i->os_support_end)) { |
| 279 |
usec_t n = now(CLOCK_REALTIME); |
| 280 |
|
| 281 |
r = table_add_many(table, |
| 282 |
TABLE_FIELD, "OS Support End", |
| 283 |
TABLE_TIMESTAMP_DATE, i->os_support_end, |
| 284 |
TABLE_FIELD, n < i->os_support_end ? "OS Support Remaining" : "OS Support Expired", |
| 285 |
TABLE_TIMESPAN_DAY, n < i->os_support_end ? i->os_support_end - n : n - i->os_support_end, |
| 286 |
TABLE_SET_COLOR, os_support_end_color(n, i->os_support_end)); |
| 287 |
if (r < 0) |
| 288 |
return table_log_add_error(r); |
| 289 |
} |
| 290 |
|
| 291 |
if (!isempty(i->os_image_id)) { |
| 292 |
r = table_add_many(table, |
| 293 |
TABLE_FIELD, "OS Image", |
| 294 |
TABLE_STRING, i->os_image_id); |
| 295 |
if (r < 0) |
| 296 |
return table_log_add_error(r); |
| 297 |
} |
| 298 |
|
| 299 |
if (!isempty(i->os_image_version)) { |
| 300 |
r = table_add_many(table, |
| 301 |
TABLE_FIELD, "OS Image Version", |
| 302 |
TABLE_STRING, i->os_image_version); |
| 303 |
if (r < 0) |
| 304 |
return table_log_add_error(r); |
| 305 |
} |
| 306 |
|
| 307 |
if (!isempty(i->kernel_name) && !isempty(i->kernel_release)) { |
| 308 |
const char *v; |
| 309 |
|
| 310 |
v = strjoina(i->kernel_name, " ", i->kernel_release); |
| 311 |
r = table_add_many(table, |
| 312 |
TABLE_FIELD, "Kernel", |
| 313 |
TABLE_STRING, v); |
| 314 |
if (r < 0) |
| 315 |
return table_log_add_error(r); |
| 316 |
} |
| 317 |
|
| 318 |
if (!isempty(i->architecture)) { |
| 319 |
r = table_add_many(table, |
| 320 |
TABLE_FIELD, "Architecture", |
| 321 |
TABLE_STRING, i->architecture); |
| 322 |
if (r < 0) |
| 323 |
return table_log_add_error(r); |
| 324 |
} |
| 325 |
|
| 326 |
if (!isempty(i->hardware_vendor)) { |
| 327 |
r = table_add_many(table, |
| 328 |
TABLE_FIELD, "Hardware Vendor", |
| 329 |
TABLE_STRING, i->hardware_vendor); |
| 330 |
if (r < 0) |
| 331 |
return table_log_add_error(r); |
| 332 |
} |
| 333 |
|
| 334 |
if (!isempty(i->hardware_model)) { |
| 335 |
r = table_add_many(table, |
| 336 |
TABLE_FIELD, "Hardware Model", |
| 337 |
TABLE_STRING, i->hardware_model); |
| 338 |
if (r < 0) |
| 339 |
return table_log_add_error(r); |
| 340 |
} |
| 341 |
|
| 342 |
if (!isempty(i->hardware_serial)) { |
| 343 |
r = table_add_many(table, |
| 344 |
TABLE_FIELD, "Hardware Serial", |
| 345 |
TABLE_STRING, i->hardware_serial); |
| 346 |
if (r < 0) |
| 347 |
return table_log_add_error(r); |
| 348 |
} |
| 349 |
|
| 350 |
if (!isempty(i->hardware_sku)) { |
| 351 |
r = table_add_many(table, |
| 352 |
TABLE_FIELD, "Hardware SKU", |
| 353 |
TABLE_STRING, i->hardware_sku); |
| 354 |
if (r < 0) |
| 355 |
return table_log_add_error(r); |
| 356 |
} |
| 357 |
|
| 358 |
if (!isempty(i->hardware_version)) { |
| 359 |
r = table_add_many(table, |
| 360 |
TABLE_FIELD, "Hardware Version", |
| 361 |
TABLE_STRING, i->hardware_version); |
| 362 |
if (r < 0) |
| 363 |
return table_log_add_error(r); |
| 364 |
} |
| 365 |
|
| 366 |
if (!isempty(i->firmware_version)) { |
| 367 |
r = table_add_many(table, |
| 368 |
TABLE_FIELD, "Firmware Version", |
| 369 |
TABLE_STRING, i->firmware_version); |
| 370 |
if (r < 0) |
| 371 |
return table_log_add_error(r); |
| 372 |
} |
| 373 |
|
| 374 |
if (timestamp_is_set(i->firmware_date)) { |
| 375 |
usec_t n = now(CLOCK_REALTIME); |
| 376 |
|
| 377 |
r = table_add_many(table, |
| 378 |
TABLE_FIELD, "Firmware Date", |
| 379 |
TABLE_TIMESTAMP_DATE, i->firmware_date); |
| 380 |
if (r < 0) |
| 381 |
return table_log_add_error(r); |
| 382 |
|
| 383 |
if (i->firmware_date < n) { |
| 384 |
r = table_add_many(table, |
| 385 |
TABLE_FIELD, "Firmware Age", |
| 386 |
TABLE_TIMESPAN_DAY, n - i->firmware_date, |
| 387 |
TABLE_SET_COLOR, n - i->firmware_date > USEC_PER_YEAR*2 ? ansi_highlight_yellow() : NULL); |
| 388 |
if (r < 0) |
| 389 |
return table_log_add_error(r); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return table_print_or_warn(table); |
| 394 |
} |
| 395 |
|
| 396 |
static int get_one_name(sd_bus *bus, const char* attr, char **ret) { |
| 397 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 398 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 399 |
const char *s; |
| 400 |
int r; |
| 401 |
|
| 402 |
assert(bus); |
| 403 |
assert(attr); |
| 404 |
|
| 405 |
/* This obtains one string property, and copy it if 'ret' is set, or print it otherwise. */ |
| 406 |
|
| 407 |
r = bus_get_property(bus, bus_hostname, attr, &error, &reply, "s"); |
| 408 |
if (r < 0) |
| 409 |
return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r)); |
| 410 |
|
| 411 |
r = sd_bus_message_read(reply, "s", &s); |
| 412 |
if (r < 0) |
| 413 |
return bus_log_parse_error(r); |
| 414 |
|
| 415 |
if (ret) { |
| 416 |
char *str; |
| 417 |
|
| 418 |
str = strdup(s); |
| 419 |
if (!str) |
| 420 |
return log_oom(); |
| 421 |
|
| 422 |
*ret = str; |
| 423 |
} else |
| 424 |
printf("%s\n", s); |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
static void status_info_done(StatusInfo *info) { |
| 430 |
assert(info); |
| 431 |
|
| 432 |
info->tags = strv_free(info->tags); |
| 433 |
} |
| 434 |
|
| 435 |
static int show_all_names(sd_bus *bus) { |
| 436 |
_cleanup_(status_info_done) StatusInfo info = { |
| 437 |
.vsock_cid = VMADDR_CID_ANY, |
| 438 |
.os_support_end = USEC_INFINITY, |
| 439 |
.firmware_date = USEC_INFINITY, |
| 440 |
}; |
| 441 |
|
| 442 |
static const struct bus_properties_map hostname_map[] = { |
| 443 |
{ "Hostname", "s", NULL, offsetof(StatusInfo, hostname) }, |
| 444 |
{ "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) }, |
| 445 |
{ "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) }, |
| 446 |
{ "IconName", "s", NULL, offsetof(StatusInfo, icon_name) }, |
| 447 |
{ "Chassis", "s", NULL, offsetof(StatusInfo, chassis) }, |
| 448 |
{ "ChassisAssetTag", "s", NULL, offsetof(StatusInfo, chassis_asset_tag)}, |
| 449 |
{ "Deployment", "s", NULL, offsetof(StatusInfo, deployment) }, |
| 450 |
{ "Location", "s", NULL, offsetof(StatusInfo, location) }, |
| 451 |
{ "Tags", "as", NULL, offsetof(StatusInfo, tags) }, |
| 452 |
{ "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) }, |
| 453 |
{ "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) }, |
| 454 |
{ "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) }, |
| 455 |
{ "OperatingSystemFancyName", "s", NULL, offsetof(StatusInfo, os_fancy_name) }, |
| 456 |
{ "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) }, |
| 457 |
{ "OperatingSystemSupportEnd", "t", NULL, offsetof(StatusInfo, os_support_end) }, |
| 458 |
{ "OperatingSystemImageID", "s", NULL, offsetof(StatusInfo, os_image_id) }, |
| 459 |
{ "OperatingSystemImageVersion", "s", NULL, offsetof(StatusInfo, os_image_version) }, |
| 460 |
{ "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) }, |
| 461 |
{ "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) }, |
| 462 |
{ "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) }, |
| 463 |
{ "HardwareSKU", "s", NULL, offsetof(StatusInfo, hardware_sku) }, |
| 464 |
{ "HardwareVersion", "s", NULL, offsetof(StatusInfo, hardware_version) }, |
| 465 |
{ "FirmwareVersion", "s", NULL, offsetof(StatusInfo, firmware_version) }, |
| 466 |
{ "FirmwareDate", "t", NULL, offsetof(StatusInfo, firmware_date) }, |
| 467 |
{ "MachineID", "ay", bus_map_id128, offsetof(StatusInfo, machine_id) }, |
| 468 |
{ "BootID", "ay", bus_map_id128, offsetof(StatusInfo, boot_id) }, |
| 469 |
{ "VSockCID", "u", NULL, offsetof(StatusInfo, vsock_cid) }, |
| 470 |
{} |
| 471 |
}, manager_map[] = { |
| 472 |
{ "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) }, |
| 473 |
{ "Architecture", "s", NULL, offsetof(StatusInfo, architecture) }, |
| 474 |
{} |
| 475 |
}; |
| 476 |
|
| 477 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL; |
| 478 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 479 |
int r; |
| 480 |
|
| 481 |
r = bus_map_all_properties(bus, |
| 482 |
"org.freedesktop.hostname1", |
| 483 |
"/org/freedesktop/hostname1", |
| 484 |
hostname_map, |
| 485 |
0, |
| 486 |
&error, |
| 487 |
&host_message, |
| 488 |
&info); |
| 489 |
if (r < 0) |
| 490 |
return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r)); |
| 491 |
|
| 492 |
r = bus_map_all_properties(bus, |
| 493 |
"org.freedesktop.systemd1", |
| 494 |
"/org/freedesktop/systemd1", |
| 495 |
manager_map, |
| 496 |
0, |
| 497 |
&error, |
| 498 |
&manager_message, |
| 499 |
&info); |
| 500 |
if (r < 0) |
| 501 |
return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r)); |
| 502 |
|
| 503 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *product_uuid_reply = NULL; |
| 504 |
r = bus_call_method(bus, |
| 505 |
bus_hostname, |
| 506 |
"GetProductUUID", |
| 507 |
&error, |
| 508 |
&product_uuid_reply, |
| 509 |
"b", |
| 510 |
false); |
| 511 |
if (r < 0) { |
| 512 |
log_full_errno(sd_bus_error_has_names( |
| 513 |
&error, |
| 514 |
BUS_ERROR_NO_PRODUCT_UUID, |
| 515 |
SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, |
| 516 |
SD_BUS_ERROR_UNKNOWN_METHOD) ? LOG_DEBUG : LOG_WARNING, |
| 517 |
r, "Failed to query product UUID, ignoring: %s", bus_error_message(&error, r)); |
| 518 |
sd_bus_error_free(&error); |
| 519 |
} else { |
| 520 |
r = bus_message_read_id128(product_uuid_reply, &info.product_uuid); |
| 521 |
if (r < 0) |
| 522 |
return bus_log_parse_error(r); |
| 523 |
} |
| 524 |
|
| 525 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *hardware_serial_reply = NULL; |
| 526 |
r = bus_call_method(bus, |
| 527 |
bus_hostname, |
| 528 |
"GetHardwareSerial", |
| 529 |
&error, |
| 530 |
&hardware_serial_reply, |
| 531 |
NULL); |
| 532 |
if (r < 0) |
| 533 |
log_full_errno(sd_bus_error_has_names( |
| 534 |
&error, |
| 535 |
BUS_ERROR_NO_HARDWARE_SERIAL, |
| 536 |
SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, |
| 537 |
SD_BUS_ERROR_UNKNOWN_METHOD) || |
| 538 |
ERRNO_IS_DEVICE_ABSENT(r) ? LOG_DEBUG : LOG_WARNING, /* old hostnamed used to send ENOENT/ENODEV back to client as is, handle that gracefully */ |
| 539 |
r, "Failed to query hardware serial, ignoring: %s", bus_error_message(&error, r)); |
| 540 |
else { |
| 541 |
r = sd_bus_message_read_basic(hardware_serial_reply, 's', &info.hardware_serial); |
| 542 |
if (r < 0) |
| 543 |
return bus_log_parse_error(r); |
| 544 |
} |
| 545 |
|
| 546 |
/* For older version of hostnamed. */ |
| 547 |
if (!arg_host) { |
| 548 |
if (sd_id128_is_null(info.machine_id)) |
| 549 |
(void) sd_id128_get_machine(&info.machine_id); |
| 550 |
if (sd_id128_is_null(info.boot_id)) |
| 551 |
(void) sd_id128_get_boot(&info.boot_id); |
| 552 |
} |
| 553 |
|
| 554 |
return print_status_info(&info); |
| 555 |
} |
| 556 |
|
| 557 |
static int get_hostname_based_on_flag(sd_bus *bus) { |
| 558 |
const char *attr; |
| 559 |
|
| 560 |
if (!!arg_static + !!arg_pretty + !!arg_transient > 1) |
| 561 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 562 |
"Cannot query more than one name type at a time"); |
| 563 |
|
| 564 |
attr = arg_pretty ? "PrettyHostname" : |
| 565 |
arg_static ? "StaticHostname" : "Hostname"; |
| 566 |
|
| 567 |
return get_one_name(bus, attr, NULL); |
| 568 |
} |
| 569 |
|
| 570 |
VERB_DEFAULT_NOARG(verb_show_status, "status", "Show current hostname settings"); |
| 571 |
static int verb_show_status(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 572 |
sd_bus *bus = userdata; |
| 573 |
int r; |
| 574 |
|
| 575 |
if (sd_json_format_enabled(arg_json_format_flags)) { |
| 576 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 577 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 578 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 579 |
const char *text = NULL; |
| 580 |
|
| 581 |
r = bus_call_method(bus, bus_hostname, "Describe", &error, &reply, NULL); |
| 582 |
if (r < 0) |
| 583 |
return log_error_errno(r, "Could not get description: %s", bus_error_message(&error, r)); |
| 584 |
|
| 585 |
r = sd_bus_message_read(reply, "s", &text); |
| 586 |
if (r < 0) |
| 587 |
return bus_log_parse_error(r); |
| 588 |
|
| 589 |
r = sd_json_parse(text, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 590 |
if (r < 0) |
| 591 |
return log_error_errno(r, "Failed to parse JSON: %m"); |
| 592 |
|
| 593 |
sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); |
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
if (arg_pretty || arg_static || arg_transient) |
| 598 |
return get_hostname_based_on_flag(bus); |
| 599 |
|
| 600 |
return show_all_names(bus); |
| 601 |
} |
| 602 |
|
| 603 |
static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) { |
| 604 |
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
| 605 |
int r; |
| 606 |
|
| 607 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 608 |
|
| 609 |
if (!error) |
| 610 |
error = &e; |
| 611 |
|
| 612 |
r = bus_call_method(bus, bus_hostname, method, error, NULL, "sb", value, arg_ask_password); |
| 613 |
if (r < 0) |
| 614 |
return log_error_errno(r, "Could not set %s: %s", target, bus_error_message(error, r)); |
| 615 |
|
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
static int set_simple_string(sd_bus *bus, const char *target, const char *method, const char *value) { |
| 620 |
return set_simple_string_internal(bus, NULL, target, method, value); |
| 621 |
} |
| 622 |
|
| 623 |
static int verb_set_hostname(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 624 |
_cleanup_free_ char *h = NULL; |
| 625 |
const char *hostname = argv[1]; |
| 626 |
sd_bus *bus = userdata; |
| 627 |
bool implicit = false, show_hint = false; |
| 628 |
int r, ret = 0; |
| 629 |
|
| 630 |
if (!arg_pretty && !arg_static && !arg_transient) |
| 631 |
arg_pretty = arg_static = arg_transient = implicit = true; |
| 632 |
|
| 633 |
if (!implicit && !arg_static && arg_transient) { |
| 634 |
_cleanup_free_ char *source = NULL; |
| 635 |
|
| 636 |
r = get_one_name(bus, "HostnameSource", &source); |
| 637 |
if (r < 0) |
| 638 |
return r; |
| 639 |
|
| 640 |
if (hostname_source_from_string(source) == HOSTNAME_STATIC) |
| 641 |
log_info("Hint: static hostname is already set, so the specified transient hostname will not be used."); |
| 642 |
} |
| 643 |
|
| 644 |
if (arg_pretty) { |
| 645 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 646 |
const char *p; |
| 647 |
|
| 648 |
/* If the passed hostname is already valid, then assume the user doesn't know anything about pretty |
| 649 |
* hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic |
| 650 |
* hostname. */ |
| 651 |
if (implicit && hostname_is_valid(hostname, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK)) |
| 652 |
p = ""; /* No pretty hostname (as it is redundant), just a static one */ |
| 653 |
else |
| 654 |
p = hostname; /* Use the passed name as pretty hostname */ |
| 655 |
|
| 656 |
r = set_simple_string_internal(bus, &error, "pretty hostname", "SetPrettyHostname", p); |
| 657 |
if (r < 0) { |
| 658 |
if (implicit && |
| 659 |
sd_bus_error_has_names(&error, |
| 660 |
BUS_ERROR_FILE_IS_PROTECTED, |
| 661 |
BUS_ERROR_READ_ONLY_FILESYSTEM)) { |
| 662 |
show_hint = true; |
| 663 |
ret = r; |
| 664 |
} else |
| 665 |
return r; |
| 666 |
} |
| 667 |
|
| 668 |
/* Now that we set the pretty hostname, let's clean up the parameter and use that as static |
| 669 |
* hostname. If the hostname was already valid as static hostname, this will only chop off the trailing |
| 670 |
* dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping |
| 671 |
* multiple dots, and dropping weird chars. Note that we clean the name up only if we also are |
| 672 |
* supposed to set the pretty name. If the pretty name is not being set we assume the user knows what |
| 673 |
* they are doing and pass the name as-is. */ |
| 674 |
h = strdup(hostname); |
| 675 |
if (!h) |
| 676 |
return log_oom(); |
| 677 |
|
| 678 |
hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */ |
| 679 |
} |
| 680 |
|
| 681 |
if (arg_static) { |
| 682 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 683 |
|
| 684 |
r = set_simple_string_internal(bus, &error, "static hostname", "SetStaticHostname", hostname); |
| 685 |
if (r < 0) { |
| 686 |
if (implicit && |
| 687 |
sd_bus_error_has_names(&error, |
| 688 |
BUS_ERROR_FILE_IS_PROTECTED, |
| 689 |
BUS_ERROR_READ_ONLY_FILESYSTEM)) { |
| 690 |
show_hint = true; |
| 691 |
ret = r; |
| 692 |
} else |
| 693 |
return r; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
if (arg_transient) { |
| 698 |
r = set_simple_string(bus, "transient hostname", "SetHostname", hostname); |
| 699 |
if (r < 0) |
| 700 |
return r; |
| 701 |
} |
| 702 |
|
| 703 |
if (show_hint) |
| 704 |
log_info("Hint: use --transient option when /etc/machine-info or /etc/hostname cannot be modified (e.g. located in read-only filesystem)."); |
| 705 |
|
| 706 |
return ret; |
| 707 |
} |
| 708 |
|
| 709 |
VERB(verb_get_or_set_hostname, "hostname", "[NAME]", VERB_ANY, 2, 0, "Get/set system hostname"); |
| 710 |
VERB(verb_get_or_set_hostname, "set-hostname", "NAME", 2, 2, 0, NULL); /* obsolete */ |
| 711 |
static int verb_get_or_set_hostname(int argc, char *argv[], uintptr_t data, void *userdata) { |
| 712 |
return argc == 1 ? get_hostname_based_on_flag(userdata) : |
| 713 |
verb_set_hostname(argc, argv, data, userdata); |
| 714 |
} |
| 715 |
|
| 716 |
VERB(verb_get_or_set_icon_name, "icon-name", "[NAME]", VERB_ANY, 2, 0, "Get/set icon name for host"); |
| 717 |
VERB(verb_get_or_set_icon_name, "set-icon-name", "NAME", 2, 2, 0, NULL); /* obsolete */ |
| 718 |
static int verb_get_or_set_icon_name(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 719 |
return argc == 1 ? get_one_name(userdata, "IconName", NULL) : |
| 720 |
set_simple_string(userdata, "icon", "SetIconName", argv[1]); |
| 721 |
} |
| 722 |
|
| 723 |
VERB(verb_get_or_set_chassis, "chassis", "[NAME]", VERB_ANY, 2, 0, "Get/set chassis type for host"); |
| 724 |
VERB(verb_get_or_set_chassis, "set-chassis", "NAME", 2, 2, 0, NULL); /* obsolete */ |
| 725 |
static int verb_get_or_set_chassis(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 726 |
return argc == 1 ? get_one_name(userdata, "Chassis", NULL) : |
| 727 |
set_simple_string(userdata, "chassis", "SetChassis", argv[1]); |
| 728 |
} |
| 729 |
|
| 730 |
VERB(verb_get_or_set_deployment, "deployment", "[NAME]", VERB_ANY, 2, 0, "Get/set deployment environment for host"); |
| 731 |
VERB(verb_get_or_set_deployment, "set-deployment", "NAME", 2, 2, 0, NULL); /* obsolete */ |
| 732 |
static int verb_get_or_set_deployment(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 733 |
return argc == 1 ? get_one_name(userdata, "Deployment", NULL) : |
| 734 |
set_simple_string(userdata, "deployment", "SetDeployment", argv[1]); |
| 735 |
} |
| 736 |
|
| 737 |
VERB(verb_get_or_set_location, "location", "[NAME]", VERB_ANY, 2, 0, "Get/set location for host"); |
| 738 |
VERB(verb_get_or_set_location, "set-location", "NAME", 2, 2, 0, NULL); /* obsolete */ |
| 739 |
static int verb_get_or_set_location(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 740 |
return argc == 1 ? get_one_name(userdata, "Location", NULL) : |
| 741 |
set_simple_string(userdata, "location", "SetLocation", argv[1]); |
| 742 |
} |
| 743 |
|
| 744 |
VERB(verb_get_or_set_tags, "tags", "[TAG …]", VERB_ANY, VERB_ANY, 0, "Get/set machine tags for host"); |
| 745 |
static int verb_get_or_set_tags(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 746 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 747 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 748 |
int r; |
| 749 |
|
| 750 |
if (argc == 1) { |
| 751 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 752 |
|
| 753 |
_cleanup_free_ char *j = NULL; |
| 754 |
r = bus_get_property(bus, bus_hostname, "Tags", &error, &reply, "as"); |
| 755 |
if (r < 0) { |
| 756 |
if (!sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY)) |
| 757 |
return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r)); |
| 758 |
|
| 759 |
/* Old hostnamed didn't know the tags concept, hence such a machine has no tags. */ |
| 760 |
} else { |
| 761 |
_cleanup_strv_free_ char **l = NULL; |
| 762 |
r = sd_bus_message_read_strv(reply, &l); |
| 763 |
if (r < 0) |
| 764 |
return bus_log_parse_error(r); |
| 765 |
|
| 766 |
j = strv_join(l, ":"); |
| 767 |
if (!j) |
| 768 |
return log_oom(); |
| 769 |
} |
| 770 |
|
| 771 |
printf("%s\n", strempty(j)); |
| 772 |
return 0; |
| 773 |
} |
| 774 |
|
| 775 |
_cleanup_strv_free_ char **l = NULL; |
| 776 |
for (int i = 1; i < argc; i++) { |
| 777 |
r = strv_split_and_extend(&l, argv[i], ":", /* filter_duplicates= */ true); |
| 778 |
if (r < 0) |
| 779 |
return log_oom(); |
| 780 |
} |
| 781 |
|
| 782 |
strv_sort(l); |
| 783 |
|
| 784 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 785 |
|
| 786 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 787 |
r = bus_message_new_method_call(bus, &m, bus_hostname, "SetTags"); |
| 788 |
if (r < 0) |
| 789 |
return bus_log_create_error(r); |
| 790 |
|
| 791 |
r = sd_bus_message_append_strv(m, l); |
| 792 |
if (r < 0) |
| 793 |
return bus_log_create_error(r); |
| 794 |
|
| 795 |
r = sd_bus_call(bus, m, /* usec= */ 0, &error, /* ret_reply= */ NULL); |
| 796 |
if (r < 0) |
| 797 |
return log_error_errno(r, "Could not set tags: %s", bus_error_message(&error, r)); |
| 798 |
|
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
static int help(void) { |
| 803 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 804 |
int r; |
| 805 |
|
| 806 |
r = option_parser_get_help_table(&options); |
| 807 |
if (r < 0) |
| 808 |
return r; |
| 809 |
|
| 810 |
r = verbs_get_help_table(&verbs); |
| 811 |
if (r < 0) |
| 812 |
return r; |
| 813 |
|
| 814 |
(void) table_sync_column_widths(0, options, verbs); |
| 815 |
|
| 816 |
help_cmdline("[OPTIONS...] COMMAND ..."); |
| 817 |
help_abstract("Query or change system hostname."); |
| 818 |
|
| 819 |
help_section("Commands"); |
| 820 |
r = table_print_or_warn(verbs); |
| 821 |
if (r < 0) |
| 822 |
return r; |
| 823 |
|
| 824 |
help_section("Options"); |
| 825 |
r = table_print_or_warn(options); |
| 826 |
if (r < 0) |
| 827 |
return r; |
| 828 |
|
| 829 |
help_man_page_reference("hostnamectl", "1"); |
| 830 |
return 0; |
| 831 |
} |
| 832 |
|
| 833 |
VERB_COMMON_HELP_HIDDEN(help); |
| 834 |
|
| 835 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 836 |
int r; |
| 837 |
|
| 838 |
assert(argc >= 0); |
| 839 |
assert(argv); |
| 840 |
|
| 841 |
OptionParser opts = { argc, argv }; |
| 842 |
|
| 843 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 844 |
switch (c) { |
| 845 |
OPTION_COMMON_HELP: |
| 846 |
return help(); |
| 847 |
|
| 848 |
OPTION_COMMON_VERSION: |
| 849 |
return version(); |
| 850 |
|
| 851 |
OPTION_COMMON_NO_ASK_PASSWORD: |
| 852 |
arg_ask_password = false; |
| 853 |
break; |
| 854 |
|
| 855 |
OPTION_COMMON_HOST: |
| 856 |
arg_transport = BUS_TRANSPORT_REMOTE; |
| 857 |
arg_host = opts.arg; |
| 858 |
break; |
| 859 |
|
| 860 |
OPTION_COMMON_MACHINE: |
| 861 |
r = parse_machine_argument(opts.arg, &arg_host, &arg_transport); |
| 862 |
if (r < 0) |
| 863 |
return r; |
| 864 |
break; |
| 865 |
|
| 866 |
OPTION_LONG("transient", NULL, "Only set transient hostname"): |
| 867 |
arg_transient = true; |
| 868 |
break; |
| 869 |
|
| 870 |
OPTION_LONG("static", NULL, "Only set static hostname"): |
| 871 |
arg_static = true; |
| 872 |
break; |
| 873 |
|
| 874 |
OPTION_LONG("pretty", NULL, "Only set pretty hostname"): |
| 875 |
arg_pretty = true; |
| 876 |
break; |
| 877 |
|
| 878 |
OPTION_COMMON_JSON: |
| 879 |
r = parse_json_argument(opts.arg, &arg_json_format_flags); |
| 880 |
if (r <= 0) |
| 881 |
return r; |
| 882 |
break; |
| 883 |
|
| 884 |
OPTION_COMMON_LOWERCASE_J: |
| 885 |
arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; |
| 886 |
break; |
| 887 |
} |
| 888 |
|
| 889 |
*ret_args = option_parser_get_args(&opts); |
| 890 |
return 1; |
| 891 |
} |
| 892 |
|
| 893 |
static int run(int argc, char *argv[]) { |
| 894 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 895 |
char **args = NULL; |
| 896 |
int r; |
| 897 |
|
| 898 |
setlocale(LC_ALL, ""); |
| 899 |
log_setup(); |
| 900 |
|
| 901 |
r = parse_argv(argc, argv, &args); |
| 902 |
if (r <= 0) |
| 903 |
return r; |
| 904 |
|
| 905 |
r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus); |
| 906 |
if (r < 0) |
| 907 |
return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM); |
| 908 |
|
| 909 |
return dispatch_verb(args, bus); |
| 910 |
} |
| 911 |
|
| 912 |
DEFINE_MAIN_FUNCTION(run); |