morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

chroot.c (786B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <stdlib.h>
      4 #include <unistd.h>
      5 
      6 #include "util.h"
      7 
      8 static void
      9 usage(void)
     10 {
     11 	eprintf("usage: chroot dir [command [arg...]]\n");
     12 }
     13 
     14 int
     15 main(int argc, char *argv[])
     16 {
     17 	char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *p;
     18 	int savederrno;
     19 
     20 	ARGBEGIN {
     21 	default:
     22 		usage();
     23 	} ARGEND;
     24 
     25 	if (argc < 1)
     26 		usage();
     27 
     28 	if ((aux = getenv("SHELL")))
     29 		shell[0] = aux;
     30 
     31 	if (chroot(argv[0]) < 0)
     32 		eprintf("chroot %s:", argv[0]);
     33 
     34 	if (chdir("/") < 0)
     35 		eprintf("chdir:");
     36 
     37 	if (argc == 1) {
     38 		p = *shell;
     39 		execvp(*shell, shell);
     40 	} else {
     41 		p = argv[1];
     42 		execvp(argv[1], argv+1);
     43 	}
     44 
     45 	savederrno = errno;
     46 	weprintf("execvp %s:", p);
     47 	_exit(savederrno == ENOENT ? 127 : 126);
     48 	/* unreachable */
     49 	return 0;
     50 }