071a603f9ddb47e9765197e81af4439498382f22
[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 <sys/resource.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42
43 #include "config.h"
44 #include "pathconfig.h"
45 #include "virtual.h"
46 #include "vserver.h"
47 #include "planetlab.h"
48 #include "vserver-internal.h"
49
50 #define NONE  ({ Py_INCREF(Py_None); Py_None; })
51
52 /*
53  * context create
54  */
55 static PyObject *
56 vserver_chcontext(PyObject *self, PyObject *args)
57 {
58   int  ctx_is_new;
59   xid_t  ctx;
60   uint_least64_t bcaps = 0;
61
62   if (!PyArg_ParseTuple(args, "I|K", &ctx, &bcaps))
63     return NULL;
64   bcaps |= ~vc_get_insecurebcaps();
65
66   if ((ctx_is_new = pl_chcontext(ctx, bcaps, 0)) < 0)
67     return PyErr_SetFromErrno(PyExc_OSError);
68
69   return PyBool_FromLong(ctx_is_new);
70 }
71
72 static PyObject *
73 vserver_setup_done(PyObject *self, PyObject *args)
74 {
75   xid_t  ctx;
76
77   if (!PyArg_ParseTuple(args, "I", &ctx))
78     return NULL;
79
80   if (pl_setup_done(ctx) < 0)
81     return PyErr_SetFromErrno(PyExc_OSError);
82
83   return NONE;
84 }
85
86 static PyObject *
87 vserver_isrunning(PyObject *self, PyObject *args)
88 {
89   xid_t  ctx;
90   PyObject *ret;
91   struct stat statbuf;
92   char fname[64];
93
94   if (!PyArg_ParseTuple(args, "I", &ctx))
95     return NULL;
96
97   sprintf(fname,"/proc/virtual/%d", ctx);
98
99   if(stat(&fname[0],&statbuf)==0)
100     ret = PyBool_FromLong(1);
101   else
102     ret = PyBool_FromLong(0);
103
104   return ret;
105 }
106
107 static PyObject *
108 __vserver_get_rlimit(xid_t xid, int resource) {
109   struct vc_rlimit limits;
110   PyObject *ret;
111
112   errno = 0;
113   if (vc_get_rlimit(xid, resource, &limits)==-1)
114     ret = PyErr_SetFromErrno(PyExc_OSError);
115   else
116     ret = Py_BuildValue("LLL",limits.hard, limits.soft, limits.min);
117
118   return ret;
119 }
120
121 static PyObject *
122 vserver_get_rlimit(PyObject *self, PyObject *args) {
123   xid_t xid;
124   int resource;
125   PyObject *ret;
126
127   if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
128     ret = NULL;
129   else
130     ret = __vserver_get_rlimit(xid, resource);
131
132   return ret;
133 }
134
135 static PyObject *
136 vserver_set_rlimit(PyObject *self, PyObject *args) {
137   struct vc_rlimit limits;
138   struct rlimit lim;
139   xid_t xid;
140   int resource, lresource;
141   PyObject *ret;
142
143   limits.min = VC_LIM_KEEP;
144   limits.soft = VC_LIM_KEEP;
145   limits.hard = VC_LIM_KEEP;
146
147   if (!PyArg_ParseTuple(args, "IiLLL", &xid, &resource, &limits.hard, &limits.soft, &limits.min))
148     return NULL;
149
150   lresource = resource;
151   switch (resource) {
152   case VC_VLIMIT_NSOCK:
153   case VC_VLIMIT_ANON:
154   case VC_VLIMIT_SHMEM:
155     goto do_vc_set_rlimit;
156   case VC_VLIMIT_OPENFD:
157     lresource = RLIMIT_NOFILE;
158     break;
159   default:
160     break;
161   }
162
163   getrlimit(lresource,&lim);
164   if (adjust_lim(&limits,&lim)) {
165     setrlimit(lresource, &lim);
166   }
167
168  do_vc_set_rlimit:
169   errno = 0;
170   if (vc_set_rlimit(xid, resource, &limits)==-1) 
171     ret = PyErr_SetFromErrno(PyExc_OSError);
172   else
173     ret = __vserver_get_rlimit(xid, resource);
174
175   return ret;
176 }
177
178 /*
179  * setsched
180  */
181 static PyObject *
182 vserver_setsched(PyObject *self, PyObject *args)
183 {
184   xid_t  ctx;
185   uint32_t  cpu_share;
186   uint32_t  cpu_sched_flags = VC_VXF_SCHED_FLAGS;
187
188   if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
189     return NULL;
190
191   /* ESRCH indicates that there are no processes in the context */
192   if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
193       errno != ESRCH)
194     return PyErr_SetFromErrno(PyExc_OSError);
195
196   return NONE;
197 }
198
199 static PyObject *
200 vserver_get_dlimit(PyObject *self, PyObject *args)
201 {
202   PyObject *res;
203   char* path;
204   unsigned xid;
205   struct vc_ctx_dlimit data;
206   int r;
207
208   if (!PyArg_ParseTuple(args, "si", &path,&xid))
209     return NULL;
210
211   memset(&data, 0, sizeof(data));
212   r = vc_get_dlimit(path, xid, 0, &data);
213   if (r>=0) {
214     res = Py_BuildValue("(i,i,i,i,i)",
215                         data.space_used,
216                         data.space_total,
217                         data.inodes_used,
218                         data.inodes_total,
219                         data.reserved);
220   } else {
221     res = PyErr_SetFromErrno(PyExc_OSError);
222   }
223
224   return res;
225 }
226
227
228 static PyObject *
229 vserver_set_dlimit(PyObject *self, PyObject *args)
230 {
231   char* path;
232   unsigned xid;
233   struct vc_ctx_dlimit data;
234
235   memset(&data,0,sizeof(data));
236   if (!PyArg_ParseTuple(args, "siiiiii", &path,
237                         &xid,
238                         &data.space_used,
239                         &data.space_total,
240                         &data.inodes_used,
241                         &data.inodes_total,
242                         &data.reserved))
243     return NULL;
244
245   if ((vc_add_dlimit(path, xid, 0) && errno != EEXIST) ||
246       vc_set_dlimit(path, xid, 0, &data))
247     return PyErr_SetFromErrno(PyExc_OSError);
248
249   return NONE;  
250 }
251
252 static PyObject *
253 vserver_unset_dlimit(PyObject *self, PyObject *args)
254 {
255   char  *path;
256   unsigned  xid;
257
258   if (!PyArg_ParseTuple(args, "si", &path, &xid))
259     return NULL;
260
261   if (vc_rem_dlimit(path, xid, 0) && errno != ESRCH)
262     return PyErr_SetFromErrno(PyExc_OSError);
263
264   return NONE;  
265 }
266
267 static PyObject *
268 vserver_killall(PyObject *self, PyObject *args)
269 {
270   xid_t ctx;
271   int   sig;
272   struct vc_ctx_flags cflags = {
273     .flagword = 0,
274     .mask = VC_VXF_PERSISTENT
275   };
276   struct vc_net_flags nflags = {
277     .flagword = 0,
278     .mask = VC_NXF_PERSISTENT
279   };
280
281   if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
282     return NULL;
283
284   if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
285     return PyErr_SetFromErrno(PyExc_OSError);
286
287   if (vc_set_cflags(ctx, &cflags) && errno != ESRCH)
288     return PyErr_SetFromErrno(PyExc_OSError);
289
290   if (vc_set_nflags(ctx, &nflags) && errno != ESRCH)
291     return PyErr_SetFromErrno(PyExc_OSError);
292
293   return NONE;
294 }
295
296 static PyObject *
297 vserver_set_bcaps(PyObject *self, PyObject *args)
298 {
299   xid_t ctx;
300   struct vc_ctx_caps caps;
301
302   if (!PyArg_ParseTuple(args, "IK", &ctx, &caps.bcaps))
303     return NULL;
304
305   caps.bmask = vc_get_insecurebcaps();
306   caps.cmask = caps.ccaps = 0;
307   if (vc_set_ccaps(ctx, &caps) == -1 && errno != ESRCH)
308     return PyErr_SetFromErrno(PyExc_OSError);
309
310   return NONE;
311 }
312
313 static PyObject *
314 vserver_text2bcaps(PyObject *self, PyObject *args)
315 {
316   struct vc_ctx_caps caps = { .bcaps = 0 };
317   const char *list;
318   int len;
319   struct vc_err_listparser err;
320
321   if (!PyArg_ParseTuple(args, "s#", &list, &len))
322     return NULL;
323
324   vc_list2bcap(list, len, &err, &caps);
325
326   return Py_BuildValue("K", caps.bcaps);
327 }
328
329 static PyObject *
330 vserver_get_bcaps(PyObject *self, PyObject *args)
331 {
332   xid_t ctx;
333   struct vc_ctx_caps caps;
334
335   if (!PyArg_ParseTuple(args, "I", &ctx))
336     return NULL;
337
338   if (vc_get_ccaps(ctx, &caps) == -1) {
339     if (errno != -ESRCH)
340       return PyErr_SetFromErrno(PyExc_OSError);
341     else
342       caps.bcaps = 0;
343   }
344
345   return Py_BuildValue("K", caps.bcaps & vc_get_insecurebcaps());
346 }
347
348 static PyObject *
349 vserver_bcaps2text(PyObject *self, PyObject *args)
350 {
351   struct vc_ctx_caps caps = { .bcaps = 0 };
352   PyObject *list;
353   const char *cap;
354
355   if (!PyArg_ParseTuple(args, "K", &caps.bcaps))
356     return NULL;
357
358   list = PyString_FromString("");
359
360   while ((cap = vc_lobcap2text(&caps.bcaps)) != NULL) {
361     if (list == NULL)
362       break;
363     PyString_ConcatAndDel(&list, PyString_FromFormat(
364                           (PyString_Size(list) > 0 ? ",CAP_%s" : "CAP_%s" ),
365                           cap));
366   }
367
368   return list;
369 }
370
371 static PyMethodDef  methods[] = {
372   { "chcontext", vserver_chcontext, METH_VARARGS,
373     "chcontext to vserver with provided flags" },
374   { "setup_done", vserver_setup_done, METH_VARARGS,
375     "Release vserver setup lock" },
376   { "setsched", vserver_setsched, METH_VARARGS,
377     "Change vserver scheduling attributes for given vserver context" },
378   { "setdlimit", vserver_set_dlimit, METH_VARARGS,
379     "Set disk limits for given vserver context" },
380   { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
381     "Remove disk limits for given vserver context" },
382   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
383     "Get disk limits for given vserver context" },
384   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
385     "Set resource limits for given resource of a vserver context" },
386   { "getrlimit", vserver_get_rlimit, METH_VARARGS,
387     "Get resource limits for given resource of a vserver context" },
388   { "killall", vserver_killall, METH_VARARGS,
389     "Send signal to all processes in vserver context" },
390   { "isrunning", vserver_isrunning, METH_VARARGS,
391     "Check if vserver is running"},
392   { "setbcaps", vserver_set_bcaps, METH_VARARGS,
393     "Set POSIX capabilities of a vserver context" },
394   { "getbcaps", vserver_get_bcaps, METH_VARARGS,
395     "Get POSIX capabilities of a vserver context" },
396   { "text2bcaps", vserver_text2bcaps, METH_VARARGS,
397     "Translate a string of capabilities to a bitmap" },
398   { "bcaps2text", vserver_bcaps2text, METH_VARARGS,
399     "Translate a capability-bitmap into a string" },
400   { NULL, NULL, 0, NULL }
401 };
402
403 PyMODINIT_FUNC
404 initvserverimpl(void)
405 {
406   PyObject  *mod;
407
408   mod = Py_InitModule("vserverimpl", methods);
409
410   /* export the set of 'safe' capabilities */
411   PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
412
413   /* export the default vserver directory */
414   PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
415
416   /* export limit-related constants */
417   PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)VC_CDLIM_KEEP);
418   PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)VC_CDLIM_INFINITY);
419   PyModule_AddIntConstant(mod, "VC_LIM_KEEP", (int)VC_LIM_KEEP);
420
421   PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU);
422   PyModule_AddIntConstant(mod, "RLIMIT_RSS", (int)RLIMIT_RSS);
423   PyModule_AddIntConstant(mod, "RLIMIT_NPROC", (int)RLIMIT_NPROC);
424   PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", (int)RLIMIT_NOFILE);
425   PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", (int)RLIMIT_MEMLOCK);
426   PyModule_AddIntConstant(mod, "RLIMIT_AS", (int)RLIMIT_AS);
427   PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", (int)RLIMIT_LOCKS);
428
429   PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", (int)RLIMIT_SIGPENDING);
430   PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", (int)RLIMIT_MSGQUEUE);
431
432   PyModule_AddIntConstant(mod, "VLIMIT_NSOCK", (int)VC_VLIMIT_NSOCK);
433   PyModule_AddIntConstant(mod, "VLIMIT_OPENFD", (int)VC_VLIMIT_OPENFD);
434   PyModule_AddIntConstant(mod, "VLIMIT_ANON", (int)VC_VLIMIT_ANON);
435   PyModule_AddIntConstant(mod, "VLIMIT_SHMEM", (int)VC_VLIMIT_SHMEM);
436
437   /* scheduler flags */
438   PyModule_AddIntConstant(mod,
439                           "VS_SCHED_CPU_GUARANTEED",
440                           VS_SCHED_CPU_GUARANTEED);
441 }