X-Git-Url: http://git.onelab.eu/?p=util-vserver-pl.git;a=blobdiff_plain;f=python%2Fvserverimpl.c;h=8c2818c6be66d7cd2f3e7c33d93d22b55fa68336;hp=71f082018a3e745ce0e50b061a77a392a46adaf4;hb=HEAD;hpb=73929cd988b29cc673ff12945fc921957bd3ce78 diff --git a/python/vserverimpl.c b/python/vserverimpl.c index 71f0820..8c2818c 100644 --- a/python/vserverimpl.c +++ b/python/vserverimpl.c @@ -49,7 +49,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "config.h" #include "vserver.h" -#include "planetlab.h" static inline PyObject *inc_and_ret_none(void) { @@ -59,60 +58,9 @@ static inline PyObject *inc_and_ret_none(void) #define NONE inc_and_ret_none() -/* - * context create - */ -static PyObject * -vserver_chcontext(PyObject *self, PyObject *args) -{ - int ctx_is_new; - xid_t ctx; - uint_least64_t bcaps = 0; - - if (!PyArg_ParseTuple(args, "I|K", &ctx, &bcaps)) - return NULL; - bcaps |= ~(vc_get_insecurebcaps() | (1 << VC_CAP_NET_BIND_SERVICE)); - - if ((ctx_is_new = pl_chcontext(ctx, bcaps, 0)) < 0) - return PyErr_SetFromErrno(PyExc_OSError); - - return PyBool_FromLong(ctx_is_new); -} - -static PyObject * -vserver_setup_done(PyObject *self, PyObject *args) -{ - xid_t ctx; - - if (!PyArg_ParseTuple(args, "I", &ctx)) - return NULL; - - if (pl_setup_done(ctx) < 0) - return PyErr_SetFromErrno(PyExc_OSError); - - return NONE; -} - -static PyObject * -vserver_isrunning(PyObject *self, PyObject *args) -{ - xid_t ctx; - PyObject *ret; - struct stat statbuf; - char fname[64]; - - if (!PyArg_ParseTuple(args, "I", &ctx)) - return NULL; +#define PL_INSECURE_BCAPS (vc_get_insecurebcaps() | (1 << VC_CAP_NET_BIND_SERVICE)) +#define PL_INSECURE_CCAPS vc_get_insecureccaps() - sprintf(fname,"/proc/virtual/%d", ctx); - - if(stat(&fname[0],&statbuf)==0) - ret = PyBool_FromLong(1); - else - ret = PyBool_FromLong(0); - - return ret; -} static PyObject * __vserver_get_rlimit(xid_t xid, int resource) { @@ -145,10 +93,11 @@ vserver_get_rlimit(PyObject *self, PyObject *args) { static PyObject * vserver_set_rlimit(PyObject *self, PyObject *args) { struct vc_rlimit limits; - struct rlimit lim; + struct vc_rlimit_mask mask; + uint32_t bitmask; xid_t xid; - int resource, lresource; - PyObject *ret; + int resource; + PyObject *ret = NULL; limits.min = VC_LIM_KEEP; limits.soft = VC_LIM_KEEP; @@ -157,54 +106,22 @@ vserver_set_rlimit(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "IiLLL", &xid, &resource, &limits.hard, &limits.soft, &limits.min)) return NULL; - lresource = resource; - switch (resource) { - case VC_VLIMIT_NSOCK: - case VC_VLIMIT_ANON: - case VC_VLIMIT_SHMEM: - goto do_vc_set_rlimit; - case VC_VLIMIT_OPENFD: - lresource = RLIMIT_NOFILE; - break; - default: - break; - } - - getrlimit(lresource,&lim); - if (adjust_lim(&limits,&lim)) { - setrlimit(lresource, &lim); - } - - do_vc_set_rlimit: errno = 0; - if (vc_set_rlimit(xid, resource, &limits)==-1) + + if (vc_get_rlimit_mask(xid, &mask)==-1) { ret = PyErr_SetFromErrno(PyExc_OSError); - else - ret = __vserver_get_rlimit(xid, resource); + } else { + bitmask = (1< 0 ? ",%s" : "%s" ), + cap)); + } + + return list; +} + static inline int convert_address(const char *str, struct vc_net_addr *addr) { @@ -425,19 +389,19 @@ get_mask(struct vc_net_addr *addr) offset = offsetof(struct sockaddr_in6, sin6_addr.s6_addr); ip = addr->vna_v6_ip.s6_addr; len = 16; - addr->vna_v6_mask.s6_addr32[9] = addr->vna_v6_mask.s6_addr32[1] = 0xffffffff; + addr->vna_v6_mask.s6_addr32[0] = addr->vna_v6_mask.s6_addr32[1] = 0xffffffff; addr->vna_v6_mask.s6_addr32[2] = addr->vna_v6_mask.s6_addr32[3] = 0x00000000; addr->vna_prefix = 64; break; default: - errno = -EINVAL; + errno = EINVAL; return -1; } if (getifaddrs(&head) == -1) return -1; for (ifa = head; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr->sa_family == family && + if (ifa->ifa_addr && ifa->ifa_addr->sa_family == family && memcmp((char *) ifa->ifa_addr + offset, ip, len) == 0) { switch (addr->vna_type) { case VC_NXA_TYPE_IPV4: @@ -461,24 +425,35 @@ get_mask(struct vc_net_addr *addr) static PyObject * vserver_net_add(PyObject *self, PyObject *args) { - struct vc_net_addr addr; + struct vc_net_addr addr = { .vna_type = 0 }; nid_t nid; const char *ip; if (!PyArg_ParseTuple(args, "Is", &nid, &ip)) return NULL; - if (convert_address(ip, &addr) == -1) - return PyErr_Format(PyExc_ValueError, "%s is not a valid IP address", ip); + /* Optimize standard case, which also needs to be handled differently */ + if (strcmp(ip, "0.0.0.0") == 0) { + addr.vna_type = VC_NXA_TYPE_MASK | VC_NXA_TYPE_IPV4; + addr.vna_flags = 0; + addr.vna_prefix = 0; + addr.vna_parent = 0; + addr.vna_v4_mask.s_addr = 0; + addr.vna_v4_ip.s_addr = 0; + } + else { + if (convert_address(ip, &addr) == -1) + return PyErr_Format(PyExc_ValueError, "%s is not a valid IP address", ip); - switch (get_mask(&addr)) { - case -1: - return PyErr_SetFromErrno(PyExc_OSError); - case 0: - /* XXX error here? */ - break; + switch (get_mask(&addr)) { + case -1: + return PyErr_SetFromErrno(PyExc_OSError); + case 0: + /* XXX error here? */ + break; + } + addr.vna_type |= VC_NXA_TYPE_ADDR; } - addr.vna_type |= VC_NXA_TYPE_ADDR; if (vc_net_add(nid, &addr) == -1 && errno != ESRCH) return PyErr_SetFromErrno(PyExc_OSError); @@ -499,18 +474,19 @@ vserver_net_remove(PyObject *self, PyObject *args) if (strcmp(ip, "all") == 0) addr.vna_type = VC_NXA_TYPE_ANY; else if (strcmp(ip, "all4") == 0) - addr.vna_type = VC_NXA_TYPE_IPV6 | VC_NXA_TYPE_ANY; + addr.vna_type = VC_NXA_TYPE_IPV4 | VC_NXA_TYPE_ANY; else if (strcmp(ip, "all6") == 0) addr.vna_type = VC_NXA_TYPE_IPV6 | VC_NXA_TYPE_ANY; else { if (convert_address(ip, &addr) == -1) return PyErr_Format(PyExc_ValueError, "%s is not a valid IP address", ip); - addr.vna_type |= VC_NXA_TYPE_ADDR; - } - switch (get_mask(&addr)) { - case -1: - return PyErr_SetFromErrno(PyExc_OSError); + switch (get_mask(&addr)) { + case -1: + return PyErr_SetFromErrno(PyExc_OSError); + } + + addr.vna_type |= VC_NXA_TYPE_ADDR; } if (vc_net_remove(nid, &addr) == -1 && errno != ESRCH) @@ -609,7 +585,7 @@ vserver_mount(PyObject *self, PyObject *args) if (secure_chdir(&dirs, guest, target) == -1) goto out; - if (mount(source, ".", type, flags, data) == -1) + if (mount(source, ".", type, flags, data) == -1 && errno != EBUSY) goto out; restore_dirs(&dirs); @@ -663,13 +639,41 @@ vserver_set_runlevel(PyObject *self, PyObject *args) return NONE; } +static PyObject * +vserver_set_name(PyObject *self, PyObject *args) +{ + xid_t ctx, slice_id; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "II", &ctx, &slice_id)) + return NULL; + + if (vc_set_vhi_name(ctx, vcVHI_CONTEXT, (char *)&slice_id, sizeof(slice_id)) != 0 && errno != ESRCH) { + return PyErr_SetFromErrno(PyExc_OSError); + } else { + return NONE; + } +} + +static PyObject * +vserver_get_name(PyObject *self, PyObject *args) +{ + xid_t ctx, slice_id; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "I", &ctx)) + return NULL; + + if (vc_get_vhi_name(ctx, vcVHI_CONTEXT, (char *)&slice_id, sizeof(slice_id)) != 0) { + ret = PyErr_SetFromErrno(PyExc_OSError); + } else { + ret = Py_BuildValue("i", slice_id); + } + return ret; +} + + static PyMethodDef methods[] = { - { "chcontext", vserver_chcontext, METH_VARARGS, - "chcontext to vserver with provided flags" }, - { "setup_done", vserver_setup_done, METH_VARARGS, - "Release vserver setup lock" }, - { "setsched", vserver_setsched, METH_VARARGS, - "Change vserver scheduling attributes for given vserver context" }, { "setdlimit", vserver_set_dlimit, METH_VARARGS, "Set disk limits for given vserver context" }, { "unsetdlimit", vserver_unset_dlimit, METH_VARARGS, @@ -680,10 +684,6 @@ static PyMethodDef methods[] = { "Set resource limits for given resource of a vserver context" }, { "getrlimit", vserver_get_rlimit, METH_VARARGS, "Get resource limits for given resource of a vserver context" }, - { "killall", vserver_killall, METH_VARARGS, - "Send signal to all processes in vserver context" }, - { "isrunning", vserver_isrunning, METH_VARARGS, - "Check if vserver is running"}, { "setbcaps", vserver_set_bcaps, METH_VARARGS, "Set POSIX capabilities of a vserver context" }, { "getbcaps", vserver_get_bcaps, METH_VARARGS, @@ -692,6 +692,14 @@ static PyMethodDef methods[] = { "Translate a string of capabilities to a bitmap" }, { "bcaps2text", vserver_bcaps2text, METH_VARARGS, "Translate a capability-bitmap into a string" }, + { "setccaps", vserver_set_ccaps, METH_VARARGS, + "Set context capabilities of a vserver context" }, + { "getccaps", vserver_get_ccaps, METH_VARARGS, + "Get context capabilities of a vserver context" }, + { "text2ccaps", vserver_text2ccaps, METH_VARARGS, + "Translate a string of context capabilities to a bitmap" }, + { "ccaps2text", vserver_ccaps2text, METH_VARARGS, + "Translate a context-capability-bitmap into a string" }, { "netadd", vserver_net_add, METH_VARARGS, "Assign an IP address to a context" }, { "netremove", vserver_net_remove, METH_VARARGS, @@ -702,6 +710,10 @@ static PyMethodDef methods[] = { "Perform the umount2() system call" }, { "setrunlevel", vserver_set_runlevel, METH_VARARGS, "Set the runlevel in utmp" }, + { "setname", vserver_set_name, METH_VARARGS, + "Set the vcVHI_CONTEXT for a xid." }, + { "getname", vserver_get_name, METH_VARARGS, + "Get the vcVHI_CONTEXT for a xid." }, { NULL, NULL, 0, NULL } }; @@ -721,6 +733,7 @@ initvserverimpl(void) /* export limit-related constants */ PyModule_AddIntConstant(mod, "DLIMIT_KEEP", (int)VC_CDLIM_KEEP); PyModule_AddIntConstant(mod, "DLIMIT_INF", (int)VC_CDLIM_INFINITY); + PyModule_AddIntConstant(mod, "VC_LIM_INFINITY", (int)VC_LIM_INFINITY); PyModule_AddIntConstant(mod, "VC_LIM_KEEP", (int)VC_LIM_KEEP); PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU); @@ -739,8 +752,4 @@ initvserverimpl(void) PyModule_AddIntConstant(mod, "VLIMIT_ANON", (int)VC_VLIMIT_ANON); PyModule_AddIntConstant(mod, "VLIMIT_SHMEM", (int)VC_VLIMIT_SHMEM); - /* scheduler flags */ - PyModule_AddIntConstant(mod, - "VS_SCHED_CPU_GUARANTEED", - VS_SCHED_CPU_GUARANTEED); }