Fixed a number of bugs:
[util-vserver.git] / python / vserverimpl.c
index 09e3782..cc500e3 100644 (file)
@@ -53,42 +53,94 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "virtual.h"
 
 /*
- * chcontext
+ * context create
  */
 static PyObject *
-vserver_chcontext(PyObject *self, PyObject *args)
+vserver_create(PyObject *self, PyObject *args)
+{
+       xid_t ctx, xid;
+
+       if (!PyArg_ParseTuple(args, "I", &ctx))
+               return NULL;
+
+       xid = vc_ctx_create(ctx);
+#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;
+}
+
+/*
+ * set flags
+ */
+static PyObject *
+vserver_flags(PyObject *self, PyObject *args)
 {
        struct vc_ctx_caps caps;
        struct vc_ctx_flags flags;
-       xid_t ctx, xid;
+#ifdef MEF_DEBUG
+       xid_t xid;
+#endif
+       xid_t ctx;
 
        caps.ccaps = ~vc_get_insecureccaps();
        caps.cmask = ~0ull;
        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;
 
-       xid = vc_ctx_create(ctx);
-       if (xid == VC_NOCTX && errno != EEXIST) {
-               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
 
-       if (vc_set_ccaps(ctx, &caps) == -1) {
+       if (vc_set_ccaps(ctx, &caps) == -1)
                return PyErr_SetFromErrno(PyExc_OSError);
-       }
 
-       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
 
-       /* context already exists, migrate to it */
-       if (xid == VC_NOCTX && vc_ctx_migrate(ctx) == -1) {
+       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;
+}
+
+/*
+ * enter
+ */
+static PyObject *
+vserver_enter(PyObject *self, PyObject *args)
+{
+       xid_t ctx, xid;
+       if (!PyArg_ParseTuple(args, "I", &ctx))
+               return NULL;
+
+       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);
        }
 
        return Py_None;
@@ -97,14 +149,15 @@ vserver_chcontext(PyObject *self, PyObject *args)
 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;
@@ -121,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;
@@ -156,16 +210,17 @@ vserver_setsched(PyObject *self, PyObject *args)
                    VC_VXSM_TOKENS_MIN | 
                    VC_VXSM_TOKENS_MAX);
 
-  if (!PyArg_ParseTuple(args, "I|I|I|I|I|I", &xid, 
+  if (!PyArg_ParseTuple(args, "I|I|I|I|I|I|I", &xid, 
                        &sched.fill_rate,
                        &sched.interval,
+                       &sched.tokens,
                        &sched.tokens_min,
                        &sched.tokens_max,
                        &cpuguaranteed))
     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
@@ -174,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);
@@ -265,8 +319,12 @@ vserver_set_dlimit(PyObject *self, PyObject *args)
 }
 
 static PyMethodDef  methods[] = {
-  { "chcontext", vserver_chcontext, METH_VARARGS,
-    "Change to the given vserver context" },
+  { "create", vserver_create, METH_VARARGS,
+    "Create a new vserver context" },
+  { "flags", vserver_flags, METH_VARARGS,
+    "Set the default flags and caps" },
+  { "enter", vserver_enter, METH_VARARGS,
+    "Enter the vserver context" },
   { "setsched", vserver_setsched, METH_VARARGS,
     "Change vserver scheduling attributes for given vserver context" },
   { "setdlimit", vserver_set_dlimit, METH_VARARGS,