From 7eb85591899fa21ea982880780b943e8e3f65929 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Thu, 30 Jun 2005 05:58:43 +0000 Subject: [PATCH] replaced all the small chroot-* programs with a single 'chroot-sh' program git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2100 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/src/.cvsignore | 1 + util-vserver/src/Makefile-files | 9 +- util-vserver/src/chroot-cat.c | 125 -------------------------- util-vserver/src/chroot-rm.c | 86 ------------------ util-vserver/src/chroot-sh.c | 192 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 196 insertions(+), 217 deletions(-) delete mode 100644 util-vserver/src/chroot-cat.c delete mode 100644 util-vserver/src/chroot-rm.c create mode 100644 util-vserver/src/chroot-sh.c diff --git a/util-vserver/src/.cvsignore b/util-vserver/src/.cvsignore index 403717d..220c033 100644 --- a/util-vserver/src/.cvsignore +++ b/util-vserver/src/.cvsignore @@ -11,6 +11,7 @@ chcontext-compat check-unixfile chroot-cat chroot-rm +chroot-sh chxid clearenv enter-namespace diff --git a/util-vserver/src/Makefile-files b/util-vserver/src/Makefile-files index 79f7682..813d344 100644 --- a/util-vserver/src/Makefile-files +++ b/util-vserver/src/Makefile-files @@ -40,6 +40,7 @@ DIETPROGS += src/chcontext-compat \ src/capchroot \ src/chain-echo \ src/check-unixfile \ + src/chroot-sh \ src/filetime \ src/lockfile \ src/readlink \ @@ -47,8 +48,6 @@ DIETPROGS += src/chcontext-compat \ src/save_ctxinfo \ src/mask2prefix \ src/chbind \ - src/chroot-cat \ - src/chroot-rm \ src/exec-cd \ src/fakerunlevel \ src/keep-ctx-alive \ @@ -100,8 +99,7 @@ pkglib_PROGRAMS += src/capchroot \ src/chain-echo \ src/chcontext-compat \ src/check-unixfile \ - src/chroot-cat \ - src/chroot-rm \ + src/chroot-sh \ src/fakerunlevel \ src/filetime \ src/keep-ctx-alive \ @@ -184,8 +182,7 @@ src_filetime_LDADD = $(VSERVER_LDADDS) src_filetime_LDFLAGS = $(VSERVER_LDFLGS) src_chain_echo_SOURCES = src/chain-echo.c -src_chroot_cat_SOURCES = src/chroot-cat.c -src_chroot_rm_SOURCES = src/chroot-rm.c +src_chroot_sh_SOURCES = src/chroot-sh.c src_exec_cd_SOURCES = src/exec-cd.c src_fakerunlevel_SOURCES = src/fakerunlevel.c src_ifspec_SOURCES = src/ifspec.c diff --git a/util-vserver/src/chroot-cat.c b/util-vserver/src/chroot-cat.c deleted file mode 100644 index 13de713..0000000 --- a/util-vserver/src/chroot-cat.c +++ /dev/null @@ -1,125 +0,0 @@ -// $Id$ --*- c -*-- - -// Copyright (C) 2003,2004,2005 Enrico Scholz <> -// -// 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; version 2 of the License. -// -// 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. - - -#ifdef HAVE_CONFIG_H -# include -#endif - -#include "util.h" - -#include -#include -#include -#include -#include - -#define ENSC_WRAPPERS_UNISTD 1 -#define ENSC_WRAPPERS_FCNTL 1 -#define ENSC_WRAPPERS_IO 1 -#include - -int wrapper_exit_code = 1; - -static void -showHelp(char const UNUSED *cmd) -{ - WRITE_MSG(1, - "Usage: chroot-cat [-i|-o|-a] [--] \n" - "\n" - "Does something similarly to 'chroot . cat OP ' without\n" - "the need for a 'cat' in the chroot.\n" - "\n" - "Options:\n" - " -a ... append to (OP = '>>')\n" - " -o ... truncate and append to (OP = '>')\n" - " -i ... use file as input (OP = '<') (default)\n" - "\n" - "Please report bugs to " PACKAGE_BUGREPORT "\n"); - exit(0); -} - -static void -showVersion() -{ - WRITE_MSG(1, - "chroot-cat " VERSION " -- cat stdin into a chrooted file\n" - "This program is part of " PACKAGE_STRING "\n\n" - "Copyright (C) 2003,2004,2005 Enrico Scholz\n" - VERSION_COPYRIGHT_DISCLAIMER); - exit(0); -} - - -int main(int argc, char *argv[]) -{ - int fd; - int idx = 1; - int fd_in; - int fd_out; - - int xflag = O_RDONLY|O_NOCTTY; - enum {dirIN, dirOUT} dir = dirIN; - - if (argc>=2) { - if (strcmp(argv[idx], "--help") ==0) showHelp(argv[0]); - if (strcmp(argv[idx], "--version")==0) showVersion(); - if (strcmp(argv[idx], "-a") ==0) { xflag = O_WRONLY|O_CREAT|O_APPEND; dir = dirOUT; ++idx; } - if (strcmp(argv[idx], "-o") ==0) { xflag = O_WRONLY|O_CREAT|O_TRUNC; dir = dirOUT; ++idx; } - if (strcmp(argv[idx], "-i") ==0) ++idx; - if (strcmp(argv[idx], "--") ==0) ++idx; - } - - if (xflag==0) { - WRITE_MSG(2, "chroot-cat: Not mode specified; use '--help' for more information\n"); - return wrapper_exit_code; - } - - if (argcidx+1) { - WRITE_MSG(2, "Too much parameters; use '--help' for more information\n"); - return wrapper_exit_code; - } - - Echroot("."); - Echdir("/"); - - fd = EopenD(argv[idx], xflag, 0644); - - switch (dir) { - case dirIN : fd_in = fd; fd_out = 1; break; - default : fd_in = 0; fd_out = fd; break; - } - - for (;;) { - char buf[4096]; - char const * ptr=buf; - ssize_t len; - - len = Eread(fd_in, buf, sizeof(buf)); - if (len<=0) break; - - EwriteAll(fd_out, ptr, len); - } - Eclose(fd); - - return EXIT_SUCCESS; -} diff --git a/util-vserver/src/chroot-rm.c b/util-vserver/src/chroot-rm.c deleted file mode 100644 index 76cfc65..0000000 --- a/util-vserver/src/chroot-rm.c +++ /dev/null @@ -1,86 +0,0 @@ -// $Id$ --*- c -*-- - -// Copyright (C) 2003,2004 Enrico Scholz -// -// 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; version 2 of the License. -// -// 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. - - -#ifdef HAVE_CONFIG_H -# include -#endif - -#include "util.h" - -#include -#include -#include -#include - -#define ENSC_WRAPPERS_PREFIX "chroot-rm: " -#define ENSC_WRAPPERS_UNISTD 1 -#include - -int wrapper_exit_code = 1; - -static void -showHelp(char const *cmd) -{ - WRITE_MSG(1, "Usage: "); - WRITE_STR(1, cmd); - WRITE_MSG(1, - " [--] +\n\n" - "This program removes by assuming the current directory\n" - "as a chroot directory.\n\n" - "Please report bugs to " PACKAGE_BUGREPORT "\n"); - exit(0); -} - -static void -showVersion() -{ - WRITE_MSG(1, - "chroot-rm " VERSION " -- removes files under current directory\n" - "This program is part of " PACKAGE_STRING "\n\n" - "Copyright (C) 2003,2004 Enrico Scholz\n" - VERSION_COPYRIGHT_DISCLAIMER); - exit(0); -} - -int main(int argc, char *argv[]) -{ - int res = EXIT_SUCCESS; - int idx = 1; - - if (argc>1) { - if (strcmp(argv[1], "--help") ==0) showHelp(argv[0]); - if (strcmp(argv[1], "--version")==0) showVersion(); - if (strcmp(argv[1], "--")==0) ++idx; - } - - if (idx==argc) { - WRITE_MSG(2, "No files given; use '--help' for more information\n"); - return EXIT_FAILURE; - } - - Echroot("."); - - for (; idx +// +// 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; version 2 of the License. +// +// 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. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include +#include +#include + +#define ENSC_WRAPPERS_PREFIX "chroot-sh" +#define ENSC_WRAPPERS_UNISTD 1 +#define ENSC_WRAPPERS_IO 1 +#define ENSC_WRAPPERS_FCNTL 1 +#include + +int wrapper_exit_code = EXIT_FAILURE; + +static void +showFD(int fd_in, int fd_out) +{ + for (;;) { + char buf[4096]; + char const * ptr=buf; + ssize_t len; + + len = Eread(fd_in, buf, sizeof(buf)); + if (len<=0) break; + + EwriteAll(fd_out, ptr, len); + } +} + +static int +redirectFileInternal(int argc, char *argv[], + int mode, bool is_input, + char const *operation) +{ + int fd; + + if (argc<2) { + WRITE_MSG(2, "Not enough parameters for '"); + WRITE_STR(2, operation); + WRITE_MSG(2, "' operation; use '--help' for more information\n"); + return wrapper_exit_code; + } + + fd = EopenD(argv[1], mode, 0644); + if (is_input) showFD(fd, 1); + else showFD( 0, fd); + Eclose(fd); + + return EXIT_SUCCESS; +} + +static int +execCat(int argc, char *argv[]) +{ + return redirectFileInternal(argc, argv, + O_RDONLY|O_NOCTTY, true, + "cat"); +} + +static int +execAppend(int argc, char *argv[]) +{ + return redirectFileInternal(argc, argv, + O_WRONLY|O_CREAT|O_APPEND, false, + "append"); +} + +static int +execTruncate(int argc, char *argv[]) +{ + return redirectFileInternal(argc, argv, + O_WRONLY|O_CREAT|O_TRUNC, false, + "truncate"); +} + +static int +execRm(int argc, char *argv[]) +{ + int i = 1; + int res = EXIT_SUCCESS; + + if (argc<2) { + WRITE_MSG(2, "No files specified for 'rm' operation; try '--help' for more information\n"); + return wrapper_exit_code; + } + + for (;i *\n\n" + "This program chroots into the current directory and executes the specified\n" + "commands there. This means that all used paths are relative to the current\n" + "directory, and symlinks can point to files under the current path only.\n" + "\n" + "The supported commands are:\n" + " cat ... gives out to stdout\n" + " append ... appends stdin to which is created when needed\n" + " truncate ... clear and fill it with stdin; the is\n" + " created when needed\n" + " rm + ... unlink the given files\n\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n"); + exit(0); +} + +static void +showVersion() +{ + WRITE_MSG(1, + "chroot-sh " VERSION " -- execute commands within a chroot\n" + "This program is part of " PACKAGE_STRING "\n\n" + "Copyright (C) 2005 Enrico Scholz\n" + VERSION_COPYRIGHT_DISCLAIMER); + exit(0); +} + + +int main(int argc, char *argv[]) +{ + struct Command const *cmd; + int idx = 1; + + if (argc>=2) { + if (strcmp(argv[idx], "--help") ==0) showHelp(); + if (strcmp(argv[idx], "--version")==0) showVersion(); + if (strcmp(argv[idx], "--")==0) ++idx; + } + + if (argccmd!=0; ++cmd) { + if (strcmp(cmd->cmd, argv[idx])==0) + return cmd->handler(argc-idx, argv+idx); + } + + WRITE_MSG(2, "Invalid command '"); + WRITE_STR(2, argv[idx]); + WRITE_MSG(2, "'; try '--help' for more information\n"); + + return wrapper_exit_code; +} -- 1.8.1.5