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"
51 vserver_chcontext(PyObject *self, PyObject *args)
55 uint32_t bcaps = ~vc_get_insecurebcaps();
56 rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
60 if (!PyArg_ParseTuple(args, "IO|K", &ctx, &resources, &flags))
62 if (!PyMapping_Check(resources))
64 PyErr_SetString(PyExc_TypeError, "invalid resources object");
67 if ((cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")) &&
68 (cpu_share = PyNumber_Int(cpu_share)))
69 rspec.cpu_share = PyInt_AsLong(cpu_share);
71 if (pl_chcontext(ctx, flags, bcaps, &rspec))
72 PyErr_SetFromErrno(PyExc_OSError);
78 vserver_set_rlimit(PyObject *self, PyObject *args) {
79 struct vc_rlimit limits;
84 limits.min = VC_LIM_KEEP;
85 limits.soft = VC_LIM_KEEP;
86 limits.hard = VC_LIM_KEEP;
88 if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
92 if (vc_set_rlimit(xid, resource, &limits))
93 ret = PyErr_SetFromErrno(PyExc_OSError);
94 else if (vc_get_rlimit(xid, resource, &limits)==-1)
95 ret = PyErr_SetFromErrno(PyExc_OSError);
97 ret = Py_BuildValue("L",limits.hard);
103 vserver_get_rlimit(PyObject *self, PyObject *args) {
104 struct vc_rlimit limits;
109 limits.min = VC_LIM_KEEP;
110 limits.soft = VC_LIM_KEEP;
111 limits.hard = VC_LIM_KEEP;
113 if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
117 if (vc_get_rlimit(xid, resource, &limits)==-1)
118 ret = PyErr_SetFromErrno(PyExc_OSError);
120 ret = Py_BuildValue("L",limits.hard);
129 vserver_setsched(PyObject *self, PyObject *args)
132 rspec_t rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
136 if (!PyArg_ParseTuple(args, "IO", &ctx, &resources))
138 if (!PyMapping_Check(resources))
140 PyErr_SetString(PyExc_TypeError, "invalid resources object");
143 if ((cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")) &&
144 (cpu_share = PyNumber_Int(cpu_share)))
145 rspec.cpu_share = PyInt_AsLong(cpu_share);
147 if (pl_setsched(ctx, rspec.cpu_share, rspec.cpu_sched_flags))
148 PyErr_SetFromErrno(PyExc_OSError);
154 vserver_get_dlimit(PyObject *self, PyObject *args)
159 struct vcmd_ctx_dlimit_v0 data;
162 if (!PyArg_ParseTuple(args, "si", &path,&xid))
165 memset(&data, 0, sizeof(data));
168 r = vserver(VCMD_get_dlimit, xid, &data);
170 res = Py_BuildValue("(i,i,i,i,i)",
177 res = PyErr_SetFromErrno(PyExc_OSError);
185 vserver_set_dlimit(PyObject *self, PyObject *args)
189 struct vcmd_ctx_dlimit_base_v0 init;
190 struct vcmd_ctx_dlimit_v0 data;
192 memset(&data,0,sizeof(data));
193 if (!PyArg_ParseTuple(args, "siiiiii", &path,
205 memset(&init, 0, sizeof(init));
209 if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
210 vserver(VCMD_set_dlimit, xid, &data))
211 return PyErr_SetFromErrno(PyExc_OSError);
217 vserver_unset_dlimit(PyObject *self, PyObject *args)
220 struct vcmd_ctx_dlimit_base_v0 init;
222 if (!PyArg_ParseTuple(args, "si", &path, &xid))
225 memset(&init, 0, sizeof(init));
229 if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
230 return PyErr_SetFromErrno(PyExc_OSError);
235 static PyMethodDef methods[] = {
236 { "chcontext", vserver_chcontext, METH_VARARGS,
237 "chcontext to vserver with provided flags" },
238 { "setsched", vserver_setsched, METH_VARARGS,
239 "Change vserver scheduling attributes for given vserver context" },
240 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
241 "Set disk limits for given vserver context" },
242 { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
243 "Remove disk limits for given vserver context" },
244 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
245 "Get disk limits for given vserver context" },
246 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
247 "Set resource limits for given resource of a vserver context" },
248 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
249 "Get resource limits for given resource of a vserver context" },
250 { NULL, NULL, 0, NULL }
254 initvserverimpl(void)
258 mod = Py_InitModule("vserverimpl", methods);
260 /* export the set of 'safe' capabilities */
261 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
263 /* export the default vserver directory */
264 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
266 /* export limit-related constants */
267 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
268 PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);