From 26a5a1114913d053319fe9f26d69a72505a82abb Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Thu, 16 Oct 2003 14:02:54 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@177 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib/getinitpid-compat.hc | 30 ++++++++++++ util-vserver/lib/getinitpid-legacy.hc | 61 ++++++++++++++++++++++++ util-vserver/lib/getinitpid.c | 46 +++++++++++++++++++ util-vserver/lib/getprocentry-legacy.c | 84 ++++++++++++++++++++++++++++++++++ util-vserver/lib/utils-legacy.h | 39 ++++++++++++++++ 5 files changed, 260 insertions(+) create mode 100644 util-vserver/lib/getinitpid-compat.hc create mode 100644 util-vserver/lib/getinitpid-legacy.hc create mode 100644 util-vserver/lib/getinitpid.c create mode 100644 util-vserver/lib/getprocentry-legacy.c create mode 100644 util-vserver/lib/utils-legacy.h diff --git a/util-vserver/lib/getinitpid-compat.hc b/util-vserver/lib/getinitpid-compat.hc new file mode 100644 index 0000000..9de1d4c --- /dev/null +++ b/util-vserver/lib/getinitpid-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_GETINITPID_COMPAT_H +#define H_UTIL_VSERVER_LIB_GETINITPID_COMPAT_H + +#include "getinitpid-legacy.hc" + +static inline ALWAYSINLINE pid_t +vc_X_getinitpid_compat(pid_t pid) +{ + return vc_X_getinitpid_legacy(pid); +} + +#endif // H_UTIL_VSERVER_LIB_GETINITPID_COMPAT_H diff --git a/util-vserver/lib/getinitpid-legacy.hc b/util-vserver/lib/getinitpid-legacy.hc new file mode 100644 index 0000000..14be1b0 --- /dev/null +++ b/util-vserver/lib/getinitpid-legacy.hc @@ -0,0 +1,61 @@ +// $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_GETINITPID_LEGACY_H +#define H_UTIL_VSERVER_LIB_GETINITPID_LEGACY_H + +#ifdef HAVE_CONFIG_H +# include +#endif +#include "compat.h" + +#include "vserver.h" +#include "utils-legacy.h" + +#include +#include +#include + +static pid_t +vc_X_getinitpid_legacy_internal(pid_t pid) +{ + size_t bufsize = utilvserver_getProcEntryBufsize(); + char buf[bufsize]; + char *pos = 0; + + pos = utilvserver_getProcEntry(pid, "\ninitpid: ", buf, bufsize); + + if (pos!=0) { + if (memcmp(pos,"none",4)==0) return 1; + else return atoi(pos); + } + else return -1; +} + +static pid_t +vc_X_getinitpid_legacy(pid_t pid) +{ + pid_t res; + do { + res = vc_X_getinitpid_legacy_internal(pid); + } while (res==-1 && errno==EAGAIN); + + return res; +} + +#endif // H_UTIL_VSERVER_LIB_GETINITPID_LEGACY_H diff --git a/util-vserver/lib/getinitpid.c b/util-vserver/lib/getinitpid.c new file mode 100644 index 0000000..75d7e2b --- /dev/null +++ b/util-vserver/lib/getinitpid.c @@ -0,0 +1,46 @@ +// $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" + +#ifdef VC_ENABLE_API_COMPAT +# include "getinitpid-compat.hc" +#endif + +#ifdef VC_ENABLE_API_LEGACY +# include "getinitpid-legacy.hc" +#endif + +#include "vserver.h" +#include "vserver-internal.h" +#include "internal.h" + +#include +#include + +pid_t +vc_X_getinitpid(pid_t pid) +{ + if (pid==0) pid=getpid(); + + CALL_VC(CALL_VC_COMPAT(vc_X_getinitpid, pid), + CALL_VC_LEGACY(vc_X_getinitpid, pid)); +} diff --git a/util-vserver/lib/getprocentry-legacy.c b/util-vserver/lib/getprocentry-legacy.c new file mode 100644 index 0000000..331bcad --- /dev/null +++ b/util-vserver/lib/getprocentry-legacy.c @@ -0,0 +1,84 @@ +// $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 "utils-legacy.h" +#include "internal.h" +#include "vserver-internal.h" + +#include +#include +#include +#include +#include + +static volatile size_t proc_bufsize = 4097; + +size_t +utilvserver_getProcEntryBufsize() +{ + return proc_bufsize; +} + +char * +utilvserver_getProcEntry(pid_t pid, + char *str, + char *buf, size_t bufsize) +{ + char status_name[ sizeof("/proc/01234/status") ]; + int fd; + size_t len; + char * res = 0; + + if (pid<=0 || (uint32_t)(pid)>99999) { + errno = EINVAL; + return 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 0; + + len = read(fd, buf, 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. + + +#ifndef H_UTIL_VSERVER_LIB_UTILS_COMPAT_H +#define H_UTIL_VSERVER_LIB_UTILS_COMPAT_H + +#include +#include + + // read /proc//status into 'buf' which has the size bufsize. When 'str' + // is non-null, search this string and return a pointer *after* it. When no + // such string could be found, return 0. When 'str' is null return 'buf'. + // + // When this function fails (result==0) and errno is EAGAIN, the buffersize + // was too small and this function should be called again with a larger + // buffer. +char * utilvserver_getProcEntry(pid_t pid, char *str, + char *buf, size_t bufsize); + + // Returns the suggested buffersize for reading a /proc/.../status + // file. Return-value can change when utilvserver_getProcEntry() was called. +size_t utilvserver_getProcEntryBufsize(); + +#endif // H_UTIL_VSERVER_LIB_UTILS_COMPAT_H -- 1.8.1.5