Read resource values from a resources dictionary
[util-vserver.git] / python / vserverimpl.c
1 /* Copyright 2005 Princeton University
2
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met: 
6
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9       
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.
14       
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.
18       
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. 
31
32 */
33
34 #include <Python.h>
35
36 #include <errno.h>
37 #include <stdint.h>
38 #include <unistd.h>
39
40 #include "config.h"
41 #include "pathconfig.h"
42 #include "planetlab.h"
43 #include "virtual.h"
44 #include "vserver.h"
45 #include "vserver-internal.h"
46
47 /*
48  * context create
49  */
50 static PyObject *
51 vserver_chcontext(PyObject *self, PyObject *args)
52 {
53   xid_t  ctx;
54   uint32_t  flags = 0;
55   uint32_t  bcaps = ~vc_get_insecurebcaps();
56   rspec_t  rspec = { 32, VC_VXF_SCHED_FLAGS, -1, -1 };
57   PyObject  *resources;
58   PyObject  *cpu_share;
59
60   if (!PyArg_ParseTuple(args, "IO!|K", &ctx, &PyDict_Type, &resources, &flags))
61     return NULL;
62   if ((cpu_share = PyMapping_GetItemString(resources, "nm_cpu_share")) &&
63       (cpu_share = PyNumber_Int(cpu_share)))
64     rspec.cpu_share = PyInt_AsLong(cpu_share);
65
66   if (pl_chcontext(ctx, flags, bcaps, &rspec))
67     PyErr_SetFromErrno(PyExc_OSError);
68
69   return Py_None;
70 }
71
72 static PyObject *
73 vserver_set_rlimit(PyObject *self, PyObject *args) {
74         struct vc_rlimit limits;
75         xid_t xid;
76         int resource;
77         PyObject *ret;
78
79         limits.min = VC_LIM_KEEP;
80         limits.soft = VC_LIM_KEEP;
81         limits.hard = VC_LIM_KEEP;
82
83         if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
84                 return NULL;
85
86         ret = Py_None;
87         if (vc_set_rlimit(xid, resource, &limits)) 
88                 ret = PyErr_SetFromErrno(PyExc_OSError);
89         else if (vc_get_rlimit(xid, resource, &limits)==-1)
90                 ret = PyErr_SetFromErrno(PyExc_OSError);
91         else
92                 ret = Py_BuildValue("L",limits.hard);
93
94         return ret;
95 }
96
97 static PyObject *
98 vserver_get_rlimit(PyObject *self, PyObject *args) {
99         struct vc_rlimit limits;
100         xid_t xid;
101         int resource;
102         PyObject *ret;
103
104         limits.min = VC_LIM_KEEP;
105         limits.soft = VC_LIM_KEEP;
106         limits.hard = VC_LIM_KEEP;
107
108         if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
109                 return NULL;
110
111         ret = Py_None;
112         if (vc_get_rlimit(xid, resource, &limits)==-1)
113                 ret = PyErr_SetFromErrno(PyExc_OSError);
114         else
115                 ret = Py_BuildValue("L",limits.hard);
116
117         return ret;
118 }
119
120 #if 0
121 /*
122  * setsched
123  */
124 static PyObject *
125 vserver_setsched(PyObject *self, PyObject *args)
126 {
127   xid_t  xid;
128   struct vc_set_sched sched;
129   struct vc_ctx_flags flags;
130   unsigned cpuguaranteed = 0;
131
132   sched.set_mask = (VC_VXSM_FILL_RATE | 
133                     VC_VXSM_INTERVAL | 
134                     VC_VXSM_TOKENS_MIN | 
135                     VC_VXSM_TOKENS_MAX);
136
137   if (!PyArg_ParseTuple(args, "I|I|I|I|I|I|I", &xid, 
138                         &sched.fill_rate,
139                         &sched.interval,
140                         &sched.tokens,
141                         &sched.tokens_min,
142                         &sched.tokens_max,
143                         &cpuguaranteed))
144     return NULL;
145
146   flags.flagword = VC_VXF_SCHED_HARD;
147   flags.mask |= VC_VXF_SCHED_HARD;
148 #define VC_VXF_SCHED_SHARE       0x00000800ull
149   if (cpuguaranteed==0) {
150           flags.flagword |= VC_VXF_SCHED_SHARE;
151           flags.mask |= VC_VXF_SCHED_SHARE;
152   }
153
154   if (vc_set_cflags(xid, &flags) == -1)
155           return PyErr_SetFromErrno(PyExc_OSError);
156
157   if (vc_set_sched(xid, &sched) == -1)
158           return PyErr_SetFromErrno(PyExc_OSError);
159
160   return Py_None;
161 }
162
163 /*
164  * setsched
165  */
166 #endif
167
168 static PyObject *
169 vserver_get_dlimit(PyObject *self, PyObject *args)
170 {
171         PyObject *res;
172         char* path;
173         unsigned xid;
174         struct vcmd_ctx_dlimit_v0 data;
175         int r;
176
177         if (!PyArg_ParseTuple(args, "si", &path,&xid))
178                 return NULL;
179
180         memset(&data, 0, sizeof(data));
181         data.name = path;
182         data.flags = 0;
183         r = vserver(VCMD_get_dlimit, xid, &data);
184         if (r>=0) {
185                 res = Py_BuildValue("(i,i,i,i,i)",
186                                     data.space_used,
187                                     data.space_total,
188                                     data.inodes_used,
189                                     data.inodes_total,
190                                     data.reserved);
191         } else {
192                 res = PyErr_SetFromErrno(PyExc_OSError);
193         }
194
195         return res;
196 }
197
198
199 static PyObject *
200 vserver_set_dlimit(PyObject *self, PyObject *args)
201 {
202         char* path;
203         unsigned xid;
204         struct vcmd_ctx_dlimit_base_v0 init;
205         struct vcmd_ctx_dlimit_v0 data;
206
207         memset(&data,0,sizeof(data));
208         if (!PyArg_ParseTuple(args, "siiiiii", &path,
209                               &xid,
210                               &data.space_used,
211                               &data.space_total,
212                               &data.inodes_used,
213                               &data.inodes_total,
214                               &data.reserved))
215                 return NULL;
216
217         data.name = path;
218         data.flags = 0;
219
220         memset(&init, 0, sizeof(init));
221         init.name = path;
222         init.flags = 0;
223
224         if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
225             vserver(VCMD_set_dlimit, xid, &data))
226           return PyErr_SetFromErrno(PyExc_OSError);
227
228         return Py_None; 
229 }
230
231 static PyMethodDef  methods[] = {
232   { "chcontext", vserver_chcontext, METH_VARARGS,
233     "chcontext to vserver with provided flags" },
234 #if 0
235   { "setsched", vserver_setsched, METH_VARARGS,
236     "Change vserver scheduling attributes for given vserver context" },
237 #endif
238   { "setdlimit", vserver_set_dlimit, METH_VARARGS,
239     "Set disk limits for given vserver context" },
240   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
241     "Get disk limits for given vserver context" },
242   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
243     "Set resource limits for given resource of a vserver context" },
244   { "getrlimit", vserver_get_rlimit, METH_VARARGS,
245     "Get resource limits for given resource of a vserver context" },
246   { NULL, NULL, 0, NULL }
247 };
248
249 PyMODINIT_FUNC
250 initvserverimpl(void)
251 {
252   PyObject  *mod;
253
254   mod = Py_InitModule("vserverimpl", methods);
255
256   /* export the set of 'safe' capabilities */
257   PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
258
259   /* export the default vserver directory */
260   PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
261
262   /* export limit-related constants */
263   PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
264   PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);
265 }