]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostnamectl: show hint when user try to set transient hostname but static hostname...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 25 Jan 2021 04:44:00 +0000 (13:44 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Feb 2021 04:49:27 +0000 (13:49 +0900)
src/hostname/hostnamectl.c
src/shared/hostname-setup.c
src/shared/hostname-setup.h

index b97411c42bf953ee5b28957c4fc475d32b76d33c..50c96d95948d39a6ff1c05d75f00db3d1a3808ac 100644 (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;
index c0465d3dcd181ace7ebb9775d1c5f3747b482e93..5cdcdbd93c870a734f4507ebfbe7daf924202327 100644 (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);
index 022f0eb835db54bcd773f66bcda669f4684359fe..1c9d08a5dfaf65770d32b0c49ee51a5e2af1a7be 100644 (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);