replaced all the small chroot-* programs with a single 'chroot-sh'
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 30 Jun 2005 05:58:43 +0000 (05:58 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 30 Jun 2005 05:58:43 +0000 (05:58 +0000)
program

git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2100 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/src/.cvsignore
util-vserver/src/Makefile-files
util-vserver/src/chroot-cat.c [deleted file]
util-vserver/src/chroot-rm.c [deleted file]
util-vserver/src/chroot-sh.c [new file with mode: 0644]

index 403717d..220c033 100644 (file)
@@ -11,6 +11,7 @@ chcontext-compat
 check-unixfile
 chroot-cat
 chroot-rm
+chroot-sh
 chxid
 clearenv
 enter-namespace
index 79f7682..813d344 100644 (file)
@@ -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 (file)
index 13de713..0000000
+++ /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 <config.h>
-#endif
-
-#include "util.h"
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#define ENSC_WRAPPERS_UNISTD   1
-#define ENSC_WRAPPERS_FCNTL    1
-#define ENSC_WRAPPERS_IO       1
-#include <wrappers.h>
-
-int    wrapper_exit_code = 1;
-
-static void
-showHelp(char const UNUSED *cmd)
-{
-  WRITE_MSG(1,
-           "Usage: chroot-cat [-i|-o|-a] [--] <file>\n"
-           "\n"
-           "Does something similarly to 'chroot . cat OP <file>' without\n"
-           "the need for a 'cat' in the chroot.\n"
-           "\n"
-           "Options:\n"
-           "    -a          ...  append to <file> (OP = '>>')\n"
-           "    -o          ...  truncate and append to <file> (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 (argc<idx+1) {
-    WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
-    return wrapper_exit_code;
-  }
-
-  if (argc>idx+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 (file)
index 76cfc65..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-// $Id$    --*- c -*--
-
-// Copyright (C) 2003,2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
-//  
-// 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 <config.h>
-#endif
-
-#include "util.h"
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-
-#define ENSC_WRAPPERS_PREFIX "chroot-rm: "
-#define ENSC_WRAPPERS_UNISTD 1
-#include <wrappers.h>
-
-int    wrapper_exit_code = 1;
-
-static void
-showHelp(char const *cmd)
-{
-  WRITE_MSG(1, "Usage:  ");
-  WRITE_STR(1, cmd);
-  WRITE_MSG(1,
-           " [--] <files>+\n\n"
-           "This program removes <files> 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<argc; ++idx) {
-    if (unlink(argv[idx])==-1) {
-      PERROR_Q(ENSC_WRAPPERS_PREFIX "unlink", argv[idx]);
-      res = EXIT_FAILURE;
-    }
-  }
-
-  return res;
-}
diff --git a/util-vserver/src/chroot-sh.c b/util-vserver/src/chroot-sh.c
new file mode 100644 (file)
index 0000000..8576fb1
--- /dev/null
@@ -0,0 +1,192 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#endif
+
+#include <lib_internal/util.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#define ENSC_WRAPPERS_PREFIX   "chroot-sh"
+#define ENSC_WRAPPERS_UNISTD   1
+#define ENSC_WRAPPERS_IO       1
+#define ENSC_WRAPPERS_FCNTL    1
+#include <ensc_wrappers/wrappers.h>
+
+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<argc; ++i) {
+    if (unlink(argv[i])==-1) {
+      PERROR_Q(ENSC_WRAPPERS_PREFIX "unlink", argv[i]);
+      res = EXIT_FAILURE;
+    }
+  }
+
+  return res;
+}
+
+static struct Command {
+    char const         *cmd;
+    int                        (*handler)(int argc, char *argv[]);
+} const                COMMANDS[] = {
+  { "cat",      execCat },
+  { "append",   execAppend },
+  { "truncate", execTruncate },
+  { "rm",       execRm },
+  { 0,0 }
+};
+
+static void
+showHelp()
+{
+  WRITE_MSG(1,
+           "Usage: chroot-sh "
+           " [--] <cmd> <args>*\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 <file>      ...  gives out <file> to stdout\n"
+           "  append <file>   ...  appends stdin to <file> which is created when needed\n"
+           "  truncate <file> ...  clear <file> and fill it with stdin; the <file> is\n"
+           "                       created when needed\n"
+           "  rm <file>+      ...  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 (argc<idx+1) {
+    WRITE_MSG(2, "No command specified; try '--help' for more information\n");
+    return wrapper_exit_code;
+  }
+
+  Echroot(".");
+  Echdir("/");
+
+  for (cmd=COMMANDS+0; cmd->cmd!=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;
+}