blob: ba3e5f86434ca5dbdfc98c3180ec68ff135d47a6 [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 Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersen2b69c401999-10-05 22:58:32 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersen2ce1edc1999-10-12 15:42:48 +000024#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <unistd.h>
Eric Andersen2ce1edc1999-10-12 15:42:48 +000027#include <errno.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000028#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Eric Andersen2b69c401999-10-05 22:58:32 +000030int chroot_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000031{
Eric Andersen808d03e2000-06-02 17:56:45 +000032 char *prog;
33
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 if ((argc < 2) || (**(argv + 1) == '-')) {
Eric Andersen67991cf2001-02-14 21:23:06 +000035 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 }
37 argc--;
38 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 if (chroot(*argv) || (chdir("/"))) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000041 perror_msg_and_die("cannot change root directory to %s", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 }
Eric Andersen2b69c401999-10-05 22:58:32 +000043
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 argc--;
45 argv++;
46 if (argc >= 1) {
Eric Andersen808d03e2000-06-02 17:56:45 +000047 prog = *argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 execvp(*argv, argv);
49 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +000050#if defined shell_main && defined CONFIG_FEATURE_SH_STANDALONE_SHELL
Matt Kraaibc0f7832001-05-17 03:54:37 +000051 char shell[] = "/bin/sh";
52 char *shell_argv[2] = { shell, NULL };
Matt Kraai69438152001-05-17 20:40:21 +000053 applet_name = shell;
Matt Kraaibc0f7832001-05-17 03:54:37 +000054 shell_main(1, shell_argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +000055 return EXIT_SUCCESS;
Eric Andersen0b7d70c2001-08-02 20:56:16 +000056#else
57 prog = getenv("SHELL");
58 if (!prog)
59 prog = "/bin/sh";
60 execlp(prog, prog, NULL);
Pavel Roskine99f7502000-07-24 20:15:30 +000061#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 }
Matt Kraai1fa1ade2000-12-18 03:57:16 +000063 perror_msg_and_die("cannot execute %s", prog);
Eric Andersen808d03e2000-06-02 17:56:45 +000064
Eric Andersencc8ed391999-10-05 16:24:54 +000065}
Erik Andersen029011b2000-03-04 21:19:32 +000066
67
68/*
69Local Variables:
70c-file-style: "linux"
71c-basic-offset: 4
72tab-width: 4
73End:
74*/