[PATCH v2 12/15] vpn-provider: Remove unprovisioned providers at startup

Jukka Rissanen jukka.rissanen at linux.intel.com
Tue Nov 27 06:10:22 PST 2012


Check if there are any providers that were provisioned
but their .config file is removed. If such providers are found,
then remove the provider files from file system.
---
 vpn/vpn-provider.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)

diff --git a/vpn/vpn-provider.c b/vpn/vpn-provider.c
index ec882f5..b6fb132 100644
--- a/vpn/vpn-provider.c
+++ b/vpn/vpn-provider.c
@@ -80,6 +80,8 @@ struct vpn_provider {
 	char **nameservers;
 	int what_changed;
 	guint notify_id;
+	char *config_file;
+	char *config_entry;
 };
 
 static void free_route(gpointer data)
@@ -808,6 +810,15 @@ static int vpn_provider_save(struct vpn_provider *provider)
 		}
 	}
 
+	if (provider->config_file != NULL && strlen(provider->config_file) > 0)
+		g_key_file_set_string(keyfile, provider->identifier,
+				"Config.file", provider->config_file);
+
+	if (provider->config_entry != NULL &&
+					strlen(provider->config_entry) > 0)
+		g_key_file_set_string(keyfile, provider->identifier,
+				"Config.ident", provider->config_entry);
+
 	if (provider->driver != NULL && provider->driver->save != NULL)
 		provider->driver->save(provider, keyfile);
 
@@ -921,6 +932,8 @@ static void provider_destruct(struct vpn_provider *provider)
 	__vpn_ipconfig_unref(provider->ipconfig_ipv6);
 
 	g_strfreev(provider->host_ip);
+	g_free(provider->config_file);
+	g_free(provider->config_entry);
 	g_free(provider);
 }
 
@@ -1804,6 +1817,9 @@ int __vpn_provider_create_from_config(GHashTable *settings,
 		provider->name = g_strdup(name);
 		provider->type = g_ascii_strdown(type, -1);
 
+		provider->config_file = g_strdup(config_ident);
+		provider->config_entry = g_strdup(config_entry);
+
 		if (provider_register(provider) == 0)
 			vpn_provider_load(provider);
 
@@ -2264,6 +2280,67 @@ void __vpn_provider_check_connections(void)
 	g_timeout_add(1000, check_vpn_count, NULL);
 }
 
+static void remove_unprovisioned_providers()
+{
+	gchar **providers;
+	GKeyFile *keyfile, *configkeyfile;
+	char *file, *section;
+	int i = 0;
+
+	providers = __connman_storage_get_providers();
+	if (providers == NULL)
+		return;
+
+	for (; providers[i] != NULL; i++) {
+		char *group = providers[i] + sizeof("provider_") - 1;
+		file = section = NULL;
+		keyfile = configkeyfile = NULL;
+
+		keyfile = __connman_storage_load_provider(group);
+		if (keyfile == NULL)
+			continue;
+
+		file = g_key_file_get_string(keyfile, group,
+					"Config.file", NULL);
+		if (file == NULL)
+			goto next;
+
+		section = g_key_file_get_string(keyfile, group,
+					"Config.ident", NULL);
+		if (section == NULL)
+			goto next;
+
+		configkeyfile = __connman_storage_load_provider_config(file);
+		if (configkeyfile == NULL) {
+			/*
+			 * Config file is missing, remove the provisioned
+			 * service.
+			 */
+			__connman_storage_remove_provider(group);
+			goto next;
+		}
+
+		if (g_key_file_has_group(configkeyfile, section) == FALSE)
+			/*
+			 * Config section is missing, remove the provisioned
+			 * service.
+			 */
+			__connman_storage_remove_provider(group);
+
+	next:
+		if (keyfile != NULL)
+			g_key_file_free(keyfile);
+
+		if (configkeyfile != NULL)
+			g_key_file_free(configkeyfile);
+
+		g_free(section);
+		g_free(file);
+	}
+
+	g_strfreev(providers);
+}
+
 int __vpn_provider_init(gboolean do_routes)
 {
 	DBG("");
@@ -2272,9 +2349,10 @@ int __vpn_provider_init(gboolean do_routes)
 
 	connection = connman_dbus_get_connection();
 
+	remove_unprovisioned_providers();
+
 	provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
 						NULL, unregister_provider);
-
 	return 0;
 }
 
-- 
1.7.11.4




More information about the connman mailing list