Get number of CPUs early, as it's read from /proc
authorRobert Swiecki <robert@swiecki.net>
Thu, 22 Jun 2017 01:06:53 +0000 (03:06 +0200)
committerRobert Swiecki <robert@swiecki.net>
Thu, 22 Jun 2017 01:06:53 +0000 (03:06 +0200)
cmdline.c
common.h
cpu.c
mount.c

index 458edf3b0220154a6c10d7c3369a5d7ef24b47ac..ffdf4f2cb0b3c3c56716017f18f7989e1e52ae84 100644 (file)
--- a/cmdline.c
+++ b/cmdline.c
@@ -353,7 +353,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
       .iface_vs_gw = "0.0.0.0",
       .kafel_file = NULL,
       .kafel_string = NULL,
-      .orig_euid = geteuid(),
+      .num_cpus = sysconf(_SC_NPROCESSORS_ONLN),
   };
   /*  *INDENT-ON* */
 
index f16c12d997840a80df001ec51e65caa177b6d725..500d97209177ac301f6866d69a5eb02e99757dc3 100644 (file)
--- a/common.h
+++ b/common.h
@@ -165,6 +165,7 @@ struct nsjconf_t {
        FILE *kafel_file;
        char *kafel_string;
        uid_t orig_euid;
+       long num_cpus;
         TAILQ_HEAD(udmaplist, idmap_t) uids;
         TAILQ_HEAD(gdmaplist, idmap_t) gids;
         TAILQ_HEAD(envlist, charptr_t) envs;
diff --git a/cpu.c b/cpu.c
index 08ad41fb73b23273b663570beda02f16b68926ae..97b0a649afb380b7a6b2699119ebfc9cdbaad6d6 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -48,18 +48,17 @@ static void cpuSetRandomCpu(cpu_set_t * mask, size_t mask_size, size_t cpu_num)
 
 bool cpuInit(struct nsjconf_t *nsjconf)
 {
-       long all_cpus = sysconf(_SC_NPROCESSORS_ONLN);
-       if (all_cpus < 0) {
-               PLOG_W("sysconf(_SC_NPROCESSORS_ONLN) returned %ld", all_cpus);
+       if (nsjconf->num_cpus < 0) {
+               PLOG_W("sysconf(_SC_NPROCESSORS_ONLN) returned %ld", nsjconf->num_cpus);
                return false;
        }
-       if (nsjconf->max_cpus > (size_t) all_cpus) {
+       if (nsjconf->max_cpus > (size_t) nsjconf->num_cpus) {
                LOG_W("Requested number of CPUs:%zu is bigger than CPUs online:%ld",
-                     nsjconf->max_cpus, all_cpus);
+                     nsjconf->max_cpus, nsjconf->num_cpus);
                return true;
        }
-       if (nsjconf->max_cpus == (size_t) all_cpus) {
-               LOG_D("All CPUs requested (%zu of %ld)", nsjconf->max_cpus, all_cpus);
+       if (nsjconf->max_cpus == (size_t) nsjconf->num_cpus) {
+               LOG_D("All CPUs requested (%zu of %ld)", nsjconf->max_cpus, nsjconf->num_cpus);
                return true;
        }
        if (nsjconf->max_cpus == 0) {
@@ -67,17 +66,17 @@ bool cpuInit(struct nsjconf_t *nsjconf)
                return true;
        }
 
-       cpu_set_t *mask = CPU_ALLOC(all_cpus);
+       cpu_set_t *mask = CPU_ALLOC(nsjconf->num_cpus);
        if (mask == NULL) {
-               PLOG_W("Failure allocating cpu_set_t for %ld CPUs", all_cpus);
+               PLOG_W("Failure allocating cpu_set_t for %ld CPUs", nsjconf->num_cpus);
                return false;
        }
 
-       size_t mask_size = CPU_ALLOC_SIZE(all_cpus);
+       size_t mask_size = CPU_ALLOC_SIZE(nsjconf->num_cpus);
        CPU_ZERO_S(mask_size, mask);
 
        for (size_t i = 0; i < nsjconf->max_cpus; i++) {
-               cpuSetRandomCpu(mask, mask_size, all_cpus);
+               cpuSetRandomCpu(mask, mask_size, nsjconf->num_cpus);
        }
 
        if (sched_setaffinity(0, mask_size, mask) == -1) {
diff --git a/mount.c b/mount.c
index 057d91f258a040ebc416e41863b74543fd80747f..1f54d55f17c2249ac709c886abb2c6138ec65344 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -248,7 +248,7 @@ static bool mountMkdirAndTest(const char *dir)
        return true;
 }
 
-static bool mountGetDir(struct nsjconf_t *nsjconf, char *dir, const char *name)
+static bool mountGetDir(char *dir, const char *name)
 {
        snprintf(dir, PATH_MAX, "/dev/shm/nsjail.%s", name);
        if (mountMkdirAndTest(dir)) {
@@ -258,15 +258,6 @@ static bool mountGetDir(struct nsjconf_t *nsjconf, char *dir, const char *name)
        if (mountMkdirAndTest(dir)) {
                return true;
        }
-       snprintf(dir, PATH_MAX, "/tmp/nsjail.%s.%d", name, (int)nsjconf->orig_euid);
-       if (mountMkdirAndTest(dir)) {
-               return true;
-       }
-       snprintf(dir, PATH_MAX, "/tmp/nsjail.%s.%" PRIx64, name, utilRnd64());
-       if (mountMkdirAndTest(dir)) {
-               return true;
-       }
-
        const char *tmp = getenv("TMPDIR");
        if (tmp) {
                snprintf(dir, PATH_MAX, "%s/nsjail.%s", name, tmp);
@@ -274,6 +265,10 @@ static bool mountGetDir(struct nsjconf_t *nsjconf, char *dir, const char *name)
                        return true;
                }
        }
+       snprintf(dir, PATH_MAX, "/tmp/nsjail.%s.%" PRIx64, name, utilRnd64());
+       if (mountMkdirAndTest(dir)) {
+               return true;
+       }
 
        LOG_E("Couldn't create tmp directory of type '%s'", name);
        return false;
@@ -300,7 +295,7 @@ static bool mountInitNsInternal(struct nsjconf_t *nsjconf)
        }
 
        char destdir[PATH_MAX];
-       if (mountGetDir(nsjconf, destdir, "root") == false) {
+       if (mountGetDir(destdir, "root") == false) {
                LOG_E("Couldn't obtain root mount directories");
                return false;
        }
@@ -311,7 +306,7 @@ static bool mountInitNsInternal(struct nsjconf_t *nsjconf)
        }
 
        char tmpdir[PATH_MAX];
-       if (mountGetDir(nsjconf, tmpdir, "tmp") == false) {
+       if (mountGetDir(tmpdir, "tmp") == false) {
                LOG_E("Couldn't obtain temporary mount directories");
                return false;
        }