From 332df7a58e86ce08cfd9331897d8b928ae6d267e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 4 Mar 2020 15:44:53 +0100 Subject: [PATCH] core: periodically cleanup unused device state files from /run Otherwise, we only prune unused files when the service terminates. Usually, NetworkManager service doesn't get restarted before shutdown of the system (nor should it be). That means, if you create (and destroy) a large number of software devices, the state files pile up. From time to time, go through the files on disk and delete those that are no longer relevant. In this case, "from time to time" means after we write/update state files 100 times. --- src/nm-manager.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/nm-manager.c b/src/nm-manager.c index 99e658f26b..ee1feb941b 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -50,6 +50,8 @@ #include "nm-dispatcher.h" #include "NetworkManagerUtils.h" +#define DEVICE_STATE_PRUNE_RATELIMIT_MAX 100u + /*****************************************************************************/ typedef struct { @@ -191,6 +193,8 @@ typedef struct { NMConnectivityState connectivity_state; + guint8 device_state_prune_ratelimit_count; + bool startup:1; bool devices_inited:1; @@ -1515,9 +1519,23 @@ manager_device_state_changed (NMDevice *device, if (NM_IN_SET (new_state, NM_DEVICE_STATE_UNMANAGED, NM_DEVICE_STATE_DISCONNECTED, - NM_DEVICE_STATE_ACTIVATED)) + NM_DEVICE_STATE_ACTIVATED)) { nm_manager_write_device_state (self, device, NULL); + G_STATIC_ASSERT_EXPR (DEVICE_STATE_PRUNE_RATELIMIT_MAX < G_MAXUINT8); + if (priv->device_state_prune_ratelimit_count++ > DEVICE_STATE_PRUNE_RATELIMIT_MAX) { + /* We write the device state to /run. The state files are named after the + * ifindex (which is assumed to be unique and not repeat -- in practice + * it may repeat). So from time to time, we prune device state files + * for interfaces that no longer exist. + * + * Otherwise, the files might pile up if you create (and destroy) a large + * number of software devices. */ + priv->device_state_prune_ratelimit_count = 0; + nm_config_device_state_prune_stale (NULL, priv->platform); + } + } + if (NM_IN_SET (new_state, NM_DEVICE_STATE_UNAVAILABLE, NM_DEVICE_STATE_DISCONNECTED))