add support for bcapabilities
[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
273   if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
274     return NULL;
275
276   if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
277     return PyErr_SetFromErrno(PyExc_OSError);
278
279   return NONE;
280 }
281
282 static PyObject *
283 vserver_set_bcaps(PyObject *self, PyObject *args)
284 {
285   xid_t ctx;
286   struct vc_ctx_caps caps;
287
288   if (!PyArg_ParseTuple(args, "IK", &ctx, &caps.bcaps))
289     return NULL;
290
291   caps.bmask = vc_get_insecurebcaps();
292   caps.cmask = caps.ccaps = 0;
293   if (vc_set_ccaps(ctx, &caps) == -1 && errno != ESRCH)
294     return PyErr_SetFromErrno(PyExc_OSError);
295
296   return NONE;
297 }
298
299 static PyObject *
300 vserver_text2bcaps(PyObject *self, PyObject *args)
301 {
302   struct vc_ctx_caps caps = { .bcaps = 0 };
303   const char *list;
304   int len;
305   struct vc_err_listparser err;
306
307   if (!PyArg_ParseTuple(args, "s#", &list, &len))
308     return NULL;
309
310   if (vc_list2bcap(list, len, &err, &caps) == -1)
311     return NULL;
312
313   return Py_BuildValue("K", caps.bcaps);
314 }
315
316 static PyObject *
317 vserver_get_bcaps(PyObject *self, PyObject *args)
318 {
319   xid_t ctx;
320   struct vc_ctx_caps caps;
321
322   if (!PyArg_ParseTuple(args, "I", &ctx))
323     return NULL;
324
325   if (vc_get_ccaps(ctx, &caps) == -1) {
326     if (errno != -ESRCH)
327       return PyErr_SetFromErrno(PyExc_OSError);
328     else
329       caps.bcaps = 0;
330   }
331
332   return Py_BuildValue("K", caps.bcaps & vc_get_insecurebcaps());
333 }
334
335 static PyObject *
336 vserver_bcaps2text(PyObject *self, PyObject *args)
337 {
338   struct vc_ctx_caps caps = { .bcaps = 0 };
339   PyObject *list;
340   const char *cap;
341
342   if (!PyArg_ParseTuple(args, "K", &caps.bcaps))
343     return NULL;
344
345   list = PyString_FromString("");
346
347   while ((cap = vc_lobcap2text(&caps.bcaps)) != NULL) {
348     if (list == NULL)
349       break;
350     PyString_ConcatAndDel(&list, PyString_FromFormat(
351                           (PyString_Size(list) > 0 ? ",CAP_%s" : "CAP_%s" ),
352                           cap));
353   }
354
355   return list;
356 }
357
358 static PyMethodDef  methods[] = {
359   { "chcontext", vserver_chcontext, METH_VARARGS,
360     "chcontext to vserver with provided flags" },
361   { "setup_done", vserver_setup_done, METH_VARARGS,
362     "Release vserver setup lock" },
363   { "setsched", vserver_setsched, METH_VARARGS,
364     "Change vserver scheduling attributes for given vserver context" },
365   { "setdlimit", vserver_set_dlimit, METH_VARARGS,
366     "Set disk limits for given vserver context" },
367   { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS,
368     "Remove disk limits for given vserver context" },
369   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
370     "Get disk limits for given vserver context" },
371   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
372     "Set resource limits for given resource of a vserver context" },
373   { "getrlimit", vserver_get_rlimit, METH_VARARGS,
374     "Get resource limits for given resource of a vserver context" },
375   { "killall", vserver_killall, METH_VARARGS,
376     "Send signal to all processes in vserver context" },
377   { "isrunning", vserver_isrunning, METH_VARARGS,
378     "Check if vserver is running"},
379   { "setbcaps", vserver_set_bcaps, METH_VARARGS,
380     "Set POSIX capabilities of a vserver context" },
381   { "getbcaps", vserver_get_bcaps, METH_VARARGS,
382     "Get POSIX capabilities of a vserver context" },
383   { "text2bcaps", vserver_text2bcaps, METH_VARARGS,
384     "Translate a string of capabilities to a bitmap" },
385   { "bcaps2text", vserver_bcaps2text, METH_VARARGS,
386     "Translate a capability-bitmap into a string" },
387   { NULL, NULL, 0, NULL }
388 };
389
390 PyMODINIT_FUNC
391 initvserverimpl(void)
392 {
393   PyObject  *mod;
394
395   mod = Py_InitModule("vserverimpl", methods);
396
397   /* export the set of 'safe' capabilities */
398   PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
399
400   /* export the default vserver directory */
401   PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
402
403   /* export limit-related constants */
404   PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)VC_CDLIM_KEEP);
405   PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)VC_CDLIM_INFINITY);
406   PyModule_AddIntConstant(mod, "VC_LIM_KEEP", (int)VC_LIM_KEEP);
407
408   PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU);
409   PyModule_AddIntConstant(mod, "RLIMIT_RSS", (int)RLIMIT_RSS);
410   PyModule_AddIntConstant(mod, "RLIMIT_NPROC", (int)RLIMIT_NPROC);
411   PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", (int)RLIMIT_NOFILE);
412   PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", (int)RLIMIT_MEMLOCK);
413   PyModule_AddIntConstant(mod, "RLIMIT_AS", (int)RLIMIT_AS);
414   PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", (int)RLIMIT_LOCKS);
415
416   PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", (int)RLIMIT_SIGPENDING);
417   PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", (int)RLIMIT_MSGQUEUE);
418
419   PyModule_AddIntConstant(mod, "VLIMIT_NSOCK", (int)VC_VLIMIT_NSOCK);
420   PyModule_AddIntConstant(mod, "VLIMIT_OPENFD", (int)VC_VLIMIT_OPENFD);
421   PyModule_AddIntConstant(mod, "VLIMIT_ANON", (int)VC_VLIMIT_ANON);
422   PyModule_AddIntConstant(mod, "VLIMIT_SHMEM", (int)VC_VLIMIT_SHMEM);
423
424   /* scheduler flags */
425   PyModule_AddIntConstant(mod,
426                           "VS_SCHED_CPU_GUARANTEED",
427                           VS_SCHED_CPU_GUARANTEED);
428 }