Always chdir() to current working directory.

This commit is contained in:
Bob Lantz
2014-11-10 16:57:12 -08:00
parent 1955e90493
commit 9b5fa1d7ed
+6 -6
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;
char *cwd = get_current_dir_name();
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)
@@ -157,18 +158,17 @@ 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) {
char *cwd = get_current_dir_name();
/* Plan B: chroot/chdir into pid's root file system */ /* 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) { /* chdir to correct working directory */
perror(cwd); if (chdir(cwd) != 0) {
return 1; perror(cwd);
} return 1;
} }
break; break;
case 'g': case 'g':