blob: 192daa894fc4512cd49b0edbd89253d2c3d9ddb4 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000013
Denis Vlasenko06af2162007-02-03 17:28:39 +000014int chroot_main(int argc, char **argv);
Eric Andersen2b69c401999-10-05 22:58:32 +000015int chroot_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000016{
Manuel Novoa III cad53642003-03-19 09:13:01 +000017 if (argc < 2) {
18 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000019 }
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 ++argv;
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +000022 if (chroot(*argv)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 bb_perror_msg_and_die("cannot change root directory to %s", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000024 }
Rob Landleyd921b2e2006-08-03 15:41:12 +000025 xchdir("/");
Eric Andersen2b69c401999-10-05 22:58:32 +000026
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 ++argv;
28 if (argc == 2) {
29 argv -= 2;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000030 argv[0] = getenv("SHELL");
31 if (!argv[0]) {
32 argv[0] = (char *) DEFAULT_SHELL;
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 }
34 argv[1] = (char *) "-i";
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 }
Eric Andersen808d03e2000-06-02 17:56:45 +000036
Denis Vlasenko1d76f432007-02-06 01:20:12 +000037 BB_EXECVP(*argv, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 bb_perror_msg_and_die("cannot execute %s", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000039}