#include <Python.h>
-#include "compat.h"
+#include <errno.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"
+
+#define NONE ({ Py_INCREF(Py_None); Py_None; })
+/*
+ * context create
+ */
static PyObject *
vserver_chcontext(PyObject *self, PyObject *args)
{
+ int result;
+ xid_t ctx;
+ uint32_t flags = 0;
+ uint32_t bcaps = ~vc_get_insecurebcaps();
+
+ if (!PyArg_ParseTuple(args, "I|K", &ctx, &flags))
+ return NULL;
+
+ if ((result = pl_chcontext(ctx, flags, bcaps)) < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ return PyBool_FromLong(result);
+}
+
+static PyObject *
+vserver_setup_done(PyObject *self, PyObject *args)
+{
+ xid_t ctx;
+
+ if (!PyArg_ParseTuple(args, "I", &ctx))
+ return NULL;
+
+ if (pl_setup_done(ctx) < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ return NONE;
+}
+
+static PyObject *
+vserver_set_rlimit(PyObject *self, PyObject *args) {
+ struct vc_rlimit limits;
+ 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))
+ return NULL;
+
+ if (vc_set_rlimit(xid, resource, &limits))
+ ret = PyErr_SetFromErrno(PyExc_OSError);
+ else if (vc_get_rlimit(xid, resource, &limits)==-1)
+ ret = PyErr_SetFromErrno(PyExc_OSError);
+ else
+ ret = Py_BuildValue("L",limits.hard);
+
+ return ret;
+}
+
+static PyObject *
+vserver_get_rlimit(PyObject *self, PyObject *args) {
+ struct vc_rlimit limits;
+ 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))
+ return NULL;
+
+ if (vc_get_rlimit(xid, resource, &limits)==-1)
+ ret = PyErr_SetFromErrno(PyExc_OSError);
+ else
+ ret = Py_BuildValue("L",limits.hard);
+
+ return ret;
+}
+
+/*
+ * setsched
+ */
+static PyObject *
+vserver_setsched(PyObject *self, PyObject *args)
+{
+ xid_t ctx;
+ uint32_t cpu_share;
+ uint32_t cpu_sched_flags = VC_VXF_SCHED_FLAGS;
+
+ if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
+ return NULL;
+
+ /* ESRCH indicates that there are no processes in the context */
+ if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
+ errno != ESRCH)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ return NONE;
+}
+
+static PyObject *
+vserver_get_dlimit(PyObject *self, PyObject *args)
+{
+ PyObject *res;
+ char* path;
+ unsigned xid;
+ struct vcmd_ctx_dlimit_v0 data;
+ int r;
+
+ if (!PyArg_ParseTuple(args, "si", &path,&xid))
+ return NULL;
+
+ memset(&data, 0, sizeof(data));
+ data.name = path;
+ data.flags = 0;
+ r = vserver(VCMD_get_dlimit, xid, &data);
+ if (r>=0) {
+ res = Py_BuildValue("(i,i,i,i,i)",
+ data.space_used,
+ data.space_total,
+ data.inodes_used,
+ data.inodes_total,
+ data.reserved);
+ } else {
+ res = PyErr_SetFromErrno(PyExc_OSError);
+ }
+
+ return res;
+}
+
+
+static PyObject *
+vserver_set_dlimit(PyObject *self, PyObject *args)
+{
+ char* path;
+ unsigned xid;
+ struct vcmd_ctx_dlimit_base_v0 init;
+ struct vcmd_ctx_dlimit_v0 data;
+
+ memset(&data,0,sizeof(data));
+ if (!PyArg_ParseTuple(args, "siiiiii", &path,
+ &xid,
+ &data.space_used,
+ &data.space_total,
+ &data.inodes_used,
+ &data.inodes_total,
+ &data.reserved))
+ return NULL;
+
+ data.name = path;
+ data.flags = 0;
+
+ memset(&init, 0, sizeof(init));
+ init.name = path;
+ init.flags = 0;
+
+ if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
+ vserver(VCMD_set_dlimit, xid, &data))
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ return NONE;
+}
+
+static PyObject *
+vserver_unset_dlimit(PyObject *self, PyObject *args)
+{
+ char *path;
unsigned xid;
- unsigned caps_remove = 0;
+ 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 NONE;
+}
- if (!PyArg_ParseTuple(args, "I|I", &xid, &caps_remove))
+static PyObject *
+vserver_killall(PyObject *self, PyObject *args)
+{
+ xid_t ctx;
+ int sig;
+
+ if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
return NULL;
- if (vc_new_s_context(xid, caps_remove, 0) < 0)
+ if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
return PyErr_SetFromErrno(PyExc_OSError);
- return Py_None;
+ return NONE;
}
static PyMethodDef methods[] = {
{ "chcontext", vserver_chcontext, METH_VARARGS,
- "Change to the given vserver context" },
+ "chcontext to vserver with provided flags" },
+ { "setup_done", vserver_setup_done, METH_VARARGS,
+ "Release vserver setup lock" },
+ { "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,
+ "Set resource limits for given resource of a vserver context" },
+ { "getrlimit", vserver_get_rlimit, METH_VARARGS,
+ "Get resource limits for given resource of a vserver context" },
+ { "killall", vserver_killall, METH_VARARGS,
+ "Send signal to all processes in vserver context" },
{ NULL, NULL, 0, NULL }
};
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);
+
+ /* scheduler flags */
+ PyModule_AddIntConstant(mod,
+ "VS_SCHED_CPU_GUARANTEED",
+ VS_SCHED_CPU_GUARANTEED);
}