1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/vm_sockets.h>
12 #include "alloc-util.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"
20 #include "errno-util.h"
21 #include "format-table.h"
22 #include "hostname-setup.h"
23 #include "hostname-util.h"
25 #include "main-func.h"
26 #include "parse-argument.h"
27 #include "polkit-agent.h"
28 #include "pretty-print.h"
29 #include "runtime-scope.h"
30 #include "string-util.h"
31 #include "time-util.h"
34 static bool arg_ask_password
= true;
35 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
36 static const char *arg_host
= NULL
;
37 static bool arg_transient
= false;
38 static bool arg_pretty
= false;
39 static bool arg_static
= false;
40 static sd_json_format_flags_t arg_json_format_flags
= SD_JSON_FORMAT_OFF
;
42 typedef struct StatusInfo
{
44 const char *static_hostname
;
45 const char *pretty_hostname
;
46 const char *icon_name
;
48 const char *chassis_asset_tag
;
49 const char *deployment
;
51 const char *kernel_name
;
52 const char *kernel_release
;
53 const char *os_pretty_name
;
54 const char *os_cpe_name
;
55 usec_t os_support_end
;
56 const char *os_image_id
;
57 const char *os_image_version
;
58 const char *virtualization
;
59 const char *architecture
;
61 const char *hardware_vendor
;
62 const char *hardware_model
;
63 const char *firmware_version
;
65 sd_id128_t machine_id
;
67 const char *hardware_serial
;
68 sd_id128_t product_uuid
;
70 const char *hardware_sku
;
71 const char *hardware_version
;
74 static const char* chassis_string_to_glyph(const char *chassis
) {
75 if (streq_ptr(chassis
, "laptop"))
76 return UTF8("💻"); /* Personal Computer */
77 if (streq_ptr(chassis
, "desktop"))
78 return UTF8("🖥️"); /* Desktop Computer */
79 if (streq_ptr(chassis
, "server"))
80 return UTF8("🖳"); /* Old Personal Computer */
81 if (streq_ptr(chassis
, "tablet"))
82 return UTF8("具"); /* Ideograph tool, implement; draw up, write, looks vaguely tabletty */
83 if (streq_ptr(chassis
, "watch"))
84 return UTF8("⌚"); /* Watch */
85 if (streq_ptr(chassis
, "handset"))
86 return UTF8("🕻"); /* Left Hand Telephone Receiver */
87 if (streq_ptr(chassis
, "vm"))
88 return UTF8("🖴"); /* Hard disk */
89 if (streq_ptr(chassis
, "container"))
90 return UTF8("☐"); /* Ballot Box */
94 static const char *os_support_end_color(usec_t n
, usec_t eol
) {
97 /* If the end of support is over, color output in red. If only a month is left, color output in
98 * yellow. If more than a year is left, color green. In between just show in regular color. */
101 return ansi_highlight_red();
103 if (left
< USEC_PER_MONTH
)
104 return ansi_highlight_yellow();
105 if (left
> USEC_PER_YEAR
)
106 return ansi_highlight_green();
111 static int print_status_info(StatusInfo
*i
) {
112 _cleanup_(table_unrefp
) Table
*table
= NULL
;
118 table
= table_new_vertical();
122 assert_se(cell
= table_get_cell(table
, 0, 0));
123 (void) table_set_ellipsize_percent(table
, cell
, 100);
125 table_set_ersatz_string(table
, TABLE_ERSATZ_UNSET
);
127 if (!isempty(i
->hostname
) &&
128 !streq_ptr(i
->hostname
, i
->static_hostname
)) {
129 r
= table_add_many(table
,
130 TABLE_FIELD
, "Transient hostname",
131 TABLE_STRING
, i
->hostname
);
133 return table_log_add_error(r
);
136 r
= table_add_many(table
,
137 TABLE_FIELD
, "Static hostname",
138 TABLE_STRING
, i
->static_hostname
);
140 return table_log_add_error(r
);
142 if (!isempty(i
->pretty_hostname
) &&
143 !streq_ptr(i
->pretty_hostname
, i
->static_hostname
)) {
144 r
= table_add_many(table
,
145 TABLE_FIELD
, "Pretty hostname",
146 TABLE_STRING
, i
->pretty_hostname
);
148 return table_log_add_error(r
);
151 if (!isempty(i
->icon_name
)) {
152 r
= table_add_many(table
,
153 TABLE_FIELD
, "Icon name",
154 TABLE_STRING
, i
->icon_name
);
156 return table_log_add_error(r
);
159 if (!isempty(i
->chassis
)) {
160 /* Possibly add a pretty symbol. Let's not bother with non-unicode fallbacks, because this is
161 * just a prettification and we can't really express this with ASCII anyway. */
162 const char *v
= chassis_string_to_glyph(i
->chassis
);
164 v
= strjoina(i
->chassis
, " ", v
);
166 r
= table_add_many(table
,
167 TABLE_FIELD
, "Chassis",
168 TABLE_STRING
, v
?: i
->chassis
);
170 return table_log_add_error(r
);
173 if (!isempty(i
->chassis_asset_tag
)) {
174 r
= table_add_many(table
,
175 TABLE_FIELD
, "Chassis Asset Tag",
176 TABLE_STRING
, i
->chassis_asset_tag
);
178 return table_log_add_error(r
);
181 if (!isempty(i
->deployment
)) {
182 r
= table_add_many(table
,
183 TABLE_FIELD
, "Deployment",
184 TABLE_STRING
, i
->deployment
);
186 return table_log_add_error(r
);
189 if (!isempty(i
->location
)) {
190 r
= table_add_many(table
,
191 TABLE_FIELD
, "Location",
192 TABLE_STRING
, i
->location
);
194 return table_log_add_error(r
);
197 if (!sd_id128_is_null(i
->machine_id
)) {
198 r
= table_add_many(table
,
199 TABLE_FIELD
, "Machine ID",
200 TABLE_ID128
, i
->machine_id
);
202 return table_log_add_error(r
);
205 if (!sd_id128_is_null(i
->boot_id
)) {
206 r
= table_add_many(table
,
207 TABLE_FIELD
, "Boot ID",
208 TABLE_ID128
, i
->boot_id
);
210 return table_log_add_error(r
);
213 if (!sd_id128_is_null(i
->product_uuid
)) {
214 r
= table_add_many(table
,
215 TABLE_FIELD
, "Product UUID",
216 TABLE_UUID
, i
->product_uuid
);
218 return table_log_add_error(r
);
221 if (i
->vsock_cid
!= VMADDR_CID_ANY
) {
222 r
= table_add_many(table
,
223 TABLE_FIELD
, "AF_VSOCK CID",
224 TABLE_UINT32
, i
->vsock_cid
);
226 return table_log_add_error(r
);
229 if (!isempty(i
->virtualization
)) {
230 r
= table_add_many(table
,
231 TABLE_FIELD
, "Virtualization",
232 TABLE_STRING
, i
->virtualization
);
234 return table_log_add_error(r
);
237 if (!isempty(i
->os_pretty_name
)) {
238 r
= table_add_many(table
,
239 TABLE_FIELD
, "Operating System",
240 TABLE_STRING
, i
->os_pretty_name
,
241 TABLE_SET_URL
, i
->home_url
);
243 return table_log_add_error(r
);
246 if (!isempty(i
->os_cpe_name
)) {
247 r
= table_add_many(table
,
248 TABLE_FIELD
, "CPE OS Name",
249 TABLE_STRING
, i
->os_cpe_name
);
251 return table_log_add_error(r
);
254 if (timestamp_is_set(i
->os_support_end
)) {
255 usec_t n
= now(CLOCK_REALTIME
);
257 r
= table_add_many(table
,
258 TABLE_FIELD
, "OS Support End",
259 TABLE_TIMESTAMP_DATE
, i
->os_support_end
,
260 TABLE_FIELD
, n
< i
->os_support_end
? "OS Support Remaining" : "OS Support Expired",
261 TABLE_TIMESPAN_DAY
, n
< i
->os_support_end
? i
->os_support_end
- n
: n
- i
->os_support_end
,
262 TABLE_SET_COLOR
, os_support_end_color(n
, i
->os_support_end
));
264 return table_log_add_error(r
);
267 if (!isempty(i
->os_image_id
)) {
268 r
= table_add_many(table
,
269 TABLE_FIELD
, "OS Image",
270 TABLE_STRING
, i
->os_image_id
);
272 return table_log_add_error(r
);
275 if (!isempty(i
->os_image_version
)) {
276 r
= table_add_many(table
,
277 TABLE_FIELD
, "OS Image Version",
278 TABLE_STRING
, i
->os_image_version
);
280 return table_log_add_error(r
);
283 if (!isempty(i
->kernel_name
) && !isempty(i
->kernel_release
)) {
286 v
= strjoina(i
->kernel_name
, " ", i
->kernel_release
);
287 r
= table_add_many(table
,
288 TABLE_FIELD
, "Kernel",
291 return table_log_add_error(r
);
294 if (!isempty(i
->architecture
)) {
295 r
= table_add_many(table
,
296 TABLE_FIELD
, "Architecture",
297 TABLE_STRING
, i
->architecture
);
299 return table_log_add_error(r
);
302 if (!isempty(i
->hardware_vendor
)) {
303 r
= table_add_many(table
,
304 TABLE_FIELD
, "Hardware Vendor",
305 TABLE_STRING
, i
->hardware_vendor
);
307 return table_log_add_error(r
);
310 if (!isempty(i
->hardware_model
)) {
311 r
= table_add_many(table
,
312 TABLE_FIELD
, "Hardware Model",
313 TABLE_STRING
, i
->hardware_model
);
315 return table_log_add_error(r
);
318 if (!isempty(i
->hardware_serial
)) {
319 r
= table_add_many(table
,
320 TABLE_FIELD
, "Hardware Serial",
321 TABLE_STRING
, i
->hardware_serial
);
323 return table_log_add_error(r
);
326 if (!isempty(i
->hardware_sku
)) {
327 r
= table_add_many(table
,
328 TABLE_FIELD
, "Hardware SKU",
329 TABLE_STRING
, i
->hardware_sku
);
331 return table_log_add_error(r
);
334 if (!isempty(i
->hardware_version
)) {
335 r
= table_add_many(table
,
336 TABLE_FIELD
, "Hardware Version",
337 TABLE_STRING
, i
->hardware_version
);
339 return table_log_add_error(r
);
342 if (!isempty(i
->firmware_version
)) {
343 r
= table_add_many(table
,
344 TABLE_FIELD
, "Firmware Version",
345 TABLE_STRING
, i
->firmware_version
);
347 return table_log_add_error(r
);
350 if (timestamp_is_set(i
->firmware_date
)) {
351 usec_t n
= now(CLOCK_REALTIME
);
353 r
= table_add_many(table
,
354 TABLE_FIELD
, "Firmware Date",
355 TABLE_TIMESTAMP_DATE
, i
->firmware_date
);
357 return table_log_add_error(r
);
359 if (i
->firmware_date
< n
) {
360 r
= table_add_many(table
,
361 TABLE_FIELD
, "Firmware Age",
362 TABLE_TIMESPAN_DAY
, n
- i
->firmware_date
,
363 TABLE_SET_COLOR
, n
- i
->firmware_date
> USEC_PER_YEAR
*2 ? ansi_highlight_yellow() : NULL
);
365 return table_log_add_error(r
);
369 r
= table_print(table
, NULL
);
371 return table_log_print_error(r
);
376 static int get_one_name(sd_bus
*bus
, const char* attr
, char **ret
) {
377 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
378 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
385 /* This obtains one string property, and copy it if 'ret' is set, or print it otherwise. */
387 r
= bus_get_property(bus
, bus_hostname
, attr
, &error
, &reply
, "s");
389 return log_error_errno(r
, "Could not get property: %s", bus_error_message(&error
, r
));
391 r
= sd_bus_message_read(reply
, "s", &s
);
393 return bus_log_parse_error(r
);
409 static int show_all_names(sd_bus
*bus
) {
411 .vsock_cid
= VMADDR_CID_ANY
,
412 .os_support_end
= USEC_INFINITY
,
413 .firmware_date
= USEC_INFINITY
,
416 static const struct bus_properties_map hostname_map
[] = {
417 { "Hostname", "s", NULL
, offsetof(StatusInfo
, hostname
) },
418 { "StaticHostname", "s", NULL
, offsetof(StatusInfo
, static_hostname
) },
419 { "PrettyHostname", "s", NULL
, offsetof(StatusInfo
, pretty_hostname
) },
420 { "IconName", "s", NULL
, offsetof(StatusInfo
, icon_name
) },
421 { "Chassis", "s", NULL
, offsetof(StatusInfo
, chassis
) },
422 { "ChassisAssetTag", "s", NULL
, offsetof(StatusInfo
, chassis_asset_tag
)},
423 { "Deployment", "s", NULL
, offsetof(StatusInfo
, deployment
) },
424 { "Location", "s", NULL
, offsetof(StatusInfo
, location
) },
425 { "KernelName", "s", NULL
, offsetof(StatusInfo
, kernel_name
) },
426 { "KernelRelease", "s", NULL
, offsetof(StatusInfo
, kernel_release
) },
427 { "OperatingSystemPrettyName", "s", NULL
, offsetof(StatusInfo
, os_pretty_name
) },
428 { "OperatingSystemCPEName", "s", NULL
, offsetof(StatusInfo
, os_cpe_name
) },
429 { "OperatingSystemSupportEnd", "t", NULL
, offsetof(StatusInfo
, os_support_end
) },
430 { "OperatingSystemImageID", "s", NULL
, offsetof(StatusInfo
, os_image_id
) },
431 { "OperatingSystemImageVersion", "s", NULL
, offsetof(StatusInfo
, os_image_version
) },
432 { "HomeURL", "s", NULL
, offsetof(StatusInfo
, home_url
) },
433 { "HardwareVendor", "s", NULL
, offsetof(StatusInfo
, hardware_vendor
) },
434 { "HardwareModel", "s", NULL
, offsetof(StatusInfo
, hardware_model
) },
435 { "HardwareSKU", "s", NULL
, offsetof(StatusInfo
, hardware_sku
) },
436 { "HardwareVersion", "s", NULL
, offsetof(StatusInfo
, hardware_version
) },
437 { "FirmwareVersion", "s", NULL
, offsetof(StatusInfo
, firmware_version
) },
438 { "FirmwareDate", "t", NULL
, offsetof(StatusInfo
, firmware_date
) },
439 { "MachineID", "ay", bus_map_id128
, offsetof(StatusInfo
, machine_id
) },
440 { "BootID", "ay", bus_map_id128
, offsetof(StatusInfo
, boot_id
) },
441 { "VSockCID", "u", NULL
, offsetof(StatusInfo
, vsock_cid
) },
444 { "Virtualization", "s", NULL
, offsetof(StatusInfo
, virtualization
) },
445 { "Architecture", "s", NULL
, offsetof(StatusInfo
, architecture
) },
449 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*host_message
= NULL
, *manager_message
= NULL
;
450 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
453 r
= bus_map_all_properties(bus
,
454 "org.freedesktop.hostname1",
455 "/org/freedesktop/hostname1",
462 return log_error_errno(r
, "Failed to query system properties: %s", bus_error_message(&error
, r
));
464 r
= bus_map_all_properties(bus
,
465 "org.freedesktop.systemd1",
466 "/org/freedesktop/systemd1",
473 return log_error_errno(r
, "Failed to query system properties: %s", bus_error_message(&error
, r
));
475 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*product_uuid_reply
= NULL
;
476 r
= bus_call_method(bus
,
484 log_full_errno(sd_bus_error_has_names(
486 BUS_ERROR_NO_PRODUCT_UUID
,
487 SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
,
488 SD_BUS_ERROR_UNKNOWN_METHOD
) ? LOG_DEBUG
: LOG_WARNING
,
489 r
, "Failed to query product UUID, ignoring: %s", bus_error_message(&error
, r
));
490 sd_bus_error_free(&error
);
492 r
= bus_message_read_id128(product_uuid_reply
, &info
.product_uuid
);
494 return bus_log_parse_error(r
);
497 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*hardware_serial_reply
= NULL
;
498 r
= bus_call_method(bus
,
502 &hardware_serial_reply
,
505 log_full_errno(sd_bus_error_has_names(
507 BUS_ERROR_NO_HARDWARE_SERIAL
,
508 SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
,
509 SD_BUS_ERROR_UNKNOWN_METHOD
) ||
510 ERRNO_IS_DEVICE_ABSENT(r
) ? LOG_DEBUG
: LOG_WARNING
, /* old hostnamed used to send ENOENT/ENODEV back to client as is, handle that gracefully */
511 r
, "Failed to query hardware serial, ignoring: %s", bus_error_message(&error
, r
));
513 r
= sd_bus_message_read_basic(hardware_serial_reply
, 's', &info
.hardware_serial
);
515 return bus_log_parse_error(r
);
518 /* For older version of hostnamed. */
520 if (sd_id128_is_null(info
.machine_id
))
521 (void) sd_id128_get_machine(&info
.machine_id
);
522 if (sd_id128_is_null(info
.boot_id
))
523 (void) sd_id128_get_boot(&info
.boot_id
);
526 return print_status_info(&info
);
529 static int get_hostname_based_on_flag(sd_bus
*bus
) {
532 if (!!arg_static
+ !!arg_pretty
+ !!arg_transient
> 1)
533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
534 "Cannot query more than one name type at a time");
536 attr
= arg_pretty
? "PrettyHostname" :
537 arg_static
? "StaticHostname" : "Hostname";
539 return get_one_name(bus
, attr
, NULL
);
542 static int show_status(int argc
, char **argv
, void *userdata
) {
543 sd_bus
*bus
= userdata
;
546 if (sd_json_format_enabled(arg_json_format_flags
)) {
547 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
548 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
549 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*v
= NULL
;
550 const char *text
= NULL
;
552 r
= bus_call_method(bus
, bus_hostname
, "Describe", &error
, &reply
, NULL
);
554 return log_error_errno(r
, "Could not get description: %s", bus_error_message(&error
, r
));
556 r
= sd_bus_message_read(reply
, "s", &text
);
558 return bus_log_parse_error(r
);
560 r
= sd_json_parse(text
, 0, &v
, NULL
, NULL
);
562 return log_error_errno(r
, "Failed to parse JSON: %m");
564 sd_json_variant_dump(v
, arg_json_format_flags
, NULL
, NULL
);
568 if (arg_pretty
|| arg_static
|| arg_transient
)
569 return get_hostname_based_on_flag(bus
);
571 return show_all_names(bus
);
574 static int set_simple_string_internal(sd_bus
*bus
, sd_bus_error
*error
, const char *target
, const char *method
, const char *value
) {
575 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
578 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
583 r
= bus_call_method(bus
, bus_hostname
, method
, error
, NULL
, "sb", value
, arg_ask_password
);
585 return log_error_errno(r
, "Could not set %s: %s", target
, bus_error_message(error
, r
));
590 static int set_simple_string(sd_bus
*bus
, const char *target
, const char *method
, const char *value
) {
591 return set_simple_string_internal(bus
, NULL
, target
, method
, value
);
594 static int set_hostname(int argc
, char **argv
, void *userdata
) {
595 _cleanup_free_
char *h
= NULL
;
596 const char *hostname
= argv
[1];
597 sd_bus
*bus
= userdata
;
598 bool implicit
= false, show_hint
= false;
601 if (!arg_pretty
&& !arg_static
&& !arg_transient
)
602 arg_pretty
= arg_static
= arg_transient
= implicit
= true;
604 if (!implicit
&& !arg_static
&& arg_transient
) {
605 _cleanup_free_
char *source
= NULL
;
607 r
= get_one_name(bus
, "HostnameSource", &source
);
611 if (hostname_source_from_string(source
) == HOSTNAME_STATIC
)
612 log_info("Hint: static hostname is already set, so the specified transient hostname will not be used.");
616 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
619 /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty
620 * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
622 if (implicit
&& hostname_is_valid(hostname
, VALID_HOSTNAME_TRAILING_DOT
|VALID_HOSTNAME_QUESTION_MARK
))
623 p
= ""; /* No pretty hostname (as it is redundant), just a static one */
625 p
= hostname
; /* Use the passed name as pretty hostname */
627 r
= set_simple_string_internal(bus
, &error
, "pretty hostname", "SetPrettyHostname", p
);
630 sd_bus_error_has_names(&error
,
631 BUS_ERROR_FILE_IS_PROTECTED
,
632 BUS_ERROR_READ_ONLY_FILESYSTEM
)) {
639 /* Now that we set the pretty hostname, let's clean up the parameter and use that as static
640 * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing
641 * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping
642 * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are
643 * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what
644 * they are doing and pass the name as-is. */
645 h
= strdup(hostname
);
649 hostname
= hostname_cleanup(h
); /* Use the cleaned up name as static hostname */
653 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
655 r
= set_simple_string_internal(bus
, &error
, "static hostname", "SetStaticHostname", hostname
);
658 sd_bus_error_has_names(&error
,
659 BUS_ERROR_FILE_IS_PROTECTED
,
660 BUS_ERROR_READ_ONLY_FILESYSTEM
)) {
669 r
= set_simple_string(bus
, "transient hostname", "SetHostname", hostname
);
675 log_info("Hint: use --transient option when /etc/machine-info or /etc/hostname cannot be modified (e.g. located in read-only filesystem).");
680 static int get_or_set_hostname(int argc
, char **argv
, void *userdata
) {
681 return argc
== 1 ? get_hostname_based_on_flag(userdata
) :
682 set_hostname(argc
, argv
, userdata
);
685 static int get_or_set_icon_name(int argc
, char **argv
, void *userdata
) {
686 return argc
== 1 ? get_one_name(userdata
, "IconName", NULL
) :
687 set_simple_string(userdata
, "icon", "SetIconName", argv
[1]);
690 static int get_or_set_chassis(int argc
, char **argv
, void *userdata
) {
691 return argc
== 1 ? get_one_name(userdata
, "Chassis", NULL
) :
692 set_simple_string(userdata
, "chassis", "SetChassis", argv
[1]);
695 static int get_or_set_deployment(int argc
, char **argv
, void *userdata
) {
696 return argc
== 1 ? get_one_name(userdata
, "Deployment", NULL
) :
697 set_simple_string(userdata
, "deployment", "SetDeployment", argv
[1]);
700 static int get_or_set_location(int argc
, char **argv
, void *userdata
) {
701 return argc
== 1 ? get_one_name(userdata
, "Location", NULL
) :
702 set_simple_string(userdata
, "location", "SetLocation", argv
[1]);
705 static int help(void) {
706 _cleanup_free_
char *link
= NULL
;
709 r
= terminal_urlify_man("hostnamectl", "1", &link
);
713 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
714 "%2$sQuery or change system hostname.%3$s\n"
715 "\n%4$sCommands:%5$s\n"
716 " status Show current hostname settings\n"
717 " hostname [NAME] Get/set system hostname\n"
718 " icon-name [NAME] Get/set icon name for host\n"
719 " chassis [NAME] Get/set chassis type for host\n"
720 " deployment [NAME] Get/set deployment environment for host\n"
721 " location [NAME] Get/set location for host\n"
722 "\n%4$sOptions:%5$s\n"
723 " -h --help Show this help\n"
724 " --version Show package version\n"
725 " --no-ask-password Do not prompt for password\n"
726 " -H --host=[USER@]HOST Operate on remote host\n"
727 " -M --machine=CONTAINER Operate on local container\n"
728 " --transient Only set transient hostname\n"
729 " --static Only set static hostname\n"
730 " --pretty Only set pretty hostname\n"
731 " --json=pretty|short|off\n"
732 " Generate JSON output\n"
733 " -j Same as --json=pretty on tty, --json=short otherwise\n"
734 "\nSee the %6$s for details.\n",
735 program_invocation_short_name
,
745 static int verb_help(int argc
, char **argv
, void *userdata
) {
749 static int parse_argv(int argc
, char *argv
[]) {
760 static const struct option options
[] = {
761 { "help", no_argument
, NULL
, 'h' },
762 { "version", no_argument
, NULL
, ARG_VERSION
},
763 { "transient", no_argument
, NULL
, ARG_TRANSIENT
},
764 { "static", no_argument
, NULL
, ARG_STATIC
},
765 { "pretty", no_argument
, NULL
, ARG_PRETTY
},
766 { "host", required_argument
, NULL
, 'H' },
767 { "machine", required_argument
, NULL
, 'M' },
768 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
769 { "json", required_argument
, NULL
, ARG_JSON
},
778 while ((c
= getopt_long(argc
, argv
, "hH:M:j", options
, NULL
)) >= 0)
789 arg_transport
= BUS_TRANSPORT_REMOTE
;
794 r
= parse_machine_argument(optarg
, &arg_host
, &arg_transport
);
800 arg_transient
= true;
811 case ARG_NO_ASK_PASSWORD
:
812 arg_ask_password
= false;
816 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
823 arg_json_format_flags
= SD_JSON_FORMAT_PRETTY_AUTO
|SD_JSON_FORMAT_COLOR_AUTO
;
830 assert_not_reached();
836 static int hostnamectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
838 static const Verb verbs
[] = {
839 { "status", VERB_ANY
, 1, VERB_DEFAULT
, show_status
},
840 { "hostname", VERB_ANY
, 2, 0, get_or_set_hostname
},
841 { "set-hostname", 2, 2, 0, get_or_set_hostname
}, /* obsolete */
842 { "icon-name", VERB_ANY
, 2, 0, get_or_set_icon_name
},
843 { "set-icon-name", 2, 2, 0, get_or_set_icon_name
}, /* obsolete */
844 { "chassis", VERB_ANY
, 2, 0, get_or_set_chassis
},
845 { "set-chassis", 2, 2, 0, get_or_set_chassis
}, /* obsolete */
846 { "deployment", VERB_ANY
, 2, 0, get_or_set_deployment
},
847 { "set-deployment", 2, 2, 0, get_or_set_deployment
}, /* obsolete */
848 { "location", VERB_ANY
, 2, 0, get_or_set_location
},
849 { "set-location", 2, 2, 0, get_or_set_location
}, /* obsolete */
850 { "help", VERB_ANY
, VERB_ANY
, 0, verb_help
}, /* Not documented, but supported since it is created. */
854 return dispatch_verb(argc
, argv
, verbs
, bus
);
857 static int run(int argc
, char *argv
[]) {
858 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
861 setlocale(LC_ALL
, "");
864 r
= parse_argv(argc
, argv
);
868 r
= bus_connect_transport(arg_transport
, arg_host
, RUNTIME_SCOPE_SYSTEM
, &bus
);
870 return bus_log_connect_error(r
, arg_transport
, RUNTIME_SCOPE_SYSTEM
);
872 return hostnamectl_main(bus
, argc
, argv
);
875 DEFINE_MAIN_FUNCTION(run
);