summaryrefslogtreecommitdiff
path: root/src/hostname
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-04-28 15:28:53 +0200
committerLennart Poettering <lennart@poettering.net>2021-04-29 16:39:09 +0200
commitf2a434a5b87fb4073357ec5e659c14e02ab17bc3 (patch)
tree6f78c505424dc445052e78de4ed976d611b8f0bd /src/hostname
parent61d44b6bebc13b49922e0e1ec4027c48778536ed (diff)
downloadsystemd-f2a434a5b87fb4073357ec5e659c14e02ab17bc3.tar.gz
hostnamed: split out how we determine the hostname a bit
No changes in code, just some splitting out of code we want to use elsewhere soon.
Diffstat (limited to 'src/hostname')
-rw-r--r--src/hostname/hostnamed.c59
1 files changed, 32 insertions, 27 deletions
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index ca01962682..d7e3e4e692 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -574,6 +574,37 @@ static int property_get_default_hostname(
return sd_bus_message_append(reply, "s", hn);
}
+static void context_determine_hostname_source(Context *c) {
+ char hostname[HOST_NAME_MAX + 1] = {};
+ _cleanup_free_ char *fallback = NULL;
+ int r;
+
+ assert(c);
+
+ if (c->hostname_source >= 0)
+ return;
+
+ (void) get_hostname_filtered(hostname);
+
+ if (streq_ptr(hostname, c->data[PROP_STATIC_HOSTNAME]))
+ c->hostname_source = HOSTNAME_STATIC;
+ else {
+ /* If the hostname was not set by us, try to figure out where it came from. If we set it to
+ * the default hostname, the file will tell us. We compare the string because it is possible
+ * that the hostname was set by an older version that had a different fallback, in the
+ * initramfs or before we reexecuted. */
+
+ r = read_one_line_file("/run/systemd/default-hostname", &fallback);
+ if (r < 0 && r != -ENOENT)
+ log_warning_errno(r, "Failed to read /run/systemd/default-hostname, ignoring: %m");
+
+ if (streq_ptr(fallback, hostname))
+ c->hostname_source = HOSTNAME_DEFAULT;
+ else
+ c->hostname_source = HOSTNAME_TRANSIENT;
+ }
+}
+
static int property_get_hostname_source(
sd_bus *bus,
const char *path,
@@ -584,36 +615,10 @@ static int property_get_hostname_source(
sd_bus_error *error) {
Context *c = userdata;
- int r;
assert(c);
context_read_etc_hostname(c);
-
- if (c->hostname_source < 0) {
- char hostname[HOST_NAME_MAX + 1] = {};
- _cleanup_free_ char *fallback = NULL;
-
- (void) get_hostname_filtered(hostname);
-
- if (streq_ptr(hostname, c->data[PROP_STATIC_HOSTNAME]))
- c->hostname_source = HOSTNAME_STATIC;
-
- else {
- /* If the hostname was not set by us, try to figure out where it came from. If we set
- * it to the default hostname, the file will tell us. We compare the string because
- * it is possible that the hostname was set by an older version that had a different
- * fallback, in the initramfs or before we reexecuted. */
-
- r = read_one_line_file("/run/systemd/default-hostname", &fallback);
- if (r < 0 && r != -ENOENT)
- log_warning_errno(r, "Failed to read /run/systemd/default-hostname, ignoring: %m");
-
- if (streq_ptr(fallback, hostname))
- c->hostname_source = HOSTNAME_DEFAULT;
- else
- c->hostname_source = HOSTNAME_TRANSIENT;
- }
- }
+ context_determine_hostname_source(c);
return sd_bus_message_append(reply, "s", hostname_source_to_string(c->hostname_source));
}