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)
65 uint_least64_t bcaps = ~vc_get_insecurebcaps();
67 if (!PyArg_ParseTuple(args, "I", &ctx))
70 if ((ctx_is_new = pl_chcontext(ctx, bcaps, 0)) < 0)
71 return PyErr_SetFromErrno(PyExc_OSError);
73 return PyBool_FromLong(ctx_is_new);
77 vserver_setup_done(PyObject *self, PyObject *args)
81 if (!PyArg_ParseTuple(args, "I", &ctx))
84 if (pl_setup_done(ctx) < 0)
85 return PyErr_SetFromErrno(PyExc_OSError);
91 vserver_isrunning(PyObject *self, PyObject *args)
98 if (!PyArg_ParseTuple(args, "I", &ctx))
101 sprintf(fname,"/proc/virtual/%d", ctx);
103 if(stat(&fname[0],&statbuf)==0)
104 ret = PyBool_FromLong(1);
106 ret = PyBool_FromLong(0);
112 __vserver_get_rlimit(xid_t xid, int resource) {
113 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;
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;
167 getrlimit(lresource,&lim);
168 if (adjust_lim(&limits,&lim)) {
169 setrlimit(lresource, &lim);
174 if (vc_set_rlimit(xid, resource, &limits)==-1)
175 ret = PyErr_SetFromErrno(PyExc_OSError);
177 ret = __vserver_get_rlimit(xid, resource);
186 vserver_setsched(PyObject *self, PyObject *args)
190 uint32_t cpu_sched_flags = VC_VXF_SCHED_FLAGS;
192 if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
195 /* ESRCH indicates that there are no processes in the context */
196 if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
198 return PyErr_SetFromErrno(PyExc_OSError);
204 vserver_get_dlimit(PyObject *self, PyObject *args)
209 struct vcmd_ctx_dlimit_v0 data;
212 if (!PyArg_ParseTuple(args, "si", &path,&xid))
215 memset(&data, 0, sizeof(data));
218 r = vserver(VCMD_get_dlimit, xid, &data);
220 res = Py_BuildValue("(i,i,i,i,i)",
227 res = PyErr_SetFromErrno(PyExc_OSError);
235 vserver_set_dlimit(PyObject *self, PyObject *args)
239 struct vcmd_ctx_dlimit_base_v0 init;
240 struct vcmd_ctx_dlimit_v0 data;
242 memset(&data,0,sizeof(data));
243 if (!PyArg_ParseTuple(args, "siiiiii", &path,
255 memset(&init, 0, sizeof(init));
259 if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
260 vserver(VCMD_set_dlimit, xid, &data))
261 return PyErr_SetFromErrno(PyExc_OSError);
267 vserver_unset_dlimit(PyObject *self, PyObject *args)
271 struct vcmd_ctx_dlimit_base_v0 init;
273 if (!PyArg_ParseTuple(args, "si", &path, &xid))
276 memset(&init, 0, sizeof(init));
280 if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
281 return PyErr_SetFromErrno(PyExc_OSError);
287 vserver_killall(PyObject *self, PyObject *args)
292 if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
295 if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
296 return PyErr_SetFromErrno(PyExc_OSError);
301 static PyMethodDef methods[] = {
302 { "chcontext", vserver_chcontext, METH_VARARGS,
303 "chcontext to vserver with provided flags" },
304 { "setup_done", vserver_setup_done, METH_VARARGS,
305 "Release vserver setup lock" },
306 { "setsched", vserver_setsched, METH_VARARGS,
307 "Change vserver scheduling attributes for given vserver context" },
308 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
309 "Set disk limits for given vserver context" },
310 { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
311 "Remove disk limits for given vserver context" },
312 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
313 "Get disk limits for given vserver context" },
314 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
315 "Set resource limits for given resource of a vserver context" },
316 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
317 "Get resource limits for given resource of a vserver context" },
318 { "killall", vserver_killall, METH_VARARGS,
319 "Send signal to all processes in vserver context" },
320 { "isrunning", vserver_isrunning, METH_VARARGS,
321 "Check if vserver is running"},
322 { NULL, NULL, 0, NULL }
326 initvserverimpl(void)
330 mod = Py_InitModule("vserverimpl", methods);
332 /* export the set of 'safe' capabilities */
333 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
335 /* export the default vserver directory */
336 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
338 /* export limit-related constants */
339 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
340 PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);
341 PyModule_AddIntConstant(mod, "VC_LIM_KEEP", (int)VC_LIM_KEEP);
343 PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU);
344 PyModule_AddIntConstant(mod, "RLIMIT_RSS", (int)RLIMIT_RSS);
345 PyModule_AddIntConstant(mod, "RLIMIT_NPROC", (int)RLIMIT_NPROC);
346 PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", (int)RLIMIT_NOFILE);
347 PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", (int)RLIMIT_MEMLOCK);
348 PyModule_AddIntConstant(mod, "RLIMIT_AS", (int)RLIMIT_AS);
349 PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", (int)RLIMIT_LOCKS);
351 PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", (int)RLIMIT_SIGPENDING);
352 PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", (int)RLIMIT_MSGQUEUE);
354 PyModule_AddIntConstant(mod, "VLIMIT_NSOCK", (int)VLIMIT_NSOCK);
355 PyModule_AddIntConstant(mod, "VLIMIT_OPENFD", (int)VLIMIT_OPENFD);
356 PyModule_AddIntConstant(mod, "VLIMIT_ANON", (int)VLIMIT_ANON);
357 PyModule_AddIntConstant(mod, "VLIMIT_SHMEM", (int)VLIMIT_SHMEM);
359 /* scheduler flags */
360 PyModule_AddIntConstant(mod,
361 "VS_SCHED_CPU_GUARANTEED",
362 VS_SCHED_CPU_GUARANTEED);