Fix to implement chcontext so that NM's vserver start works correctly.
[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 "config.h"
37 #include "compat.h"
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <sys/ioctl.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <stdint.h>
49
50 #include "vserver.h"
51 #include "vserver-internal.h"
52 #include "sched_cmd.h"
53 #include "virtual.h"
54
55 #define MEF_DEBUG 1
56 /*
57  * context create
58  */
59 static PyObject *
60 vserver_chcontext(PyObject *self, PyObject *args)
61 {
62         xid_t xid, ctx;
63         struct vc_ctx_caps caps;
64         struct vc_ctx_flags flags;
65         struct vc_vx_info vc;
66         unsigned long long v;
67
68         v = VC_VXF_STATE_SETUP;
69         if (!PyArg_ParseTuple(args, "I|K", &ctx, &v))
70                 return NULL;
71
72         caps.ccaps = ~vc_get_insecureccaps();
73         caps.cmask = ~0ull;
74         caps.bcaps = ~vc_get_insecurebcaps();
75         caps.bmask = ~0ull;
76
77         xid = VC_NOCTX;
78         if (vc_get_vx_info(ctx,&vc) != 0) {
79                 xid = vc_ctx_create(ctx);
80                 if (xid == VC_NOCTX && errno != EEXIST)
81                         return PyErr_SetFromErrno(PyExc_OSError);
82         }
83
84         flags.mask = flags.flagword = v;
85         if (vc_set_cflags(ctx, &flags) == -1)
86                 return PyErr_SetFromErrno(PyExc_OSError);
87
88         if (xid == VC_NOCTX && vc_ctx_migrate(ctx) == -1)
89                 return PyErr_SetFromErrno(PyExc_OSError);
90
91 #ifdef MEF_DEBUG
92         printf("vserver_create xid = %d(%d)\n",xid,ctx);
93 #endif
94         return Py_None;
95 }
96
97 static PyObject *
98 vserver_set_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, "IiL", &xid, &resource, &limits.hard))
109                 return NULL;
110
111         ret = Py_None;
112         if (vc_set_rlimit(xid, resource, &limits)) 
113                 ret = PyErr_SetFromErrno(PyExc_OSError);
114         else if (vc_get_rlimit(xid, resource, &limits)==-1)
115                 ret = PyErr_SetFromErrno(PyExc_OSError);
116         else
117                 ret = Py_BuildValue("L",limits.hard);
118
119         return ret;
120 }
121
122 static PyObject *
123 vserver_get_rlimit(PyObject *self, PyObject *args) {
124         struct vc_rlimit limits;
125         xid_t xid;
126         int resource;
127         PyObject *ret;
128
129         limits.min = VC_LIM_KEEP;
130         limits.soft = VC_LIM_KEEP;
131         limits.hard = VC_LIM_KEEP;
132
133         if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
134                 return NULL;
135
136         ret = Py_None;
137         if (vc_get_rlimit(xid, resource, &limits)==-1)
138                 ret = PyErr_SetFromErrno(PyExc_OSError);
139         else
140                 ret = Py_BuildValue("L",limits.hard);
141
142         return ret;
143 }
144
145 /*
146  * setsched
147  */
148 static PyObject *
149 vserver_setsched(PyObject *self, PyObject *args)
150 {
151   xid_t  xid;
152   struct vc_set_sched sched;
153   struct vc_ctx_flags flags;
154   unsigned cpuguaranteed = 0;
155
156   sched.set_mask = (VC_VXSM_FILL_RATE | 
157                     VC_VXSM_INTERVAL | 
158                     VC_VXSM_TOKENS_MIN | 
159                     VC_VXSM_TOKENS_MAX);
160
161   if (!PyArg_ParseTuple(args, "I|I|I|I|I|I|I", &xid, 
162                         &sched.fill_rate,
163                         &sched.interval,
164                         &sched.tokens,
165                         &sched.tokens_min,
166                         &sched.tokens_max,
167                         &cpuguaranteed))
168     return NULL;
169
170   flags.flagword = VC_VXF_SCHED_HARD;
171   flags.mask |= VC_VXF_SCHED_HARD;
172 #define VC_VXF_SCHED_SHARE       0x00000800ull
173   if (cpuguaranteed==0) {
174           flags.flagword |= VC_VXF_SCHED_SHARE;
175           flags.mask |= VC_VXF_SCHED_SHARE;
176   }
177
178   if (vc_set_cflags(xid, &flags) == -1)
179           return PyErr_SetFromErrno(PyExc_OSError);
180
181   if (vc_set_sched(xid, &sched) == -1)
182           return PyErr_SetFromErrno(PyExc_OSError);
183
184   return Py_None;
185 }
186
187 /*
188  * setsched
189  */
190
191 /*  inode vserver commands */
192 #define VCMD_add_dlimit         VC_CMD(DLIMIT, 1, 0)
193 #define VCMD_rem_dlimit         VC_CMD(DLIMIT, 2, 0)
194 #define VCMD_set_dlimit         VC_CMD(DLIMIT, 5, 0)
195 #define VCMD_get_dlimit         VC_CMD(DLIMIT, 6, 0)
196
197 #define CDLIM_UNSET             (0ULL)
198 #define CDLIM_INFINITY          (~0ULL)
199 #define CDLIM_KEEP              (~1ULL)
200
201 static PyObject *
202 vserver_get_dlimit(PyObject *self, PyObject *args)
203 {
204         PyObject *res;
205         char* path;
206         unsigned xid;
207         struct vcmd_ctx_dlimit_v0 data;
208         int r;
209
210         if (!PyArg_ParseTuple(args, "si", &path,&xid))
211                 return NULL;
212
213         memset(&data, 0, sizeof(data));
214         data.name = path;
215         data.flags = 0;
216         r = vserver(VCMD_get_dlimit, xid, &data);
217         if (r>=0) {
218                 res = Py_BuildValue("(i,i,i,i,i)",
219                                     data.space_used,
220                                     data.space_total,
221                                     data.inodes_used,
222                                     data.inodes_total,
223                                     data.reserved);
224         } else {
225                 res = PyErr_SetFromErrno(PyExc_OSError);
226         }
227
228         return res;
229 }
230
231
232 static PyObject *
233 vserver_set_dlimit(PyObject *self, PyObject *args)
234 {
235         char* path;
236         unsigned xid;
237         struct vcmd_ctx_dlimit_base_v0 init;
238         struct vcmd_ctx_dlimit_v0 data;
239         int r;
240
241         memset(&data,0,sizeof(data));
242         if (!PyArg_ParseTuple(args, "siiiiii", &path,
243                               &xid,
244                               &data.space_used,
245                               &data.space_total,
246                               &data.inodes_used,
247                               &data.inodes_total,
248                               &data.reserved))
249                 return NULL;
250
251         data.name = path;
252         data.flags = 0;
253
254         memset(&init, 0, sizeof(init));
255         init.name = path;
256         init.flags = 0;
257
258         r = vserver(VCMD_rem_dlimit, xid, &init);
259         if (r<0){}
260         r = vserver(VCMD_add_dlimit, xid, &init);
261         if (r<0){}
262         r = vserver(VCMD_set_dlimit, xid, &data);
263         if (r<0){}
264         return Py_None; 
265 }
266
267 static PyMethodDef  methods[] = {
268   { "chcontext", vserver_chcontext, METH_VARARGS,
269     "chcontext to vserver with provided flags" },
270   { "setsched", vserver_setsched, METH_VARARGS,
271     "Change vserver scheduling attributes for given vserver context" },
272   { "setdlimit", vserver_set_dlimit, METH_VARARGS,
273     "Set disk limits for given vserver context" },
274   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
275     "Get disk limits for given vserver context" },
276   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
277     "Set resource limits for given resource of a vserver context" },
278   { "getrlimit", vserver_get_rlimit, METH_VARARGS,
279     "Get resource limits for given resource of a vserver context" },
280   { NULL, NULL, 0, NULL }
281 };
282
283 PyMODINIT_FUNC
284 initvserverimpl(void)
285 {
286   Py_InitModule("vserverimpl", methods);
287 }