[CRIU] [PATCH 1/3] scripts: Move scripts code into separate sources

Pavel Emelyanov xemul at parallels.com
Wed Sep 3 12:43:23 PDT 2014


Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 Makefile.crtools         |  1 +
 action-scripts.c         | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 cr-dump.c                |  1 +
 cr-restore.c             |  1 +
 cr-service.c             | 15 ++++--------
 crtools.c                | 12 +++-------
 include/action-scripts.h | 14 +++++++++++
 include/cr_options.h     |  8 -------
 include/util.h           |  1 -
 net.c                    |  1 +
 util.c                   | 36 -----------------------------
 11 files changed, 85 insertions(+), 65 deletions(-)
 create mode 100644 action-scripts.c
 create mode 100644 include/action-scripts.h

diff --git a/Makefile.crtools b/Makefile.crtools
index ef30b05..2a6d4d2 100644
--- a/Makefile.crtools
+++ b/Makefile.crtools
@@ -15,6 +15,7 @@ obj-y	+= cr-show.o
 obj-y	+= cr-check.o
 obj-y	+= cr-dedup.o
 obj-y	+= util.o
+obj-y	+= action-scripts.o
 obj-y	+= sysctl.o
 obj-y	+= ptrace.o
 obj-y	+= kcmp-ids.o
diff --git a/action-scripts.c b/action-scripts.c
new file mode 100644
index 0000000..fc95f9a
--- /dev/null
+++ b/action-scripts.c
@@ -0,0 +1,60 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#include "cr_options.h"
+#include "list.h"
+#include "xmalloc.h"
+#include "log.h"
+#include "servicefd.h"
+#include "cr-service.h"
+#include "action-scripts.h"
+
+int run_scripts(char *action)
+{
+	struct script *script;
+	int ret = 0;
+	char image_dir[PATH_MAX];
+
+	pr_debug("Running %s scripts\n", action);
+
+	if (setenv("CRTOOLS_SCRIPT_ACTION", action, 1)) {
+		pr_perror("Can't set CRTOOLS_SCRIPT_ACTION=%s", action);
+		return -1;
+	}
+
+	sprintf(image_dir, "/proc/%ld/fd/%d", (long) getpid(), get_service_fd(IMG_FD_OFF));
+	if (setenv("CRTOOLS_IMAGE_DIR", image_dir, 1)) {
+		pr_perror("Can't set CRTOOLS_IMAGE_DIR=%s", image_dir);
+		return -1;
+	}
+
+	list_for_each_entry(script, &opts.scripts, node) {
+		if (script->path == SCRIPT_RPC_NOTIFY) {
+			pr_debug("\tRPC\n");
+			ret |= send_criu_rpc_script(action, script->arg);
+		} else {
+			pr_debug("\t[%s]\n", script->path);
+			ret |= system(script->path);
+		}
+	}
+
+	unsetenv("CRTOOLS_SCRIPT_ACTION");
+	return ret;
+}
+
+int add_script(char *path, int arg)
+{
+	struct script *script;
+
+	script = xmalloc(sizeof(struct script));
+	if (script == NULL)
+		return 1;
+
+	script->path = optarg;
+	script->arg = arg;
+	list_add(&script->node, &opts.scripts);
+
+	return 0;
+}
diff --git a/cr-dump.c b/cr-dump.c
index 0d28403..e544fdc 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -72,6 +72,7 @@
 #include "plugin.h"
 #include "irmap.h"
 #include "sysfs_parse.h"
+#include "action-scripts.h"
 
 #include "asm/dump.h"
 
diff --git a/cr-restore.c b/cr-restore.c
index 05efbda..000baf0 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -71,6 +71,7 @@
 #include "cgroup.h"
 #include "timerfd.h"
 #include "file-lock.h"
+#include "action-scripts.h"
 
 #include "parasite-syscall.h"
 
diff --git a/cr-service.c b/cr-service.c
index c42b238..ca741a0 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -25,6 +25,7 @@
 #include "net.h"
 #include "mount.h"
 #include "cgroup.h"
+#include "action-scripts.h"
 
 #include "setproctitle.h"
 
@@ -297,17 +298,9 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
 		}
 	}
 
