Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
Rob Landley | 8e34ca6 | 2007-12-03 19:28:51 -0600 | [diff] [blame] | 3 | * chroot.c - Run command in new root directory. |
Rob Landley | ebff8ee | 2007-12-03 20:05:14 -0600 | [diff] [blame] | 4 | * |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 5 | * Copyright 2007 Rob Landley <rob@landley.net> |
| 6 | * |
Rob Landley | ebff8ee | 2007-12-03 20:05:14 -0600 | [diff] [blame] | 7 | * Not in SUSv3. |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 8 | |
Rob Landley | 5a9c37f | 2008-08-15 14:14:10 -0500 | [diff] [blame] | 9 | USE_CHROOT(NEWTOY(chroot, "^<1", TOYFLAG_USR|TOYFLAG_SBIN)) |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 10 | |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 11 | config CHROOT |
| 12 | bool "chroot" |
| 13 | default y |
| 14 | help |
| 15 | usage: chroot NEWPATH [commandline...] |
| 16 | |
| 17 | Run command within a new root directory. If no command, run /bin/sh. |
| 18 | */ |
Rob Landley | 8e34ca6 | 2007-12-03 19:28:51 -0600 | [diff] [blame] | 19 | |
| 20 | #include "toys.h" |
| 21 | |
| 22 | void chroot_main(void) |
| 23 | { |
Rob Landley | ebff8ee | 2007-12-03 20:05:14 -0600 | [diff] [blame] | 24 | char *binsh[] = {"/bin/sh", "-i", 0}; |
Rob Landley | 8e34ca6 | 2007-12-03 19:28:51 -0600 | [diff] [blame] | 25 | if (chdir(*toys.optargs) || chroot(".")) |
| 26 | perror_exit("%s", *toys.optargs); |
| 27 | xexec(toys.optargs[1] ? toys.optargs+1 : binsh); |
| 28 | } |