From 1ebbdce8199d983100040664c6d9171a568b6280 Mon Sep 17 00:00:00 2001
From: Marc Fiuczynski <mef@cs.princeton.edu>
Date: Thu, 22 Sep 2005 21:43:18 +0000
Subject: [PATCH] Fixed a number of bugs: - vserver_enter function not exported
 properly as a method. - reimplemented vserver_enter. - vserver_flags needed
 flags to VC_VXF_STATE_SETUP set.

---
 python/vserverimpl.c | 67 ++++++++++++++++++++++++++++++--------------
 1 file changed, 46 insertions(+), 21 deletions(-)

diff --git a/python/vserverimpl.c b/python/vserverimpl.c
index 4136525..cc500e3 100644
--- a/python/vserverimpl.c
+++ b/python/vserverimpl.c
@@ -64,9 +64,11 @@ vserver_create(PyObject *self, PyObject *args)
 		return NULL;
 
 	xid = vc_ctx_create(ctx);
-	if (xid == VC_NOCTX && errno != EEXIST) {
+#ifdef MEF_DEBUG
+	printf("vserver_create xid = %d\n",xid);
+#endif
+	if (xid == VC_NOCTX && errno != EEXIST)
 		return PyErr_SetFromErrno(PyExc_OSError);
-	}
 	return Py_None;
 }
 
@@ -78,6 +80,9 @@ vserver_flags(PyObject *self, PyObject *args)
 {
 	struct vc_ctx_caps caps;
 	struct vc_ctx_flags flags;
+#ifdef MEF_DEBUG
+	xid_t xid;
+#endif
 	xid_t ctx;
 
 	caps.ccaps = ~vc_get_insecureccaps();
@@ -85,19 +90,34 @@ vserver_flags(PyObject *self, PyObject *args)
 	caps.bcaps = ~vc_get_insecurebcaps();
 	caps.bmask = ~0ull;
 
-	flags.flagword = VC_VXF_INFO_LOCK;
+	flags.flagword = VC_VXF_STATE_SETUP| VC_VXF_INFO_LOCK;
 	flags.mask = VC_VXF_STATE_SETUP | VC_VXF_INFO_LOCK;
 
 	if (!PyArg_ParseTuple(args, "I", &ctx))
 		return NULL;
 
-	if (vc_set_ccaps(ctx, &caps) == -1) {
+#ifdef MEF_DEBUG
+	xid = vc_get_task_xid(0);
+	printf("vserver_flags xid = %d, ctx = %d\n",xid,ctx);
+#endif
+
+	if (vc_set_ccaps(ctx, &caps) == -1)
 		return PyErr_SetFromErrno(PyExc_OSError);
-	}
 
-	if (vc_set_cflags(ctx, &flags) == -1) {
+#ifdef MEF_DEBUG
+	xid = vc_get_task_xid(0);
+	printf("vserver_flags xid = %d, ctx = %d\n",xid,ctx);
+#endif
+
+	if (vc_set_cflags(ctx, &flags) == -1)
 		return PyErr_SetFromErrno(PyExc_OSError);
-	}
+
+#ifdef MEF_DEBUG
+	xid = vc_get_task_xid(0);
+	printf("vserver_flags xid = %d, ctx = %d\n",xid,ctx);
+#endif
+
+
 	return Py_None;
 }
 
@@ -111,28 +131,33 @@ vserver_enter(PyObject *self, PyObject *args)
 	if (!PyArg_ParseTuple(args, "I", &ctx))
 		return NULL;
 
-	xid = vc_ctx_create(ctx);
-	if (xid == VC_NOCTX && errno != EEXIST) {
-		return PyErr_SetFromErrno(PyExc_OSError);
+	xid = vc_get_task_xid(0);
+#ifdef MEF_DEBUG
+	printf("vserver_enter xid = %d\n",xid);
+#endif
+	if (xid != ctx) {
+		if (xid!=0) {
+			errno=EPERM;
+			return PyErr_SetFromErrno(PyExc_OSError);
+		} else if (vc_ctx_migrate(ctx) == -1)
+			return PyErr_SetFromErrno(PyExc_OSError);
 	}
 
-	if (xid == VC_NOCTX && vc_ctx_migrate(ctx) == -1) {
-		return PyErr_SetFromErrno(PyExc_OSError);
-	}
 	return Py_None;
 }
 
 static PyObject *
 vserver_set_rlimit(PyObject *self, PyObject *args) {
 	struct vc_rlimit limits;
-	int xid, resource;
+	xid_t xid;
+	int resource;
 	PyObject *ret;
 
 	limits.min = VC_LIM_KEEP;
 	limits.soft = VC_LIM_KEEP;
 	limits.hard = VC_LIM_KEEP;
 
-	if (!PyArg_ParseTuple(args, "iiL", &xid, &resource, &limits.hard))
+	if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
 		return NULL;
 
 	ret = Py_None;
@@ -149,14 +174,15 @@ vserver_set_rlimit(PyObject *self, PyObject *args) {
 static PyObject *
 vserver_get_rlimit(PyObject *self, PyObject *args) {
 	struct vc_rlimit limits;
-	int xid, resource;
+	xid_t xid;
+	int resource;
 	PyObject *ret;
 
 	limits.min = VC_LIM_KEEP;
 	limits.soft = VC_LIM_KEEP;
 	limits.hard = VC_LIM_KEEP;
 
-	if (!PyArg_ParseTuple(args, "ii", &xid, &resource))
+	if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
 		return NULL;
 
 	ret = Py_None;
@@ -194,7 +220,7 @@ vserver_setsched(PyObject *self, PyObject *args)
     return NULL;
 
   flags.flagword = VC_VXF_INFO_LOCK;
-  flags.mask = VC_VXF_STATE_SETUP | VC_VXF_INFO_LOCK;
+  flags.mask = VC_VXF_INFO_LOCK;
   flags.flagword |= VC_VXF_SCHED_HARD;
   flags.mask |= VC_VXF_SCHED_HARD;
 #define VC_VXF_SCHED_SHARE       0x00000800ull
@@ -203,9 +229,8 @@ vserver_setsched(PyObject *self, PyObject *args)
 	  flags.mask |= VC_VXF_SCHED_SHARE;
   }
 
-  if (vc_set_cflags(xid, &flags) == -1) {
+  if (vc_set_cflags(xid, &flags) == -1)
 	  return PyErr_SetFromErrno(PyExc_OSError);
-  }
 
   if (vc_set_sched(xid, &sched) == -1)
 	  return PyErr_SetFromErrno(PyExc_OSError);
@@ -298,7 +323,7 @@ static PyMethodDef  methods[] = {
     "Create a new vserver context" },
   { "flags", vserver_flags, METH_VARARGS,
     "Set the default flags and caps" },
-  { "enter", vserver_flags, METH_VARARGS,
+  { "enter", vserver_enter, METH_VARARGS,
     "Enter the vserver context" },
   { "setsched", vserver_setsched, METH_VARARGS,
     "Change vserver scheduling attributes for given vserver context" },
-- 
2.47.0