merge with 0.30.213
[util-vserver.git] / python / vserverimpl.c
index 3aff725..d16dfa3 100644 (file)
@@ -4,17 +4,17 @@ Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met: 
 
-    * Redistributions of source code must retain the above copyright
-      notice, this list of conditions and the following disclaimer.
+* Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
       
-    * Redistributions in binary form must reproduce the above
-      copyright notice, this list of conditions and the following
-      disclaimer in the documentation and/or other materials provided
-      with the distribution.
+* Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following
+disclaimer in the documentation and/or other materials provided
+with the distribution.
       
-    * Neither the name of the copyright holder nor the names of its
-      contributors may be used to endorse or promote products derived
-      from this software without specific prior written permission.
+* Neither the name of the copyright holder nor the names of its
+contributors may be used to endorse or promote products derived
+from this software without specific prior written permission.
       
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -33,255 +33,311 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include <Python.h>
 
-#include "config.h"
-#include "compat.h"
-
-#include <stdio.h>
-#include <stdlib.h>
 #include <errno.h>
-#include <string.h>
-#include <sys/ioctl.h>
+#include <stdint.h>
+#include <sys/resource.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
 #include <unistd.h>
-#include <stdint.h>
 
+#include "config.h"
+#include "pathconfig.h"
+#include "virtual.h"
 #include "vserver.h"
+#include "planetlab.h"
 #include "vserver-internal.h"
-#include "sched_cmd.h"
-#include "virtual.h"
 
-#define MEF_DEBUG 1
+#define NONE  ({ Py_INCREF(Py_None); Py_None; })
+
 /*
  * context create
  */
 static PyObject *
 vserver_chcontext(PyObject *self, PyObject *args)
 {
-       xid_t xid, ctx;
-       struct vc_ctx_caps caps;
-       struct vc_ctx_flags flags;
-       struct vc_vx_info vc;
-       unsigned long long v;
-
-       v = VC_VXF_STATE_SETUP;
-       if (!PyArg_ParseTuple(args, "I|K", &ctx, &v))
-               return NULL;
-
-       caps.ccaps = ~vc_get_insecureccaps();
-       caps.cmask = ~0ull;
-       caps.bcaps = ~vc_get_insecurebcaps();
-       caps.bmask = ~0ull;
-
-       xid = VC_NOCTX;
-       if (vc_get_vx_info(ctx,&vc) != 0) {
-               xid = vc_ctx_create(ctx);
-               if (xid == VC_NOCTX && errno != EEXIST)
-                       return PyErr_SetFromErrno(PyExc_OSError);
-       }
-
-       flags.mask = flags.flagword = v;
-       if (vc_set_cflags(ctx, &flags) == -1)
-               return PyErr_SetFromErrno(PyExc_OSError);
-
-       if (xid == VC_NOCTX && vc_ctx_migrate(ctx) == -1)
-               return PyErr_SetFromErrno(PyExc_OSError);
-
-#ifdef MEF_DEBUG
-       printf("vserver_create xid = %d(%d)\n",xid,ctx);
-#endif
-       return Py_None;
+  int  ctx_is_new;
+  xid_t  ctx;
+  uint_least64_t bcaps = ~vc_get_insecurebcaps();
+
+  if (!PyArg_ParseTuple(args, "I", &ctx))
+    return NULL;
+
+  if ((ctx_is_new = pl_chcontext(ctx, bcaps, 0)) < 0)
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return PyBool_FromLong(ctx_is_new);
 }
 
 static PyObject *
