blob: 06823796acca9e83fcc92344069ef78db1e67981 [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001/* vi: set sw=4 ts=4:
2 *
Rob Landley8e34ca62007-12-03 19:28:51 -06003 * chroot.c - Run command in new root directory.
Rob Landleyebff8ee2007-12-03 20:05:14 -06004 *
Rob Landley28964802008-01-19 17:08:39 -06005 * Copyright 2007 Rob Landley <rob@landley.net>
6 *
Rob Landleyebff8ee2007-12-03 20:05:14 -06007 * Not in SUSv3.
Rob Landley28964802008-01-19 17:08:39 -06008
Rob Landley5a9c37f2008-08-15 14:14:10 -05009USE_CHROOT(NEWTOY(chroot, "^<1", TOYFLAG_USR|TOYFLAG_SBIN))
Rob Landley55928b12008-01-19 17:43:27 -060010
Rob Landley28964802008-01-19 17:08:39 -060011config 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 Landley8e34ca62007-12-03 19:28:51 -060019
20#include "toys.h"
21
22void chroot_main(void)
23{
Rob Landleyebff8ee2007-12-03 20:05:14 -060024 char *binsh[] = {"/bin/sh", "-i", 0};
Rob Landley8e34ca62007-12-03 19:28:51 -060025 if (chdir(*toys.optargs) || chroot("."))
26 perror_exit("%s", *toys.optargs);
27 xexec(toys.optargs[1] ? toys.optargs+1 : binsh);
28}