xref: /original-bsd/usr.sbin/chroot/chroot.c (revision ee66054d)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1988, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)chroot.c	8.1 (Berkeley) 06/09/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <err.h>
21 #include <errno.h>
22 #include <paths.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 void usage __P((void));
29 
30 int
31 main(argc, argv)
32 	int argc;
33 	char *argv[];
34 {
35 	int ch;
36 	char *shell;
37 
38 	while ((ch = getopt(argc, argv, "")) != EOF)
39 		switch(ch) {
40 		case '?':
41 		default:
42 			usage();
43 		}
44 	argc -= optind;
45 	argv += optind;
46 
47 	if (argc < 1)
48 		usage();
49 
50 	if (chdir(argv[0]) || chroot("."))
51 		err(1, "%s", argv[0]);
52 
53 	if (argv[1]) {
54 		execvp(argv[1], &argv[1]);
55 		err(1, "%s", argv[1]);
56 	}
57 
58 	if (!(shell = getenv("SHELL")))
59 		shell = _PATH_BSHELL;
60 	execlp(shell, shell, "-i", NULL);
61 	err(1, "%s", shell);
62 	/* NOTREACHED */
63 }
64 
65 void
66 usage()
67 {
68 	(void)fprintf(stderr, "usage: chroot newroot [command]\n");
69 	exit(1);
70 }
71