Skip to contentSkip to conversation

storage: Refactor dirs creation logic to cleanup on failure

PATCH In review Tim Kourt 2 participants 4 messages Apr 9, 2020

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

PATCH 1/3 storage: Refactor dirs creation logic to cleanup on failure

---
 src/storage.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
src/storage.c +9 -1
index a075d31d..00d93933 100644
@@ -200,16 +200,24 @@ bool storage_create_dirs(void)
 	}
 
 	storage_path = l_strdup(state_dirs[0]);
-	storage_hotspot_path = l_strdup_printf("%s/hotspot/", state_dirs[0]);
 	l_strv_free(state_dirs);
 
 	if (create_dirs(storage_path)) {
 		l_error("Failed to create %s", storage_path);
+
+		l_free(storage_path);
+
 		return false;
 	}
 
+	storage_hotspot_path = l_strdup_printf("%s/hotspot/", storage_path);
+
 	if (create_dirs(storage_hotspot_path)) {
 		l_error("Failed to create %s", storage_hotspot_path);
+
+		l_free(storage_path);
+		l_free(storage_hotspot_path);
+
 		return false;
 	}
 
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 2/3 main: Fix failure cleanup sequence

---
 src/main.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
src/main.c +15 -9
index acd72131..b5ccc63b 100644
@@ -476,12 +476,12 @@ int main(int argc, char *argv[])
 	exit_status = EXIT_FAILURE;
 
 	if (!storage_create_dirs())
-		goto fail_dbus;
+		goto failed_dirs;
 
 	genl = l_genl_new();
 	if (!genl) {
 		l_error("Failed to open generic netlink socket");
-		goto fail_genl;
+		goto failed_genl;
 	}
 
 	if (getenv("IWD_GENL_DEBUG"))
@@ -490,7 +490,7 @@ int main(int argc, char *argv[])
 	rtnl = l_netlink_new(NETLINK_ROUTE);
 	if (!rtnl) {
 		l_error("Failed to open route netlink socket");
-		goto fail_rtnl;
+		goto failed_rtnl;
 	}
 
 	if (getenv("IWD_RTNL_DEBUG"))
@@ -499,7 +499,7 @@ int main(int argc, char *argv[])
 	dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
 	if (!dbus) {
 		l_error("Failed to initialize D-Bus");
-		goto fail_dbus;
+		goto failed_dbus;
 	}
 
 	if (enable_dbus_debug)
@@ -510,18 +510,24 @@ int main(int argc, char *argv[])
 	dbus_init(dbus);
 
 	exit_status = l_main_run_with_signal(signal_handler, NULL);
+
 	plugin_exit();
 
 	iwd_modules_exit();
 
 	dbus_exit();
 	l_dbus_destroy(dbus);
-	storage_cleanup_dirs();
-fail_dbus:
-	l_genl_unref(genl);
-fail_rtnl:
+failed_dbus:
+
 	l_netlink_destroy(rtnl);
-fail_genl:
+failed_rtnl:
+
+	l_genl_unref(genl);
+failed_genl:
+
+	storage_cleanup_dirs();
+failed_dirs:
+
 	l_settings_free(iwd_config);
 
 	l_timeout_remove(timeout);
Signature · 1 line
2.13.6
author
#2 lore raw

PATCH 3/3 main: Simplify config file search logic

In reply to Tim Kourt
---
 src/main.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
src/main.c +11 -12
index b5ccc63b..a0384179 100644
@@ -374,7 +374,6 @@ int main(int argc, char *argv[])
 	struct l_dbus *dbus;
 	const char *config_dir;
 	char **config_dirs;
-	int i;
 
 	for (;;) {
 		int opt;
@@ -456,18 +455,18 @@ int main(int argc, char *argv[])
 	iwd_config = l_settings_new();
 
 	config_dirs = l_strsplit(config_dir, ':');
-	for (i = 0; config_dirs[i]; i++) {
-		char *path = l_strdup_printf("%s/%s", config_dirs[i],
-								"main.conf");
-		bool result = l_settings_load_from_file(iwd_config, path);
-		l_free(path);
-
-		if (result) {
-			l_info("Loaded configuration from %s/main.conf",
-							config_dirs[i]);
-			break;
-		}
+
+	for (; *config_dirs; config_dirs++) {
+		L_AUTO_FREE_VAR(char *, path) =
+			l_strdup_printf("%s/%s", *config_dirs, "main.conf");
+
+		if (!l_settings_load_from_file(iwd_config, path))
+			continue;
+
+		l_info("Loaded configuration from %s", path);
+		break;
 	}
+
 	l_strv_free(config_dirs);
 
 	__eapol_set_config(iwd_config);
Signature · 1 line
2.13.6
#3 lore raw
In reply to Tim Kourt

Hi Tim,

Quoted 4 lines · Tim Kourt
---
  src/storage.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)

All applied, thanks.

Regards,
-Denis