sbase

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

chroot.c (752B)


      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 	ARGBEGIN {
     21 	default:
     22 		usage();
     23 	} ARGEND
     24 
     25 	if (!argc)
     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 		cmd = *shell;
     39 		execvp(cmd, shell);
     40 	} else {
     41 		cmd = argv[1];
     42 		execvp(cmd, argv + 1);
     43 	}
     44 
     45 	savederrno = errno;
     46 	weprintf("execvp %s:", cmd);
     47 
     48 	_exit(126 + (savederrno == ENOENT));
     49 }