X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=src%2Fhostname%2Fhostnamectl.c;h=7f9bb49e0c900a2267794902c20903916acdcfd0;hb=6e866b331d7cd4a5e0759dd160dea6edabd3678e;hp=dcbad99ae948ddac1a581069034a86fe465722f8;hpb=13d276d0d7fb7a1150663395ef30a885a7389db4;p=systemd.git diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index dcbad99ae..7f9bb49e0 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -1,39 +1,24 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ +/* 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 . -***/ - -#include -#include #include #include +#include +#include #include #include "sd-bus.h" #include "sd-id128.h" -#include "hostname-util.h" -#include "bus-util.h" + +#include "alloc-util.h" +#include "architecture.h" #include "bus-error.h" -#include "util.h" +#include "bus-util.h" +#include "hostname-util.h" +#include "main-func.h" +#include "pretty-print.h" #include "spawn-polkit-agent.h" -#include "build.h" -#include "architecture.h" +#include "util.h" +#include "verbs.h" static bool arg_ask_password = true; static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; @@ -42,32 +27,21 @@ static bool arg_transient = false; static bool arg_pretty = false; static bool arg_static = false; -static void polkit_agent_open_if_enabled(void) { - - /* Open the polkit agent as a child process if necessary */ - if (!arg_ask_password) - return; - - if (arg_transport != BUS_TRANSPORT_LOCAL) - return; - - polkit_agent_open(); -} - 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) { @@ -111,8 +85,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); @@ -126,8 +109,8 @@ static void print_status_info(StatusInfo *i) { } static int show_one_name(sd_bus *bus, const char* attr) { - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *s; int r; @@ -138,10 +121,8 @@ static int show_one_name(sd_bus *bus, const char* attr) { "org.freedesktop.hostname1", attr, &error, &reply, "s"); - if (r < 0) { - log_error("Could not get property: %s", bus_error_message(&error, -r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r)); r = sd_bus_message_read(reply, "s", &s); if (r < 0) @@ -152,7 +133,7 @@ static int show_one_name(sd_bus *bus, const char* attr) { return 0; } -static int show_all_names(sd_bus *bus) { +static int show_all_names(sd_bus *bus, sd_bus_error *error) { StatusInfo info = {}; static const struct bus_properties_map hostname_map[] = { @@ -167,6 +148,7 @@ static int show_all_names(sd_bus *bus) { { "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) }, {} }; @@ -176,44 +158,37 @@ static int show_all_names(sd_bus *bus) { {} }; + _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, - &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) { - assert(args); +static int show_status(int argc, char **argv, void *userdata) { + sd_bus *bus = userdata; + int r; if (arg_pretty || arg_static || arg_transient) { const char *attr; @@ -227,15 +202,22 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { arg_static ? "StaticHostname" : "Hostname"; return show_one_name(bus, attr); - } else - return show_all_names(bus); + } else { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + + r = show_all_names(bus, &error); + if (r < 0) + return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r)); + + return 0; + } } static int set_simple_string(sd_bus *bus, const char *method, const char *value) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; int r = 0; - polkit_agent_open_if_enabled(); + polkit_agent_open_if_enabled(arg_transport, arg_ask_password); r = sd_bus_call_method( bus, @@ -246,45 +228,46 @@ static int set_simple_string(sd_bus *bus, const char *method, const char *value) &error, NULL, "sb", value, arg_ask_password); if (r < 0) - log_error("Could not set property: %s", bus_error_message(&error, -r)); - return r; + return log_error_errno(r, "Could not set property: %s", bus_error_message(&error, -r)); + + return 0; } -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; - 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; if (arg_pretty) { const char *p; - /* If the passed hostname is already valid, then - * assume the user doesn't know anything about pretty - * hostnames, so let's unset the pretty hostname, and - * just set the passed hostname as static/dynamic + /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty + * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic * hostname. */ - - if (arg_static && hostname_is_valid(hostname, true)) { - p = ""; - /* maybe get rid of trailing dot */ - hostname = hostname_cleanup(hostname); - } else { - p = h = strdup(hostname); - if (!p) - return log_oom(); - - hostname_cleanup(hostname); - } + if (arg_static && hostname_is_valid(hostname, true)) + p = ""; /* No pretty hostname (as it is redundant), just a static one */ + else + p = hostname; /* Use the passed name as pretty hostname */ r = set_simple_string(bus, "SetPrettyHostname", p); if (r < 0) return r; + + /* Now that we set the pretty hostname, let's clean up the parameter and use that as static + * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing + * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping + * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are + * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what + * he does and pass the name as-is. */ + h = strdup(hostname); + if (!h) + return log_oom(); + + hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */ } if (arg_static) { @@ -302,35 +285,30 @@ 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); +static int set_deployment(int argc, char **argv, void *userdata) { + return set_simple_string(userdata, "SetDeployment", argv[1]); +} - return set_simple_string(bus, "SetDeployment", args[1]); +static int set_location(int argc, char **argv, void *userdata) { + return set_simple_string(userdata, "SetLocation", argv[1]); } -static int set_location(sd_bus *bus, char **args, unsigned n) { - assert(args); - assert(n == 2); +static int help(void) { + _cleanup_free_ char *link = NULL; + int r; - return set_simple_string(bus, "SetLocation", args[1]); -} + r = terminal_urlify_man("hostnamectl", "1", &link); + if (r < 0) + return log_oom(); -static void help(void) { printf("%s [OPTIONS...] COMMAND ...\n\n" "Query or change system hostname.\n\n" " -h --help Show this help\n" @@ -348,7 +326,16 @@ static void help(void) { " set-chassis NAME Set chassis type for host\n" " set-deployment NAME Set deployment environment for host\n" " set-location NAME Set location for host\n" - , program_invocation_short_name); + "\nSee the %s for details.\n" + , program_invocation_short_name + , link + ); + + return 0; +} + +static int verb_help(int argc, char **argv, void *userdata) { + return help(); } static int parse_argv(int argc, char *argv[]) { @@ -383,13 +370,10 @@ static int parse_argv(int argc, char *argv[]) { switch (c) { case 'h': - help(); - return 0; + return help(); case ARG_VERSION: - puts(PACKAGE_STRING); - puts(SYSTEMD_FEATURES); - return 0; + return version(); case 'H': arg_transport = BUS_TRANSPORT_REMOTE; @@ -429,86 +413,22 @@ 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[]) { - _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL; +static int run(int argc, char *argv[]) { + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); @@ -517,16 +437,13 @@ int main(int argc, char *argv[]) { r = parse_argv(argc, argv); if (r <= 0) - goto finish; - - r = bus_open_transport(arg_transport, arg_host, false, &bus); - if (r < 0) { - log_error_errno(r, "Failed to create bus connection: %m"); - goto finish; - } + return r; - r = hostnamectl_main(bus, argc, argv); + r = bus_connect_transport(arg_transport, arg_host, false, &bus); + if (r < 0) + return log_error_errno(r, "Failed to create bus connection: %m"); -finish: - return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; + return hostnamectl_main(bus, argc, argv); } + +DEFINE_MAIN_FUNCTION(run);