From c66c874f68a1a82de7f2f3e30c9d5ff5eb33e866 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Mon, 9 Feb 2004 23:30:20 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@847 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib/flags.c | 69 ++++++++++++++++++++++++++ util-vserver/lib/flags_list.c | 58 ++++++++++++++++++++++ util-vserver/lib/testsuite/Makefile-files | 25 ++++++++++ util-vserver/lib/testsuite/flags.c | 81 +++++++++++++++++++++++++++++++ util-vserver/src/testsuite/chbind-test.c | 34 +++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 util-vserver/lib/flags.c create mode 100644 util-vserver/lib/flags_list.c create mode 100644 util-vserver/lib/testsuite/Makefile-files create mode 100644 util-vserver/lib/testsuite/flags.c create mode 100644 util-vserver/src/testsuite/chbind-test.c diff --git a/util-vserver/lib/flags.c b/util-vserver/lib/flags.c new file mode 100644 index 0000000..8d0101a --- /dev/null +++ b/util-vserver/lib/flags.c @@ -0,0 +1,69 @@ +// $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 "vserver.h" +#include +#include + +#define DECL(STR, VAL) { STR, sizeof(STR)-1, VAL } + +static struct { + char const * const id; + size_t len; + unsigned char val; +} const FLAGVALUES[] = { + DECL("lock", S_CTX_INFO_LOCK), + DECL("sched", S_CTX_INFO_SCHED), + DECL("nproc", S_CTX_INFO_NPROC), + DECL("private", S_CTX_INFO_PRIVATE), + DECL("init", S_CTX_INFO_INIT), + DECL("hideinfo", S_CTX_INFO_HIDEINFO), + DECL("ulimit", S_CTX_INFO_ULIMIT), +}; + +unsigned int +vc_text2flag(char const *str, size_t len) +{ + size_t i; + if (len==0) len=strlen(str); + + for (i=0; i0; i/=2, --idx) + if (val & i) return FLAGVALUES[idx].id; + + return 0; +} diff --git a/util-vserver/lib/flags_list.c b/util-vserver/lib/flags_list.c new file mode 100644 index 0000000..8362fd6 --- /dev/null +++ b/util-vserver/lib/flags_list.c @@ -0,0 +1,58 @@ +// $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 "vserver.h" +#include + +uint32_t +vc_textlist2flag(char const *str, size_t len, + char const **err_ptr, size_t *err_len) +{ + uint32_t res = 0; + + if (len==0) len = strlen(str); + + for (;len>0;) { + char const *ptr = strchr(str, ','); + size_t cnt = ptr ? (size_t)(ptr-str) : len; + unsigned int tmp; + + if (cnt>=len) { cnt=len; len=0; } + else len-=(cnt+1); + + tmp = vc_text2flag(str,cnt); + + if (tmp!=0) res |= tmp; + else { + if (err_ptr) *err_ptr = str; + if (err_len) *err_len = cnt; + return res; + } + + if (ptr==0) break; + str = ptr+1; + } + + if (err_ptr) *err_ptr = 0; + if (err_len) *err_len = 0; + return res; +} diff --git a/util-vserver/lib/testsuite/Makefile-files b/util-vserver/lib/testsuite/Makefile-files new file mode 100644 index 0000000..2a6b582 --- /dev/null +++ b/util-vserver/lib/testsuite/Makefile-files @@ -0,0 +1,25 @@ +## $Id$ --*- makefile -*-- + +## 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. + +lib_testsuite_DIETPROGS = lib/testsuite/flags + +lib_testsuite_PRGS = lib/testsuite/flags +lib_testsuite_TSTS = lib/testsuite/flags + +lib_testsuite_flags_SOURCES = lib/testsuite/flags.c +lib_testsuite_flags_LDADD = lib/libvserver.la +lib_testsuite_flags_LDFLAGS = $(VSERVER_LDFLAGS) diff --git a/util-vserver/lib/testsuite/flags.c b/util-vserver/lib/testsuite/flags.c new file mode 100644 index 0000000..3cafb40 --- /dev/null +++ b/util-vserver/lib/testsuite/flags.c @@ -0,0 +1,81 @@ +// $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 "vserver.h" +#include +#include + +#define TEST_T2F(X,Y,Z) assert(vc_text2flag(X,Y)==Z) +#define TEST_F2T(Y,X) { \ + char const *x=vc_hiflag2text(X); \ + assert((x==0 && Y==0) || (x!=0 && Y!=0)); \ + if (x!=0) assert(strcmp(x, Y)==0); \ + } + +#define TEST_LIST(STR,LEN,EXP,ERR_POS,ERR_LEN) { \ + char const *err_ptr; \ + size_t err_len; \ + char buf[] = STR; \ + uint32_t res; \ + res = vc_textlist2flag(buf, LEN, &err_ptr, &err_len); \ + assert(res==(EXP)); \ + assert(err_len==ERR_LEN); \ + if (ERR_POS==-1) assert(err_ptr==0); \ + else assert(err_ptr==buf+(ERR_POS)); \ + } + + +int main() +{ + TEST_T2F("lock", 0, S_CTX_INFO_LOCK); + TEST_T2F("lockXXXX", 4, S_CTX_INFO_LOCK); + TEST_T2F("locXXXXX", 3, 0); + TEST_T2F("sched", 0, S_CTX_INFO_SCHED); + TEST_T2F("nproc", 0, S_CTX_INFO_NPROC); + TEST_T2F("private", 0, S_CTX_INFO_PRIVATE); + TEST_T2F("init", 0, S_CTX_INFO_INIT); + TEST_T2F("hideinfo", 0, S_CTX_INFO_HIDEINFO); + TEST_T2F("ulimit", 0, S_CTX_INFO_ULIMIT); + TEST_T2F("XXX", 0, 0); + TEST_T2F("", 0, 0); + + TEST_F2T("lock", S_CTX_INFO_LOCK); + TEST_F2T("sched", S_CTX_INFO_SCHED); + TEST_F2T("nproc", S_CTX_INFO_NPROC); + TEST_F2T("private", S_CTX_INFO_PRIVATE); + TEST_F2T("init", S_CTX_INFO_INIT); + TEST_F2T("hideinfo", S_CTX_INFO_HIDEINFO); + TEST_F2T("ulimit", S_CTX_INFO_ULIMIT); + TEST_F2T(0, 0); + TEST_F2T("ulimit", 64 | 128 | 23 ); + TEST_F2T("init", 23); + + TEST_LIST("lock", 0, S_CTX_INFO_LOCK, -1,0); + TEST_LIST("lock,sched,", 0, S_CTX_INFO_LOCK|S_CTX_INFO_SCHED, -1,0); + TEST_LIST("lock,XXX", 0, S_CTX_INFO_LOCK, 5,3); + TEST_LIST("", 0, 0, -1,0); + TEST_LIST("X", 0, 0, 0,1); + TEST_LIST("lock,sched,", 10, S_CTX_INFO_LOCK|S_CTX_INFO_SCHED, -1,0); + + return 0; +} + diff --git a/util-vserver/src/testsuite/chbind-test.c b/util-vserver/src/testsuite/chbind-test.c new file mode 100644 index 0000000..a3122bd --- /dev/null +++ b/util-vserver/src/testsuite/chbind-test.c @@ -0,0 +1,34 @@ +// $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 + +#define ENSC_TESTSUITE 1 +#define main fake_main +#include "src/chbind.c" +#undef main + +int main() +{ + if (0) fake_main(0,0); + test(); + + return EXIT_SUCCESS; +} -- 1.8.1.5