summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-01-25 13:44:00 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-02-09 13:49:27 +0900
commitf2d3ec7abf0743a6225ea04bb45bfe467ff20d99 (patch)
treee1ee19fad68524c9dd688b2b82d714b904660288
parente8acf0918654082bf24de9e73bf11577d2899c36 (diff)
downloadsystemd-f2d3ec7abf0743a6225ea04bb45bfe467ff20d99.tar.gz
hostnamectl: show hint when user try to set transient hostname but static hostname is already used
-rw-r--r--src/hostname/hostnamectl.c32
-rw-r--r--src/shared/hostname-setup.c2
-rw-r--r--src/shared/hostname-setup.h4
3 files changed, 33 insertions, 5 deletions
diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c
index b97411c42b..50c96d9594 100644
--- a/src/hostname/hostnamectl.c
+++ b/src/hostname/hostnamectl.c
@@ -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;
diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c
index c0465d3dcd..5cdcdbd93c 100644
--- a/src/shared/hostname-setup.c
+++ b/src/shared/hostname-setup.c
@@ -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);
diff --git a/src/shared/hostname-setup.h b/src/shared/hostname-setup.h
index 022f0eb835..1c9d08a5df 100644
--- a/src/shared/hostname-setup.h
+++ b/src/shared/hostname-setup.h
@@ -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);