initial checkin
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Sun, 27 Jun 2004 14:40:17 +0000 (14:40 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Sun, 27 Jun 2004 14:40:17 +0000 (14:40 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1605 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/build-all [new file with mode: 0755]
util-vserver/lib_internal/command-appendparameter.c [new file with mode: 0644]
util-vserver/lib_internal/command-exec.c [new file with mode: 0644]
util-vserver/lib_internal/command-free.c [new file with mode: 0644]
util-vserver/lib_internal/command-init.c [new file with mode: 0644]
util-vserver/lib_internal/command-reset.c [new file with mode: 0644]
util-vserver/lib_internal/command-wait.c [new file with mode: 0644]
util-vserver/lib_internal/command.h [new file with mode: 0644]
util-vserver/lib_internal/testsuite/.cvsignore [new file with mode: 0644]
util-vserver/lib_internal/testsuite/Makefile-files [new file with mode: 0644]
util-vserver/lib_internal/testsuite/command.c [new file with mode: 0644]

diff --git a/util-vserver/build-all b/util-vserver/build-all
new file mode 100755 (executable)
index 0000000..f03b731
--- /dev/null
@@ -0,0 +1,88 @@
+#! /bin/sh
+
+# Copyright (C) 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.
+
+set -e
+
+confflags="-C --enable-maintainer-mode --prefix=/usr --sysconfdir=/etc --localstatedir=/var"
+configure="`pwd`/configure $confflags"
+make=eval\ "${CLEAN:+make -j2 silent.clean}${CLEAN:-:}; \
+           echo -e \"== executing 'make all'...\" && \
+           make -j2 ${SILENT:+-s silent.}all && \
+           echo -e \"\n== executing 'make check'...\" && \
+           make -j2 ${SILENT:+-s silent.}check"
+
+LANG=C
+
+## Usage: xtermTitle <title>
+function xtermTitle
+{
+    tty -s || return 0
+    echo -ne "\e]0;$@\007"
+}
+
+## Usage: operate <dir> <configure-opts>*
+function operate()
+{
+    d=$1
+    shift
+    
+    echo  "******************"
+    echo $"** Operating in directory '$d'..."
+    xtermTitle "Operating in directory '$d'... ($@)"
+    
+    mkdir -p $d && cd $d
+
+    test -e Makefile || $configure "$@"
+    $make
+    cd -
+}
+
+case "$1" in
+    dist)
+       test -e Makefile || ./configure $confflags
+       $make
+       exit
+       ;;
+    debian-woody|debian-sarge|fc-1-90|fc-1)
+       cat util-vserver-"$2".tar.bz2 | \
+           ssh $1 "cd /tmp && rm -rf /tmp/util-vserver-* && \
+                   tar xjf - && cd util-vserver-$2 && \
+                   export PATH=/usr/lib/ccache:/usr/lib/ccache/bin:\$PATH && \
+                       ./configure && \
+                   make ${SILENT:+-s silent.}all && \
+                   echo -e '\n\n\n' &&
+                   make ${SILENT:+-s silent.}check"
+       exit
+       ;;
+    *)  test -z "$1" || {
+           echo $"Unknown option '$1'" >&2
+           exit 1
+       }
+esac
+
+test -d "Build" || {
+    d=$(mktemp -d /tmp/build-ensc.XXXXXX)
+    rm -f Build
+    ln -sf $d Build
+}
+
+operate Build/diet-noopt   CFLAGS='-O0 -g3' CXXFLAGS='-O0 -g3'
+operate Build/diet-opt     --enable-release
+operate Build/nodiet-noopt --disable-dietlibc CFLAGS='-O0 -g3' CXXFLAGS='-O0 -g3' 
+operate Build/nodiet-opt   --enable-release --disable-dietlibc
+operate Build/gcc32-opt    --enable-release --disable-dietlibc CC='ccache gcc32'  CXX='ccache g++'
+operate Build/gcc34-opt    --enable-release --disable-dietlibc CC='ccache gcc33'  CXX='ccache g++33'
diff --git a/util-vserver/lib_internal/command-appendparameter.c b/util-vserver/lib_internal/command-appendparameter.c
new file mode 100644 (file)
index 0000000..26c34c5
--- /dev/null
@@ -0,0 +1,30 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 "command.h"
+
+void
+Command_appendParameter(struct Command *cmd, char const *param)
+{
+  char const **        p = Vector_pushback(&cmd->params);
+  *p = param;
+}
diff --git a/util-vserver/lib_internal/command-exec.c b/util-vserver/lib_internal/command-exec.c
new file mode 100644 (file)
index 0000000..9f6b17a
--- /dev/null
@@ -0,0 +1,76 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 "command.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <assert.h>
+
+static inline bool
+initPipes(int p[2])
+{
+  return (pipe(p)!=-1 &&
+         fcntl(p[1], F_SETFD, FD_CLOEXEC)!=-1);
+}
+
+bool
+Command_exec(struct Command *cmd, bool do_fork)
+{
+  int          p[2];
+
+  Vector_zeroEnd(&cmd->params);
+
+  if (!do_fork)
+    cmd->pid = 0;
+  else if (!initPipes(p) ||
+          (cmd->pid = fork())==-1) {
+    cmd->err = errno;
+    return false;
+  }
+  
+  if (cmd->pid==0) {
+    if (do_fork) close(p[0]);
+
+    execv(cmd->filename ? cmd->filename : ((char **)(Vector_begin(&cmd->params)))[0],
+         cmd->params.data);
+    cmd->err = errno;
+    assert(cmd->err != 0);
+
+    if (do_fork) {
+      write(p[1], &cmd->err, sizeof(cmd->err));
+      _exit(1);        // implicates 'close(p[1])'
+    }
+  }
+  else {
+    close(p[1]);
+    if (read(p[0], &cmd->err, sizeof(cmd->err))==0)
+      cmd->err = 0;
+    else       // cleanup zombies
+      while (wait4(cmd->pid, 0,0,0)==-1 &&
+            (errno==EINTR || errno==EAGAIN)) {};
+    close(p[0]);
+  }
+
+  return cmd->err==0;
+}
diff --git a/util-vserver/lib_internal/command-free.c b/util-vserver/lib_internal/command-free.c
new file mode 100644 (file)
index 0000000..8163503
--- /dev/null
@@ -0,0 +1,29 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 "command.h"
+
+void
+Command_free(struct Command *cmd)
+{
+  Vector_free(&cmd->params);
+}
diff --git a/util-vserver/lib_internal/command-init.c b/util-vserver/lib_internal/command-init.c
new file mode 100644 (file)
index 0000000..1484766
--- /dev/null
@@ -0,0 +1,33 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 "command.h"
+
+void
+Command_init(struct Command *cmd, size_t UNUSED param_count)
+{
+  Vector_init(&cmd->params, sizeof(char *));
+  cmd->filename =  0;
+  cmd->pid      = -1;
+  cmd->rc       = -1;
+  cmd->err      =  0;
+}
diff --git a/util-vserver/lib_internal/command-reset.c b/util-vserver/lib_internal/command-reset.c
new file mode 100644 (file)
index 0000000..3610176
--- /dev/null
@@ -0,0 +1,31 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 "command.h"
+
+void
+Command_reset(struct Command *cmd)
+{
+  cmd->pid = -1;
+  cmd->rc  = -1;
+  cmd->err =  0;
+}
diff --git a/util-vserver/lib_internal/command-wait.c b/util-vserver/lib_internal/command-wait.c
new file mode 100644 (file)
index 0000000..a03573a
--- /dev/null
@@ -0,0 +1,42 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 "command.h"
+#include <errno.h>
+
+bool
+Command_wait(struct Command *cmd, bool do_block)
+{
+  int          rc;
+  
+  if (cmd->rc!=-1) return true;
+
+  switch (wait4(cmd->pid, &rc,
+               !do_block ? WNOHANG : 0,
+               &cmd->rusage)==-1) {
+    case  0    :  break;
+    case -1    :  cmd->err = errno; break;
+    default    :  cmd->rc  = rc;    return true;
+  }
+
+  return false;
+}
diff --git a/util-vserver/lib_internal/command.h b/util-vserver/lib_internal/command.h
new file mode 100644 (file)
index 0000000..2ab497a
--- /dev/null
@@ -0,0 +1,50 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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.
+
+
+#ifndef H_UTIL_VSERVER_LIB_INTERNAL_COMMAND_H
+#define H_UTIL_VSERVER_LIB_INTERNAL_COMMAND_H
+
+#include <ensc_vector/vector.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+#include <stdbool.h>
+
+struct Command
+{
+    char const *       filename;
+    struct Vector      params;
+    pid_t              pid;
+    int                        rc;
+    int                        err;
+    struct rusage      rusage;
+};
+
+void   Command_init(struct Command *, size_t param_count);
+void   Command_free(struct Command *);
+void   Command_reset(struct Command *);
+bool   Command_exec(struct Command *, bool do_fork);
+void   Command_appendParameter(struct Command *, char const *);
+/**
+ *  \args do_hang  when true, do not return before command exited, or
+ *                 an error (e.g. signal) occured
+ *  \returns       \c true iff command/processes exited; in this case,
+ *                 exitcode is available in the \c rc member
+ */
+bool   Command_wait(struct Command *, bool do_block);
+
+#endif //  H_UTIL_VSERVER_LIB_INTERNAL_COMMAND_H
diff --git a/util-vserver/lib_internal/testsuite/.cvsignore b/util-vserver/lib_internal/testsuite/.cvsignore
new file mode 100644 (file)
index 0000000..f412478
--- /dev/null
@@ -0,0 +1,2 @@
+.libs
+.libs
diff --git a/util-vserver/lib_internal/testsuite/Makefile-files b/util-vserver/lib_internal/testsuite/Makefile-files
new file mode 100644 (file)
index 0000000..a968b68
--- /dev/null
@@ -0,0 +1,22 @@
+## $Id$        --*- makefile -*--
+
+## Copyright (C) 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.
+
+check_PROGRAMS +=              lib_internal/testsuite/command
+
+lib_internal_testsuite_command_SOURCES =       lib_internal/testsuite/command.c
+lib_internal_testsuite_command_LDADD =         lib_internal/libinternal.a \
+                                               libensc_vector.a
diff --git a/util-vserver/lib_internal/testsuite/command.c b/util-vserver/lib_internal/testsuite/command.c
new file mode 100644 (file)
index 0000000..3dd5235
--- /dev/null
@@ -0,0 +1,48 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 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 <lib_internal/util.h>
+#include <lib_internal/command.h>
+
+int wrapper_exit_code = 255;
+
+int
+main(int argc, char *argv[])
+{
+  struct Command               cmd;
+  ssize_t                      i;
+  
+  if (argc<3) {
+    WRITE_MSG(2, "Not enough parameters\n");
+    return EXIT_FAILURE;
+  }
+
+  Command_init(&cmd, 5);
+  for (i=2; i<argc; ++i)
+    Command_appendParameter(&cmd, argv[i]);
+
+  if (Command_exec(&cmd, argv[1][0]!=0)) {
+    Command_wait(&cmd, true);
+  }
+
+  Command_free(&cmd);
+}