d5f018dd6b9f9b1bda1ad964842626d4b3083d95
[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 #define NONE  ({ Py_INCREF(Py_None); Py_None; })
48
49 /*
50  * context create
51  */
52 static PyObject *
53 vserver_chcontext(PyObject *self, PyObject *args)
54 {
55   int  result;
56   xid_t  ctx;
57   uint32_t  flags = 0;
58   uint32_t  bcaps = ~vc_get_insecurebcaps();
59
60   if (!PyArg_ParseTuple(args, "I|K", &ctx, &flags))
61     return NULL;
62
63   if ((result = pl_chcontext(ctx, flags, bcaps)) < 0)
64     return PyErr_SetFromErrno(PyExc_OSError);
65
66   return PyBool_FromLong(result);
67 }
68
69 static PyObject *
70 vserver_setup_done(PyObject *self, PyObject *args)
71 {
72   xid_t  ctx;
73
74   if (!PyArg_ParseTuple(args, "I", &ctx))
75     return NULL;
76
77   if (pl_setup_done(ctx) < 0)
78     return PyErr_SetFromErrno(PyExc_OSError);
79
80   return NONE;
81 }
82
83 static PyObject *
84 vserver_set_rlimit(PyObject *self, PyObject *args) {
85         struct vc_rlimit limits;
86         xid_t xid;
87         int resource;
88         PyObject *ret;
89
90         limits.min = VC_LIM_KEEP;
91         limits.soft = VC_LIM_KEEP;
92         limits.hard = VC_LIM_KEEP;
93
94         if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
95                 return NULL;
96
97         if (vc_set_rlimit(xid, resource, &limits)) 
98                 ret = PyErr_SetFromErrno(PyExc_OSError);
99         else if (vc_get_rlimit(xid, resource, &limits)==-1)
100                 ret = PyErr_SetFromErrno(PyExc_OSError);
101         else
102                 ret = Py_BuildValue("L",limits.hard);
103
104         return ret;
105 }
106
107 static PyObject *
108 vserver_get_rlimit(PyObject *self, PyObject *args) {
109         struct vc_rlimit limits;
110         xid_t xid;
111         int resource;
112         PyObject *ret;
113
114         limits.min = VC_LIM_KEEP;
115         limits.soft = VC_LIM_KEEP;
116         limits.hard = VC_LIM_KEEP;
117
118         if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
119                 return NULL;
120
121         if (vc_get_rlimit(xid, resource, &limits)==-1)
122                 ret = PyErr_SetFromErrno(PyExc_OSError);
123         else
124                 ret = Py_BuildValue("L",limits.hard);
125
126         return ret;
127 }
128
129 /*
130  * setsched
131  */
132 static PyObject *
133 vserver_setsched(PyObject *self, PyObject *args)
134 {
135   xid_t  ctx;
136   uint32_t  cpu_share;
137   uint32_t  cpu_sched_flags = VC_VXF_SCHED_FLAGS;
138
139   if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
140     return NULL;
141
142   /* ESRCH indicates that there are no processes in the context */
143   if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
144       errno != ESRCH)
145     return PyErr_SetFromErrno(PyExc_OSError);
146
147   return NONE;
148 }
149
150 static PyObject *
151 vserver_get_dlimit(PyObject *self, PyObject *args)
152 {
153         PyObject *res;
154         char* path;
155         unsigned xid;
156         struct vcmd_ctx_dlimit_v0 data;
157         int r;
158
159         if (!PyArg_ParseTuple(args, "si", &path,&xid))
160                 return NULL;
161
162         memset(&data, 0, sizeof(data));
163         data.name = path;
164         data.flags = 0;
165         r = vserver(VCMD_get_dlimit, xid, &data);
166         if (r>=0) {
167                 res = Py_BuildValue("(i,i,i,i,i)",
168                                     data.space_used,
169                                     data.space_total,
170                                     data.inodes_used,
171                                     data.inodes_total,
172                                     data.reserved);
173         } else {
174                 res = PyErr_SetFromErrno(PyExc_OSError);
175         }
176
177         return res;
178 }
179
180
181 static PyObject *
182 vserver_set_dlimit(PyObject *self, PyObject *args)
183 {
184         char* path;
185         unsigned xid;
186         struct vcmd_ctx_dlimit_base_v0 init;
187         struct vcmd_ctx_dlimit_v0 data;
188
189         memset(&data,0,sizeof(data));
190         if (!PyArg_ParseTuple(args, "siiiiii", &path,
191                               &xid,
192                               &data.space_used,
193                               &data.space_total,
194                               &data.inodes_used,
195                               &data.inodes_total,
196                               &data.reserved))
197                 return NULL;
198
199         data.name = path;
200         data.flags = 0;
201
202         memset(&init, 0, sizeof(init));
203         init.name = path;
204         init.flags = 0;
205
206         if ((vserver(VCMD_add_dlimit, xid, &init) && errno != EEXIST) ||
207             vserver(VCMD_set_dlimit, xid, &data))
208           return PyErr_SetFromErrno(PyExc_OSError);
209
210         return NONE;    
211 }
212
213 static PyObject *
214 vserver_unset_dlimit(PyObject *self, PyObject *args)
215 {
216   char  *path;
217   unsigned  xid;
218   struct vcmd_ctx_dlimit_base_v0  init;
219
220   if (!PyArg_ParseTuple(args, "si", &path, &xid))
221     return NULL;
222
223   memset(&init, 0, sizeof(init));
224   init.name = path;
225   init.flags = 0;
226
227   if (vserver(VCMD_rem_dlimit, xid, &init) && errno != ESRCH)
228     return PyErr_SetFromErrno(PyExc_OSError);
229
230   return NONE;  
231 }
232
233 static PyObject *
234 vserver_killall(PyObject *self, PyObject *args)
235 {
236   xid_t  ctx;
237   int  sig;
238
239   if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
240     return NULL;
241
242   if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
243     return PyErr_SetFromErrno(PyExc_OSError);
244
245   return NONE;
246 }
247
248 static PyMethodDef  methods[] = {
249   { "chcontext", vserver_chcontext, METH_VARARGS,
250     "chcontext to vserver with provided flags" },
251   { "setup_done", vserver_setup_done, METH_VARARGS,
252     "Release vserver setup lock" },
253   { "setsched", vserver_setsched, METH_VARARGS,
254     "Change vserver scheduling attributes for given vserver context" },
255   { "setdlimit", vserver_set_dlimit, METH_VARARGS,
256     "Set disk limits for given vserver context" },
257   { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
258     "Remove disk limits for given vserver context" },
259   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
260     "Get disk limits for given vserver context" },
261   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
262     "Set resource limits for given resource of a vserver context" },
263   { "getrlimit", vserver_get_rlimit, METH_VARARGS,
264     "Get resource limits for given resource of a vserver context" },
265   { "killall", vserver_killall, METH_VARARGS,
266     "Send signal to all processes in vserver context" },
267   { NULL, NULL, 0, NULL }
268 };
269
270 PyMODINIT_FUNC
271 initvserverimpl(void)
272 {
273   PyObject  *mod;
274
275   mod = Py_InitModule("vserverimpl", methods);
276
277   /* export the set of 'safe' capabilities */
278   PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
279
280   /* export the default vserver directory */
281   PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
282
283   /* export limit-related constants */
284   PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)CDLIM_KEEP);
285   PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)CDLIM_INFINITY);
286
287   /* scheduler flags */
288   PyModule_AddIntConstant(mod,
289                           "VS_SCHED_CPU_GUARANTEED",
290                           VS_SCHED_CPU_GUARANTEED);
291 }