From d4f5e93605c6c48c6d4cceeb3a684a0670fa6995 Mon Sep 17 00:00:00 2001 From: Keith Bostic Date: Mon, 5 Dec 1988 23:24:38 -0800 Subject: [PATCH] allow command to be appended; redo perror messages to use strerror SCCS-vsn: usr.sbin/chroot/chroot.c 5.3 --- usr/src/usr.sbin/chroot/chroot.c | 43 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/usr/src/usr.sbin/chroot/chroot.c b/usr/src/usr.sbin/chroot/chroot.c index f2ffb9fc20..714e8f70f0 100644 --- a/usr/src/usr.sbin/chroot/chroot.c +++ b/usr/src/usr.sbin/chroot/chroot.c @@ -22,7 +22,7 @@ char copyright[] = #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)chroot.c 5.2 (Berkeley) %G%"; +static char sccsid[] = "@(#)chroot.c 5.3 (Berkeley) %G%"; #endif /* not lint */ #include @@ -31,26 +31,33 @@ main(argc, argv) int argc; char **argv; { - char *shell, *getenv(); + extern int errno; + char *shell, *getenv(), *strerror(); - if (argc != 2) { - fprintf(stderr, "usage: chroot directory\n"); - exit(1); - } - if (chdir(argv[1])) { - fprintf(stderr, "chdir: %s: ", argv[1]); - perror((char *)NULL); - exit(1); - } - if (chroot(argv[1])) { - fprintf(stderr, "chroot: %s: ", argv[1]); - perror((char *)NULL); + if (argc < 2) { + fprintf(stderr, "usage: chroot newroot [command]\n"); exit(1); } + if (chdir(argv[1]) || chroot(argv[1])) + fatal(argv[1]); setuid(getuid()); - if (!(shell = getenv("SHELL"))) - shell = "/bin/sh"; - execlp(shell, shell, "-i", (char *)NULL); - fprintf(stderr, "chroot: no shell %s", shell); + if (argv[2]) { + execvp(argv[2], &argv[2]); + fatal(argv[2]); + } else { + if (!(shell = getenv("SHELL"))) + shell = "/bin/sh"; + execlp(shell, shell, "-i", (char *)NULL); + fatal(shell); + } + /* NOTREACHED */ +} + +fatal(msg) + char *msg; +{ + extern int errno; + + fprintf(stderr, "chroot: %s: %s\n", msg, strerror(errno)); exit(1); } -- 2.20.1