Makes sense to check that this actually compiles
[util-vserver.git] / python / vserverimpl.c
index 77df9f5..9048e24 100644 (file)
@@ -33,39 +33,43 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include <Python.h>
 
-#include "config.h"
-#include "compat.h"
-
-#include <stdio.h>
-#include <stdlib.h>
 #include <errno.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
 #include <stdint.h>
+#include <unistd.h>
 
+#include "config.h"
+#include "pathconfig.h"
+#include "planetlab.h"
+#include "virtual.h"
 #include "vserver.h"
 #include "vserver-internal.h"
-#include "sched_cmd.h"
-#include "virtual.h"
 
 /*
- * chcontext
+ * context create
  */
 static PyObject *
 vserver_chcontext(PyObject *self, PyObject *args)
 {
-  unsigned  xid;
-  unsigned  caps_remove = 0;
-
-  if (!PyArg_ParseTuple(args, "I|I", &xid, &caps_remove))
+  xid_t  ctx;
+  uint32_t  flags = 0;
+  uint32_t  bcaps = ~vc_get_insecurebcaps();
+  rspec_t  rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
+  PyObject  *resources;
+  PyObject  *cpu_share;
+
+  if (!PyArg_ParseTuple(args, "IO|K", &ctx, &resources, &flags))
     return NULL;
-
-  if (vc_new_s_context(xid, caps_remove, 0) < 0)
-    return PyErr_SetFromErrno(PyExc_OSError);
+  if (!PyMapping_Check(resources))
+    {
+      PyErr_SetString(PyExc_TypeError, "invalid resources object");
+      return NULL;
+    }
+  if ((cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")) &&
+      (cpu_share = PyNumber_Int(cpu_share)))
+    rspec.cpu_share = PyInt_AsLong(cpu_share);
+
+  if (pl_chcontext(ctx, flags, bcaps, &rspec))
+    PyErr_SetFromErrno(PyExc_OSError);
 
   return Py_None;
 }
@@ -73,14 +77,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;
@@ -97,14 +102,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;
@@ -122,41 +128,28 @@ vserver_get_rlimit(PyObject *self, PyObject *args) {
 static PyObject *
 vserver_setsched(PyObject *self, PyObject *args)
 {
-  unsigned  xid;
-  struct vc_set_sched sched;
-
-  sched.set_mask = (VC_VXSM_FILL_RATE | 
-                   VC_VXSM_INTERVAL | 
-                   VC_VXSM_TOKENS_MIN | 
-                   VC_VXSM_TOKENS_MAX);
-
-  if (!PyArg_ParseTuple(args, "I|I|I|I|I", &xid, 
-                       &sched.fill_rate,
-                       &sched.interval,
-                       &sched.tokens_min,
-                       &sched.tokens_max))
-    return NULL;
+  xid_t  ctx;
+  rspec_t  rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
+  PyObject  *resources;
+  PyObject  *cpu_share;
 
-  if (vc_set_sched(xid, &sched) == -1)
-    return PyErr_SetFromErrno(PyExc_OSError);
+  if (!PyArg_ParseTuple(args, "IO", &ctx, &resources))
+    return NULL;
+  if (!PyMapping_Check(resources))
+    {
+      PyErr_SetString(PyExc_TypeError, "invalid resources object");
+      return NULL;
+    }
+  if ((cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")) &&
+      (cpu_share = PyNumber_Int(cpu_share)))
+    rspec.cpu_share = PyInt_AsLong(cpu_share);
+
+  if (pl_setsched(ctx, rspec.cpu_share, rspec.cpu_sched_flags))
+    PyErr_SetFromErrno(PyExc_OSError);
 
   return Py_None;
 }
 
-/*
- * setsched
- */
-
-/*  inode vserver commands */
-#define VCMD_add_dlimit                VC_CMD(DLIMIT, 1, 0)
-#define VCMD_rem_dlimit                VC_CMD(DLIMIT, 2, 0)
-#define VCMD_set_dlimit                VC_CMD(DLIMIT, 5, 0)
-#define VCMD_get_dlimit                VC_CMD(DLIMIT, 6, 0)
-
-#define CDLIM_UNSET             (0ULL)
-#define CDLIM_INFINITY          (~0ULL)
-#define CDLIM_KEEP              (~1ULL)
-
 static PyObject *
 vserver_get_dlimit(PyObject *self, PyObject *args)
 {
@@ -195,7 +188,6 @@ vserver_set_dlimit(PyObject *self, PyObject *args)
        unsigned xid;
        struct vcmd_ctx_dlimit_base_v0 init;
        struct vcmd_ctx_dlimit_v0 data;
-       int r;
 
        memset(&data,0,sizeof(data));
        if (!PyArg_ParseTuple(args, "siiiiii", &path,
@@ -214,22 +206,42 @@ vserver_set_dlimit(PyObject *self, PyObject *args)
        init.name = path;
        init.flags = 0;
 
-       r = vserver(VCMD_rem_dlimit, xid, &init);
-       if (r<0){}
-       r = vserver(VCMD_add_dlimit, xid, &init);
-       if (r<0){}
-       r = vserver(VCMD_set_dlimit, xid, &data);
-       if (r<0){}
+       if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
+            vserver(VCMD_set_dlimit, xid, &data))
+          return PyErr_SetFromErrno(PyExc_OSError);
+
        return Py_None; 
 }
 
+static PyObject *
+vserver_unset_dlimit(PyObject *self, PyObject *args)
+{
+  char  *path;
+  unsigned  xid;
+  struct vcmd_ctx_dlimit_base_v0  init;
+
+  if (!PyArg_ParseTuple(args, "si", &path, &xid))
+    return NULL;
+
+  memset(&init, 0, sizeof(init));
+  init.name = path;
+  init.flags = 0;
+
+  if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return Py_None;      
+}
+
 static PyMethodDef  methods[] = {
   { "chcontext", vserver_chcontext, METH_VARARGS,
-    "Change to the given vserver context" },
+    "chcontext to vserver with provided flags" },
   { "setsched", vserver_setsched, METH_VARARGS,
     "Change vserver scheduling attributes for given vserver context" },
   { "setdlimit", vserver_set_dlimit, METH_VARARGS,
     "Set disk limits for given vserver context" },
+  { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
+    "Remove disk limits for given vserver context" },
   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
     "Get disk limits for given vserver context" },
   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
@@ -242,5 +254,17 @@ static PyMethodDef  methods[] = {
 PyMODINIT_FUNC
 initvserverimpl(void)
 {
-  Py_InitModule("vserverimpl", methods);
+  PyObject  *mod;
+
+  mod = Py_InitModule("vserverimpl", methods);
+
+  /* export the set of 'safe' capabilities */
+  PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
+
+  /* export the default vserver directory */
+  PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
+
+  /* export limit-related constants */
+  PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
+  PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);
 }