added setsched and dlimit vserver syscall support
[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 /*
56  * chcontext
57  */
58 static PyObject *
59 vserver_chcontext(PyObject *self, PyObject *args)
60 {
61   unsigned  xid;
62   unsigned  caps_remove = 0;
63
64   if (!PyArg_ParseTuple(args, "I|I", &xid, &caps_remove))
65     return NULL;
66
67   if (vc_new_s_context(xid, caps_remove, 0) < 0)
68     return PyErr_SetFromErrno(PyExc_OSError);
69
70   return Py_None;
71 }
72
73 /*
74  * setsched
75  */
76 static PyObject *
77 vserver_setsched(PyObject *self, PyObject *args)
78 {
79   unsigned  xid;
80   struct vc_set_sched sched;
81
82   sched.set_mask = (VC_VXSM_FILL_RATE | 
83                     VC_VXSM_INTERVAL | 
84                     VC_VXSM_TOKENS_MIN | 
85                     VC_VXSM_TOKENS_MAX);
86
87   if (!PyArg_ParseTuple(args, "I|I|I|I|I", &xid, 
88                         &sched.fill_rate,
89                         &sched.interval,
90                         &sched.tokens_min,
91                         &sched.tokens_max))
92     return NULL;
93
94   if (vc_set_sched(xid, &sched) == -1)
95     return PyErr_SetFromErrno(PyExc_OSError);
96
97   return Py_None;
98 }
99
100 /*
101  * setsched
102  */
103
104 /*  inode vserver commands */
105 #define VCMD_add_dlimit         VC_CMD(DLIMIT, 1, 0)
106 #define VCMD_rem_dlimit         VC_CMD(DLIMIT, 2, 0)
107 #define VCMD_set_dlimit         VC_CMD(DLIMIT, 5, 0)
108 #define VCMD_get_dlimit         VC_CMD(DLIMIT, 6, 0)
109
110 struct  vcmd_ctx_dlimit_base_v0 {
111         char *name;
112         uint32_t flags;
113 };
114
115 struct  vcmd_ctx_dlimit_v0 {
116         char *name;
117         uint32_t space_used;                    /* used space in kbytes */
118         uint32_t space_total;                   /* maximum space in kbytes */
119         uint32_t inodes_used;                   /* used inodes */
120         uint32_t inodes_total;                  /* maximum inodes */
121         uint32_t reserved;                      /* reserved for root in % */
122         uint32_t flags;
123 };
124
125 #define CDLIM_UNSET             (0ULL)
126 #define CDLIM_INFINITY          (~0ULL)
127 #define CDLIM_KEEP              (~1ULL)
128
129 static PyObject *
130 vserver_dlimit(PyObject *self, PyObject *args)
131 {
132         PyObject *res;
133         char* path;
134         unsigned xid;
135         struct vcmd_ctx_dlimit_base_v0 init;
136         struct vcmd_ctx_dlimit_v0 data;
137         int r;
138
139         memset(&data,0,sizeof(data));
140         if (!PyArg_ParseTuple(args, "s(iiiii)", &path, 
141                               &data.space_used,
142                               &data.space_total,
143                               &data.inodes_used,
144                               &data.inodes_total,
145                               &data.reserved))
146                 return NULL;
147
148         data.name = path;
149         data.flags = 0;
150
151         init.name = path;
152         init.flags = 0;
153
154         r = vserver(VCMD_rem_dlimit, xid, &init);
155         if (r<0){}
156         r = vserver(VCMD_add_dlimit, xid, &init);
157         if (r<0){}
158         r = vserver(VCMD_set_dlimit, xid, &data);
159         if (r<0){}
160
161         memset(&data, 0, sizeof(data));
162         data.name = path;
163         data.flags = 0;
164         r = vserver(VCMD_get_dlimit, xid, &data);
165         if (r>=0) {
166                 res = Py_BuildValue("(i,i,i,i,i)",
167                                     data.space_used,
168                                     data.space_total,
169                                     data.inodes_used,
170                                     data.inodes_total,
171                                     data.reserved);
172         } else {
173                 res = PyErr_SetFromErrno(PyExc_OSError);
174         }
175
176         return res;
177 }
178
179
180
181 static PyMethodDef  methods[] = {
182   { "chcontext", vserver_chcontext, METH_VARARGS,
183     "Change to the given vserver context" },
184   { "setsched", vserver_setsched, METH_VARARGS,
185     "Change vserver scheduling attributes for given vserver context" },
186   { "dlimit", vserver_dlimit, METH_VARARGS,
187     "Set disk limits for given vserver context" },
188   { NULL, NULL, 0, NULL }
189 };
190
191 PyMODINIT_FUNC
192 initvserverimpl(void)
193 {
194   Py_InitModule("vserverimpl", methods);
195 }