sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

chroot.c (763B)


      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: %s dir [cmd [arg ...]]\n", argv0);
     12 }
     13 
     14 int
     15 main(int argc, char *argv[])
     16 {
     17 	char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *cmd;
     18 	int savederrno;
     19 
     20 	argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
     21 
     22 	if (!argc)
     23 		usage();
     24 
     25 	if ((aux = getenv("SHELL")))
     26 		shell[0] = aux;
     27 
     28 	if (chroot(argv[0]) < 0)
     29 		eprintf("chroot %s:", argv[0]);
     30 
     31 	if (chdir("/") < 0)
     32 		eprintf("chdir:");
     33 
     34 	if (argc == 1) {
     35 		cmd = *shell;
     36 		execvp(cmd, shell);
     37 	} else {
     38 		cmd = argv[1];
     39 		execvp(cmd, argv + 1);
     40 	}
     41 
     42 	savederrno = errno;
     43 	weprintf("execvp %s:", cmd);
     44 
     45 	_exit(126 + (savederrno == ENOENT));
     46 }