Fixed NFS so it supports 2.4.x kernels and NFSv3. Should close bug #1009.
[oweals/busybox.git] / chroot.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chroot implementation for busybox
4  *
5  *
6  * Copyright (C) 1999,2000 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include "internal.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29
30
31 static const char chroot_usage[] = "chroot NEWROOT [COMMAND...]\n"
32 #ifndef BB_FEATURE_TRIVIAL_HELP
33         "\nRun COMMAND with root directory set to NEWROOT.\n"
34 #endif
35         ;
36
37
38
39 int chroot_main(int argc, char **argv)
40 {
41         char *prog;
42
43         if ((argc < 2) || (**(argv + 1) == '-')) {
44                 usage(chroot_usage);
45         }
46         argc--;
47         argv++;
48
49         if (chroot(*argv) || (chdir("/"))) {
50                 fatalError("chroot: cannot change root directory to %s: %s\n", 
51                                 *argv, strerror(errno));
52         }
53
54         argc--;
55         argv++;
56         if (argc >= 1) {
57                 prog = *argv;
58                 execvp(*argv, argv);
59         } else {
60                 prog = getenv("SHELL");
61                 if (!prog)
62                         prog = "/bin/sh";
63                 execlp(prog, prog, NULL);
64         }
65         fatalError("chroot: cannot execute %s: %s\n", 
66                         prog, strerror(errno));
67
68 }
69
70
71 /*
72 Local Variables:
73 c-file-style: "linux"
74 c-basic-offset: 4
75 tab-width: 4
76 End:
77 */