1 /* Copyright 2005 Princeton University
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the following
12 disclaimer in the documentation and/or other materials provided
13 with the distribution.
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
23 UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
41 #include "pathconfig.h"
42 #include "planetlab.h"
45 #include "vserver-internal.h"
47 #define NONE ({ Py_INCREF(Py_None); Py_None; })
50 get_rspec(PyObject *resources, rspec_t *rspec)
54 PyObject *sched_flags = NULL;
56 if (!PyMapping_Check(resources))
58 PyErr_SetString(PyExc_TypeError, "invalid rspec");
63 if (!(cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")))
65 if (!PyInt_Check(cpu_share))
67 PyErr_SetString(PyExc_TypeError, "nm_cpu_share not an integer");
70 rspec->cpu_share = PyInt_AS_LONG(cpu_share);
72 /* check whether this share should be guaranteed */
73 rspec->cpu_sched_flags = VC_VXF_SCHED_FLAGS;
75 if ((sched_flags = PyMapping_GetItemString(resources, "nm_sched_flags")))
79 if (!(flagstr = PyString_AsString(sched_flags)))
81 else if (!strcmp(flagstr, "guaranteed"))
82 rspec->cpu_sched_flags &= ~VC_VXF_SCHED_SHARE;
83 Py_DECREF(sched_flags);
86 /* not an error if nm_sched_flags is missing */
99 vserver_chcontext(PyObject *self, PyObject *args)
103 uint32_t bcaps = ~vc_get_insecurebcaps();
104 rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
107 if (!PyArg_ParseTuple(args, "IO|K", &ctx, &resources, &flags) ||
108 get_rspec(resources, &rspec))
111 if (pl_chcontext(ctx, flags, bcaps, &rspec))
112 return PyErr_SetFromErrno(PyExc_OSError);
118 vserver_set_rlimit(PyObject *self, PyObject *args) {
119 struct vc_rlimit limits;
124 limits.min = VC_LIM_KEEP;
125 limits.soft = VC_LIM_KEEP;
126 limits.hard = VC_LIM_KEEP;
128 if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
131 if (vc_set_rlimit(xid, resource, &limits))
132 ret = PyErr_SetFromErrno(PyExc_OSError);
133 else if (vc_get_rlimit(xid, resource, &limits)==-1)
134 ret = PyErr_SetFromErrno(PyExc_OSError);
136 ret = Py_BuildValue("L",limits.hard);
142 vserver_get_rlimit(PyObject *self, PyObject *args) {
143 struct vc_rlimit limits;
148 limits.min = VC_LIM_KEEP;
149 limits.soft = VC_LIM_KEEP;
150 limits.hard = VC_LIM_KEEP;
152 if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
155 if (vc_get_rlimit(xid, resource, &limits)==-1)
156 ret = PyErr_SetFromErrno(PyExc_OSError);
158 ret = Py_BuildValue("L",limits.hard);
167 vserver_setsched(PyObject *self, PyObject *args)
170 rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
173 if (!PyArg_ParseTuple(args, "IO", &ctx, &resources) ||
174 get_rspec(resources, &rspec))
177 /* ESRCH indicates that there are no processes in the context */
178 if (pl_setsched(ctx, rspec.cpu_share, rspec.cpu_sched_flags) &&
180 return PyErr_SetFromErrno(PyExc_OSError);
186 vserver_get_dlimit(PyObject *self, PyObject *args)
191 struct vcmd_ctx_dlimit_v0 data;
194 if (!PyArg_ParseTuple(args, "si", &path,&xid))
197 memset(&data, 0, sizeof(data));
200 r = vserver(VCMD_get_dlimit, xid, &data);
202 res = Py_BuildValue("(i,i,i,i,i)",
209 res = PyErr_SetFromErrno(PyExc_OSError);
217 vserver_set_dlimit(PyObject *self, PyObject *args)
221 struct vcmd_ctx_dlimit_base_v0 init;
222 struct vcmd_ctx_dlimit_v0 data;
224 memset(&data,0,sizeof(data));
225 if (!PyArg_ParseTuple(args, "siiiiii", &path,
237 memset(&init, 0, sizeof(init));
241 if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
242 vserver(VCMD_set_dlimit, xid, &data))
243 return PyErr_SetFromErrno(PyExc_OSError);
249 vserver_unset_dlimit(PyObject *self, PyObject *args)
253 struct vcmd_ctx_dlimit_base_v0 init;
255 if (!PyArg_ParseTuple(args, "si", &path, &xid))
258 memset(&init, 0, sizeof(init));
262 if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
263 return PyErr_SetFromErrno(PyExc_OSError);
269 vserver_killall(PyObject *self, PyObject *args)
274 if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
277 if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
278 return PyErr_SetFromErrno(PyExc_OSError);
283 static PyMethodDef methods[] = {
284 { "chcontext", vserver_chcontext, METH_VARARGS,
285 "chcontext to vserver with provided flags" },
286 { "setsched", vserver_setsched, METH_VARARGS,
287 "Change vserver scheduling attributes for given vserver context" },
288 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
289 "Set disk limits for given vserver context" },
290 { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
291 "Remove disk limits for given vserver context" },
292 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
293 "Get disk limits for given vserver context" },
294 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
295 "Set resource limits for given resource of a vserver context" },
296 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
297 "Get resource limits for given resource of a vserver context" },
298 { "killall", vserver_killall, METH_VARARGS,
299 "Send signal to all processes in vserver context" },
300 { NULL, NULL, 0, NULL }
304 initvserverimpl(void)
308 mod = Py_InitModule("vserverimpl", methods);
310 /* export the set of 'safe' capabilities */
311 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
313 /* export the default vserver directory */
314 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
316 /* export limit-related constants */
317 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
318 PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);