diff mbox series

[1/5] Factorize calls of system()

Message ID 20200309101542.27373-2-sbabic@denx.de
State Accepted
Headers show
Series Extend logs and send log to Hawkbit Server | expand

Commit Message

Stefano Babic March 9, 2020, 10:15 a.m. UTC
There are just two places where a system() is called:
	- during pre- and post- update command
	- in the shell script handler

Part of code is duplicate. Factorize it in the run_system_cmd() already
used by the installed and expose it to be used globally. This is laso in
preparation for a better support of shell commands.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 core/installer.c               | 27 ++++++---------------------
 core/pctl.c                    | 20 ++++++++++++++++++++
 handlers/shell_scripthandler.c | 10 ++--------
 include/pctl.h                 |  1 +
 4 files changed, 29 insertions(+), 29 deletions(-)
diff mbox series

Patch

diff --git a/core/installer.c b/core/installer.c
index cc3c1ea..61dc06d 100644
--- a/core/installer.c
+++ b/core/installer.c
@@ -32,6 +32,7 @@ 
 #include "parsers.h"
 #include "bootloader.h"
 #include "progress.h"
+#include "pctl.h"
 
 /*
  * function returns:
@@ -458,28 +459,11 @@  void cleanup_files(struct swupdate_cfg *software) {
 #endif
 }
 
-static int run_system_cmd(const char *cmd, const char *desc)
-{
-	int ret = 0;
-	if ((strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) > 0)
-		&& (strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) < SWUPDATE_GENERAL_STRING_SIZE)) {
-		DEBUG("Running %s command '%s' ...", desc, cmd);
-		ret = system(cmd);
-		if (WIFEXITED(ret)) {
-			DEBUG("%s command returned %d", desc, WEXITSTATUS(ret));
-		} else {
-			ERROR("%s command returned %d: '%s'", desc, ret, strerror(errno));
-			return -1;
-		}
-	}
-
-	return ret;
-}
-
 int preupdatecmd(struct swupdate_cfg *swcfg)
 {
 	if (swcfg) {
-		return run_system_cmd(swcfg->globals.preupdatecmd, "Pre-update");
+		DEBUG("Running Pre-update command");
+		return run_system_cmd(swcfg->globals.preupdatecmd);
 	}
 
 	return 0;
@@ -489,8 +473,9 @@  int postupdate(struct swupdate_cfg *swcfg, const char *info)
 {
 	swupdate_progress_done(info);
 
-	if ((swcfg) && (run_system_cmd(swcfg->globals.postupdatecmd, "Post-update") == -1)) {
-		return -1;
+	if (swcfg) {
+		DEBUG("Running Post-update command");
+		return run_system_cmd(swcfg->globals.postupdatecmd);
 	}
 
 	return 0;
diff --git a/core/pctl.c b/core/pctl.c
index d8b2c76..134dd10 100644
--- a/core/pctl.c
+++ b/core/pctl.c
@@ -14,6 +14,7 @@ 
 #include <sys/prctl.h>
 #endif
 #include <errno.h>
+#include <string.h>
 #include <pthread.h>
 #include <network_ipc.h>
 #include <pctl.h>
@@ -202,6 +203,25 @@  void start_subprocess(sourcetype type, const char *name, const char *cfgfile,
 	start_swupdate_subprocess(type, name, cfgfile, argc, argv, start, NULL);
 }
 
+int run_system_cmd(const char *cmd)
+{
+	int ret = 0;
+
+	if ((strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) > 0)
+		&& (strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) < SWUPDATE_GENERAL_STRING_SIZE)) {
+		DEBUG("Running %s command...", cmd);
+		ret = system(cmd);
+		if (WIFEXITED(ret)) {
+			TRACE("%s command returned %d", cmd, WEXITSTATUS(ret));
+		} else {
+			ERROR("%s command returned %d: '%s'", cmd, ret, strerror(errno));
+			return -1;
+		}
+	}
+
+	return ret;
+}
+
 /*
  * The handler supervises the subprocesses
  * (Downloader, Webserver, Suricatta)
diff --git a/handlers/shell_scripthandler.c b/handlers/shell_scripthandler.c
index ca0fc34..fc6ff06 100644
--- a/handlers/shell_scripthandler.c
+++ b/handlers/shell_scripthandler.c
@@ -19,6 +19,7 @@ 
 #include "swupdate.h"
 #include "handler.h"
 #include "util.h"
+#include "pctl.h"
 
 static void shell_handler(void);
 static void shell_preinstall_handler(void);
@@ -41,14 +42,7 @@  static int execute_shell_script(struct img_type *img, const char *fnname)
 	snprintf(shellscript, sizeof(shellscript),
 		 "%s%s %s %s", tmp, img->fname, fnname, img->type_data);
 
-	ret = system(shellscript);
-	if (WIFEXITED(ret)) {
-		ret = WEXITSTATUS(ret);
-		TRACE("Calling shell script %s: return with %d",
-			shellscript, ret);
-	} else {
-		ERROR("%s returns '%s'", img->fname, strerror(errno));
-	}
+	ret = run_system_cmd(shellscript);
 
 	return ret;
 }
diff --git a/include/pctl.h b/include/pctl.h
index 0518531..de5f3b5 100644
--- a/include/pctl.h
+++ b/include/pctl.h
@@ -40,5 +40,6 @@  void sigchld_handler (int __attribute__ ((__unused__)) signum);
 
 int pctl_getfd_from_type(sourcetype s);
 const char *pctl_getname_from_type(sourcetype s);
+int run_system_cmd(const char *cmd);
 
 #endif