From: Enrico Scholz Date: Thu, 9 Oct 2003 01:41:06 +0000 (+0000) Subject: initial checkin X-Git-Tag: VERSION_0_10~1295 X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a10f15f15ac3c657f960b3a67e05dffc422f0c71;p=util-vserver.git initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@63 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- diff --git a/util-vserver/lib/getctx.c b/util-vserver/lib/getctx.c new file mode 100644 index 0000000..9a76ed8 --- /dev/null +++ b/util-vserver/lib/getctx.c @@ -0,0 +1,73 @@ +// $Id$ --*- c++ -*-- + +// Copyright (C) 2003 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 "vserver.h" +#include "internal.h" +#include +#include +#include +#include + +#define CTX_TAG "\ns_context: " + +ctx_t +getctx(pid_t pid) +{ + static volatile size_t bufsize=4097; + // TODO: is this really race-free? + size_t cur_bufsize = bufsize; + int fd; + char status_name[ sizeof("/proc/01234/status") ]; + char buf[cur_bufsize]; + size_t len; + char *pos = 0; + + strcpy(status_name, "/proc/"); + len = utilvserver_uint2str(status_name+sizeof("/proc/")-1, + sizeof(status_name)-sizeof("/proc//status")+1, + pid, 10); + strcpy(status_name+sizeof("/proc/")+len-1, "/status"); + + fd = open(status_name, O_RDONLY); + if (fd==-1) return CTX_NOCTX; + + len = read(fd, buf, cur_bufsize); + close(fd); + + if (len +// +// 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 "internal.h" + +#include +#include +#include + +size_t +utilvserver_uint2str(char *buf, size_t len, unsigned int val, unsigned char base) +{ + char *ptr = buf+len-1; + register size_t res; + if (base>=36 || len==0) return 0; + + *ptr = '\0'; + while (ptr>buf) { + unsigned char digit = val%base; + + --ptr; + *ptr = (digit<10 ? '0'+digit : + digit<36 ? 'a'+digit-10 : + (assert(false),'?')); + + val /= base; + if (val==0) break; + } + + assert(ptr>=buf && ptr<=buf+len-1); + + res = buf+len-ptr; + memmove(buf, ptr, res); + + return res-1; +} diff --git a/util-vserver/lib/internal.h b/util-vserver/lib/internal.h new file mode 100644 index 0000000..f7c8e76 --- /dev/null +++ b/util-vserver/lib/internal.h @@ -0,0 +1,28 @@ +// $Id$ --*- c++ -*-- + +// Copyright (C) 2003 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. + + +#ifndef H_UTIL_VSERVER_LIB_INTERNAL_H +#define H_UTIL_VSERVER_LIB_INTERNAL_H + +#include + +size_t +utilvserver_uint2str(char *buf, size_t len, unsigned int val, unsigned char base); + + +#endif // H_UTIL_VSERVER_LIB_INTERNAL_H diff --git a/util-vserver/src/mask2prefix.c b/util-vserver/src/mask2prefix.c new file mode 100644 index 0000000..6de83b8 --- /dev/null +++ b/util-vserver/src/mask2prefix.c @@ -0,0 +1,70 @@ +// $Id$ --*- c++ -*-- + +// Copyright (C) 2003 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 + +static inline void +showHelp(int fd, int exit_code) +{ + WRITE_MSG(fd, + "Usage: mask2prefix \n"); + exit(exit_code); +} + +int main(int argc, char *argv[]) +{ + char *err_ptr, *ptr; + int len = 0; + size_t i; + + if (argc!=2) showHelp(2,255); + + ptr = argv[1]; + for (i=0; i<4; ++i) { + unsigned int val = strtol(ptr, &err_ptr, 10); + + switch (*err_ptr) { + case '.' : + case '\0' : break; + default : + WRITE_MSG(2, "Invalid mask specified\n"); + return 255; + } + + if (val>=0x100) { + WRITE_MSG(2, "Invalid mask specified\n"); + return 255; + } + + while (val&0x80) { + ++len; + val <<= 1; + } + + if (val!=0xff00) break; + + ptr = err_ptr+1; + } + + return len; +} diff --git a/util-vserver/src/save_ctxinfo.c b/util-vserver/src/save_ctxinfo.c new file mode 100644 index 0000000..6dc899e --- /dev/null +++ b/util-vserver/src/save_ctxinfo.c @@ -0,0 +1,92 @@ +// $Id$ --*- c++ -*-- + +// Copyright (C) 2003 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. + +// Saves current ctx + vserver-info into 'argv[1] + /run' which must be a dead +// symlink + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "vserver.h" +#include "internal.h" +#include "util.h" + +#include +#include +#include +#include +#include + +inline static void +checkParams(int argc, char *argv[]) +{ + if (argc<3) { + WRITE_MSG(2, "Usage: save_ctxinfo *\n"); + exit(255); + } +} + +int main(int argc, char *argv[]) +{ + char runfile[(checkParams(argc,argv),strlen(argv[1])) + sizeof("/run")]; + char dstfile[PATH_MAX]; + int fd; + char buf[32]; + ctx_t ctx; + size_t len; + size_t len1 = strlen(argv[1]); + + strcpy(runfile, argv[1]); + strcpy(runfile+len1, "/run"); + + ctx=getcctx(); + if (ctx==-1) { + perror("getctx()"); + return -1; + } + + if (readlink(runfile, dstfile, sizeof(dstfile))==-1) { + perror("readlink()"); + return -1; + } + + fd = open(dstfile, O_EXCL|O_CREAT|O_WRONLY, 0644); + if (fd==-1) { + perror("open()"); + return -1; + } + + len = utilvserver_uint2str(buf, sizeof(buf), ctx, 10); + + if (write(fd, buf, len) !=len || + write(fd, "\n", 1) !=1 || + write(fd, argv[1], len1)!=len1 || + write(fd, "\n", 1) !=1) { + perror("write()"); + return -1; + } + + if (close(fd)==-1) { + perror("close()"); + return -1; + } + + execv(argv[2], argv+2); + perror("execv()"); + return -1; +} diff --git a/util-vserver/tests/getctx.c b/util-vserver/tests/getctx.c new file mode 100644 index 0000000..2d4fc49 --- /dev/null +++ b/util-vserver/tests/getctx.c @@ -0,0 +1,34 @@ +// $Id$ --*- c++ -*-- + +// Copyright (C) 2003 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 "vserver.h" + +#include + + +int main(int argc, char *argv[]) +{ + if (argc==1) printf("%i\n", getcctx()); + else printf("%i\n", getctx(atoi(argv[1]))); + + return 0; +}