1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-22 06:50:18 +03:00

hostnamectl: show hint when user try to set transient hostname but static hostname is already used

This commit is contained in:
Yu Watanabe 2021-01-25 13:44:00 +09:00
parent e8acf09186
commit f2d3ec7abf
3 changed files with 33 additions and 5 deletions

View File

@ -14,6 +14,7 @@
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-map-properties.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "main-func.h"
#include "pretty-print.h"
@ -117,12 +118,17 @@ static void print_status_info(StatusInfo *i) {
printf(" Hardware Model: %s\n", i->hardware_model);
}
static int show_one_name(sd_bus *bus, const char* attr) {
static int get_one_name(sd_bus *bus, const char* attr, char **ret) {
_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;
assert(bus);
assert(attr);
/* This obtains one string property, and copy it if 'ret' is set, or print it otherwise. */
r = sd_bus_get_property(
bus,
"org.freedesktop.hostname1",
@ -137,7 +143,16 @@ static int show_one_name(sd_bus *bus, const char* attr) {
if (r < 0)
return bus_log_parse_error(r);
printf("%s\n", s);
if (ret) {
char *str;
str = strdup(s);
if (!str)
return log_oom();
*ret = str;
} else
printf("%s\n", s);
return 0;
}
@ -211,7 +226,7 @@ static int show_status(int argc, char **argv, void *userdata) {
attr = arg_pretty ? "PrettyHostname" :
arg_static ? "StaticHostname" : "Hostname";
return show_one_name(bus, attr);
return get_one_name(bus, attr, NULL);
} else {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
@ -260,6 +275,17 @@ static int set_hostname(int argc, char **argv, void *userdata) {
if (!arg_pretty && !arg_static && !arg_transient)
arg_pretty = arg_static = arg_transient = implicit = true;
if (!implicit && !arg_static && arg_transient) {
_cleanup_free_ char *source = NULL;
r = get_one_name(bus, "HostnameSource", &source);
if (r < 0)
return r;
if (hostname_source_from_string(source) == HOSTNAME_STATIC)
log_info("Hint: static hostname is already set, so the specified transient hostname will not be used.");
}
if (arg_pretty) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *p;

View File

@ -233,4 +233,4 @@ static const char* const hostname_source_table[] = {
[HOSTNAME_FALLBACK] = "fallback",
};
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(hostname_source, HostnameSource);
DEFINE_STRING_TABLE_LOOKUP(hostname_source, HostnameSource);

View File

@ -11,7 +11,9 @@ typedef enum HostnameSource {
_HOSTNAME_INVALID = -1,
} HostnameSource;
const char* hostname_source_to_string(HostnameSource source);
const char* hostname_source_to_string(HostnameSource source) _const_;
HostnameSource hostname_source_from_string(const char *str) _pure_;
int sethostname_idempotent(const char *s);
int shorten_overlong(const char *s, char **ret);