chdir() to correct path after calling chroot()

Since chroot() doesn't chdir() by default, we are left in
an unreachable directory in node.pexec() (and in xterms.)

fixes #370
This commit is contained in:
Bob Lantz
2014-09-04 02:22:13 -07:00
parent d4ca1db60b
commit b85943dc0a
+8 -1
View File
@@ -99,6 +99,7 @@ int main(int argc, char *argv[])
char path[PATH_MAX]; char path[PATH_MAX];
int nsid; int nsid;
int pid; int pid;
static struct sched_param sp; static struct sched_param sp;
while ((c = getopt(argc, argv, "+cdnpa:g:r:vh")) != -1) while ((c = getopt(argc, argv, "+cdnpa:g:r:vh")) != -1)
switch(c) { switch(c) {
@@ -156,12 +157,18 @@ int main(int argc, char *argv[])
sprintf(path, "/proc/%d/ns/mnt", pid); sprintf(path, "/proc/%d/ns/mnt", pid);
nsid = open(path, O_RDONLY); nsid = open(path, O_RDONLY);
if (nsid < 0 || setns(nsid, 0) != 0) { if (nsid < 0 || setns(nsid, 0) != 0) {
/* Plan B: chroot into pid's root file system */ char *cwd = get_current_dir_name();
/* Plan B: chroot/chdir into pid's root file system */
sprintf(path, "/proc/%d/root", pid); sprintf(path, "/proc/%d/root", pid);
if (chroot(path) < 0) { if (chroot(path) < 0) {
perror(path); perror(path);
return 1; return 1;
} }
/* need to chdir to correct working directory */
if (chdir(cwd) != 0) {
perror(cwd);
return 1;
}
} }
break; break;
case 'g': case 'g':