From 6afa2d4b4b24055db250911aa8674c09fa623752 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Tue, 13 Jan 2004 23:19:23 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@577 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/src/util-exitlikeprocess.c | 42 ++++++ util-vserver/src/vps.c | 219 ++++++++++++++++++++++++++++++++ 2 files changed, 261 insertions(+) create mode 100644 util-vserver/src/util-exitlikeprocess.c create mode 100644 util-vserver/src/vps.c diff --git a/util-vserver/src/util-exitlikeprocess.c b/util-vserver/src/util-exitlikeprocess.c new file mode 100644 index 0000000..749801c --- /dev/null +++ b/util-vserver/src/util-exitlikeprocess.c @@ -0,0 +1,42 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 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 +#include +#include +#include + +void +exitLikeProcess(int pid) +{ + int status; + + if (wait4(pid, &status, 0,0)==-1) return; + + if (WIFEXITED(status)) + exit(WEXITSTATUS(status)); + + if (WIFSIGNALED(status)) { + kill(getpid(), WTERMSIG(status)); + exit(1); + } +} diff --git a/util-vserver/src/vps.c b/util-vserver/src/vps.c new file mode 100644 index 0000000..aac59f4 --- /dev/null +++ b/util-vserver/src/vps.c @@ -0,0 +1,219 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 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 "wrappers.h" +#include "wrappers-vserver.h" +#include "pathconfig.h" + +#include +#include +#include + +#define CTXNR_WIDTH 5 +#define HUNK_SIZE 0x40; +#define CONTEXT_WIDTH 20 +#define CONTEXT_PLACE " " + +int wrapper_exit_code = 254; + +struct ContextMapping { + xid_t ctx; + char const * id; +}; + +static struct ContextMapping *mapping = 0; +static size_t mapping_len = 0; + +static size_t +writeContextInfo(xid_t ctx, char const *name) +{ + char buf[sizeof(ctx)*3+1]; + size_t l = utilvserver_fmt_ulong(buf, ctx); + size_t l1 = name==0 ? 0 : strlen(name); + size_t res = CTXNR_WIDTH + 1; + + if (l= len) { + len += HUNK_SIZE; + buf = Erealloc(buf, len+1); + } + + l = Eread(fd, buf+offset, len - offset); + if (l==0) break; + + offset += l; + } + + buf[offset] = '\0'; + + if (total_len) + *total_len = offset; + + return buf; +} + +static void +processOutput(char *data, size_t len) +{ + size_t pid_end; + char * eol_pos = strchr(data, '\n'); + char * pos; + + if (eol_pos==0) eol_pos = data + len; + else *eol_pos = '\0'; + + pos = strstr(data, "PID"); + if (pos==0) { + WRITE_MSG(2, "Failed to parse ps-output\n"); + exit(wrapper_exit_code); + } + + pid_end = pos-data + 4; + + write(1, data, pid_end); + write(1, "CONTEXT" CONTEXT_PLACE, CONTEXT_WIDTH); + write(1, data+pid_end, eol_pos-(data+pid_end)); + write(1, "\n", 1); + + len -= eol_pos-data; + data = eol_pos+1; + + while (len > 1) { + char const *vserver_name = 0; + xid_t ctx; + size_t l; + + --len; + eol_pos = strchr(data, '\n'); + + if (eol_pos==0) eol_pos = data + len; + + ctx = extractCtx(data + pid_end - 6); + vserver_name = resolveCtx(ctx); + + write(1, data, pid_end); + l = writeContextInfo(ctx, vserver_name); + if (l