-vserver_set_rlimit(PyObject *self, PyObject *args) {
-       struct vc_rlimit limits;
-       xid_t xid;
-       int resource;
-       PyObject *ret;
-
-       limits.min = VC_LIM_KEEP;
-       limits.soft = VC_LIM_KEEP;
-       limits.hard = VC_LIM_KEEP;
-
-       if (!PyArg_ParseTuple(args, "IiL", &xid, &resource, &limits.hard))
-               return NULL;
-
-       ret = Py_None;
-       if (vc_set_rlimit(xid, resource, &limits)) 
-               ret = PyErr_SetFromErrno(PyExc_OSError);
-       else if (vc_get_rlimit(xid, resource, &limits)==-1)
-               ret = PyErr_SetFromErrno(PyExc_OSError);
-       else
-               ret = Py_BuildValue("L",limits.hard);
-
-       return ret;
+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_get_rlimit(PyObject *self, PyObject *args) {
-       struct vc_rlimit limits;
-       xid_t xid;
-       int resource;
-       PyObject *ret;
+vserver_isrunning(PyObject *self, PyObject *args)
+{
+  xid_t  ctx;
+  PyObject *ret;
+  struct stat statbuf;
+  char fname[64];
 
-       limits.min = VC_LIM_KEEP;
-       limits.soft = VC_LIM_KEEP;
-       limits.hard = VC_LIM_KEEP;
+  if (!PyArg_ParseTuple(args, "I", &ctx))
+    return NULL;
 
-       if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
-               return NULL;
+  sprintf(fname,"/proc/virtual/%d", ctx);
 
-       ret = Py_None;
-       if (vc_get_rlimit(xid, resource, &limits)==-1)
-               ret = PyErr_SetFromErrno(PyExc_OSError);
-       else
-               ret = Py_BuildValue("L",limits.hard);
+  if(stat(&fname[0],&statbuf)==0)
+    ret = PyBool_FromLong(1);
+  else
+    ret = PyBool_FromLong(0);
 
-       return ret;
+  return ret;
 }
 
-/*
- * setsched
- */
 static PyObject *
-vserver_setsched(PyObject *self, PyObject *args)
-{
-  xid_t  xid;
-  struct vc_set_sched sched;
-  struct vc_ctx_flags flags;
-  unsigned cpuguaranteed = 0;
-
-  sched.set_mask = (VC_VXSM_FILL_RATE | 
-                   VC_VXSM_INTERVAL | 
-                   VC_VXSM_TOKENS_MIN | 
-                   VC_VXSM_TOKENS_MAX);
-
-  if (!PyArg_ParseTuple(args, "I|I|I|I|I|I|I", &xid, 
-                       &sched.fill_rate,
-                       &sched.interval,
-                       &sched.tokens,
-                       &sched.tokens_min,
-                       &sched.tokens_max,
-                       &cpuguaranteed))
+__vserver_get_rlimit(xid_t xid, int resource) {
+  struct vc_rlimit limits;
+  PyObject *ret;
+
+  errno = 0;
+  if (vc_get_rlimit(xid, resource, &limits)==-1)
+    ret = PyErr_SetFromErrno(PyExc_OSError);
+  else
+    ret = Py_BuildValue("LLL",limits.hard, limits.soft, limits.min);
+
+  return ret;
+}
+
+static PyObject *
+vserver_get_rlimit(PyObject *self, PyObject *args) {
+  xid_t xid;
+  int resource;
+  PyObject *ret;
+
+  if (!PyArg_ParseTuple(args, "Ii", &xid, &resource))
+    ret = NULL;
+  else
+    ret = __vserver_get_rlimit(xid, resource);
+
+  return ret;
+}
+
+static PyObject *
+vserver_set_rlimit(PyObject *self, PyObject *args) {
+  struct vc_rlimit limits;
+  struct rlimit lim;
+  xid_t xid;
+  int resource, lresource;
+  PyObject *ret;
+
+  limits.min = VC_LIM_KEEP;
+  limits.soft = VC_LIM_KEEP;
+  limits.hard = VC_LIM_KEEP;
+
+  if (!PyArg_ParseTuple(args, "IiLLL", &xid, &resource, &limits.hard, &limits.soft, &limits.min))
     return NULL;
 
-  flags.flagword = VC_VXF_SCHED_HARD;
-  flags.mask |= VC_VXF_SCHED_HARD;
-#define VC_VXF_SCHED_SHARE       0x00000800ull
-  if (cpuguaranteed==0) {
-         flags.flagword |= VC_VXF_SCHED_SHARE;
-         flags.mask |= VC_VXF_SCHED_SHARE;
+  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;
   }
 
-  if (vc_set_cflags(xid, &flags) == -1)
-         return PyErr_SetFromErrno(PyExc_OSError);
+  getrlimit(lresource,&lim);
+  if (adjust_lim(&limits,&lim)) {
+    setrlimit(lresource, &lim);
+  }
 
-  if (vc_set_sched(xid, &sched) == -1)
-         return PyErr_SetFromErrno(PyExc_OSError);
+ do_vc_set_rlimit:
+  errno = 0;
+  if (vc_set_rlimit(xid, resource, &limits)==-1) 
+    ret = PyErr_SetFromErrno(PyExc_OSError);
+  else
+    ret = __vserver_get_rlimit(xid, resource);
 
-  return Py_None;
+  return ret;
 }
 
 /*
  * setsched
  */
+static PyObject *
+vserver_setsched(PyObject *self, PyObject *args)
+{
+  xid_t  ctx;
+  uint32_t  cpu_share;
+  uint32_t  cpu_sched_flags = VC_VXF_SCHED_FLAGS;
+
+  if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_share, &cpu_sched_flags))
+    return NULL;
 
-/*  inode vserver commands */
-#define VCMD_add_dlimit                VC_CMD(DLIMIT, 1, 0)
-#define VCMD_rem_dlimit                VC_CMD(DLIMIT, 2, 0)
-#define VCMD_set_dlimit                VC_CMD(DLIMIT, 5, 0)
-#define VCMD_get_dlimit                VC_CMD(DLIMIT, 6, 0)
+  /* ESRCH indicates that there are no processes in the context */
+  if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
+      errno != ESRCH)
+    return PyErr_SetFromErrno(PyExc_OSError);
 
-#define CDLIM_UNSET             (0ULL)
-#define CDLIM_INFINITY          (~0ULL)
-#define CDLIM_KEEP              (~1ULL)
+  return NONE;
+}
 
 static PyObject *
 vserver_get_dlimit(PyObject *self, PyObject *args)
 {
-       PyObject *res;
-       char* path;
-       unsigned xid;
-       struct vcmd_ctx_dlimit_v0 data;
-       int r;
-
-       if (!PyArg_ParseTuple(args, "si", &path,&xid))
-               return NULL;
-
-       memset(&data, 0, sizeof(data));
-       data.name = path;
-       data.flags = 0;
-       r = vserver(VCMD_get_dlimit, xid, &data);
-       if (r>=0) {
-               res = Py_BuildValue("(i,i,i,i,i)",
-                                   data.space_used,
-                                   data.space_total,
-                                   data.inodes_used,
-                                   data.inodes_total,
-                                   data.reserved);
-       } else {
-               res = PyErr_SetFromErrno(PyExc_OSError);
-       }
-
-       return res;
+  PyObject *res;
+  char* path;
+  unsigned xid;
+  struct vc_ctx_dlimit data;
+  int r;
+
+  if (!PyArg_ParseTuple(args, "si", &path,&xid))
+    return NULL;
+
+  memset(&data, 0, sizeof(data));
+  r = vc_get_dlimit(path, xid, 0, &data);
+  if (r>=0) {
+    res = Py_BuildValue("(i,i,i,i,i)",
+                       data.space_used,
+                       data.space_total,
+                       data.inodes_used,
+                       data.inodes_total,
+                       data.reserved);
+  } else {
+    res = PyErr_SetFromErrno(PyExc_OSError);
+  }
+
+  return res;
 }
 
 
 static PyObject *
 vserver_set_dlimit(PyObject *self, PyObject *args)
 {
-       char* path;
-       unsigned xid;
-       struct vcmd_ctx_dlimit_base_v0 init;
-       struct vcmd_ctx_dlimit_v0 data;
-       int r;
-
-       memset(&data,0,sizeof(data));
-       if (!PyArg_ParseTuple(args, "siiiiii", &path,
-                             &xid,
-                             &data.space_used,
-                             &data.space_total,
-                             &data.inodes_used,
-                             &data.inodes_total,
-                             &data.reserved))
-               return NULL;
-
-       data.name = path;
-       data.flags = 0;
-
-       memset(&init, 0, sizeof(init));
-       init.name = path;
-       init.flags = 0;
-
-       r = vserver(VCMD_rem_dlimit, xid, &init);
-       if (r<0){}
-       r = vserver(VCMD_add_dlimit, xid, &init);
-       if (r<0){}
-       r = vserver(VCMD_set_dlimit, xid, &data);
-       if (r<0){}
-       return Py_None; 
+  char* path;
+  unsigned xid;
+  struct vc_ctx_dlimit data;
+
+  memset(&data,0,sizeof(data));
+  if (!PyArg_ParseTuple(args, "siiiiii", &path,
+                       &xid,
+                       &data.space_used,
+                       &data.space_total,
+                       &data.inodes_used,
+                       &data.inodes_total,
+                       &data.reserved))
+    return NULL;
+
+  if ((vc_add_dlimit(path, xid, 0) && errno != EEXIST) ||
+      vc_set_dlimit(path, xid, 0, &data))
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return NONE; 
+}
+
+static PyObject *
+vserver_unset_dlimit(PyObject *self, PyObject *args)
+{
+  char  *path;
+  unsigned  xid;
+
+  if (!PyArg_ParseTuple(args, "si", &path, &xid))
+    return NULL;
+
+  if (vc_rem_dlimit(path, xid, 0) && errno != ESRCH)
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return NONE; 
+}
+
+static PyObject *
+vserver_killall(PyObject *self, PyObject *args)
+{
+  xid_t  ctx;
+  int  sig;
+
+  if (!PyArg_ParseTuple(args, "Ii", &ctx, &sig))
+    return NULL;
+
+  if (vc_ctx_kill(ctx, 0, sig) && errno != ESRCH)
+    return PyErr_SetFromErrno(PyExc_OSError);
+
+  return NONE;
 }
 
 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,
