[systemd-devel] [PATCH] networkd: send hostname to dhcp server

Lennart Poettering lennart at poettering.net
Tue Jul 1 03:24:18 PDT 2014


On Mon, 30.06.14 16:52, Eugene Yakubovich (eugene.yakubovich at coreos.com) wrote:

>          uint32_t xid;
>          usec_t start_time;
>          uint16_t secs;
> @@ -178,6 +179,20 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
>          return 0;
>  }
>  
> +int sd_dhcp_client_set_hostname(sd_dhcp_client *client,
> +                        const char *hostname) {
> +
> +        assert_return(client, -EINVAL);
> +        assert_return(hostname, -EINVAL);
> +
> +        if (client->hostname && streq(client->hostname, hostname) )
> +                return 0;

streq_ptr()...

spurious space between ) and ).

> +
> +        client->hostname = strdup(hostname);

Missing OOM check.

> +/* make sure the hostname is not "localhost" */
> +static bool is_localhost(const char *hostname) {
> +        char *hostend;
> +
> +        assert(hostname);
> +
> +        hostend = strchr(hostname, '.');
> +        if (hostend)
> +                return strneq(hostname, "localhost", (hostend - hostname));
> +        else
> +                return streq(hostname, "localhost");
> +}

We have a similar check in pam_systemd, and in hostnamed. I'd prefer if
we could unify that and stick it in src/shared/util.[ch]. I am not
convinced though that check for all names with the "localhost."
prefix. That appears too broad to me. I'd really just check for
"localhost" and "localhost.localdomain", where the former is what
everybody uses, and the latter a redhatism...

(also, you could simplify your code above with startswith(hostname, "localhost.")...)

> +
> +                r = gethostname(hostname, sizeof(hostname));
> +                if (r < 0)
> +                        return r;

getting the local hostname is not as easy as it might appear. Please use
gethostname_malloc() here.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list