[systemd-devel] [PATCH 4/4] hostname-setup: try to set hostname only when necessary

Lennart Poettering lennart at poettering.net
Thu Oct 2 02:58:40 PDT 2014


On Thu, 02.10.14 09:57, Michal Sekletar (msekleta at redhat.com) wrote:

> ---
>  src/core/hostname-setup.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/src/core/hostname-setup.c b/src/core/hostname-setup.c
> index 8aa1cff..1df7a56 100644
> --- a/src/core/hostname-setup.c
> +++ b/src/core/hostname-setup.c
> @@ -80,6 +80,15 @@ int hostname_setup(void) {
>                          log_info("No hostname configured.");
>  
>                  hn = "localhost";
> +        } else {
> +                char old_hn[HOST_NAME_MAX + 1] = {};
> +
> +                r = gethostname(old_hn, sizeof(old_hn));
> +                if (!r) {
> +                        if (streq(hn, old_hn))
> +                                return 0;
> +                } else
> +                        log_warning("Failed to get hostname: %m");
>          }
>  
>          if (sethostname(hn, strlen(hn)) < 0) {

Hmm, not too much of a fan of this one, but I figure it's OK. However,
I'd really prefer if we'd move this into util.c into a nice utility
function. Maybe like this:

          int sethostname_idempotent(const char *s);

And then convert all users of sethostname() we have right now to
invoke this new call. The call could then also do the strlen()
internally, hence be nicer even beyond your specific usecase.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list