mirror of
https://github.com/clearlinux/systemd-stable.git
synced 2026-09-05 13:11:32 +00:00
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.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user