X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=python%2Fvserverimpl.c;h=fbb94f53af77776150eaa19980e2b6f8d69a7987;hb=d00807c4ad78dec0938b0cb802514acc151cf33c;hp=3e76914b163573cb23a85791c22badcecaae4d86;hpb=59453e0c34a2f2814832260fc9db9170a86b6c82;p=util-vserver.git diff --git a/python/vserverimpl.c b/python/vserverimpl.c index 3e76914..fbb94f5 100644 --- a/python/vserverimpl.c +++ b/python/vserverimpl.c @@ -33,57 +33,101 @@ POSSIBILITY OF SUCH DAMAGE. #include -#include "config.h" -#include "compat.h" - -#include -#include #include -#include -#include -#include -#include -#include -#include #include +#include +#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" + +#define NONE ({ Py_INCREF(Py_None); Py_None; }) + +static int +get_rspec(PyObject *resources, rspec_t *rspec) +{ + int result = -1; + PyObject *cpu_share; + PyObject *sched_flags = NULL; + + if (!PyMapping_Check(resources)) + { + PyErr_SetString(PyExc_TypeError, "invalid rspec"); + return -1; + } + + /* get CPU share */ + if (!(cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share"))) + return -1; + if (!PyInt_Check(cpu_share)) + { + PyErr_SetString(PyExc_TypeError, "nm_cpu_share not an integer"); + goto out; + } + rspec->cpu_share = PyInt_AS_LONG(cpu_share); + + /* check whether this share should be guaranteed */ + rspec->cpu_sched_flags = VC_VXF_SCHED_FLAGS; + result = 0; + if ((sched_flags = PyMapping_GetItemString(resources, "nm_sched_flags"))) + { + const char *flagstr; + + if (!(flagstr = PyString_AsString(sched_flags))) + result = -1; + else if (!strcmp(flagstr, "guaranteed")) + rspec->cpu_sched_flags &= ~VC_VXF_SCHED_SHARE; + Py_DECREF(sched_flags); + } + else + /* not an error if nm_sched_flags is missing */ + PyErr_Clear(); + + out: + Py_DECREF(cpu_share); + + return result; +} /* - * 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; + + if (!PyArg_ParseTuple(args, "IO|K", &ctx, &resources, &flags) || + get_rspec(resources, &rspec)) return NULL; - if (vc_new_s_context(xid, caps_remove, 0) < 0) + if (pl_chcontext(ctx, flags, bcaps, &rspec)) return PyErr_SetFromErrno(PyExc_OSError); - return Py_None; + return NONE; } static PyObject * -__vserver_rlimit(PyObject *self, PyObject *args, int resource) { +vserver_set_rlimit(PyObject *self, PyObject *args) { struct vc_rlimit limits; - int xid; + 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, "iL", &xid, &limits.hard)) + if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard)) return NULL; - ret = Py_None; if (vc_set_rlimit(xid, resource, &limits)) ret = PyErr_SetFromErrno(PyExc_OSError); else if (vc_get_rlimit(xid, resource, &limits)==-1) @@ -94,18 +138,27 @@ __vserver_rlimit(PyObject *self, PyObject *args, int resource) { return ret; } - static PyObject * -vserver_memlimit(PyObject *self, PyObject *args) { - return __vserver_rlimit(self,args,5); -} +vserver_get_rlimit(PyObject *self, PyObject *args) { + struct vc_rlimit limits; + xid_t xid; + int resource; + PyObject *ret; -static PyObject * -vserver_tasklimit(PyObject *self, PyObject *args) { - return __vserver_rlimit(self,args,6); -} + 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 @@ -113,56 +166,22 @@ vserver_tasklimit(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)) + xid_t ctx; + rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 }; + PyObject *resources; + + if (!PyArg_ParseTuple(args, "IO", &ctx, &resources) || + get_rspec(resources, &rspec)) return NULL; - if (vc_set_sched(xid, &sched) == -1) + /* ESRCH indicates that there are no processes in the context */ + if (pl_setsched(ctx, rspec.cpu_share, rspec.cpu_sched_flags) && + errno != ESRCH) return PyErr_SetFromErrno(PyExc_OSError); - return Py_None; + return 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) - -struct vcmd_ctx_dlimit_base_v0 { - char *name; - uint32_t flags; -}; - -struct vcmd_ctx_dlimit_v0 { - char *name; - uint32_t space_used; /* used space in kbytes */ - uint32_t space_total; /* maximum space in kbytes */ - uint32_t inodes_used; /* used inodes */ - uint32_t inodes_total; /* maximum inodes */ - uint32_t reserved; /* reserved for root in % */ - uint32_t flags; -}; - -#define CDLIM_UNSET (0ULL) -#define CDLIM_INFINITY (~0ULL) -#define CDLIM_KEEP (~1ULL) - static PyObject * vserver_get_dlimit(PyObject *self, PyObject *args) { @@ -201,7 +220,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, @@ -220,33 +238,82 @@ 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){} - return Py_None; + 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; + 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; +} + +static PyObject * +vserver_killall(PyObject *self, PyObject *args) +{ + xid_t ctx; + int sig; + + if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig)) + return NULL; + + if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH) + return PyErr_SetFromErrno(PyExc_OSError); + + return 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" }, - { "tasklimit", vserver_tasklimit, METH_VARARGS, - "Set task limits for given vserver context" }, - { "memlimit", vserver_memlimit, METH_VARARGS, - "Set memory 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); }