diff options
Diffstat (limited to 'src/hostname/hostnamectl.c')
-rw-r--r-- | src/hostname/hostnamectl.c | 231 |
1 files changed, 71 insertions, 160 deletions
diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index e06b111b43..8587f5c59f 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -1,22 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** - This file is part of systemd. - - Copyright 2012 Lennart Poettering - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ #include <getopt.h> #include <locale.h> @@ -33,7 +15,9 @@ #include "bus-util.h" #include "hostname-util.h" #include "spawn-polkit-agent.h" +#include "terminal-util.h" #include "util.h" +#include "verbs.h" static bool arg_ask_password = true; static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; @@ -43,19 +27,20 @@ static bool arg_pretty = false; static bool arg_static = false; typedef struct StatusInfo { - char *hostname; - char *static_hostname; - char *pretty_hostname; - char *icon_name; - char *chassis; - char *deployment; - char *location; - char *kernel_name; - char *kernel_release; - char *os_pretty_name; - char *os_cpe_name; - char *virtualization; - char *architecture; + const char *hostname; + const char *static_hostname; + const char *pretty_hostname; + const char *icon_name; + const char *chassis; + const char *deployment; + const char *location; + const char *kernel_name; + const char *kernel_release; + const char *os_pretty_name; + const char *os_cpe_name; + const char *virtualization; + const char *architecture; + const char *home_url; } StatusInfo; static void print_status_info(StatusInfo *i) { @@ -99,8 +84,17 @@ static void print_status_info(StatusInfo *i) { if (!isempty(i->virtualization)) printf(" Virtualization: %s\n", i->virtualization); - if (!isempty(i->os_pretty_name)) - printf(" Operating System: %s\n", i->os_pretty_name); + if (!isempty(i->os_pretty_name)) { + _cleanup_free_ char *formatted = NULL; + const char *t = i->os_pretty_name; + + if (i->home_url) { + if (terminal_urlify(i->home_url, i->os_pretty_name, &formatted) >= 0) + t = formatted; + } + + printf(" Operating System: %s\n", t); + } if (!isempty(i->os_cpe_name)) printf(" CPE OS Name: %s\n", i->os_cpe_name); @@ -153,6 +147,7 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) { { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) }, { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) }, { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) }, + { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) }, {} }; @@ -162,49 +157,38 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) { {} }; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL; int r; r = bus_map_all_properties(bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", hostname_map, + 0, error, + &host_message, &info); if (r < 0) - goto fail; + return r; - bus_map_all_properties(bus, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - manager_map, - error, - &info); + r = bus_map_all_properties(bus, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + manager_map, + 0, + error, + &manager_message, + &info); print_status_info(&info); -fail: - free(info.hostname); - free(info.static_hostname); - free(info.pretty_hostname); - free(info.icon_name); - free(info.chassis); - free(info.deployment); - free(info.location); - free(info.kernel_name); - free(info.kernel_release); - free(info.os_pretty_name); - free(info.os_cpe_name); - free(info.virtualization); - free(info.architecture); - return r; } -static int show_status(sd_bus *bus, char **args, unsigned n) { +static int show_status(int argc, char **argv, void *userdata) { + sd_bus *bus = userdata; int r; - assert(args); - if (arg_pretty || arg_static || arg_transient) { const char *attr; @@ -247,14 +231,12 @@ static int set_simple_string(sd_bus *bus, const char *method, const char *value) return r; } -static int set_hostname(sd_bus *bus, char **args, unsigned n) { +static int set_hostname(int argc, char **argv, void *userdata) { _cleanup_free_ char *h = NULL; - const char *hostname = args[1]; + const char *hostname = argv[1]; + sd_bus *bus = userdata; int r; - assert(args); - assert(n == 2); - if (!arg_pretty && !arg_static && !arg_transient) arg_pretty = arg_static = arg_transient = true; @@ -301,35 +283,23 @@ static int set_hostname(sd_bus *bus, char **args, unsigned n) { return 0; } -static int set_icon_name(sd_bus *bus, char **args, unsigned n) { - assert(args); - assert(n == 2); - - return set_simple_string(bus, "SetIconName", args[1]); +static int set_icon_name(int argc, char **argv, void *userdata) { + return set_simple_string(userdata, "SetIconName", argv[1]); } -static int set_chassis(sd_bus *bus, char **args, unsigned n) { - assert(args); - assert(n == 2); - - return set_simple_string(bus, "SetChassis", args[1]); +static int set_chassis(int argc, char **argv, void *userdata) { + return set_simple_string(userdata, "SetChassis", argv[1]); } -static int set_deployment(sd_bus *bus, char **args, unsigned n) { - assert(args); - assert(n == 2); - - return set_simple_string(bus, "SetDeployment", args[1]); +static int set_deployment(int argc, char **argv, void *userdata) { + return set_simple_string(userdata, "SetDeployment", argv[1]); } -static int set_location(sd_bus *bus, char **args, unsigned n) { - assert(args); - assert(n == 2); - - return set_simple_string(bus, "SetLocation", args[1]); +static int set_location(int argc, char **argv, void *userdata) { + return set_simple_string(userdata, "SetLocation", argv[1]); } -static void help(void) { +static int help(void) { printf("%s [OPTIONS...] COMMAND ...\n\n" "Query or change system hostname.\n\n" " -h --help Show this help\n" @@ -348,6 +318,12 @@ static void help(void) { " set-deployment NAME Set deployment environment for host\n" " set-location NAME Set location for host\n" , program_invocation_short_name); + + return 0; +} + +static int verb_help(int argc, char **argv, void *userdata) { + return help(); } static int parse_argv(int argc, char *argv[]) { @@ -382,8 +358,7 @@ static int parse_argv(int argc, char *argv[]) { switch (c) { case 'h': - help(); - return 0; + return help(); case ARG_VERSION: return version(); @@ -426,82 +401,18 @@ static int parse_argv(int argc, char *argv[]) { static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) { - static const struct { - const char* verb; - const enum { - MORE, - LESS, - EQUAL - } argc_cmp; - const int argc; - int (* const dispatch)(sd_bus *bus, char **args, unsigned n); - } verbs[] = { - { "status", LESS, 1, show_status }, - { "set-hostname", EQUAL, 2, set_hostname }, - { "set-icon-name", EQUAL, 2, set_icon_name }, - { "set-chassis", EQUAL, 2, set_chassis }, - { "set-deployment", EQUAL, 2, set_deployment }, - { "set-location", EQUAL, 2, set_location }, + static const Verb verbs[] = { + { "status", VERB_ANY, 1, VERB_DEFAULT, show_status }, + { "set-hostname", 2, 2, 0, set_hostname }, + { "set-icon-name", 2, 2, 0, set_icon_name }, + { "set-chassis", 2, 2, 0, set_chassis }, + { "set-deployment", 2, 2, 0, set_deployment }, + { "set-location", 2, 2, 0, set_location }, + { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */ + {} }; - int left; - unsigned i; - - assert(argc >= 0); - assert(argv); - - left = argc - optind; - - if (left <= 0) - /* Special rule: no arguments means "status" */ - i = 0; - else { - if (streq(argv[optind], "help")) { - help(); - return 0; - } - - for (i = 0; i < ELEMENTSOF(verbs); i++) - if (streq(argv[optind], verbs[i].verb)) - break; - - if (i >= ELEMENTSOF(verbs)) { - log_error("Unknown operation %s", argv[optind]); - return -EINVAL; - } - } - - switch (verbs[i].argc_cmp) { - - case EQUAL: - if (left != verbs[i].argc) { - log_error("Invalid number of arguments."); - return -EINVAL; - } - - break; - - case MORE: - if (left < verbs[i].argc) { - log_error("Too few arguments."); - return -EINVAL; - } - - break; - - case LESS: - if (left > verbs[i].argc) { - log_error("Too many arguments."); - return -EINVAL; - } - - break; - - default: - assert_not_reached("Unknown comparison operator."); - } - - return verbs[i].dispatch(bus, argv + optind, left); + return dispatch_verb(argc, argv, verbs, bus); } int main(int argc, char *argv[]) { |