diff mbox series

[2/3] Make save_state available to coprocesses

Message ID 20200830124908.88129-2-sbabic@denx.de
State Changes Requested
Headers show
Series [1/3] Start network threads before processes | expand

Commit Message

Stefano Babic Aug. 30, 2020, 12:49 p.m. UTC
The update state sets bootloader variable to track the update state. Due
to privilege separations, coprocesses are not automatically allowed to
write into the bootloader interface. Create an IPC to let coprocesses
like webserver and suricatta to ask the main installer to change the
update state.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 core/network_thread.c |  7 +++++++
 core/state.c          | 22 +++++++++++++++++++++-
 include/network_ipc.h |  4 +++-
 3 files changed, 31 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/core/network_thread.c b/core/network_thread.c
index 41f17e5..7b2d796 100644
--- a/core/network_thread.c
+++ b/core/network_thread.c
@@ -30,6 +30,7 @@ 
 #include "swupdate.h"
 #include "pctl.h"
 #include "generated/autoconf.h"
+#include "state.h"
 
 #ifdef CONFIG_SYSTEMD
 #include <systemd/sd-daemon.h>
@@ -213,6 +214,7 @@  void *network_thread (void *data)
 	int pipe;
 	fd_set pipefds;
 	struct timeval tv;
+	update_state_t value;
 
 	if (!instp) {
 		TRACE("Fatal error: Network thread aborting...");
@@ -404,6 +406,11 @@  void *network_thread (void *data)
 				if (set_aes_key(msg.data.aeskeymsg.key_ascii, msg.data.aeskeymsg.ivt_ascii))
 					msg.type = NACK;
 				break;
+			case SET_UPDATE_STATE:
+				value = *(update_state_t *)msg.data.msg;
+				ret = save_state((char *)STATE_KEY, value);
+				msg.type = (ret == 0) ? ACK : NACK;
+				break;
 			default:
 				msg.type = NACK;
 			}
diff --git a/core/state.c b/core/state.c
index 245dfb8..b0d928c 100644
--- a/core/state.c
+++ b/core/state.c
@@ -12,6 +12,10 @@ 
 #include <util.h>
 #include <bootloader.h>
 #include <state.h>
+#include <network_ipc.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "pctl.h"
 
 /*
  * This check is to avoid to corrupt the environment
@@ -28,16 +32,32 @@ 
 
 static server_op_res_t do_save_state(char *key, char* value)
 {
+	char c;
 	CHECK_STATE_VAR(key);
+	if (!value)
+		return -EINVAL;
+	c = *value;
+	if (c < STATE_OK || c > STATE_LAST)
+		return -EINVAL;
 	return bootloader_env_set(key, value) == 0 ? SERVER_OK : SERVER_EERR;
 }
 
 server_op_res_t save_state(char *key, update_state_t value)
 {
 	char value_str[2] = {value, '\0'};
-	return do_save_state(key, value_str);
+	ipc_message msg;
+	if (pid == getpid()) {
+		memset(&msg, 0, sizeof(msg));
+		msg.magic = IPC_MAGIC;
+		msg.type = SET_UPDATE_STATE;
+		msg.data.msg[0] = (char)value;
+		return (ipc_send_cmd(&msg));
+	} else { /* Main process */
+		return do_save_state(key, value_str);
+	}
 }
 
+
 server_op_res_t save_state_string(char *key, update_state_t value)
 {
 	return do_save_state(key, get_state_string(value));
diff --git a/include/network_ipc.h b/include/network_ipc.h
index 520ae09..16923a9 100644
--- a/include/network_ipc.h
+++ b/include/network_ipc.h
@@ -33,7 +33,9 @@  typedef enum {
 	POST_UPDATE,
 	SWUPDATE_SUBPROCESS,
 	REQ_INSTALL_DRYRUN,
-	SET_AES_KEY
+	SET_AES_KEY,
+	SET_UPDATE_STATE,	/* set bootloader ustate */
+	GET_UPDATE_STATE	/* return bootloader ustate */
 } msgtype;
 
 /*