aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

chroot.c (1176B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #if FEATURE_CHROOT
      4 #ifndef _DEFAULT_SOURCE
      5 #define _DEFAULT_SOURCE
      6 #endif
      7 #ifndef _XOPEN_SOURCE
      8 #define _XOPEN_SOURCE 500
      9 #endif
     10 #endif
     11 
     12 #include <errno.h>
     13 #include <stdlib.h>
     14 #include <unistd.h>
     15 
     16 #include "util.h"
     17 #include "wexec.h"
     18 
     19 static void
     20 usage(void)
     21 {
     22   eprintf("usage: %s dir [cmd [arg ...]]\n", argv0);
     23 }
     24 
     25 // ?man chroot: run command in new root
     26 // ?man arguments: dir [cmd [arg ...]]
     27 // ?man run a command or shell with a substitute root directory
     28 int
     29 main(int argc, char *argv[])
     30 {
     31   char *shell[] = {"/bin/sh", "-i", NULL}, *aux, *cmd;
     32   int   savederrno;
     33 
     34   ARGBEGIN
     35   {
     36     default:
     37       usage();
     38   }
     39   ARGEND
     40 
     41   if (!argc)
     42     usage();
     43 
     44   if ((aux = getenv("SHELL")))
     45     shell[0] = aux;
     46 
     47 #if FEATURE_CHROOT
     48   if (chroot(argv[0]) < 0)
     49     eprintf("chroot %s:", argv[0]);
     50 #else
     51   eprintf("chroot: not supported\n");
     52 #endif
     53 
     54   if (chdir("/") < 0)
     55     eprintf("chdir:");
     56 
     57   if (argc == 1) {
     58     cmd = *shell;
     59     wexecvp_self(cmd, shell);
     60   } else {
     61     cmd = argv[1];
     62     wexecvp_self(cmd, argv + 1);
     63   }
     64 
     65   savederrno = errno;
     66   weprintf("wexecvp %s:", cmd);
     67 
     68   _exit(126 + (savederrno == ENOENT));
     69 }