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.
43 #include <sys/ioctl.h>
44 #include <sys/types.h>
50 #include "pathconfig.h"
52 #include "vserver-internal.h"
53 #include "sched_cmd.h"
61 vserver_chcontext(PyObject *self, PyObject *args)
64 struct vc_ctx_caps caps;
65 struct vc_ctx_flags flags;
69 v = VC_VXF_STATE_SETUP;
70 if (!PyArg_ParseTuple(args, "I|K", &ctx, &v))
73 caps.ccaps = ~vc_get_insecureccaps();
75 caps.bcaps = ~vc_get_insecurebcaps();
79 if (vc_get_vx_info(ctx,&vc) != 0) {
80 xid = vc_ctx_create(ctx);
81 if (xid == VC_NOCTX && errno != EEXIST)
82 return PyErr_SetFromErrno(PyExc_OSError);
85 flags.mask = flags.flagword = v;
86 if (vc_set_cflags(ctx, &flags) == -1)
87 return PyErr_SetFromErrno(PyExc_OSError);
89 if (xid == VC_NOCTX && vc_ctx_migrate(ctx) == -1)
90 return PyErr_SetFromErrno(PyExc_OSError);
93 printf("vserver_create xid = %d(%d)\n",xid,ctx);
99 vserver_set_rlimit(PyObject *self, PyObject *args) {
100 struct vc_rlimit limits;
105 limits.min = VC_LIM_KEEP;
106 limits.soft = VC_LIM_KEEP;
107 limits.hard = VC_LIM_KEEP;
109 if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
113 if (vc_set_rlimit(xid, resource, &limits))
114 ret = PyErr_SetFromErrno(PyExc_OSError);
115 else if (vc_get_rlimit(xid, resource, &limits)==-1)
116 ret = PyErr_SetFromErrno(PyExc_OSError);
118 ret = Py_BuildValue("L",limits.hard);
124 vserver_get_rlimit(PyObject *self, PyObject *args) {
125 struct vc_rlimit limits;
130 limits.min = VC_LIM_KEEP;
131 limits.soft = VC_LIM_KEEP;
132 limits.hard = VC_LIM_KEEP;
134 if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
138 if (vc_get_rlimit(xid, resource, &limits)==-1)
139 ret = PyErr_SetFromErrno(PyExc_OSError);
141 ret = Py_BuildValue("L",limits.hard);
150 vserver_setsched(PyObject *self, PyObject *args)
153 struct vc_set_sched sched;
154 struct vc_ctx_flags flags;
155 unsigned cpuguaranteed = 0;
157 sched.set_mask = (VC_VXSM_FILL_RATE |
162 if (!PyArg_ParseTuple(args, "I|I|I|I|I|I|I", &xid,
171 flags.flagword = VC_VXF_SCHED_HARD;
172 flags.mask |= VC_VXF_SCHED_HARD;
173 #define VC_VXF_SCHED_SHARE 0x00000800ull
174 if (cpuguaranteed==0) {
175 flags.flagword |= VC_VXF_SCHED_SHARE;
176 flags.mask |= VC_VXF_SCHED_SHARE;
179 if (vc_set_cflags(xid, &flags) == -1)
180 return PyErr_SetFromErrno(PyExc_OSError);
182 if (vc_set_sched(xid, &sched) == -1)
183 return PyErr_SetFromErrno(PyExc_OSError);
193 vserver_get_dlimit(PyObject *self, PyObject *args)
198 struct vcmd_ctx_dlimit_v0 data;
201 if (!PyArg_ParseTuple(args, "si", &path,&xid))
204 memset(&data, 0, sizeof(data));
207 r = vserver(VCMD_get_dlimit, xid, &data);
209 res = Py_BuildValue("(i,i,i,i,i)",
216 res = PyErr_SetFromErrno(PyExc_OSError);
224 vserver_set_dlimit(PyObject *self, PyObject *args)
228 struct vcmd_ctx_dlimit_base_v0 init;
229 struct vcmd_ctx_dlimit_v0 data;
231 memset(&data,0,sizeof(data));
232 if (!PyArg_ParseTuple(args, "siiiiii", &path,
244 memset(&init, 0, sizeof(init));
248 if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
249 vserver(VCMD_set_dlimit, xid, &data))
250 return PyErr_SetFromErrno(PyExc_OSError);
255 static PyMethodDef methods[] = {
256 { "chcontext", vserver_chcontext, METH_VARARGS,
257 "chcontext to vserver with provided flags" },
258 { "setsched", vserver_setsched, METH_VARARGS,
259 "Change vserver scheduling attributes for given vserver context" },
260 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
261 "Set disk limits for given vserver context" },
262 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
263 "Get disk limits for given vserver context" },
264 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
265 "Set resource limits for given resource of a vserver context" },
266 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
267 "Get resource limits for given resource of a vserver context" },
268 { NULL, NULL, 0, NULL }
272 initvserverimpl(void)
276 mod = Py_InitModule("vserverimpl", methods);
278 /* export the set of 'safe' capabilities */
279 PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
281 /* export the default vserver directory */
282 PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
284 /* export a constant to indicate unchanged disk quota values */
285 PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);