Centos 7修改hostname浅析

#define FALLBACK_HOSTNAME4 "localhost.localdomain"

 

static void

_set_hostname (NMPolicy *self,

               const char *new_hostname,

               const char *msg)

{

    NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);

    gs_free char *old_hostname = NULL;

    const char *name;

 

    /* The incoming hostname *can* be NULL, which will get translated to

     * 'localhost.localdomain' or such in the hostname policy code, but we

     * keep cur_hostname = NULL in the case because we need to know that

     * there was no valid hostname to start with.

     */

 

    /* Clear lookup addresses if we have a hostname, so that we don't

     * restart the reverse lookup thread later.

     */

    if (new_hostname)

        g_clear_object (&priv->lookup.addr);

 

    /* Update the DNS only if the hostname is actually

     * going to change.

     */

    if (!nm_streq0 (priv->cur_hostname, new_hostname)) {

        g_free (priv->cur_hostname);

        priv->cur_hostname = g_strdup (new_hostname);

 

        /* Notify the DNS manager of the hostname change so that the domain part, if

         * present, can be added to the search list.

         */

        nm_dns_manager_set_hostname (priv->dns_manager, priv->cur_hostname,

                                     all_devices_not_active (self));

    }

 

     /* Finally, set kernel hostname */

    if (!new_hostname)

        name = FALLBACK_HOSTNAME4;

    else if (!new_hostname[0]) {

        g_warn_if_reached ();

        name = FALLBACK_HOSTNAME4;

    } else

        name = new_hostname;

 

    /* Don't set the hostname if it isn't actually changing */

    if (   (old_hostname = _get_hostname (self))

        && (nm_streq (name, old_hostname))) {

        _LOGT (LOGD_DNS, "set-hostname: hostname already set to '%s' (%s)", name, msg);

        return;

    }

 

    /* Keep track of the last set hostname */

    g_free (priv->last_hostname);

    priv->last_hostname = g_strdup (name);

    priv->changing_hostname = TRUE;

 

    _LOGI (LOGD_DNS, "set-hostname: set hostname to '%s' (%s)", name, msg);

 

    /* Ask NMSettings to update the transient hostname using its

     * systemd-hostnamed proxy */

    nm_hostname_manager_set_transient_hostname (priv->hostname_manager,

                                                name,

                                                settings_set_hostname_cb,

                                                g_object_ref (self));

}