+    "Remove disk limits for given vserver context" },
   { "getdlimit", vserver_get_dlimit, METH_VARARGS,
     "Get disk limits for given vserver context" },
   { "setrlimit", vserver_set_rlimit, METH_VARARGS,
     "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"},
   { NULL, NULL, 0, NULL }
 };
 
 PyMODINIT_FUNC
 initvserverimpl(void)
 {
-  Py_InitModule("vserverimpl", methods);
+  PyObject  *mod;
+
+  mod = Py_InitModule("vserverimpl", methods);
+
+  /* export the set of 'safe' capabilities */
+  PyModule_AddIntConstant(mod, "CAP_SAFE", ~vc_get_insecurebcaps());
+
+  /* export the default vserver directory */
+  PyModule_AddStringConstant(mod, "VSERVER_BASEDIR", DEFAULT_VSERVERDIR);
+
+  /* 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_KEEP", (int)VC_LIM_KEEP);
+
+  PyModule_AddIntConstant(mod, "RLIMIT_CPU", (int)RLIMIT_CPU);
+  PyModule_AddIntConstant(mod, "RLIMIT_RSS", (int)RLIMIT_RSS);
+  PyModule_AddIntConstant(mod, "RLIMIT_NPROC", (int)RLIMIT_NPROC);
+  PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", (int)RLIMIT_NOFILE);
+  PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", (int)RLIMIT_MEMLOCK);
+  PyModule_AddIntConstant(mod, "RLIMIT_AS", (int)RLIMIT_AS);
+  PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", (int)RLIMIT_LOCKS);
+
+  PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", (int)RLIMIT_SIGPENDING);
+  PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", (int)RLIMIT_MSGQUEUE);
+
+  PyModule_AddIntConstant(mod, "VLIMIT_NSOCK", (int)VC_VLIMIT_NSOCK);
+  PyModule_AddIntConstant(mod, "VLIMIT_OPENFD", (int)VC_VLIMIT_OPENFD);
+  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);
 }