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 #define NONE ({ Py_INCREF(Py_None); Py_None; })
56 vserver_chcontext(PyObject *self, PyObject *args)
60 uint_least64_t bcaps = ~vc_get_insecurebcaps();
62 if (!PyArg_ParseTuple(args, "I", &ctx))
65 if ((ctx_is_new = pl_chcontext(ctx, bcaps, 0)) < 0)
66 return PyErr_SetFromErrno(PyExc_OSError);
68 return PyBool_FromLong(ctx_is_new);
72 vserver_setup_done(PyObject *self, PyObject *args)
76 if (!PyArg_ParseTuple(args, "I", &ctx))
79 if (pl_setup_done(ctx) < 0)
80 return PyErr_SetFromErrno(PyExc_OSError);
86 vserver_isrunning(PyObject *self, PyObject *args)
93 if (!PyArg_ParseTuple(args, "I", &ctx))
96 sprintf(fname,"/proc/virtual/%d", ctx);
98 if(stat(&fname[0],&statbuf)==0)
99 ret = PyBool_FromLong(1);
101 ret = PyBool_FromLong(0);
107 __vserver_get_rlimit(xid_t xid, int resource) {
108 struct vc_rlimit limits;
112 if (vc_get_rlimit(xid, resource, &limits)==-1)
113 ret = PyErr_SetFromErrno(PyExc_OSError);
115 ret = Py_BuildValue("LLL",limits.hard, limits.soft, limits.min);
121 vserver_get_rlimit(PyObject *self, PyObject *args) {
126 if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
129 ret = __vserver_get_rlimit(xid, resource);
135 vserver_set_rlimit(PyObject *self, PyObject *args) {
136 struct vc_rlimit limits;
139 int resource, lresource;
142 limits.min = VC_LIM_KEEP;
143 limits.soft = VC_LIM_KEEP;
144 limits.hard = VC_LIM_KEEP;
146 if (!PyArg_ParseTuple(args, "IiLLL", &xid, &resource, &limits.hard, &limits.soft, &limits.min))
149 lresource = resource;
151 case VC_VLIMIT_NSOCK:
153 case VC_VLIMIT_SHMEM:
154 goto do_vc_set_rlimit;
155 case VC_VLIMIT_OPENFD:
156 lresource = RLIMIT_NOFILE;
162 getrlimit(lresource,&lim);
163 if (adjust_lim(&limits,&lim)) {
164 setrlimit(lresource, &lim);
169 if (vc_set_rlimit(xid, resource, &limits)==-1)
170 ret = PyErr_SetFromErrno(PyExc_OSError);
172 ret = __vserver_get_rlimit(xid, resource);
181 vserver_setsched(PyObject *self, PyObject *args)
185 uint32_t cpu_sched_flags = VC_VXF_SCHED_FLAGS;
187 if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
190 /* ESRCH indicates that there are no processes in the context */
191 if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
193 return PyErr_SetFromErrno(PyExc_OSError);
199 vserver_get_dlimit(PyObject *self, PyObject *args)
204 struct vc_ctx_dlimit data;
207 if (!PyArg_ParseTuple(args, "si", &path,&xid))
210 memset(&data, 0, sizeof(data));
211 r = vc_get_dlimit(path, xid, 0, &data);
213 res = Py_BuildValue("(i,i,i,i,i)",
220 res = PyErr_SetFromErrno(PyExc_OSError);
228 vserver_set_dlimit(PyObject *self, PyObject *args)
232 struct vc_ctx_dlimit data;
234 memset(&data,0,sizeof(data));
235 if (!PyArg_ParseTuple(args, "siiiiii", &path,
244 if ((vc_add_dlimit(path, xid, 0) && errno != EEXIST) ||
245 vc_set_dlimit(path, xid, 0, &data))
246 return PyErr_SetFromErrno(PyExc_OSError);
252 vserver_unset_dlimit(PyObject *self, PyObject *args)
257 if (!PyArg_ParseTuple(args, "si", &path, &xid))
260 if (vc_rem_dlimit(path, xid, 0) && errno != ESRCH)
261 return PyErr_SetFromErrno(PyExc_OSError);
267 vserver_killall(PyObject *self, PyObject *args)
272 if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
275 if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
276 return PyErr_SetFromErrno(PyExc_OSError);
281 static PyMethodDef methods[] = {
282 { "chcontext", vserver_chcontext, METH_VARARGS,
283 "chcontext to vserver with provided flags" },
284 { "setup_done", vserver_setup_done, METH_VARARGS,
285 "Release vserver setup lock" },
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 { "isrunning", vserver_isrunning, METH_VARARGS,
301 "Check if vserver is running"},
302 { NULL, NULL, 0, NULL }
306 initvserverimpl(void)
310 mod = Py_InitModule("vserverimpl", methods);
312 /* export the set of 'safe' capabilities */
313 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
315 /* export the default vserver directory */
316 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
318 /* export limit-related constants */
319 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)VC_CDLIM_KEEP);
320 PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)VC_CDLIM_INFINITY);
321 PyModule_AddIntConstant(mod, "VC_LIM_KEEP", (int)VC_LIM_KEEP);
323 PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU);
324 PyModule_AddIntConstant(mod, "RLIMIT_RSS", (int)RLIMIT_RSS);
325 PyModule_AddIntConstant(mod, "RLIMIT_NPROC", (int)RLIMIT_NPROC);
326 PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", (int)RLIMIT_NOFILE);
327 PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", (int)RLIMIT_MEMLOCK);
328 PyModule_AddIntConstant(mod, "RLIMIT_AS", (int)RLIMIT_AS);
329 PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", (int)RLIMIT_LOCKS);
331 PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", (int)RLIMIT_SIGPENDING);
332 PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", (int)RLIMIT_MSGQUEUE);
334 PyModule_AddIntConstant(mod, "VLIMIT_NSOCK", (int)VC_VLIMIT_NSOCK);
335 PyModule_AddIntConstant(mod, "VLIMIT_OPENFD", (int)VC_VLIMIT_OPENFD);
336 PyModule_AddIntConstant(mod, "VLIMIT_ANON", (int)VC_VLIMIT_ANON);
337 PyModule_AddIntConstant(mod, "VLIMIT_SHMEM", (int)VC_VLIMIT_SHMEM);
339 /* scheduler flags */
340 PyModule_AddIntConstant(mod,
341 "VS_SCHED_CPU_GUARANTEED",
342 VS_SCHED_CPU_GUARANTEED);