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.
38 #include <sys/resource.h>
39 #include <sys/types.h>
44 #include "pathconfig.h"
47 #include "planetlab.h"
48 #include "vserver-internal.h"
50 /* I don't like needing to define __KERNEL__ -- mef */
52 #include "kernel/limit.h"
55 #define NONE ({ Py_INCREF(Py_None); Py_None; })
61 vserver_chcontext(PyObject *self, PyObject *args)
66 uint32_t bcaps = ~vc_get_insecurebcaps();
68 if (!PyArg_ParseTuple(args, "I|K", &ctx, &flags))
71 if ((result = pl_chcontext(ctx, flags, bcaps, 0)) < 0)
72 return PyErr_SetFromErrno(PyExc_OSError);
74 return PyBool_FromLong(result);
78 vserver_setup_done(PyObject *self, PyObject *args)
82 if (!PyArg_ParseTuple(args, "I", &ctx))
85 if (pl_setup_done(ctx) < 0)
86 return PyErr_SetFromErrno(PyExc_OSError);
92 vserver_isrunning(PyObject *self, PyObject *args)
99 if (!PyArg_ParseTuple(args, "I", &ctx))
102 sprintf(fname,"/proc/virtual/%d", ctx);
104 if(stat(&fname[0],&statbuf)==0)
105 ret = Py_BuildValue("i",1);
107 ret = Py_BuildValue("i",0);
113 __vserver_get_rlimit(xid_t xid, int resource) {
114 struct vc_rlimit limits;
117 if (vc_get_rlimit(xid, resource, &limits)==-1)
118 ret = PyErr_SetFromErrno(PyExc_OSError);
120 ret = Py_BuildValue("LLL",limits.hard, limits.soft, limits.min);
126 vserver_get_rlimit(PyObject *self, PyObject *args) {
131 if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
134 ret = __vserver_get_rlimit(xid, resource);
140 vserver_set_rlimit(PyObject *self, PyObject *args) {
141 struct vc_rlimit limits;
142 struct rlimit olim, nlim;
144 int resource, lresource;
147 limits.min = VC_LIM_KEEP;
148 limits.soft = VC_LIM_KEEP;
149 limits.hard = VC_LIM_KEEP;
151 if (!PyArg_ParseTuple(args, "IiLLL", &xid, &resource, &limits.hard, &limits.soft, &limits.min))
154 lresource = resource;
159 goto do_vc_set_rlimit;
161 lresource = RLIMIT_NOFILE;
166 getrlimit(lresource,&olim);
167 if ((limits.min != VC_LIM_KEEP) && (limits.min > olim.rlim_cur)) {
168 nlim.rlim_cur = limits.min;
169 if (limits.min > olim.rlim_max) {
170 nlim.rlim_max = limits.min;
172 nlim.rlim_max = olim.rlim_max;
174 setrlimit(lresource, &nlim);
178 if (vc_set_rlimit(xid, resource, &limits))
179 ret = PyErr_SetFromErrno(PyExc_OSError);
181 ret = __vserver_get_rlimit(xid, resource);
190 vserver_setsched(PyObject *self, PyObject *args)
194 uint32_t cpu_sched_flags = VC_VXF_SCHED_FLAGS;
196 if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
199 /* ESRCH indicates that there are no processes in the context */
200 if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
202 return PyErr_SetFromErrno(PyExc_OSError);
208 vserver_get_dlimit(PyObject *self, PyObject *args)
213 struct vcmd_ctx_dlimit_v0 data;
216 if (!PyArg_ParseTuple(args, "si", &path,&xid))
219 memset(&data, 0, sizeof(data));
222 r = vserver(VCMD_get_dlimit, xid, &data);
224 res = Py_BuildValue("(i,i,i,i,i)",
231 res = PyErr_SetFromErrno(PyExc_OSError);
239 vserver_set_dlimit(PyObject *self, PyObject *args)
243 struct vcmd_ctx_dlimit_base_v0 init;
244 struct vcmd_ctx_dlimit_v0 data;
246 memset(&data,0,sizeof(data));
247 if (!PyArg_ParseTuple(args, "siiiiii", &path,
259 memset(&init, 0, sizeof(init));
263 if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
264 vserver(VCMD_set_dlimit, xid, &data))
265 return PyErr_SetFromErrno(PyExc_OSError);
271 vserver_unset_dlimit(PyObject *self, PyObject *args)
275 struct vcmd_ctx_dlimit_base_v0 init;
277 if (!PyArg_ParseTuple(args, "si", &path, &xid))
280 memset(&init, 0, sizeof(init));
284 if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
285 return PyErr_SetFromErrno(PyExc_OSError);
291 vserver_killall(PyObject *self, PyObject *args)
296 if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
299 if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
300 return PyErr_SetFromErrno(PyExc_OSError);
305 static PyMethodDef methods[] = {
306 { "chcontext", vserver_chcontext, METH_VARARGS,
307 "chcontext to vserver with provided flags" },
308 { "setup_done", vserver_setup_done, METH_VARARGS,
309 "Release vserver setup lock" },
310 { "setsched", vserver_setsched, METH_VARARGS,
311 "Change vserver scheduling attributes for given vserver context" },
312 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
313 "Set disk limits for given vserver context" },
314 { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
315 "Remove disk limits for given vserver context" },
316 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
317 "Get disk limits for given vserver context" },
318 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
319 "Set resource limits for given resource of a vserver context" },
320 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
321 "Get resource limits for given resource of a vserver context" },
322 { "killall", vserver_killall, METH_VARARGS,
323 "Send signal to all processes in vserver context" },
324 { "isrunning", vserver_isrunning, METH_VARARGS,
325 "Check if vserver is running"},
326 { NULL, NULL, 0, NULL }
330 initvserverimpl(void)
334 mod = Py_InitModule("vserverimpl", methods);
336 /* export the set of 'safe' capabilities */
337 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
339 /* export the default vserver directory */
340 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
342 /* export limit-related constants */
343 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
344 PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);
345 PyModule_AddIntConstant(mod, "VC_LIM_KEEP", (int)VC_LIM_KEEP);
347 PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU);
348 PyModule_AddIntConstant(mod, "RLIMIT_RSS", (int)RLIMIT_RSS);
349 PyModule_AddIntConstant(mod, "RLIMIT_NPROC", (int)RLIMIT_NPROC);
350 PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", (int)RLIMIT_NOFILE);
351 PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", (int)RLIMIT_MEMLOCK);
352 PyModule_AddIntConstant(mod, "RLIMIT_AS", (int)RLIMIT_AS);
353 PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", (int)RLIMIT_LOCKS);
355 PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", (int)RLIMIT_SIGPENDING);
356 PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", (int)RLIMIT_MSGQUEUE);
358 PyModule_AddIntConstant(mod, "VLIMIT_NSOCK", (int)VLIMIT_NSOCK);
359 PyModule_AddIntConstant(mod, "VLIMIT_OPENFD", (int)VLIMIT_OPENFD);
360 PyModule_AddIntConstant(mod, "VLIMIT_ANON", (int)VLIMIT_ANON);
361 PyModule_AddIntConstant(mod, "VLIMIT_SHMEM", (int)VLIMIT_SHMEM);
363 /* scheduler flags */
364 PyModule_AddIntConstant(mod,
365 "VS_SCHED_CPU_GUARANTEED",
366 VS_SCHED_CPU_GUARANTEED);