From: Robert Swiecki Date: Thu, 22 Jun 2017 01:06:53 +0000 (+0200) Subject: Get number of CPUs early, as it's read from /proc X-Git-Tag: 1.5~29 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7e0a4cdba8045ef405ad5b0b20ce11032412c211;p=platform%2Fupstream%2Fnsjail.git Get number of CPUs early, as it's read from /proc --- diff --git a/cmdline.c b/cmdline.c index 458edf3..ffdf4f2 100644 --- 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* */ diff --git a/common.h b/common.h index f16c12d..500d972 100644 --- 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 08ad41f..97b0a64 100644 --- 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 057d91f..1f54d55 100644 --- 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; }