diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.c | 20 | ||||
-rw-r--r-- | src/shared/util.h | 2 |
2 files changed, 22 insertions, 0 deletions
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); |