-	if (req->notify_scripts) {
-		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, &opts.scripts);
-	}
+	if (req->notify_scripts &&
+			add_script(SCRIPT_RPC_NOTIFY, sk))
+		return -1;
 
 	for (i = 0; i < req->n_veths; i++) {
 		if (veth_pair_add(req->veths[i]->if_in, req->veths[i]->if_out))
diff --git a/crtools.c b/crtools.c
index 130cd29..8528cbc 100644
--- a/crtools.c
+++ b/crtools.c
@@ -36,6 +36,7 @@
 #include "plugin.h"
 #include "mount.h"
 #include "cgroup.h"
+#include "action-scripts.h"
 
 #include "setproctitle.h"
 
@@ -310,16 +311,9 @@ int main(int argc, char *argv[], char *envp[])
 			}
 			break;
 		case 1049:
-			{
-				struct script *script;
-
-				script = xmalloc(sizeof(struct script));
-				if (script == NULL)
-					return 1;
+			if (add_script(optarg, 0))
+				return 1;
 
-				script->path = optarg;
-				list_add(&script->node, &opts.scripts);
-			}
 			break;
 		case 1050:
 			opts.use_page_server = true;
diff --git a/include/action-scripts.h b/include/action-scripts.h
new file mode 100644
index 0000000..87c3b30
--- /dev/null
+++ b/include/action-scripts.h
@@ -0,0 +1,14 @@
+#ifndef __CR_ACTION_SCRIPTS_H__
+#define __CR_ACTION_SCRIPTS_H__
+
+struct script {
+	struct list_head node;
+	char *path;
+	int arg;
+};
+
+#define SCRIPT_RPC_NOTIFY	(char *)0x1
+
+extern int add_script(char *path, int arg);
+extern int run_scripts(char *action);
+#endif /* __CR_ACTION_SCRIPTS_H__ */
diff --git a/include/cr_options.h b/include/cr_options.h
index b603804..e00d075 100644
--- a/include/cr_options.h
+++ b/include/cr_options.h
@@ -5,14 +5,6 @@
 
 #include "list.h"
 
-struct script {
-	struct list_head node;
-	char *path;
-	int arg;
-};
-
-#define SCRIPT_RPC_NOTIFY	(char *)0x1
-
 /*
  * CPU capability options.
  */
diff --git a/include/util.h b/include/util.h
index 908172a..732605c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -261,7 +261,6 @@ static inline int read_img_str(int fd, char **pstr, int size)
 
 extern void *shmalloc(size_t bytes);
 extern void shfree_last(void *ptr);
-extern int run_scripts(char *action);
 
 extern int cr_system(int in, int out, int err, char *cmd, char *const argv[]);
 extern int cr_daemon(int nochdir, int noclose);
diff --git a/net.c b/net.c
index 177b759..bdf8d44 100644
--- a/net.c
+++ b/net.c
@@ -18,6 +18,7 @@
 #include "tun.h"
 #include "util-pie.h"
 #include "plugin.h"
+#include "action-scripts.h"
 
 #include "protobuf.h"
 #include "protobuf/netdev.pb-c.h"
diff --git a/util.c b/util.c
index 3b31537..ad153b5 100644
--- a/util.c
+++ b/util.c
@@ -1,15 +1,12 @@
 #define _XOPEN_SOURCE
 
-#include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
-#include <unistd.h>
 #include <stdbool.h>
 #include <limits.h>
 #include <signal.h>
-#include <limits.h>
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
@@ -457,39 +454,6 @@ void shfree_last(void *ptr)
 	rst_mem_free_last(RM_SHARED);
 }
 
-int run_scripts(char *action)
-{
-	struct script *script;
-	int ret = 0;
-	char image_dir[PATH_MAX];
-
-	pr_debug("Running %s scripts\n", action);
-
-	if (setenv("CRTOOLS_SCRIPT_ACTION", action, 1)) {
-		pr_perror("Can't set CRTOOLS_SCRIPT_ACTION=%s", action);
-		return -1;
-	}
-
-	sprintf(image_dir, "/proc/%ld/fd/%d", (long) getpid(), get_service_fd(IMG_FD_OFF));
-	if (setenv("CRTOOLS_IMAGE_DIR", image_dir, 1)) {
-		pr_perror("Can't set CRTOOLS_IMAGE_DIR=%s", image_dir);
-		return -1;
-	}
-
-	list_for_each_entry(script, &opts.scripts, node) {
-		if (script->path == SCRIPT_RPC_NOTIFY) {
-			pr_debug("\tRPC\n");
-			ret |= send_criu_rpc_script(action, script->arg);
-		} else {
-			pr_debug("\t[%s]\n", script->path);
-			ret |= system(script->path);
-		}
-	}
-
-	unsetenv("CRTOOLS_SCRIPT_ACTION");
-	return ret;
-}
-
 #define DUP_SAFE(fd, out)						\
 	({							\
 		int ret__;					\
-- 
1.8.4.2



More information about the CRIU mailing list