initial checkin
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 16 Oct 2003 14:02:54 +0000 (14:02 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 16 Oct 2003 14:02:54 +0000 (14:02 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@177 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/lib/getinitpid-compat.hc [new file with mode: 0644]
util-vserver/lib/getinitpid-legacy.hc [new file with mode: 0644]
util-vserver/lib/getinitpid.c [new file with mode: 0644]
util-vserver/lib/getprocentry-legacy.c [new file with mode: 0644]
util-vserver/lib/utils-legacy.h [new file with mode: 0644]

diff --git a/util-vserver/lib/getinitpid-compat.hc b/util-vserver/lib/getinitpid-compat.hc
new file mode 100644 (file)
index 0000000..9de1d4c
--- /dev/null
@@ -0,0 +1,30 @@
+// $Id$    --*- c++ -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 (file)
index 0000000..14be1b0
--- /dev/null
@@ -0,0 +1,61 @@
+// $Id$    --*- c++ -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#endif
+#include "compat.h"
+
+#include "vserver.h"
+#include "utils-legacy.h"
+
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+
+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 (file)
index 0000000..75d7e2b
--- /dev/null
@@ -0,0 +1,46 @@
+// $Id$    --*- c++ -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#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 <unistd.h>
+#include <sys/types.h>
+
+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 (file)
index 0000000..331bcad
--- /dev/null
@@ -0,0 +1,84 @@
+// $Id$    --*- c++ -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#endif
+#include "compat.h"
+
+#include "utils-legacy.h"
+#include "internal.h"
+#include "vserver-internal.h"
+
+#include <string.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+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<bufsize) {
+    buf[len] = '\0';
+    if (str)
+      res    = strstr(buf, str) + strlen(str);
+    else
+      res    = buf;
+  }
+  else if (len!=(size_t)-1) {
+    if (proc_bufsize==bufsize)
+      proc_bufsize = bufsize * 2 - 1;
+
+    errno = EAGAIN;
+  }
+
+  return res;
+}
diff --git a/util-vserver/lib/utils-legacy.h b/util-vserver/lib/utils-legacy.h
new file mode 100644 (file)
index 0000000..f162431
--- /dev/null
@@ -0,0 +1,39 @@
+// $Id$    --*- c++ -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <stdlib.h>
+#include <sys/types.h>
+
+  // read /proc/<pid>/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