blob: 633e66b383e4c23a726b4200d27af4d3b213ac4a [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 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
11
Pere Orga34425382011-03-31 14:43:25 +020012//usage:#define chroot_trivial_usage
13//usage: "NEWROOT [PROG ARGS]"
14//usage:#define chroot_full_usage "\n\n"
15//usage: "Run PROG with root directory set to NEWROOT"
16//usage:
17//usage:#define chroot_example_usage
18//usage: "$ ls -l /bin/ls\n"
19//usage: "lrwxrwxrwx 1 root root 12 Apr 13 00:46 /bin/ls -> /BusyBox\n"
20//usage: "# mount /dev/hdc1 /mnt -t minix\n"
21//usage: "# chroot /mnt\n"
22//usage: "# ls -l /bin/ls\n"
23//usage: "-rwxr-xr-x 1 root root 40816 Feb 5 07:45 /bin/ls*\n"
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000026
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000027int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010028int chroot_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000029{
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 ++argv;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010031 if (!*argv)
32 bb_show_usage();
Denis Vlasenko394eebe2008-02-25 20:30:24 +000033 xchroot(*argv);
Eric Andersen2b69c401999-10-05 22:58:32 +000034
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 ++argv;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010036 if (!*argv) { /* no 2nd param (PROG), use shell */
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 argv -= 2;
Denys Vlasenko681efe22011-03-08 21:00:36 +010038 argv[0] = (char *) get_shell_name();
39 argv[1] = (char *) "-i"; /* GNU coreutils 8.4 compat */
40 /*argv[2] = NULL; - already is */
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 }
Eric Andersen808d03e2000-06-02 17:56:45 +000042
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020043 BB_EXECVP_or_die(argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000044}