Files
sbase/chroot.c
T

49 lines
760 B
C
Raw Normal View History

2013-10-07 17:03:26 +01:00
/* See LICENSE file for copyright and license details. */
#include <errno.h>
2013-06-14 18:55:25 +02:00
#include <stdlib.h>
#include <unistd.h>
2014-11-13 18:29:30 +01:00
2013-06-14 18:55:25 +02:00
#include "util.h"
2014-04-22 15:13:51 +03:00
static void
usage(void)
{
eprintf("usage: chroot dir [command [arg...]]\n");
}
2013-06-14 18:55:25 +02:00
int
2014-04-18 11:51:18 +01:00
main(int argc, char *argv[])
2013-06-14 18:55:25 +02:00
{
char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *p;
int savederrno;
ARGBEGIN {
default:
usage();
} ARGEND;
2013-06-14 18:55:25 +02:00
2014-11-13 18:29:30 +01:00
if (argc < 1)
2013-06-14 18:55:25 +02:00
usage();
2014-11-13 18:29:30 +01:00
if ((aux = getenv("SHELL")))
shell[0] = aux;
2014-11-13 18:29:30 +01:00
if (chroot(argv[0]) == -1)
eprintf("chroot %s:", argv[0]);
2014-11-13 18:29:30 +01:00
if (chdir("/") == -1)
eprintf("chdir:");
2013-06-14 18:55:25 +02:00
2014-11-13 18:29:30 +01:00
if (argc == 1) {
p = *shell;
2013-06-14 18:55:25 +02:00
execvp(*shell, shell);
} else {
p = argv[1];
execvp(argv[1], argv+1);
2013-06-14 18:55:25 +02:00
}
savederrno = errno;
weprintf("execvp %s:", p);
_exit(savederrno == ENOENT ? 127 : 126);
2013-06-14 18:55:25 +02:00
}