xref: /csrg-svn/usr.sbin/chroot/chroot.c (revision 62852)
135499Sbostic /*
2*62852Sbostic  * Copyright (c) 1988, 1993
3*62852Sbostic  *	The Regents of the University of California.  All rights reserved.
435499Sbostic  *
542792Sbostic  * %sccs.include.redist.c%
635499Sbostic  */
735499Sbostic 
835499Sbostic #ifndef lint
9*62852Sbostic static char copyright[] =
10*62852Sbostic "@(#) Copyright (c) 1988, 1993\n\
11*62852Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235499Sbostic #endif /* not lint */
1335499Sbostic 
1435499Sbostic #ifndef lint
15*62852Sbostic static char sccsid[] = "@(#)chroot.c	8.1 (Berkeley) 06/09/93";
1635499Sbostic #endif /* not lint */
1735499Sbostic 
1857923Sbostic #include <sys/types.h>
1957923Sbostic 
2062851Sbostic #include <err.h>
2157923Sbostic #include <errno.h>
2257923Sbostic #include <paths.h>
2335499Sbostic #include <stdio.h>
2457923Sbostic #include <stdlib.h>
2557923Sbostic #include <string.h>
2657923Sbostic #include <unistd.h>
2735499Sbostic 
2857923Sbostic void usage __P((void));
2957923Sbostic 
3057923Sbostic int
main(argc,argv)3135499Sbostic main(argc, argv)
3235499Sbostic 	int argc;
3357923Sbostic 	char *argv[];
3435498Sbostic {
3557923Sbostic 	int ch;
3657923Sbostic 	char *shell;
3735499Sbostic 
3857923Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3957923Sbostic 		switch(ch) {
4057923Sbostic 		case '?':
4157923Sbostic 		default:
4257923Sbostic 			usage();
4357923Sbostic 		}
4457923Sbostic 	argc -= optind;
4557923Sbostic 	argv += optind;
4657923Sbostic 
4762851Sbostic 	if (argc < 1)
4857923Sbostic 		usage();
4957923Sbostic 
5062851Sbostic 	if (chdir(argv[0]) || chroot("."))
5162851Sbostic 		err(1, "%s", argv[0]);
5262851Sbostic 
5362851Sbostic 	if (argv[1]) {
5462851Sbostic 		execvp(argv[1], &argv[1]);
5562851Sbostic 		err(1, "%s", argv[1]);
5635499Sbostic 	}
5762851Sbostic 
5862851Sbostic 	if (!(shell = getenv("SHELL")))
5962851Sbostic 		shell = _PATH_BSHELL;
6062851Sbostic 	execlp(shell, shell, "-i", NULL);
6162851Sbostic 	err(1, "%s", shell);
6236288Sbostic 	/* NOTREACHED */
6336288Sbostic }
6436288Sbostic 
6557923Sbostic void
usage()6657923Sbostic usage()
6757923Sbostic {
6857923Sbostic 	(void)fprintf(stderr, "usage: chroot newroot [command]\n");
6957923Sbostic 	exit(1);
7057923Sbostic }
71