From bdebb8e2cf82498c589f38bf1d6878ff43451846 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Fri, 6 Jul 2018 20:26:44 +0200 Subject: [PATCH 1/4] main: drop unused define --- src/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.c b/src/main.c index 7da0c3c2a..c74ff1b5a 100644 --- a/src/main.c +++ b/src/main.c @@ -59,7 +59,6 @@ #endif #define NM_DEFAULT_PID_FILE NMRUNDIR "/NetworkManager.pid" -#define NM_DEFAULT_SYSTEM_STATE_FILE NMSTATEDIR "/NetworkManager.state" #define CONFIG_ATOMIC_SECTION_PREFIXES ((char **) NULL) From 060f2138ee988255f160371d2a4bc2a45f942ced Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Fri, 6 Jul 2018 21:09:58 +0200 Subject: [PATCH 2/4] manager: rename nm_manager_write_device_state() Rename nm_manager_write_device_state() to nm_manager_write_device_state_all(), and split out the code to write a single device state to a new function. --- src/main.c | 2 +- src/nm-manager.c | 113 +++++++++++++++++++++++++---------------------- src/nm-manager.h | 3 +- 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/main.c b/src/main.c index c74ff1b5a..a5bfb6af1 100644 --- a/src/main.c +++ b/src/main.c @@ -443,7 +443,7 @@ done: * state here. We don't bother updating the state as devices * change during regular operation. If NM is killed with SIGKILL, * it misses to update the state. */ - nm_manager_write_device_state (manager); + nm_manager_write_device_state_all (manager); nm_manager_stop (manager); diff --git a/src/nm-manager.c b/src/nm-manager.c index 630180963..b4c2b08fe 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -5934,66 +5934,75 @@ start_factory (NMDeviceFactory *factory, gpointer user_data) nm_device_factory_start (factory); } -void -nm_manager_write_device_state (NMManager *self) +gboolean +nm_manager_write_device_state (NMManager *self, NMDevice *device) { NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); - NMDevice *device; - gs_unref_hashtable GHashTable *seen_ifindexes = NULL; + int ifindex; + gboolean managed; + NMConfigDeviceStateManagedType managed_type; + NMConnection *settings_connection; + const char *uuid = NULL; + const char *perm_hw_addr_fake = NULL; + gboolean perm_hw_addr_is_fake; + guint32 route_metric_default_aspired; + guint32 route_metric_default_effective; int nm_owned; + ifindex = nm_device_get_ip_ifindex (device); + if (ifindex <= 0) + return FALSE; + if (ifindex == 1) { + /* ignore loopback */ + return FALSE; + } + + if (!nm_platform_link_get (priv->platform, ifindex)) + return FALSE; + + managed = nm_device_get_managed (device, FALSE); + if (managed) { + settings_connection = NM_CONNECTION (nm_device_get_settings_connection (device)); + if (settings_connection) + uuid = nm_connection_get_uuid (settings_connection); + managed_type = NM_CONFIG_DEVICE_STATE_MANAGED_TYPE_MANAGED; + } else if (nm_device_get_unmanaged_flags (device, NM_UNMANAGED_USER_EXPLICIT)) + managed_type = NM_CONFIG_DEVICE_STATE_MANAGED_TYPE_UNMANAGED; + else + managed_type = NM_CONFIG_DEVICE_STATE_MANAGED_TYPE_UNKNOWN; + + perm_hw_addr_fake = nm_device_get_permanent_hw_address_full (device, FALSE, &perm_hw_addr_is_fake); + if (perm_hw_addr_fake && !perm_hw_addr_is_fake) + perm_hw_addr_fake = NULL; + + nm_owned = nm_device_is_software (device) ? nm_device_is_nm_owned (device) : -1; + + route_metric_default_effective = _device_route_metric_get (self, ifindex, NM_DEVICE_TYPE_UNKNOWN, + TRUE, &route_metric_default_aspired); + + return nm_config_device_state_write (ifindex, + managed_type, + perm_hw_addr_fake, + uuid, + nm_owned, + route_metric_default_aspired, + route_metric_default_effective); +} + +void +nm_manager_write_device_state_all (NMManager *self) +{ + NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); + gs_unref_hashtable GHashTable *seen_ifindexes = NULL; + NMDevice *device; + seen_ifindexes = g_hash_table_new (nm_direct_hash, NULL); c_list_for_each_entry (device, &priv->devices_lst_head, devices_lst) { - int ifindex; - gboolean managed; - NMConfigDeviceStateManagedType managed_type; - NMConnection *settings_connection; - const char *uuid = NULL; - const char *perm_hw_addr_fake = NULL; - gboolean perm_hw_addr_is_fake; - guint32 route_metric_default_aspired; - guint32 route_metric_default_effective; - - ifindex = nm_device_get_ip_ifindex (device); - if (ifindex <= 0) - continue; - if (ifindex == 1) { - /* ignore loopback */ - continue; + if (nm_manager_write_device_state (self, device)) { + g_hash_table_add (seen_ifindexes, + GINT_TO_POINTER (nm_device_get_ip_ifindex (device))); } - - if (!nm_platform_link_get (priv->platform, ifindex)) - continue; - - managed = nm_device_get_managed (device, FALSE); - if (managed) { - settings_connection = NM_CONNECTION (nm_device_get_settings_connection (device)); - if (settings_connection) - uuid = nm_connection_get_uuid (settings_connection); - managed_type = NM_CONFIG_DEVICE_STATE_MANAGED_TYPE_MANAGED; - } else if (nm_device_get_unmanaged_flags (device, NM_UNMANAGED_USER_EXPLICIT)) - managed_type = NM_CONFIG_DEVICE_STATE_MANAGED_TYPE_UNMANAGED; - else - managed_type = NM_CONFIG_DEVICE_STATE_MANAGED_TYPE_UNKNOWN; - - perm_hw_addr_fake = nm_device_get_permanent_hw_address_full (device, FALSE, &perm_hw_addr_is_fake); - if (perm_hw_addr_fake && !perm_hw_addr_is_fake) - perm_hw_addr_fake = NULL; - - nm_owned = nm_device_is_software (device) ? nm_device_is_nm_owned (device) : -1; - - route_metric_default_effective = _device_route_metric_get (self, ifindex, NM_DEVICE_TYPE_UNKNOWN, - TRUE, &route_metric_default_aspired); - - if (nm_config_device_state_write (ifindex, - managed_type, - perm_hw_addr_fake, - uuid, - nm_owned, - route_metric_default_aspired, - route_metric_default_effective)) - g_hash_table_add (seen_ifindexes, GINT_TO_POINTER (ifindex)); } nm_config_device_state_prune_unseen (seen_ifindexes); diff --git a/src/nm-manager.h b/src/nm-manager.h index 5a4628467..37ddbff8b 100644 --- a/src/nm-manager.h +++ b/src/nm-manager.h @@ -101,7 +101,8 @@ NMSettingsConnection **nm_manager_get_activatable_connections (NMManager *manage guint *out_len, gboolean sort); -void nm_manager_write_device_state (NMManager *manager); +void nm_manager_write_device_state_all (NMManager *manager); +gboolean nm_manager_write_device_state (NMManager *manager, NMDevice *device); /* Device handling */ From 8bbe61206fdc5cbf251da51440b4e041a02d641f Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Fri, 6 Jul 2018 21:35:04 +0200 Subject: [PATCH 3/4] manager: update the device state file upon (dis)connection Update the device state file every time the device is connected, disconnected, or becomes unmanaged. In this way, NM becomes more robust against crashes or forced terminations because it can resume the previous device state seamlessly. --- src/nm-manager.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/nm-manager.c b/src/nm-manager.c index b4c2b08fe..eeaefccb4 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1471,6 +1471,12 @@ manager_device_state_changed (NMDevice *device, break; } + if (NM_IN_SET (new_state, + NM_DEVICE_STATE_UNMANAGED, + NM_DEVICE_STATE_DISCONNECTED, + NM_DEVICE_STATE_ACTIVATED)) + nm_manager_write_device_state (self, device); + if ( new_state == NM_DEVICE_STATE_UNAVAILABLE || new_state == NM_DEVICE_STATE_DISCONNECTED) nm_settings_device_added (priv->settings, device); From 614f4733e20082397d50da89957492d95ea55131 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Fri, 6 Jul 2018 21:44:06 +0200 Subject: [PATCH 4/4] manager: use NM_IN_SET() No functional change. --- src/nm-manager.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/nm-manager.c b/src/nm-manager.c index eeaefccb4..003536a3e 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1459,17 +1459,13 @@ manager_device_state_changed (NMDevice *device, && new_state > NM_DEVICE_STATE_UNMANAGED) retry_connections_for_parent_device (self, device); - switch (new_state) { - case NM_DEVICE_STATE_UNMANAGED: - case NM_DEVICE_STATE_UNAVAILABLE: - case NM_DEVICE_STATE_DISCONNECTED: - case NM_DEVICE_STATE_PREPARE: - case NM_DEVICE_STATE_FAILED: + if (NM_IN_SET (new_state, + NM_DEVICE_STATE_UNMANAGED, + NM_DEVICE_STATE_UNAVAILABLE, + NM_DEVICE_STATE_DISCONNECTED, + NM_DEVICE_STATE_PREPARE, + NM_DEVICE_STATE_FAILED)) _notify (self, PROP_ACTIVE_CONNECTIONS); - break; - default: - break; - } if (NM_IN_SET (new_state, NM_DEVICE_STATE_UNMANAGED, @@ -1477,8 +1473,9 @@ manager_device_state_changed (NMDevice *device, NM_DEVICE_STATE_ACTIVATED)) nm_manager_write_device_state (self, device); - if ( new_state == NM_DEVICE_STATE_UNAVAILABLE - || new_state == NM_DEVICE_STATE_DISCONNECTED) + if (NM_IN_SET (new_state, + NM_DEVICE_STATE_UNAVAILABLE, + NM_DEVICE_STATE_DISCONNECTED)) nm_settings_device_added (priv->settings, device); }