Clean up code for setting rlimits
[util-vserver-pl.git] / python / vserverimpl.c
index 3ac9069..9d4a503 100644 (file)
@@ -145,9 +145,10 @@ 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;
+  int resource;
   PyObject *ret;
 
   limits.min = VC_LIM_KEEP;
@@ -157,30 +158,18 @@ 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<<resource);
+    if ((mask.min|mask.soft|mask.hard) & bitmask) 
+      if (vc_set_rlimit(xid, resource, &limits)==-1) 
+       ret = PyErr_SetFromErrno(PyExc_OSError);
+      else
+       ret = __vserver_get_rlimit(xid, resource);
+  }
 
   return ret;
 }
@@ -192,14 +181,14 @@ static PyObject *
 vserver_setsched(PyObject *self, PyObject *args)
 {
   xid_t  ctx;
+  uint32_t  cpu_min;
   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))
+  if (!PyArg_ParseTuple(args, "II|I", &ctx, &cpu_min, &cpu_share))
     return NULL;
 
   /* ESRCH indicates that there are no processes in the context */
-  if (pl_setsched(ctx, cpu_share, cpu_sched_flags) &&
+  if (pl_setsched(ctx, cpu_min, cpu_share) &&
       errno != ESRCH)
     return PyErr_SetFromErrno(PyExc_OSError);
 
@@ -430,7 +419,7 @@ get_mask(struct vc_net_addr *addr)
     addr->vna_prefix = 64;
     break;
   default:
-    errno = -EINVAL;
+    errno = EINVAL;
     return -1;
   }
 
@@ -505,12 +494,13 @@ vserver_net_remove(PyObject *self, PyObject *args)
   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)
@@ -739,8 +729,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);
 }