diff mbox series

[v3,1/1] Create and destroy scripts/datadst temp folders on install.

Message ID 20210827045104.65353-1-james.hilliard1@gmail.com
State Changes Requested
Headers show
Series [v3,1/1] Create and destroy scripts/datadst temp folders on install. | expand

Commit Message

James Hilliard Aug. 27, 2021, 4:51 a.m. UTC
Under some circumstances these folders may get removed after
startup, for example if /tmp has a mountpoint change after
swupdate is started.

In order to make swupdate better able to handle these sort of
race conditions we can create/destroy these folders for each
update installation.

Fixes:
[ERROR] : SWUPDATE failed [0] ERROR : I cannot open /tmp/scripts/format.lua 2
[ERROR] : SWUPDATE failed [0] ERROR : extracting script to /tmp/scripts/ failed
[ERROR] : SWUPDATE failed [1] Installation failed !

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes v2 -> v3:
  - move create_directory/remove_directory functions to util
  - move create/remove directory calls to network_initializer
Changes v1 -> v2:
  - just create/destroy datadst once for each install
---
 core/stream_interface.c |  9 +++++++
 core/swupdate.c         | 57 -----------------------------------------
 core/util.c             | 40 +++++++++++++++++++++++++++++
 include/util.h          |  5 ++++
 4 files changed, 54 insertions(+), 57 deletions(-)
diff mbox series

Patch

diff --git a/core/stream_interface.c b/core/stream_interface.c
index da0c733..89525a5 100644
--- a/core/stream_interface.c
+++ b/core/stream_interface.c
@@ -525,6 +525,10 @@  void *network_initializer(void *data)
 		notify(START, RECOVERY_NO_ERROR, INFOLEVEL, "Software Update started !");
 		TRACE("Software update started");
 
+		/* Create directories for scripts/datadst */
+		swupdate_create_directory(SCRIPTS_DIR_SUFFIX);
+		swupdate_create_directory(DATADST_DIR_SUFFIX);
+
 		req = &inst.req;
 
 		/*
@@ -657,6 +661,11 @@  void *network_initializer(void *data)
 
 		/* release temp files we may have created */
 		cleanup_files(software);
+
+#ifndef CONFIG_NOCLEANUP
+		swupdate_remove_directory(SCRIPTS_DIR_SUFFIX);
+		swupdate_remove_directory(DATADST_DIR_SUFFIX);
+#endif
 	}
 
 	pthread_exit((void *)0);
diff --git a/core/swupdate.c b/core/swupdate.c
index 949a647..a31aeb1 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -23,7 +23,6 @@ 
 #include <pthread.h>
 #include <signal.h>
 #include <sys/wait.h>
-#include <ftw.h>
 
 #include "bsdqueue.h"
 #include "cpiohdr.h"
@@ -244,53 +243,6 @@  static int parse_image_selector(const char *selector, struct swupdate_cfg *sw)
 	return 0;
 }
 
-static void create_directory(const char* path) {
-	char* dpath;
-	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
-		ENOMEM_ASPRINTF) {
-		ERROR("OOM: Directory %s not created", path);
-		return;
-	}
-	if (mkdir(dpath, 0777)) {
-		WARN("Directory %s cannot be created due to : %s",
-		     path, strerror(errno));
-	}
-	free(dpath);
-}
-
-#ifndef CONFIG_NOCLEANUP
-static int _remove_directory_cb(const char *fpath, const struct stat *sb,
-                                int typeflag, struct FTW *ftwbuf)
-{
-	(void)sb;
-	(void)typeflag;
-	(void)ftwbuf;
-	return remove(fpath);
-}
-
-static int remove_directory(const char* path)
-{
-	char* dpath;
-	int ret;
-	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
-		ENOMEM_ASPRINTF) {
-		ERROR("OOM: Directory %s not removed", path);
-		return -ENOMEM;
-	}
-	ret = nftw(dpath, _remove_directory_cb, 64, FTW_DEPTH | FTW_PHYS);
-	free(dpath);
-	return ret;
-}
-#endif
-
-static void swupdate_cleanup(void)
-{
-#ifndef CONFIG_NOCLEANUP
-	remove_directory(SCRIPTS_DIR_SUFFIX);
-	remove_directory(DATADST_DIR_SUFFIX);
-#endif
-}
-
 static void swupdate_init(struct swupdate_cfg *sw)
 {
 	/* Initialize internal tree to store configuration */
@@ -303,15 +255,6 @@  static void swupdate_init(struct swupdate_cfg *sw)
 	LIST_INIT(&sw->extprocs);
 	sw->cert_purpose = SSL_PURPOSE_DEFAULT;
 
-
-	/* Create directories for scripts */
-	create_directory(SCRIPTS_DIR_SUFFIX);
-	create_directory(DATADST_DIR_SUFFIX);
-
-	if (atexit(swupdate_cleanup) != 0) {
-		TRACE("Cannot setup SWUpdate cleanup on exit");
-	}
-
 #ifdef CONFIG_MTD
 	mtd_init();
 	ubi_init();
diff --git a/core/util.c b/core/util.c
index 6188650..7e96652 100644
--- a/core/util.c
+++ b/core/util.c
@@ -12,6 +12,7 @@ 
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <ftw.h>
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/param.h>
@@ -146,6 +147,45 @@  const char* get_tmpdirscripts(void)
 	return TMPDIRSCRIPT;
 }
 
+void swupdate_create_directory(const char* path) {
+	char* dpath;
+	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
+		ENOMEM_ASPRINTF) {
+		ERROR("OOM: Directory %s not created", path);
+		return;
+	}
+	if (mkdir(dpath, 0777)) {
+		WARN("Directory %s cannot be created due to : %s",
+			 path, strerror(errno));
+	}
+	free(dpath);
+}
+
+#ifndef CONFIG_NOCLEANUP
+static int _remove_directory_cb(const char *fpath, const struct stat *sb,
+								int typeflag, struct FTW *ftwbuf)
+{
+	(void)sb;
+	(void)typeflag;
+	(void)ftwbuf;
+	return remove(fpath);
+}
+
+int swupdate_remove_directory(const char* path)
+{
+	char* dpath;
+	int ret;
+	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
+		ENOMEM_ASPRINTF) {
+		ERROR("OOM: Directory %s not removed", path);
+		return -ENOMEM;
+	}
+	ret = nftw(dpath, _remove_directory_cb, 64, FTW_DEPTH | FTW_PHYS);
+	free(dpath);
+	return ret;
+}
+#endif
+
 char **splitargs(char *args, int *argc)
 {
 	char **argv = NULL;
diff --git a/include/util.h b/include/util.h
index 9f29f5f..a817f1c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -245,6 +245,11 @@  unsigned long long ustrtoull(const char *cp, unsigned int base);
 const char* get_tmpdir(void);
 const char* get_tmpdirscripts(void);
 
+void swupdate_create_directory(const char* path);
+#ifndef CONFIG_NOCLEANUP
+int swupdate_remove_directory(const char* path);
+#endif
+
 int swupdate_mount(const char *device, const char *dir, const char *fstype);
 int swupdate_umount(const char *dir);