1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chroot implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 //config:config CHROOT
10 //config:	bool "chroot (3.7 kb)"
11 //config:	default y
12 //config:	help
13 //config:	chroot is used to change the root directory and run a command.
14 //config:	The default command is '/bin/sh'.
15 
16 //applet:IF_CHROOT(APPLET_NOEXEC(chroot, chroot, BB_DIR_USR_SBIN, BB_SUID_DROP, chroot))
17 
18 //kbuild:lib-$(CONFIG_CHROOT) += chroot.o
19 
20 /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
21 
22 //usage:#define chroot_trivial_usage
23 //usage:       "NEWROOT [PROG ARGS]"
24 //usage:#define chroot_full_usage "\n\n"
25 //usage:       "Run PROG with root directory set to NEWROOT"
26 //usage:
27 //usage:#define chroot_example_usage
28 //usage:       "$ ls -l /bin/ls\n"
29 //usage:       "lrwxrwxrwx    1 root     root          12 Apr 13 00:46 /bin/ls -> /BusyBox\n"
30 //usage:       "# mount /dev/hdc1 /mnt -t minix\n"
31 //usage:       "# chroot /mnt\n"
32 //usage:       "# ls -l /bin/ls\n"
33 //usage:       "-rwxr-xr-x    1 root     root        40816 Feb  5 07:45 /bin/ls*\n"
34 
35 #include "libbb.h"
36 
37 int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
chroot_main(int argc UNUSED_PARAM,char ** argv)38 int chroot_main(int argc UNUSED_PARAM, char **argv)
39 {
40 	++argv;
41 	if (!*argv)
42 		bb_show_usage();
43 
44 	xchroot(*argv);
45 
46 	++argv;
47 	if (!*argv) { /* no 2nd param (PROG), use shell */
48 		argv -= 2;
49 		argv[0] = (char *) get_shell_name();
50 		argv[1] = (char *) "-i"; /* GNU coreutils 8.4 compat */
51 		/*argv[2] = NULL; - already is */
52 	}
53 
54 	BB_EXECVP_or_die(argv);
55 }
56