blob: 5c067c1bd095af71b300dd83b5e83c464fb1ddaa [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen2b69c401999-10-05 22:58:32 +00002/*
3 * Mini chroot implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen2b69c401999-10-05 22:58:32 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen2b69c401999-10-05 22:58:32 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config CHROOT
10//config: bool "chroot"
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(chroot, BB_DIR_USR_SBIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_CHROOT) += chroot.o
Eric Andersen2b69c401999-10-05 22:58:32 +000019
Manuel Novoa III cad53642003-03-19 09:13:01 +000020/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
21
Pere Orga34425382011-03-31 14:43:25 +020022//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010038int chroot_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 ++argv;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010041 if (!*argv)
42 bb_show_usage();
Denis Vlasenko394eebe2008-02-25 20:30:24 +000043 xchroot(*argv);
Eric Andersen2b69c401999-10-05 22:58:32 +000044
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 ++argv;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010046 if (!*argv) { /* no 2nd param (PROG), use shell */
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 argv -= 2;
Denys Vlasenko681efe22011-03-08 21:00:36 +010048 argv[0] = (char *) get_shell_name();
49 argv[1] = (char *) "-i"; /* GNU coreutils 8.4 compat */
50 /*argv[2] = NULL; - already is */
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 }
Eric Andersen808d03e2000-06-02 17:56:45 +000052
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020053 BB_EXECVP_or_die(argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000054}