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