Beispiel #1
0
static int context_update_kernel_hostname(Context *c) {
    const char *static_hn;
    const char *hn;

    assert(c);

    static_hn = c->data[PROP_STATIC_HOSTNAME];

    /* /etc/hostname with something other than "localhost"
     * has the highest preference ... */
    if (hostname_is_useful(static_hn))
        hn = static_hn;

    /* ... the transient host name, (ie: DHCP) comes next ... */
    else if (!isempty(c->data[PROP_HOSTNAME]))
        hn = c->data[PROP_HOSTNAME];

    /* ... fallback to static "localhost.*" ignored above ... */
    else if (!isempty(static_hn))
        hn = static_hn;

    /* ... and the ultimate fallback */
    else
        hn = "localhost";

    if (sethostname_idempotent(hn) < 0)
        return -errno;

    return 0;
}
Beispiel #2
0
int hostname_setup(void) {
        int r;
        _cleanup_free_ char *b = NULL;
        const char *hn;
        bool enoent = false;

        r = read_hostname_config("/etc/hostname", &b);
        if (r < 0) {
                if (r == -ENOENT)
                        enoent = true;
                else
                        log_warning_errno(r, "Failed to read configured hostname: %m");

                hn = NULL;
        } else
                hn = b;

        if (isempty(hn)) {
                /* Don't override the hostname if it is already set
                 * and not explicitly configured */
                if (hostname_is_set())
                        return 0;

                if (enoent)
                        log_info("No hostname configured.");

                hn = "localhost";
        }

        r = sethostname_idempotent(hn);
        if (r < 0)
                return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);

        log_info("Set hostname to <%s>.", hn);
        return 0;
}