From: Enrico Scholz Date: Tue, 14 Oct 2003 15:19:14 +0000 (+0000) Subject: initial checkin X-Git-Tag: version_0_23_96~37 X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb74f15ef5e31b2c8ecb330542a37808821861ae;p=util-vserver.git initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/branches/SYSCALL_SWITCH@141 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- diff --git a/util-vserver/lib/checkversion.c b/util-vserver/lib/checkversion.c new file mode 100644 index 0000000..f2ff63f --- /dev/null +++ b/util-vserver/lib/checkversion.c @@ -0,0 +1,43 @@ +// $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 "compat.h" + +#include "vserver.h" +#include "getversion-internal.hc" + +int +utilvserver_checkCompatVersion() +{ + static int res=0; + static int v_errno; + + if (res==0) { + res = vc_get_version_internal(VC_CAT_COMPAT); + v_errno = errno; +#ifdef VC_ENABLE_API_LEGACY + if (res==-1 && errno==ENOSYS) res=0; +#endif + } + + errno = v_errno; + return res; +} diff --git a/util-vserver/lib/getctx-compat.hc b/util-vserver/lib/getctx-compat.hc new file mode 100644 index 0000000..96de609 --- /dev/null +++ b/util-vserver/lib/getctx-compat.hc @@ -0,0 +1,30 @@ +// $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_GETCTX_COMPAT_H +#define H_UTIL_VSERVER_LIB_GETCTX_COMPAT_H + +#include "getctx-legacy.hc" + +static inline ALWAYSINLINE ctx_t +vc_X_getctx_compat(pid_t pid) +{ + return vc_X_getctx_legacy(pid); +} + +#endif // H_UTIL_VSERVER_LIB_GETCTX_COMPAT_H diff --git a/util-vserver/lib/getctx-legacy.hc b/util-vserver/lib/getctx-legacy.hc new file mode 100644 index 0000000..17928c9 --- /dev/null +++ b/util-vserver/lib/getctx-legacy.hc @@ -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. + + +#ifndef H_UTIL_VSERVER_LIB_GETCTX_LEGACY_H +#define H_UTIL_VSERVER_LIB_GETCTX_LEGACY_H + +#ifdef HAVE_CONFIG_H +# include +#endif +#include "compat.h" + +#include "vserver.h" +#include "vserver-internal.h" +#include +#include +#include +#include + +#define CTX_TAG "\ns_context: " + +static ctx_t +vc_X_getctx_legacy(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 VC_NOCTX; + + len = read(fd, buf, cur_bufsize); + close(fd); + + if (len #endif - #include "compat.h" #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"); +#include "vserver-internal.h" - fd = open(status_name, O_RDONLY); - if (fd==-1) return CTX_NOCTX; +#ifdef VC_ENABLE_API_COMPAT +# include "getctx-compat.hc" +#endif - len = read(fd, buf, cur_bufsize); - close(fd); +#ifdef VC_ENABLE_API_LEGACY +# include "getctx-legacy.hc" +#endif - if (len - if (pos!=0) return atoi(pos+sizeof(CTX_TAG)-1); - else return CTX_NOCTX; -} - -#if 0 ctx_t -getcctx() +vc_X_getctx(pid_t pid) { - return getctx(getpid()); + CALL_VC(CALL_VC_COMPAT(vc_X_getctx, pid), + CALL_VC_LEGACY(vc_X_getctx, pid)); } -#endif diff --git a/util-vserver/lib/getversion-internal.hc b/util-vserver/lib/getversion-internal.hc new file mode 100644 index 0000000..a4a14d0 --- /dev/null +++ b/util-vserver/lib/getversion-internal.hc @@ -0,0 +1,36 @@ +// $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_GETVERSION_INTERNAL_H +#define H_UTIL_VSERVER_LIB_GETVERSION_INTERNAL_H + +#ifdef HAVE_CONFIG_H +# include +#endif +#include "compat.h" + +#include "vserver-internal.h" +#include "linuxvirtual.h" + +static inline ALWAYSINLINE int +vc_get_version_internal(int cat) +{ + return sys_virtual_context(VC_CMD(VERSION, 0, 0), cat, 0); +} + +#endif // H_UTIL_VSERVER_LIB_GETVERSION_INTERNAL_H diff --git a/util-vserver/lib/getversion.c b/util-vserver/lib/getversion.c new file mode 100644 index 0000000..94e4168 --- /dev/null +++ b/util-vserver/lib/getversion.c @@ -0,0 +1,30 @@ +// $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 "compat.h" + +#include "getversion-internal.hc" + +int +vc_get_version(int cat) +{ + return vc_get_version(cat); +} diff --git a/util-vserver/lib/safechroot-internal.hc b/util-vserver/lib/safechroot-internal.hc new file mode 100644 index 0000000..0539889 --- /dev/null +++ b/util-vserver/lib/safechroot-internal.hc @@ -0,0 +1,48 @@ +// $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_SAFECHROOT_INTERNAL_H +#define H_UTIL_VSERVER_LIB_SAFECHROOT_INTERNAL_H + +#ifdef HAVE_CONFIG_H +# include +#endif +#include "compat.h" + +#include +#include + +#ifndef NDEBUG +static void +vc_tell_unsafe_chroot() +{ + static int flag = -1; + if (flag==-1) { + char const * const e = getenv("VC_TELL_UNSAFE_CHROOT"); + flag = e ? atoi(e) : 0; + flag = flag ? 1 : 0; + } + + if (flag) write(2, "Unsafe chroot() used\n", 23); +} +#else +static ALWAYSINLINE UNUSED void vc_tell_unsafe_chroot() {} +#endif + + +#endif // H_UTIL_VSERVER_LIB_SAFECHROOT_INTERNAL_H diff --git a/util-vserver/lib/uint2str.c b/util-vserver/lib/uint2str.c new file mode 100644 index 0000000..2ba5bf1 --- /dev/null +++ b/util-vserver/lib/uint2str.c @@ -0,0 +1,54 @@ +// $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 "compat.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; +}