diff -urN NetworkManager-0.7.1.orig/src/NetworkManagerPolicy.c NetworkManager-0.7.1/src/NetworkManagerPolicy.c --- NetworkManager-0.7.1.orig/src/NetworkManagerPolicy.c 2009-04-13 00:29:59.000000000 +0200 +++ NetworkManager-0.7.1/src/NetworkManagerPolicy.c 2009-06-01 16:31:59.000000000 +0200 @@ -44,24 +44,6 @@ #include "nm-named-manager.h" #include "nm-vpn-manager.h" -typedef struct LookupThread LookupThread; - -typedef void (*LookupCallback) (LookupThread *thread, gpointer user_data); - -struct LookupThread { - GThread *thread; - - GMutex *lock; - gboolean die; - int ret; - - guint32 ip4_addr; - char hostname[NI_MAXHOST + 1]; - - LookupCallback callback; - gpointer user_data; -}; - struct NMPolicy { NMManager *manager; guint update_state_id; @@ -74,96 +56,8 @@ gulong vpn_deactivated_id; NMDevice *default_device; - - LookupThread *lookup; }; -static gboolean -lookup_thread_run_cb (gpointer user_data) -{ - LookupThread *thread = (LookupThread *) user_data; - - (*thread->callback) (thread, thread->user_data); - return FALSE; -} - -static gpointer -lookup_thread_worker (gpointer data) -{ - LookupThread *thread = (LookupThread *) data; - struct sockaddr_in addr; - - g_mutex_lock (thread->lock); - if (thread->die) { - g_mutex_unlock (thread->lock); - return (gpointer) NULL; - } - g_mutex_unlock (thread->lock); - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = thread->ip4_addr; - - thread->ret = getnameinfo ((struct sockaddr *) &addr, sizeof (struct sockaddr_in), - thread->hostname, NI_MAXHOST, NULL, 0, - NI_NAMEREQD); - if (thread->ret == 0) { - int i; - - for (i = 0; i < strlen (thread->hostname); i++) - thread->hostname[i] = tolower (thread->hostname[i]); - } - - /* Don't track the idle handler ID because by the time the g_idle_add() - * returns the ID, the handler may already have run and freed the - * LookupThread. - */ - g_idle_add (lookup_thread_run_cb, thread); - return (gpointer) TRUE; -} - -static void -lookup_thread_free (LookupThread *thread) -{ - g_return_if_fail (thread != NULL); - - g_mutex_free (thread->lock); - memset (thread, 0, sizeof (LookupThread)); - g_free (thread); -} - -static LookupThread * -lookup_thread_new (guint32 ip4_addr, LookupCallback callback, gpointer user_data) -{ - LookupThread *thread; - - thread = g_malloc0 (sizeof (LookupThread)); - if (!thread) - return NULL; - - thread->lock = g_mutex_new (); - thread->callback = callback; - thread->user_data = user_data; - thread->ip4_addr = ip4_addr; - - thread->thread = g_thread_create (lookup_thread_worker, thread, FALSE, NULL); - if (!thread->thread) { - lookup_thread_free (thread); - return NULL; - } - - return thread; -} - -static void -lookup_thread_die (LookupThread *thread) -{ - g_return_if_fail (thread != NULL); - - g_mutex_lock (thread->lock); - thread->die = TRUE; - g_mutex_unlock (thread->lock); -} - #define INVALID_TAG "invalid" static const char * @@ -253,233 +147,6 @@ return best; } -#define FALLBACK_HOSTNAME "localhost.localdomain" - -static gboolean -update_etc_hosts (const char *hostname) -{ - char *contents = NULL; - char **lines = NULL, **line; - GError *error = NULL; - gboolean initial_comments = TRUE; - gboolean added = FALSE; - gsize contents_len = 0; - GString *new_contents; - gboolean success = FALSE; - - g_return_val_if_fail (hostname != NULL, FALSE); - - if (!g_file_get_contents (SYSCONFDIR "/hosts", &contents, &contents_len, &error)) { - nm_warning ("%s: couldn't read " SYSCONFDIR "/hosts: (%d) %s", - __func__, error ? error->code : 0, - (error && error->message) ? error->message : "(unknown)"); - if (error) - g_error_free (error); - } else { - lines = g_strsplit_set (contents, "\n\r", 0); - g_free (contents); - } - - new_contents = g_string_sized_new (contents_len ? contents_len + 100 : 200); - if (!new_contents) { - nm_warning ("%s: not enough memory to update " SYSCONFDIR "/hosts", __func__); - return FALSE; - } - - /* Replace any 127.0.0.1 entry that is at the beginning of the file or right - * after initial comments. If there is no 127.0.0.1 entry at the beginning - * or after initial comments, add one there and ignore any other 127.0.0.1 - * entries. - */ - for (line = lines; lines && *line; line++) { - gboolean add_line = TRUE; - - /* This is the first line after the initial comments */ - if (initial_comments && (*line[0] != '#')) { - initial_comments = FALSE; - g_string_append_printf (new_contents, "127.0.0.1\t%s", hostname); - if (strcmp (hostname, FALLBACK_HOSTNAME)) - g_string_append_printf (new_contents, "\t" FALLBACK_HOSTNAME); - g_string_append (new_contents, "\tlocalhost\n"); - added = TRUE; - - /* Don't add the entry if it's supposed to be the actual localhost reverse mapping */ - if (!strncmp (*line, "127.0.0.1", strlen ("127.0.0.1")) && strstr (*line, "localhost")) - add_line = FALSE; - } - - if (add_line) { - g_string_append (new_contents, *line); - /* Only append the new line if this isn't the last line in the file */ - if (*(line+1)) - g_string_append_c (new_contents, '\n'); - } - } - - /* Hmm, /etc/hosts was empty for some reason */ - if (!added) { - g_string_append (new_contents, "# Do not remove the following line, or various programs"); - g_string_append (new_contents, "# that require network functionality will fail."); - g_string_append (new_contents, "127.0.0.1\t" FALLBACK_HOSTNAME "\tlocalhost"); - } - - error = NULL; - if (!g_file_set_contents (SYSCONFDIR "/hosts", new_contents->str, -1, &error)) { - nm_warning ("%s: couldn't update " SYSCONFDIR "/hosts: (%d) %s", - __func__, error ? error->code : 0, - (error && error->message) ? error->message : "(unknown)"); - if (error) - g_error_free (error); - } else - success = TRUE; - - g_string_free (new_contents, TRUE); - return success; -} - -static void -set_system_hostname (const char *new_hostname, const char *msg) -{ - char old_hostname[HOST_NAME_MAX + 1]; - int ret = 0; - const char *name = new_hostname ? new_hostname : FALLBACK_HOSTNAME; - - old_hostname[HOST_NAME_MAX] = '\0'; - errno = 0; - ret = gethostname (old_hostname, HOST_NAME_MAX); - if (ret != 0) { - nm_warning ("%s: couldn't get the system hostname: (%d) %s", - __func__, errno, strerror (errno)); - } else { - /* Do nothing if the hostname isn't actually changing */ - if ( (new_hostname && !strcmp (old_hostname, new_hostname)) - || (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME))) - return; - } - - nm_info ("Setting system hostname to '%s' (%s)", name, msg); - - ret = sethostname (name, strlen (name)); - if (ret == 0) { - if (!update_etc_hosts (name)) { - /* error updating /etc/hosts; fallback to localhost.localdomain */ - nm_info ("Setting system hostname to '" FALLBACK_HOSTNAME "' (error updating /etc/hosts)"); - ret = sethostname (FALLBACK_HOSTNAME, strlen (FALLBACK_HOSTNAME)); - if (ret != 0) { - nm_warning ("%s: couldn't set the fallback system hostname (%s): (%d) %s", - __func__, FALLBACK_HOSTNAME, errno, strerror (errno)); - } - } - nm_utils_call_dispatcher ("hostname", NULL, NULL, NULL); - } else { - nm_warning ("%s: couldn't set the system hostname to '%s': (%d) %s", - __func__, name, errno, strerror (errno)); - } -} - -static void -lookup_callback (LookupThread *thread, gpointer user_data) -{ - NMPolicy *policy = (NMPolicy *) user_data; - - /* If the thread was told to die or it's not the current in-progress - * hostname lookup, nothing to do. - */ - if (thread->die || (thread != policy->lookup)) - goto done; - - policy->lookup = NULL; - if (!strlen (thread->hostname)) { - char *msg; - - /* No valid IP4 config (!!); fall back to localhost.localdomain */ - msg = g_strdup_printf ("address lookup failed: %d", thread->ret); - set_system_hostname (NULL, msg); - g_free (msg); - } else - set_system_hostname (thread->hostname, "from address lookup"); - -done: - lookup_thread_free (thread); -} - -static void -update_system_hostname (NMPolicy *policy, NMDevice *best) -{ - char *configured_hostname = NULL; - NMActRequest *best_req = NULL; - NMDHCP4Config *dhcp4_config; - NMIP4Config *ip4_config; - NMIP4Address *addr; - - g_return_if_fail (policy != NULL); - - if (policy->lookup) { - lookup_thread_die (policy->lookup); - policy->lookup = NULL; - } - - /* A configured hostname (via the system-settings service) overrides - * all automatic hostname determination. If there is no configured hostname, - * the best device's automatically determined hostname (from DHCP, VPN, PPP, - * etc) is used. If there is no automatically determined hostname, reverse - * DNS lookup using the best device's IP address is started to determined the - * the hostname. - */ - - /* Try a configured hostname first */ - g_object_get (G_OBJECT (policy->manager), NM_MANAGER_HOSTNAME, &configured_hostname, NULL); - if (configured_hostname) { - set_system_hostname (configured_hostname, "from system configuration"); - g_free (configured_hostname); - return; - } - - /* Try automatically determined hostname from the best device's IP config */ - if (!best) - best = get_best_device (policy->manager, &best_req); - - if (!best) { - /* No best device; fall back to localhost.localdomain */ - set_system_hostname (NULL, "no default device"); - return; - } - - /* Grab a hostname out of the device's DHCP4 config */ - dhcp4_config = nm_device_get_dhcp4_config (best); - if (dhcp4_config) { - const char *dhcp4_hostname; - - dhcp4_hostname = nm_dhcp4_config_get_option (dhcp4_config, "host_name"); - if (dhcp4_hostname && strlen (dhcp4_hostname)) { - set_system_hostname (dhcp4_hostname, "from DHCP"); - return; - } - } - - /* No configured hostname, no automatically determined hostname either. Start - * reverse DNS of the current IP address to try and find it. - */ - ip4_config = nm_device_get_ip4_config (best); - if ( !ip4_config - || (nm_ip4_config_get_num_nameservers (ip4_config) == 0) - || (nm_ip4_config_get_num_addresses (ip4_config) == 0)) { - /* No valid IP4 config (!!); fall back to localhost.localdomain */ - set_system_hostname (NULL, "no IPv4 config"); - return; - } - - addr = nm_ip4_config_get_address (ip4_config, 0); - g_assert (addr); /* checked for > 1 address above */ - - /* Start the hostname lookup thread */ - policy->lookup = lookup_thread_new (nm_ip4_address_get_address (addr), lookup_callback, policy); - if (!policy->lookup) { - /* Fall back to 'localhost.localdomain' */ - set_system_hostname (NULL, "error starting hostname thread"); - } -} - static void update_routing_and_dns (NMPolicy *policy, gboolean force_update) { @@ -597,9 +264,6 @@ nm_info ("Policy set (%s) as default for routing and DNS.", ip_iface); out: - /* Update the system hostname */ - update_system_hostname (policy, best); - policy->default_device = best; } @@ -707,12 +371,6 @@ } static void -hostname_changed (NMManager *manager, GParamSpec *pspec, gpointer user_data) -{ - update_system_hostname ((NMPolicy *) user_data, NULL); -} - -static void schedule_activate_check (NMPolicy *policy, NMDevice *device) { ActivateData *data; @@ -991,10 +649,6 @@ G_CALLBACK (global_state_changed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); - id = g_signal_connect (manager, "notify::hostname", - G_CALLBACK (hostname_changed), policy); - policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); - id = g_signal_connect (manager, "device-added", G_CALLBACK (device_added), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); @@ -1033,14 +687,6 @@ g_return_if_fail (policy != NULL); - /* Tell any existing hostname lookup thread to die, it'll get cleaned up - * by the lookup thread callback. - */ - if (policy->lookup) { - lookup_thread_die (policy->lookup); - policy->lookup = NULL; - } - for (iter = policy->pending_activation_checks; iter; iter = g_slist_next (iter)) { ActivateData *data = (ActivateData *) iter->data;