summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-03-04 13:21:48 +0100
committerThomas Haller <thaller@redhat.com>2020-03-04 16:48:09 +0100
commitad9e7488167ab25a5915040e813e76a5b669257b (patch)
treee24e779d07f24f70d2e6a281103adde0d4d61a63
parent627b543a379814f8667acd59a3f870a5e14e910f (diff)
downloadNetworkManager-ad9e7488167ab25a5915040e813e76a5b669257b.tar.gz
core: cleanup nm_config_device_state_prune_unseen() and accept NMPlatform for skipping pruning
-rw-r--r--src/nm-config.c36
-rw-r--r--src/nm-config.h3
-rw-r--r--src/nm-manager.c8
3 files changed, 29 insertions, 18 deletions
diff --git a/src/nm-config.c b/src/nm-config.c
index 4b4ffd3122..49b61d5bd7 100644
--- a/src/nm-config.c
+++ b/src/nm-config.c
@@ -2299,6 +2299,8 @@ _config_device_state_data_new (int ifindex, GKeyFile *kf)
return device_state;
}
+#define DEVICE_STATE_FILENAME_LEN_MAX 60
+
/**
* nm_config_device_state_load:
* @ifindex: the ifindex for which the state is to load
@@ -2310,7 +2312,7 @@ NMConfigDeviceStateData *
nm_config_device_state_load (int ifindex)
{
NMConfigDeviceStateData *device_state;
- char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR) + 60];
+ char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + DEVICE_STATE_FILENAME_LEN_MAX + 1];
gs_unref_keyfile GKeyFile *kf = NULL;
const char *nm_owned_str;
@@ -2394,7 +2396,7 @@ nm_config_device_state_write (int ifindex,
const char *next_server,
const char *root_path)
{
- char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR) + 60];
+ char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + DEVICE_STATE_FILENAME_LEN_MAX + 1];
GError *local = NULL;
gs_unref_keyfile GKeyFile *kf = NULL;
@@ -2477,35 +2479,43 @@ nm_config_device_state_write (int ifindex,
}
void
-nm_config_device_state_prune_unseen (GHashTable *seen_ifindexes)
+nm_config_device_state_prune_unseen (GHashTable *preserve_ifindexes,
+ NMPlatform *preserve_in_platform)
{
GDir *dir;
const char *fn;
- int ifindex;
- gsize fn_len;
- char buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + 30 + 3] = NM_CONFIG_DEVICE_STATE_DIR"/";
+ char buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + DEVICE_STATE_FILENAME_LEN_MAX + 1] = NM_CONFIG_DEVICE_STATE_DIR"/";
char *buf_p = &buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/")];
- g_return_if_fail (seen_ifindexes);
-
dir = g_dir_open (NM_CONFIG_DEVICE_STATE_DIR, 0, NULL);
if (!dir)
return;
while ((fn = g_dir_read_name (dir))) {
+ int ifindex;
+ gsize fn_len;
+
ifindex = _device_state_parse_filename (fn);
if (ifindex <= 0)
continue;
- if (g_hash_table_contains (seen_ifindexes, GINT_TO_POINTER (ifindex)))
+
+ if ( preserve_ifindexes
+ && g_hash_table_contains (preserve_ifindexes, GINT_TO_POINTER (ifindex)))
+ continue;
+
+ if ( preserve_in_platform
+ && nm_platform_link_get (preserve_in_platform, ifindex))
continue;
- fn_len = strlen (fn) + 1;
+ fn_len = strlen (fn);
+ nm_assert (fn_len > 0);
nm_assert (&buf_p[fn_len] < &buf[G_N_ELEMENTS (buf)]);
- memcpy (buf_p, fn, fn_len);
+ memcpy (buf_p, fn, fn_len + 1u);
nm_assert (({
char bb[30];
- nm_sprintf_buf (bb, "%d", ifindex);
- nm_streq0 (bb, buf_p);
+
+ nm_streq0 (nm_sprintf_buf (bb, "%d", ifindex),
+ buf_p);
}));
_LOGT ("device-state: prune #%d (%s)", ifindex, buf);
(void) unlink (buf);
diff --git a/src/nm-config.h b/src/nm-config.h
index d9460ebb46..048d64f41f 100644
--- a/src/nm-config.h
+++ b/src/nm-config.h
@@ -258,7 +258,8 @@ gboolean nm_config_device_state_write (int ifindex,
const char *next_server,
const char *root_path);
-void nm_config_device_state_prune_unseen (GHashTable *seen_ifindexes);
+void nm_config_device_state_prune_unseen (GHashTable *preserve_ifindexes,
+ NMPlatform *preserve_in_platform);
const GHashTable *nm_config_device_state_get_all (NMConfig *self);
const NMConfigDeviceStateData *nm_config_device_state_get (NMConfig *self,
diff --git a/src/nm-manager.c b/src/nm-manager.c
index e49c739a10..b9a1b5bbcf 100644
--- a/src/nm-manager.c
+++ b/src/nm-manager.c
@@ -6555,19 +6555,19 @@ void
nm_manager_write_device_state_all (NMManager *self)
{
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
- gs_unref_hashtable GHashTable *seen_ifindexes = NULL;
+ gs_unref_hashtable GHashTable *preserve_ifindexes = NULL;
NMDevice *device;
- seen_ifindexes = g_hash_table_new (nm_direct_hash, NULL);
+ preserve_ifindexes = g_hash_table_new (nm_direct_hash, NULL);
c_list_for_each_entry (device, &priv->devices_lst_head, devices_lst) {
if (nm_manager_write_device_state (self, device)) {
- g_hash_table_add (seen_ifindexes,
+ g_hash_table_add (preserve_ifindexes,
GINT_TO_POINTER (nm_device_get_ip_ifindex (device)));
}
}
- nm_config_device_state_prune_unseen (seen_ifindexes);
+ nm_config_device_state_prune_unseen (preserve_ifindexes, NULL);
}
static gboolean