changeset 191:7f55c59f5122

Add chroot.
author Rob Landley <rob@landley.net>
date Mon, 03 Dec 2007 19:28:51 -0600
parents e864c5ed1d25
children 8c0809cee2b0
files toys/Config.in toys/chroot.c toys/toylist.h
diffstat 3 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/toys/Config.in	Mon Dec 03 18:57:57 2007 -0600
+++ b/toys/Config.in	Mon Dec 03 19:28:51 2007 -0600
@@ -40,6 +40,14 @@
 	  -t	Show tabs as ^I
 	  -v	Don't use ^x or M-x escapes.
 
+config CHROOT
+	bool "chroot"
+	default y
+	help
+	  usage: chroot NEWPATH [commandline...]
+
+	  Run command within a new root directory.  If no command, run /bin/sh.
+
 config COUNT
 	bool "count"
 	default y
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toys/chroot.c	Mon Dec 03 19:28:51 2007 -0600
@@ -0,0 +1,14 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * chroot.c - Run command in new root directory.
+ */
+
+#include "toys.h"
+
+void chroot_main(void)
+{
+	char *binsh[] = {"/bin/sh", 0};
+	if (chdir(*toys.optargs) || chroot("."))
+		perror_exit("%s", *toys.optargs);
+	xexec(toys.optargs[1] ? toys.optargs+1 : binsh);
+}
--- a/toys/toylist.h	Mon Dec 03 18:57:57 2007 -0600
+++ b/toys/toylist.h	Mon Dec 03 19:28:51 2007 -0600
@@ -119,6 +119,7 @@
 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_BIN))
 USE_BZCAT(NEWTOY(bzcat, "", TOYFLAG_USR|TOYFLAG_BIN))
 USE_CATV(NEWTOY(catv, "vte", TOYFLAG_USR|TOYFLAG_BIN))
+USE_CHROOT(NEWTOY(chroot, "<1", TOYFLAG_USR|TOYFLAG_SBIN))
 USE_COUNT(NEWTOY(count, "", TOYFLAG_USR|TOYFLAG_BIN))
 USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK))
 USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN))