From ccdbeeb8b502e06355c2e55c9f980e5142891b7c Mon Sep 17 00:00:00 2001
From: WangFengTu <wangfengtu@huawei.com>
Date: Thu, 11 Mar 2021 09:18:51 +0800
Subject: [PATCH] support isula exec --workdir

Signed-off-by: WangFengTu <wangfengtu@huawei.com>
---
 src/lxc/attach.c           |  4 ++--
 src/lxc/tools/arguments.h  |  1 +
 src/lxc/tools/lxc_attach.c | 15 +++++++++++++--
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index c5fc561..2ed2329 100644
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -791,9 +791,9 @@ static int attach_child_main(struct attach_clone_payload *payload)
 
 #ifdef HAVE_ISULAD
 	/* isulad: set workdir */
-	if (init_ctx->container->lxc_conf->init_cwd) {
+	if (options->initial_cwd || init_ctx->container->lxc_conf->init_cwd) {
 		char *init_cwd;
-		init_cwd = init_ctx->container->lxc_conf->init_cwd;
+		init_cwd = options->initial_cwd ? options->initial_cwd : init_ctx->container->lxc_conf->init_cwd;
 		/* try to create workdir if not exist */
 		struct stat st;
 		if (stat(init_cwd, &st) < 0 && mkdir_p(init_cwd, 0750) < 0) {
diff --git a/src/lxc/tools/arguments.h b/src/lxc/tools/arguments.h
index 41ea109..c16d99f 100644
--- a/src/lxc/tools/arguments.h
+++ b/src/lxc/tools/arguments.h
@@ -41,6 +41,7 @@ struct lxc_arguments {
 	/* for lxc-start */
 	const char *share_ns[32]; /* size must be greater than LXC_NS_MAX */
 #ifdef HAVE_ISULAD
+	char *workdir;
 	const char *container_info; /* isulad: file used to store pid and ppid info of container */
 	char *terminal_fifos[3]; /* isulad add, fifos used to redirct stdin/out/err */
 	const char *exit_monitor_fifo; /* isulad: fifo used to monitor state of monitor process */
diff --git a/src/lxc/tools/lxc_attach.c b/src/lxc/tools/lxc_attach.c
index dbddc2a..3bfbe6a 100644
--- a/src/lxc/tools/lxc_attach.c
+++ b/src/lxc/tools/lxc_attach.c
@@ -76,6 +76,7 @@ static const struct option my_longopts[] = {
 	{"uid", required_argument, 0, 'u'},
 	{"gid", required_argument, 0, 'g'},
 #else
+	{"workdir", required_argument, 0, 'w'},
 	{"user", required_argument, 0, 'u'},
 	{"in-fifo", required_argument, 0, OPT_INPUT_FIFO}, /* isulad add terminal fifos*/
 	{"out-fifo", required_argument, 0, OPT_OUTPUT_FIFO},
@@ -143,8 +144,9 @@ Options :\n\
 "
 #else
 "\
-  --user        User ID (format: UID[:GID])\n\
-  --timeout   Timeout in seconds (default: 0)\n\
+  --user            User ID (format: UID[:GID])\n\
+  -w, --workdir     Working directory inside the container.\n\
+  --timeout         Timeout in seconds (default: 0)\n\
 "
 #endif
 ,
@@ -295,6 +297,9 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg)
 			return -1;
 		}
 		break;
+	case 'w':
+		args->workdir=arg;
+		break;
 	case OPT_INPUT_FIFO:
 		args->terminal_fifos[0] = arg;
 		break;
@@ -639,6 +644,12 @@ int main(int argc, char *argv[])
 		attach_options.open_stdin = true;
 	}
 
+#ifdef HAVE_ISULAD
+	if (my_args.workdir) {
+		attach_options.initial_cwd = my_args.workdir;
+	}
+#endif
+
 	/* isulad: add do attach background */
 	if (attach_options.attach_flags & LXC_ATTACH_TERMINAL)
 		wexit = do_attach_foreground(c, &command, &attach_options, &errmsg);
-- 
2.20.1

