forked from pool/systemd
86 lines
2.6 KiB
Diff
86 lines
2.6 KiB
Diff
Based on 605f81a8968b2df8a28cca2cf11db99ab948a2af Mon Sep 17 00:00:00 2001
|
|
From: Michal Sekletar <msekleta@redhat.com>
|
|
Date: Tue, 21 Oct 2014 18:17:54 +0200
|
|
Subject: [PATCH] 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.
|
|
---
|
|
src/core/hostname-setup.c | 2 +-
|
|
src/hostname/hostnamed.c | 2 +-
|
|
src/nspawn/nspawn.c | 2 +-
|
|
src/shared/util.c | 20 ++++++++++++++++++++
|
|
src/shared/util.h | 2 ++
|
|
5 files changed, 25 insertions(+), 3 deletions(-)
|
|
|
|
--- src/core/hostname-setup.c
|
|
+++ src/core/hostname-setup.c 2014-10-29 00:00:00.000000000 +0000
|
|
@@ -99,7 +99,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;
|
|
}
|
|
--- src/hostname/hostnamed.c
|
|
+++ src/hostname/hostnamed.c 2014-10-29 14:13:26.124337751 +0000
|
|
@@ -244,7 +244,7 @@ static int context_write_data_hostname(C
|
|
else
|
|
hn = c->data[PROP_HOSTNAME];
|
|
|
|
- if (sethostname(hn, strlen(hn)) < 0)
|
|
+ if (sethostname_idempotent(hn) < 0)
|
|
return -errno;
|
|
|
|
return 0;
|
|
--- src/nspawn/nspawn.c
|
|
+++ src/nspawn/nspawn.c 2014-10-29 00:00:00.000000000 +0000
|
|
@@ -981,7 +981,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;
|
|
--- src/shared/util.c
|
|
+++ src/shared/util.c 2014-10-29 00:00:00.000000000 +0000
|
|
@@ -6451,6 +6451,26 @@ int fd_warn_permissions(const char *path
|
|
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;
|
|
+}
|
|
+
|
|
unsigned long personality_from_string(const char *p) {
|
|
|
|
/* Parse a personality specifier. We introduce our own
|
|
--- src/shared/util.h
|
|
+++ src/shared/util.h 2014-10-29 14:14:15.764337717 +0000
|
|
@@ -899,3 +899,5 @@ union file_handle_union {
|
|
};
|
|
|
|
int umount_recursive(const char *target, int flags);
|
|
+
|
|
+int sethostname_idempotent(const char *s);
|