Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini chroot implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //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 Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 19 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */ |
| 21 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 37 | int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 38 | int chroot_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | ++argv; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 41 | if (!*argv) |
| 42 | bb_show_usage(); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 43 | xchroot(*argv); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 44 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | ++argv; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 46 | if (!*argv) { /* no 2nd param (PROG), use shell */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | argv -= 2; |
Denys Vlasenko | 681efe2 | 2011-03-08 21:00:36 +0100 | [diff] [blame] | 48 | argv[0] = (char *) get_shell_name(); |
| 49 | argv[1] = (char *) "-i"; /* GNU coreutils 8.4 compat */ |
| 50 | /*argv[2] = NULL; - already is */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | } |
Eric Andersen | 808d03e | 2000-06-02 17:56:45 +0000 | [diff] [blame] | 52 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 53 | BB_EXECVP_or_die(argv); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | } |