[4/6] actions: Introduce explicit scripts mode

Submitted by Pavel Emelianov on April 21, 2016, 12:56 p.m.

Details

Message ID 5718CE0E.5060401@virtuozzo.com
State Accepted
Series "Cleanup action scripts"
Commit bd061c834041eb847350b7804e694f001724f24b
Headers show

Commit Message

Pavel Emelianov April 21, 2016, 12:56 p.m.
We effectively have two modes of actions -- call shell
scripts and call RPC. So make ths code work such. As a
side effect -- we don't mess with environment variables
in case of RPC notifications.

Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
---
 criu/action-scripts.c | 46 +++++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/action-scripts.c b/criu/action-scripts.c
index 4529767..bd5c094 100644
--- a/criu/action-scripts.c
+++ b/criu/action-scripts.c
@@ -27,12 +27,17 @@  static const char *action_names[ACT_MAX] = {
 struct script {
 	struct list_head node;
 	char *path;
-	int arg;
 };
 
-static LIST_HEAD(scripts);
+enum {
+	SCRIPTS_NONE,
+	SCRIPTS_SHELL,
+	SCRIPTS_RPC
+};
 
-#define SCRIPT_RPC_NOTIFY	(char *)0x1
+static int scripts_mode = SCRIPTS_NONE;
+static int rpc_sk;
+static LIST_HEAD(scripts);
 
 int run_scripts(enum script_actions act)
 {
@@ -44,9 +49,15 @@  int run_scripts(enum script_actions act)
 
 	pr_debug("Running %s scripts\n", action);
 
-	if (unlikely(list_empty(&scripts)))
+	if (scripts_mode == SCRIPTS_NONE)
 		return 0;
 
+	if (scripts_mode == SCRIPTS_RPC) {
+		pr_debug("\tRPC\n");
+		ret = send_criu_rpc_script(act, (char *)action, rpc_sk);
+		goto out;
+	}
+
 	if (setenv("CRTOOLS_SCRIPT_ACTION", action, 1)) {
 		pr_perror("Can't set CRTOOLS_SCRIPT_ACTION=%s", action);
 		return -1;
@@ -67,16 +78,13 @@  int run_scripts(enum script_actions act)
 	}
 
 	list_for_each_entry(script, &scripts, node) {
-		if (script->path == SCRIPT_RPC_NOTIFY) {
-			pr_debug("\tRPC\n");
-			ret |= send_criu_rpc_script(act, (char *)action, script->arg);
-		} else {
-			pr_debug("\t[%s]\n", script->path);
-			ret |= system(script->path);
-		}
+		pr_debug("\t[%s]\n", script->path);
+		ret |= system(script->path);
 	}
 
 	unsetenv("CRTOOLS_SCRIPT_ACTION");
+
+out:
 	if (ret)
 		pr_err("One of more action scripts failed\n");
 	return ret;
@@ -86,12 +94,14 @@  int add_script(char *path)
 {
 	struct script *script;
 
+	BUG_ON(scripts_mode == SCRIPTS_RPC);
+	scripts_mode = SCRIPTS_SHELL;
+
 	script = xmalloc(sizeof(struct script));
 	if (script == NULL)
 		return 1;
 
 	script->path = path;
-	script->arg = 0;
 	list_add(&script->node, &scripts);
 
 	return 0;
@@ -99,15 +109,9 @@  int add_script(char *path)
 
 int add_rpc_notify(int sk)
 {
-	struct script *script;
-
-	script = xmalloc(sizeof(struct script));
-	if (script == NULL)
-		return 1;
-
-	script->path = SCRIPT_RPC_NOTIFY;
-	script->arg = sk;
-	list_add(&script->node, &scripts);
+	BUG_ON(scripts_mode == SCRIPTS_SHELL);
+	scripts_mode = SCRIPTS_RPC;
 
+	rpc_sk = sk;
 	return 0;
 }