--- /dev/null
+// $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 "vserver.h"
+#include "internal.h"
+#include <string.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#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<cur_bufsize)
+ pos = strstr(buf, CTX_TAG);
+ else if (len!=(size_t)-1) {
+ bufsize = cur_bufsize * 2 - 1;
+ errno = EAGAIN;
+ }
+
+ if (pos!=0) return atoi(pos+sizeof(CTX_TAG)-1);
+ else return CTX_NOCTX;
+}
+
+#if 0
+ctx_t
+getcctx()
+{
+ return getctx(getpid());
+}
+#endif
--- /dev/null
+// $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 "internal.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <string.h>
+
+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;
+}
--- /dev/null
+// $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_INTERNAL_H
+#define H_UTIL_VSERVER_LIB_INTERNAL_H
+
+#include <stdlib.h>
+
+size_t
+utilvserver_uint2str(char *buf, size_t len, unsigned int val, unsigned char base);
+
+
+#endif // H_UTIL_VSERVER_LIB_INTERNAL_H
--- /dev/null
+// $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 "util.h"
+#include <stdlib.h>
+
+static inline void
+showHelp(int fd, int exit_code)
+{
+ WRITE_MSG(fd,
+ "Usage: mask2prefix <mask>\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;
+}
--- /dev/null
+// $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.
+
+// Saves current ctx + vserver-info into 'argv[1] + /run' which must be a dead
+// symlink
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "vserver.h"
+#include "internal.h"
+#include "util.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <limits.h>
+
+inline static void
+checkParams(int argc, char *argv[])
+{
+ if (argc<3) {
+ WRITE_MSG(2, "Usage: save_ctxinfo <VSERVER_DIR> <cmd> <args>*\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;
+}
--- /dev/null
+// $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 "vserver.h"
+
+#include <stdio.h>
+
+
+int main(int argc, char *argv[])
+{
+ if (argc==1) printf("%i\n", getcctx());
+ else printf("%i\n", getctx(atoi(argv[1])));
+
+ return 0;
+}