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"
48 get_rspec(PyObject *resources, rspec_t *rspec)
52 if (!PyMapping_Check(resources))
54 PyErr_SetString(PyExc_TypeError, "invalid rspec");
57 if ((cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")))
59 if (PyInt_Check(cpu_share))
61 rspec->cpu_share = PyInt_AS_LONG(cpu_share);
64 PyErr_SetString(PyExc_TypeError, "nm_cpu_share not an integer");
74 vserver_chcontext(PyObject *self, PyObject *args)
78 uint32_t bcaps = ~vc_get_insecurebcaps();
79 rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
82 if (!PyArg_ParseTuple(args, "IO|K", &ctx, &resources, &flags) ||
83 get_rspec(resources, &rspec))
86 if (pl_chcontext(ctx, flags, bcaps, &rspec))
87 return PyErr_SetFromErrno(PyExc_OSError);
93 vserver_set_rlimit(PyObject *self, PyObject *args) {
94 struct vc_rlimit limits;
99 limits.min = VC_LIM_KEEP;
100 limits.soft = VC_LIM_KEEP;
101 limits.hard = VC_LIM_KEEP;
103 if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
107 if (vc_set_rlimit(xid, resource, &limits))
108 ret = PyErr_SetFromErrno(PyExc_OSError);
109 else if (vc_get_rlimit(xid, resource, &limits)==-1)
110 ret = PyErr_SetFromErrno(PyExc_OSError);
112 ret = Py_BuildValue("L",limits.hard);
118 vserver_get_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, "Ii", &xid, &resource))
132 if (vc_get_rlimit(xid, resource, &limits)==-1)
133 ret = PyErr_SetFromErrno(PyExc_OSError);
135 ret = Py_BuildValue("L",limits.hard);
144 vserver_setsched(PyObject *self, PyObject *args)
147 rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
150 if (!PyArg_ParseTuple(args, "IO", &ctx, &resources) ||
151 get_rspec(resources, &rspec))
154 if (pl_setsched(ctx, rspec.cpu_share, rspec.cpu_sched_flags))
155 return PyErr_SetFromErrno(PyExc_OSError);
161 vserver_get_dlimit(PyObject *self, PyObject *args)
166 struct vcmd_ctx_dlimit_v0 data;
169 if (!PyArg_ParseTuple(args, "si", &path,&xid))
172 memset(&data, 0, sizeof(data));
175 r = vserver(VCMD_get_dlimit, xid, &data);
177 res = Py_BuildValue("(i,i,i,i,i)",
184 res = PyErr_SetFromErrno(PyExc_OSError);
192 vserver_set_dlimit(PyObject *self, PyObject *args)
196 struct vcmd_ctx_dlimit_base_v0 init;
197 struct vcmd_ctx_dlimit_v0 data;
199 memset(&data,0,sizeof(data));
200 if (!PyArg_ParseTuple(args, "siiiiii", &path,
212 memset(&init, 0, sizeof(init));
216 if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
217 vserver(VCMD_set_dlimit, xid, &data))
218 return PyErr_SetFromErrno(PyExc_OSError);
224 vserver_unset_dlimit(PyObject *self, PyObject *args)
228 struct vcmd_ctx_dlimit_base_v0 init;
230 if (!PyArg_ParseTuple(args, "si", &path, &xid))
233 memset(&init, 0, sizeof(init));
237 if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
238 return PyErr_SetFromErrno(PyExc_OSError);
244 vserver_killall(PyObject *self, PyObject *args)
249 if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
252 if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
253 return PyErr_SetFromErrno(PyExc_OSError);
258 static PyMethodDef methods[] = {
259 { "chcontext", vserver_chcontext, METH_VARARGS,
260 "chcontext to vserver with provided flags" },
261 { "setsched", vserver_setsched, METH_VARARGS,
262 "Change vserver scheduling attributes for given vserver context" },
263 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
264 "Set disk limits for given vserver context" },
265 { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
266 "Remove disk limits for given vserver context" },
267 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
268 "Get disk limits for given vserver context" },
269 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
270 "Set resource limits for given resource of a vserver context" },
271 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
272 "Get resource limits for given resource of a vserver context" },
273 { "killall", vserver_killall, METH_VARARGS,
274 "Send signal to all processes in vserver context" },
275 { NULL, NULL, 0, NULL }
279 initvserverimpl(void)
283 mod = Py_InitModule("vserverimpl", methods);
285 /* export the set of 'safe' capabilities */
286 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
288 /* export the default vserver directory */
289 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
291 /* export limit-related constants */
292 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
293 PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);