| 1 | /* |
| 2 | * Copyright (c) 1988 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | */ |
| 7 | |
| 8 | #ifndef lint |
| 9 | char copyright[] = |
| 10 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ |
| 11 | All rights reserved.\n"; |
| 12 | #endif /* not lint */ |
| 13 | |
| 14 | #ifndef lint |
| 15 | static char sccsid[] = "@(#)chroot.c 5.9 (Berkeley) %G%"; |
| 16 | #endif /* not lint */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <paths.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | void fatal __P((char *)); |
| 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 < 2) |
| 48 | usage(); |
| 49 | |
| 50 | if (chdir(argv[1]) || chroot(".")) |
| 51 | fatal(argv[1]); |
| 52 | if (argv[2]) { |
| 53 | execvp(argv[2], &argv[2]); |
| 54 | fatal(argv[2]); |
| 55 | } else { |
| 56 | if (!(shell = getenv("SHELL"))) |
| 57 | shell = _PATH_BSHELL; |
| 58 | execlp(shell, shell, "-i", (char *)NULL); |
| 59 | fatal(shell); |
| 60 | } |
| 61 | /* NOTREACHED */ |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | fatal(msg) |
| 66 | char *msg; |
| 67 | { |
| 68 | (void)fprintf(stderr, "chroot: %s: %s\n", msg, strerror(errno)); |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | usage() |
| 74 | { |
| 75 | (void)fprintf(stderr, "usage: chroot newroot [command]\n"); |
| 76 | exit(1); |
| 77 | } |