force symlink creation
[util-vserver.git] / src / vsh.c
index 64cf739..7d28bf4 100644 (file)
--- a/src/vsh.c
+++ b/src/vsh.c
@@ -44,6 +44,7 @@
 
 //--------------------------------------------------------------------
 #include "vserver.h"
+#include "planetlab.h"
 
 #undef CONFIG_VSERVER_LEGACY
 
@@ -205,19 +206,14 @@ static int sandbox_chroot(uid_t uid)
 
 struct resources {
        char *name;
-       int *limit;
+       unsigned long long *limit;
 };
 
 #define VSERVERCONF "/etc/vservers/"
-static void get_limits(char *context, int *cpu, int *mem, int *task) {
+static void get_limits(char *context, struct resources *list){
        FILE *fb;
        size_t len = strlen(VSERVERCONF) + strlen(context) + strlen(".conf") + NULLBYTE_SIZE;
        char *conf = (char *)malloc(len);       
-       struct resources list[] = 
-               {{"MEMLIMIT", mem},
-                {"CPULIMIT", cpu},
-                {"TASKLIMIT", task},
-                {0,0}};
        struct resources *r;
 
        sprintf(conf, "%s%s.conf", VSERVERCONF, context);
@@ -267,7 +263,7 @@ static void get_limits(char *context, int *cpu, int *mem, int *task) {
 }
 
 
-static int sandbox_processes(xid_t xid, char *context)
+static int sandbox_processes(xid_t ctx, char *context)
 {
 #ifdef CONFIG_VSERVER_LEGACY
        int     flags;
@@ -276,7 +272,7 @@ static int sandbox_processes(xid_t xid, char *context)
        flags |= 1; /* VX_INFO_LOCK -- cannot request a new vx_id */
        /* flags |= 4; VX_INFO_NPROC -- limit number of procs in a context */
 
-       (void) vc_new_s_context(xid, 0, flags);
+       (void) vc_new_s_context(ctx, 0, flags);
 
        /* use legacy dirty hack for capremove */
        if (vc_new_s_context(VC_SAMECTX, vc_get_insecurebcaps(), flags) == VC_NOCTX) {
@@ -285,72 +281,61 @@ static int sandbox_processes(xid_t xid, char *context)
                exit(1);
        }
 #else
-       struct vc_rlimit limits;
-       struct vc_ctx_caps caps;
-       struct vc_ctx_flags flags;
-       int ctx;
-       int cpu = VC_LIM_KEEP;
-       int mem = VC_LIM_KEEP;
-       int task = VC_LIM_KEEP;
-       get_limits(context,&cpu, &mem, &task);
-       (void) (sandbox_chroot(xid));
-
-       caps.ccaps = ~vc_get_insecureccaps();
-       caps.cmask = ~0ull;
-       caps.bcaps = ~vc_get_insecurebcaps();
-       caps.bmask = ~0ull;
-
-       flags.flagword = VC_VXF_INFO_LOCK;
-       flags.mask = VC_VXF_STATE_SETUP | VC_VXF_INFO_LOCK;
-
-       ctx = vc_ctx_create(xid); 
-       if (ctx == VC_NOCTX && errno != EEXIST) {
-               PERROR("vc_ctx_create(%d)", xid);
-               exit(1);
-       }
-
-       /* (re)set the various limits on the (possibly new) context */
-
-       /* CPU    */
-       /* not yet */
+       int  ctx_is_new;
+       unsigned long long cpu = VC_LIM_KEEP;
+       unsigned long long mem = VC_LIM_KEEP;
+       unsigned long long task = VC_LIM_KEEP;
+       unsigned long long cpuguaranteed = 0;
+       struct resources list[] = 
+               {{"MEMLIMIT", &mem},
+                {"CPULIMIT", &cpu},
+                {"CPUGUARANTEED", &cpuguaranteed},
+                {"TASKLIMIT", &task},
+                {0,0}};
 
-       /* MEM    */
-       limits.min  = VC_LIM_KEEP;
-       limits.soft = VC_LIM_KEEP;
-       limits.hard = mem;
-       if (vc_set_rlimit(xid, RLIMIT_RSS, &limits)) {
-               PERROR("vc_set_rlimit(%d, %d, %d/%d/%d)",
-                      xid, RLIMIT_RSS, limits.min, limits.soft, limits.hard);
+       get_limits(context,list);
+
+       /* check whether the slice has been disabled */
+       if (!cpu)
+         {
+           fprintf(stderr, "*** this slice has been suspended ***\n");
+           exit(0);
+         }
+
+       (void) (sandbox_chroot(ctx));
+
+        if ((ctx_is_new = pl_chcontext(ctx, 0, ~vc_get_insecurebcaps())) < 0)
+          {
+            PERROR("pl_chcontext(%u)", ctx);
+            exit(1);
+          }
+       if (ctx_is_new)
+         {
+           /* set resources */
+           struct vc_rlimit limits;
+
+           limits.min = VC_LIM_KEEP;
+           limits.soft = VC_LIM_KEEP;
+           limits.hard = mem;
+           if (vc_set_rlimit(ctx, RLIMIT_RSS, &limits))
+             {
+               PERROR("pl_setrlimit(%u, RLIMIT_RSS)", ctx);
                exit(1);
-       }
-       
-       /* TASK   */
-       limits.min  = VC_LIM_KEEP;
-       limits.soft = VC_LIM_KEEP;
-       limits.hard = task;
-       if (vc_set_rlimit(xid, RLIMIT_NPROC, &limits)) {
-               PERROR("vc_set_rlimit(%d, %d, %d/%d/%d)",
-                      xid, RLIMIT_NPROC, limits.min, limits.soft, limits.hard);
+             }
+           limits.hard = task;
+           if (vc_set_rlimit(ctx, RLIMIT_NPROC, &limits))
+             {
+               PERROR("pl_setrlimit(%u, RLIMIT_NPROC)", ctx);
                exit(1);
-       }
-       
-       if (vc_set_ccaps(xid, &caps) == -1) {
-               PERROR("vc_set_ccaps(%d, 0x%16ullx/0x%16ullx, 0x%16ullx/0x%16ullx)",
-                      xid, caps.ccaps, caps.cmask, caps.bcaps, caps.bmask);
-               exit(1);
-       }
-
-       if (vc_set_cflags(xid, &flags) == -1) {
-               PERROR("vc_set_cflags(%d, 0x%16llx/0x%16llx)",
-                      xid, flags.flagword, flags.mask);
+             }
+           cpuguaranteed &= VS_SCHED_CPU_GUARANTEED;
+           if (pl_setsched(ctx, cpu, cpuguaranteed) < 0)
+             {
+               PERROR("pl_setsched(&u)", ctx);
                exit(1);
-       }
-
-       /* context already exists, migrate to it */
-       if (ctx == VC_NOCTX && vc_ctx_migrate(xid) == -1) {
-               PERROR("vc_ctx_migrate(%d)", xid);
-               exit(1);
-       }
+             }
+           pl_setup_done(ctx);
+         }
 #endif
        return 0;
 }