#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"
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",
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;
}
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;
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;
_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);