blob: 62cfdc244cb71d00248f5a73786c2f7f92f17763 [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 *
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Eric Andersencbe31da2001-02-20 06:14:08 +000012#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000013
Eric Andersen2b69c401999-10-05 22:58:32 +000014int chroot_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000015{
Manuel Novoa III cad53642003-03-19 09:13:01 +000016 if (argc < 2) {
17 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000018 }
Eric Andersencc8ed391999-10-05 16:24:54 +000019
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 ++argv;
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +000021 if (chroot(*argv)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 bb_perror_msg_and_die("cannot change root directory to %s", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000023 }
Rob Landleyd921b2e2006-08-03 15:41:12 +000024 xchdir("/");
Eric Andersen2b69c401999-10-05 22:58:32 +000025
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 ++argv;
27 if (argc == 2) {
28 argv -= 2;
29 if (!(*argv = getenv("SHELL"))) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +000030 *argv = (char *) DEFAULT_SHELL;
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 }
32 argv[1] = (char *) "-i";
Erik Andersene49d5ec2000-02-08 19:58:47 +000033 }
Eric Andersen808d03e2000-06-02 17:56:45 +000034
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 execvp(*argv, argv);
36 bb_perror_msg_and_die("cannot execute %s", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000037}