summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Sekletar <msekleta@redhat.com>2014-10-21 18:17:54 +0200
committerMichal Sekletar <msekleta@redhat.com>2014-10-27 10:37:46 +0100
commit605f81a8968b2df8a28cca2cf11db99ab948a2af (patch)
tree5f7bc76ca61e8165860510ee181c57ecbfdd3055
parentcaa2f4c0c9613b2e02aafa308c8fb092576014a9 (diff)
util: introduce sethostname_idempotent
Function queries system hostname and applies changes only when necessary. Also, migrate all client of sethostname to sethostname_idempotent while at it.
-rw-r--r--src/core/hostname-setup.c2
-rw-r--r--src/hostname/hostnamed.c2
-rw-r--r--src/nspawn/nspawn.c2
-rw-r--r--src/shared/util.c20
-rw-r--r--src/shared/util.h2
5 files changed, 25 insertions, 3 deletions
diff --git a/src/core/hostname-setup.c b/src/core/hostname-setup.c
index 8aa1cff1d3..57baa79275 100644
--- a/src/core/hostname-setup.c
+++ b/src/core/hostname-setup.c
@@ -82,7 +82,7 @@ int hostname_setup(void) {
hn = "localhost";
}
- if (sethostname(hn, strlen(hn)) < 0) {
+ if (sethostname_idempotent(hn) < 0) {
log_warning("Failed to set hostname to <%s>: %m", hn);
return -errno;
}
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index 0cffb5f683..a449610bb8 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -287,7 +287,7 @@ static int context_update_kernel_hostname(Context *c) {
else
hn = "localhost";
- if (sethostname(hn, strlen(hn)) < 0)
+ if (sethostname_idempotent(hn) < 0)
return -errno;
return 0;
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index c567c8d272..b6d9bc631c 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -1289,7 +1289,7 @@ static int setup_hostname(void) {
if (arg_share_system)
return 0;
- if (sethostname(arg_machine, strlen(arg_machine)) < 0)
+ if (sethostname_idempotent(arg_machine) < 0)
return -errno;
return 0;
diff --git a/src/shared/util.c b/src/shared/util.c
index bc97c67f76..7d94a28302 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -7175,3 +7175,23 @@ int free_and_strdup(char **p, const char *s) {
return 0;
}
+
+int sethostname_idempotent(const char *s) {
+ int r;
+ char buf[HOST_NAME_MAX + 1] = {};
+
+ assert(s);
+
+ r = gethostname(buf, sizeof(buf));
+ if (r < 0)
+ return -errno;
+
+ if (streq(buf, s))
+ return 0;
+
+ r = sethostname(buf, strlen(buf));
+ if (r < 0)
+ return -errno;
+
+ return 1;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 887cdc4a44..35584467c1 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -997,3 +997,5 @@ int unquote_first_word(const char **p, char **ret);
int unquote_many_words(const char **p, ...) _sentinel_;
int free_and_strdup(char **p, const char *s);
+
+int sethostname_idempotent(const char *s);