summaryrefslogtreecommitdiff
path: root/src/chroot.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/chroot.c')
-rw-r--r--src/chroot.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/src/chroot.c b/src/chroot.c
index 6c2d63f55..418ea673f 100644
--- a/src/chroot.c
+++ b/src/chroot.c
@@ -49,13 +49,15 @@ static inline bool gid_unset (gid_t gid) { return gid == (gid_t) -1; }
enum
{
GROUPS = UCHAR_MAX + 1,
- USERSPEC
+ USERSPEC,
+ SKIP_CHDIR
};
static struct option const long_opts[] =
{
{"groups", required_argument, NULL, GROUPS},
{"userspec", required_argument, NULL, USERSPEC},
+ {"skip-chdir", no_argument, NULL, SKIP_CHDIR},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
@@ -194,9 +196,14 @@ Run COMMAND with root directory set to NEWROOT.\n\
"), stdout);
fputs (_("\
- --userspec=USER:GROUP specify user and group (ID or name) to use\n\
--groups=G_LIST specify supplementary groups as g1,g2,..,gN\n\
"), stdout);
+ fputs (_("\
+ --userspec=USER:GROUP specify user and group (ID or name) to use\n\
+"), stdout);
+ printf (_("\
+ --skip-chdir do not change working directory to %s\n\
+"), quote ("/"));
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -218,6 +225,7 @@ main (int argc, char **argv)
char *userspec = NULL;
char const *username = NULL;
char const *groups = NULL;
+ bool skip_chdir = false;
/* Parsed user and group IDs. */
uid_t uid = -1;
@@ -254,6 +262,10 @@ main (int argc, char **argv)
groups = optarg;
break;
+ case SKIP_CHDIR:
+ skip_chdir = true;
+ break;
+
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -269,9 +281,19 @@ main (int argc, char **argv)
usage (EXIT_CANCELED);
}
+ char const *newroot = argv[optind];
+ bool is_oldroot = is_root (newroot);
+
+ if (! is_oldroot && skip_chdir)
+ {
+ error (0, 0, _("option --skip-chdir only permitted if NEWROOT is old %s"),
+ quote ("/"));
+ usage (EXIT_CANCELED);
+ }
+
/* Only do chroot specific actions if actually changing root.
The main difference here is that we don't change working dir. */
- if (! is_root (argv[optind]))
+ if (! is_oldroot)
{
/* We have to look up users and groups twice.
- First, outside the chroot to load potentially necessary passwd/group
@@ -307,14 +329,14 @@ main (int argc, char **argv)
}
#endif
- if (chroot (argv[optind]) != 0)
+ if (chroot (newroot) != 0)
error (EXIT_CANCELED, errno, _("cannot change root directory to %s"),
- argv[optind]);
-
- if (chdir ("/"))
- error (EXIT_CANCELED, errno, _("cannot chdir to root directory"));
+ newroot);
}
+ if (! skip_chdir && chdir ("/"))
+ error (EXIT_CANCELED, errno, _("cannot chdir to root directory"));
+
if (argc == optind + 1)
{
/* No command. Run an interactive shell. */