[hacktree] chroot_break: New utility



commit 6475a5166479a3c4c23478001b3364edcc4d8940
Author: Colin Walters <walters verbum org>
Date:   Tue Oct 18 12:23:16 2011 -0400

    chroot_break: New utility
    
    Inside the OS, some utilities and functions are going to need to
    "break out" and be run from the real root.  This is a convenience
    utility to do so.

 parallel-debian/chroot_break.c |  117 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 117 insertions(+), 0 deletions(-)
---
diff --git a/parallel-debian/chroot_break.c b/parallel-debian/chroot_break.c
new file mode 100644
index 0000000..2a34716
--- /dev/null
+++ b/parallel-debian/chroot_break.c
@@ -0,0 +1,117 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * chroot_break: Exit out of active chroot(2) if any.  Requires root privileges.
+ *
+ * Copyright (C) 2011 Colin Walters <walters verbum org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+int
+usage (const char *self,
+       int         ecode)
+{
+  fprintf (stderr, "usage: %s PROGRAM [ARGS...]\n", self);
+
+  return ecode;
+}
+
+int
+main (int  argc,
+      char **argv)
+{
+  const char *template = "/tmp/chroot.XXXXXX";
+  char *tmpdir;
+  DIR *tmp;
+  struct stat current;
+  struct stat up;
+
+  if (argc < 2)
+    return usage (argv[0], 1);
+
+  tmpdir = strdup (template);
+  if (!mkdtemp (tmpdir))
+    {
+      perror ("mkdtemp");
+      return 1;
+    }
+
+  tmp = opendir ("/tmp");
+  if (!tmp)
+    {
+      perror ("opening /tmp");
+      return 1;
+    }
+
+  if (chroot (tmpdir) < 0)
+    {
+      perror ("chroot into tempdir");
+      return 1;
+    }
+
+  do 
+    {
+      if (stat (".", &current) < 0)
+	{
+	  perror ("stat");
+	  return 1;
+	}
+      if (stat ("..", &up) < 0)
+	{
+	  perror ("stat");
+	  return 1;
+	}
+      if (current.st_dev == up.st_dev
+	  && current.st_ino == up.st_ino)
+	break;
+      if (chdir ("..") < 0)
+	{
+	  perror ("chdir");
+	  return 1;
+	}
+    } while (1);
+
+  if (chroot (".") < 0)
+    {
+      perror ("chroot into real root");
+      return 1;
+    }
+
+  if (unlinkat (dirfd (tmp), strrchr (tmpdir, '/') + 1, AT_REMOVEDIR) < 0)
+    {
+      perror ("cleaning up tmpdir");
+      return 1;
+    }
+
+  closedir (tmp);
+
+  if (execv (argv[1], &argv[1]) < 0)
+    {
+      perror ("Running child process");
+      return 1;
+    }
+  /* Should not be reached */
+  return 1;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]