aboutgitcodebugslistschat
path: root/test/nstool.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-04-06 13:28:19 +1000
committerStefano Brivio <sbrivio@redhat.com>2023-04-08 01:12:12 +0200
commit1a3ade90371f7e9490e8f3be0aff83f2e178c327 (patch)
tree8baf4cd296f435ec60eb9e51b66ef116a1b26afd /test/nstool.c
parent98031bee73b30ec82a4bc5f078175061d6157b4f (diff)
downloadpasst-1a3ade90371f7e9490e8f3be0aff83f2e178c327.tar
passt-1a3ade90371f7e9490e8f3be0aff83f2e178c327.tar.gz
passt-1a3ade90371f7e9490e8f3be0aff83f2e178c327.tar.bz2
passt-1a3ade90371f7e9490e8f3be0aff83f2e178c327.tar.lz
passt-1a3ade90371f7e9490e8f3be0aff83f2e178c327.tar.xz
passt-1a3ade90371f7e9490e8f3be0aff83f2e178c327.tar.zst
passt-1a3ade90371f7e9490e8f3be0aff83f2e178c327.zip
nstool: Enter holder's cwd when changing mount ns with nstool exec
If we enter a mount namespace with nstool exec our working directory will be changed to / in the new mount ns. This is surprising if we haven't actually altered any mounts yet in the new ns. Instead, change the working directory to match that of the holder process in this situation. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'test/nstool.c')
-rw-r--r--test/nstool.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/test/nstool.c b/test/nstool.c
index c7f2b25..e6d7d37 100644
--- a/test/nstool.c
+++ b/test/nstool.c
@@ -420,6 +420,7 @@ static void cmd_exec(int argc, char *argv[])
const struct ns_type *nst;
int ctlfd, flags, opt, rc;
const char *const *xargs;
+ struct holder_info info;
bool keepcaps = false;
struct ucred peercred;
const char *exe;
@@ -444,7 +445,7 @@ static void cmd_exec(int argc, char *argv[])
sockpath = argv[optind];
- ctlfd = connect_ctl(sockpath, false, NULL, &peercred);
+ ctlfd = connect_ctl(sockpath, false, &info, &peercred);
flags = detect_namespaces(peercred.pid);
@@ -475,6 +476,15 @@ static void cmd_exec(int argc, char *argv[])
nst->name, strerror(errno));
}
+ /* If we've entered a mount ns, our cwd has changed to /.
+ * Switch to the cwd of the holder, which is probably less
+ * surprising. */
+ if (flags & CLONE_NEWNS) {
+ rc = chdir(info.cwd);
+ if (rc < 0)
+ die("chdir(\"%s\"): %s\n", info.cwd, strerror(errno));
+ }
+
/* Fork to properly enter PID namespace */
xpid = fork();
if (xpid < 0)