From 6475a5166479a3c4c23478001b3364edcc4d8940 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 18 Oct 2011 12:23:16 -0400 Subject: [PATCH] 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 file changed, 117 insertions(+) create mode 100644 parallel-debian/chroot_break.c diff --git a/parallel-debian/chroot_break.c b/parallel-debian/chroot_break.c new file mode 100644 index 00000000..2a34716b --- /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 + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +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 (".", ¤t) < 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; +}