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>
51 #include "vserver-internal.h"
52 #include "sched_cmd.h"
59 vserver_create(PyObject *self, PyObject *args)
63 if (!PyArg_ParseTuple(args, "I", &ctx))
66 xid = vc_ctx_create(ctx);
67 if (xid == VC_NOCTX && errno != EEXIST) {
68 return PyErr_SetFromErrno(PyExc_OSError);
77 vserver_flags(PyObject *self, PyObject *args)
79 struct vc_ctx_caps caps;
80 struct vc_ctx_flags flags;
83 caps.ccaps = ~vc_get_insecureccaps();
85 caps.bcaps = ~vc_get_insecurebcaps();
88 flags.flagword = VC_VXF_INFO_LOCK;
89 flags.mask = VC_VXF_STATE_SETUP | VC_VXF_INFO_LOCK;
91 if (!PyArg_ParseTuple(args, "I", &ctx))
94 if (vc_set_ccaps(ctx, &caps) == -1) {
95 return PyErr_SetFromErrno(PyExc_OSError);
98 if (vc_set_cflags(ctx, &flags) == -1) {
99 return PyErr_SetFromErrno(PyExc_OSError);
108 vserver_enter(PyObject *self, PyObject *args)
111 if (!PyArg_ParseTuple(args, "I", &ctx))
114 xid = vc_ctx_create(ctx);
115 if (xid == VC_NOCTX && errno != EEXIST) {
116 return PyErr_SetFromErrno(PyExc_OSError);
119 if (xid == VC_NOCTX && vc_ctx_migrate(ctx) == -1) {
120 return PyErr_SetFromErrno(PyExc_OSError);
126 vserver_set_rlimit(PyObject *self, PyObject *args) {
127 struct vc_rlimit limits;
131 limits.min = VC_LIM_KEEP;
132 limits.soft = VC_LIM_KEEP;
133 limits.hard = VC_LIM_KEEP;
135 if (!PyArg_ParseTuple(args, "iiL", &xid, &resource, &limits.hard))
139 if (vc_set_rlimit(xid, resource, &limits))
140 ret = PyErr_SetFromErrno(PyExc_OSError);
141 else if (vc_get_rlimit(xid, resource, &limits)==-1)
142 ret = PyErr_SetFromErrno(PyExc_OSError);
144 ret = Py_BuildValue("L",limits.hard);
150 vserver_get_rlimit(PyObject *self, PyObject *args) {
151 struct vc_rlimit limits;
155 limits.min = VC_LIM_KEEP;
156 limits.soft = VC_LIM_KEEP;
157 limits.hard = VC_LIM_KEEP;
159 if (!PyArg_ParseTuple(args, "ii", &xid, &resource))
163 if (vc_get_rlimit(xid, resource, &limits)==-1)
164 ret = PyErr_SetFromErrno(PyExc_OSError);
166 ret = Py_BuildValue("L",limits.hard);
175 vserver_setsched(PyObject *self, PyObject *args)
178 struct vc_set_sched sched;
179 struct vc_ctx_flags flags;
180 unsigned cpuguaranteed = 0;
182 sched.set_mask = (VC_VXSM_FILL_RATE |
187 if (!PyArg_ParseTuple(args, "I|I|I|I|I|I|I", &xid,
196 flags.flagword = VC_VXF_INFO_LOCK;
197 flags.mask = VC_VXF_STATE_SETUP | VC_VXF_INFO_LOCK;
198 flags.flagword |= VC_VXF_SCHED_HARD;
199 flags.mask |= VC_VXF_SCHED_HARD;
200 #define VC_VXF_SCHED_SHARE 0x00000800ull
201 if (cpuguaranteed==0) {
202 flags.flagword |= VC_VXF_SCHED_SHARE;
203 flags.mask |= VC_VXF_SCHED_SHARE;
206 if (vc_set_cflags(xid, &flags) == -1) {
207 return PyErr_SetFromErrno(PyExc_OSError);
210 if (vc_set_sched(xid, &sched) == -1)
211 return PyErr_SetFromErrno(PyExc_OSError);
220 /* inode vserver commands */
221 #define VCMD_add_dlimit VC_CMD(DLIMIT, 1, 0)
222 #define VCMD_rem_dlimit VC_CMD(DLIMIT, 2, 0)
223 #define VCMD_set_dlimit VC_CMD(DLIMIT, 5, 0)
224 #define VCMD_get_dlimit VC_CMD(DLIMIT, 6, 0)
226 #define CDLIM_UNSET (0ULL)
227 #define CDLIM_INFINITY (~0ULL)
228 #define CDLIM_KEEP (~1ULL)
231 vserver_get_dlimit(PyObject *self, PyObject *args)
236 struct vcmd_ctx_dlimit_v0 data;
239 if (!PyArg_ParseTuple(args, "si", &path,&xid))
242 memset(&data, 0, sizeof(data));
245 r = vserver(VCMD_get_dlimit, xid, &data);
247 res = Py_BuildValue("(i,i,i,i,i)",
254 res = PyErr_SetFromErrno(PyExc_OSError);
262 vserver_set_dlimit(PyObject *self, PyObject *args)
266 struct vcmd_ctx_dlimit_base_v0 init;
267 struct vcmd_ctx_dlimit_v0 data;
270 memset(&data,0,sizeof(data));
271 if (!PyArg_ParseTuple(args, "siiiiii", &path,
283 memset(&init, 0, sizeof(init));
287 r = vserver(VCMD_rem_dlimit, xid, &init);
289 r = vserver(VCMD_add_dlimit, xid, &init);
291 r = vserver(VCMD_set_dlimit, xid, &data);
296 static PyMethodDef methods[] = {
297 { "create", vserver_create, METH_VARARGS,
298 "Create a new vserver context" },
299 { "flags", vserver_flags, METH_VARARGS,
300 "Set the default flags and caps" },
301 { "enter", vserver_flags, METH_VARARGS,
302 "Enter the vserver context" },
303 { "setsched", vserver_setsched, METH_VARARGS,
304 "Change vserver scheduling attributes for given vserver context" },
305 { "setdlimit", vserver_set_dlimit, METH_VARARGS,
306 "Set disk limits for given vserver context" },
307 { "getdlimit", vserver_get_dlimit, METH_VARARGS,
308 "Get disk limits for given vserver context" },
309 { "setrlimit", vserver_set_rlimit, METH_VARARGS,
310 "Set resource limits for given resource of a vserver context" },
311 { "getrlimit", vserver_get_rlimit, METH_VARARGS,
312 "Get resource limits for given resource of a vserver context" },
313 { NULL, NULL, 0, NULL }
317 initvserverimpl(void)
319 Py_InitModule("vserverimpl", methods);