From: Robert Swiecki Date: Sat, 10 Feb 2018 13:38:01 +0000 (+0100) Subject: mnt: replace sys/queue with std::vector X-Git-Tag: 2.5~44 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ecd4c32d9a2eaef68514c6373efcc7c3fc036f85;p=platform%2Fupstream%2Fnsjail.git mnt: replace sys/queue with std::vector --- diff --git a/Makefile b/Makefile index 3176b20..2f48990 100644 --- a/Makefile +++ b/Makefile @@ -94,8 +94,8 @@ caps.o: caps.h nsjail.h log.h macros.h util.h cgroup.o: cgroup.h nsjail.h log.h util.h cmdline.o: cmdline.h nsjail.h caps.h config.h log.h macros.h mnt.h sandbox.h cmdline.o: user.h util.h -config.o: caps.h nsjail.h cmdline.h config.h log.h macros.h mnt.h user.h -config.o: util.h +config.o: caps.h nsjail.h cmdline.h config.h config.pb.h log.h macros.h mnt.h +config.o: user.h util.h contain.o: contain.h nsjail.h caps.h cgroup.h cpu.h log.h mnt.h net.h pid.h contain.o: user.h uts.h cpu.o: cpu.h nsjail.h log.h util.h @@ -110,3 +110,4 @@ subproc.o: sandbox.h user.h util.h uts.o: uts.h nsjail.h log.h user.o: user.h nsjail.h log.h macros.h subproc.h util.h util.o: util.h nsjail.h log.h macros.h +config.pb.o: config.pb.h diff --git a/cmdline.cc b/cmdline.cc index a9835cb..d14e63b 100644 --- a/cmdline.cc +++ b/cmdline.cc @@ -235,9 +235,8 @@ void logParams(struct nsjconf_t* nsjconf) { logYesNo(nsjconf->disable_no_new_privs), nsjconf->max_cpus); { - struct mounts_t* p; - TAILQ_FOREACH(p, &nsjconf->mountpts, pointers) { - LOG_I("%s: %s", p->isSymlink ? "Symlink" : "Mount point", + for (const auto& p : nsjconf->mountpts) { + LOG_I("%s: %s", p.isSymlink ? "Symlink" : "Mount point", mnt::describeMountPt(p)); } } @@ -388,8 +387,6 @@ std::unique_ptr parseArgs(int argc, char* argv[]) { nsjconf->openfds.push_back(STDOUT_FILENO); nsjconf->openfds.push_back(STDERR_FILENO); - TAILQ_INIT(&nsjconf->mountpts); - static char cmdlineTmpfsSz[PATH_MAX] = "size=4194304"; // Generate options array for getopt_long. diff --git a/contain.cc b/contain.cc index e0eb293..ff0eb89 100644 --- a/contain.cc +++ b/contain.cc @@ -33,7 +33,6 @@ #include #include #include -#include #include #include diff --git a/mnt.cc b/mnt.cc index 35b4c82..25389e8 100644 --- a/mnt.cc +++ b/mnt.cc @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -119,26 +118,26 @@ static bool isDir(const char* path) { return false; } -static bool mountPt(struct mounts_t* mpt, const char* newroot, const char* tmpdir) { +static bool mountPt(struct mount_t* mpt, const char* newroot, const char* tmpdir) { char dst[PATH_MAX]; - snprintf(dst, sizeof(dst), "%s/%s", newroot, mpt->dst); + snprintf(dst, sizeof(dst), "%s/%s", newroot, mpt->dst.c_str()); - LOG_D("Mounting '%s'", describeMountPt(mpt)); + LOG_D("Mounting '%s'", describeMountPt(*mpt)); char srcpath[PATH_MAX]; - if (mpt->src != NULL && strlen(mpt->src) > 0) { - snprintf(srcpath, sizeof(srcpath), "%s", mpt->src); + if (!mpt->src.empty()) { + snprintf(srcpath, sizeof(srcpath), "%s", mpt->src.c_str()); } else { snprintf(srcpath, sizeof(srcpath), "none"); } if (mpt->isSymlink) { - if (util::createDirRecursively(dst) == false) { + if (!util::createDirRecursively(dst)) { LOG_W("Couldn't create upper directories for '%s'", dst); return false; } } else if (mpt->isDir) { - if (util::createDirRecursively(dst) == false) { + if (!util::createDirRecursively(dst)) { LOG_W("Couldn't create upper directories for '%s'", dst); return false; } @@ -146,7 +145,7 @@ static bool mountPt(struct mounts_t* mpt, const char* newroot, const char* tmpdi PLOG_W("mkdir('%s')", dst); } } else { - if (util::createDirRecursively(dst) == false) { + if (!util::createDirRecursively(dst)) { LOG_W("Couldn't create upper directories for '%s'", dst); return false; } @@ -172,7 +171,7 @@ static bool mountPt(struct mounts_t* mpt, const char* newroot, const char* tmpdi return true; } - if (mpt->src_content) { + if (!mpt->src_content.empty()) { static uint64_t df_counter = 0; snprintf( srcpath, sizeof(srcpath), "%s/dynamic_file.%" PRIu64, tmpdir, ++df_counter); @@ -182,8 +181,10 @@ static bool mountPt(struct mounts_t* mpt, const char* newroot, const char* tmpdi PLOG_W("open(srcpath, O_CREAT|O_EXCL|O_CLOEXEC|O_WRONLY, 0644) failed"); return false; } - if (util::writeToFd(fd, mpt->src_content, mpt->src_content_len) == false) { - LOG_W("Writting %zu bytes to '%s' failed", mpt->src_content_len, srcpath); + if (util::writeToFd(fd, mpt->src_content.data(), mpt->src_content.length()) == + false) { + LOG_W("Writting %zu bytes to '%s' failed", mpt->src_content.length(), + srcpath); close(fd); return false; } @@ -195,17 +196,17 @@ static bool mountPt(struct mounts_t* mpt, const char* newroot, const char* tmpdi * Initially mount it as RW, it will be remounted later on if needed */ unsigned long flags = mpt->flags & ~(MS_RDONLY); - if (mount(srcpath, dst, mpt->fs_type, flags, mpt->options) == -1) { + if (mount(srcpath, dst, mpt->fs_type.c_str(), flags, mpt->options.c_str()) == -1) { if (errno == EACCES) { PLOG_W( "mount('%s') src:'%s' dst:'%s' failed. " "Try fixing this problem by applying 'chmod o+x' to the '%s' " "directory and its ancestors", - describeMountPt(mpt), srcpath, dst, srcpath); + describeMountPt(*mpt), srcpath, dst, srcpath); } else { - PLOG_W("mount('%s') src:'%s' dst:'%s' failed", describeMountPt(mpt), + PLOG_W("mount('%s') src:'%s' dst:'%s' failed", describeMountPt(*mpt), srcpath, dst); - if (mpt->fs_type && strcmp(mpt->fs_type, "proc") == 0) { + if (strcmp(mpt->fs_type.c_str(), "proc") == 0) { PLOG_W( "procfs can only be mounted if the original /proc doesn't have " "any other file-systems mounted on top of it (e.g. /dev/null " @@ -217,26 +218,26 @@ static bool mountPt(struct mounts_t* mpt, const char* newroot, const char* tmpdi mpt->mounted = true; } - if (mpt->src_content && unlink(srcpath) == -1) { + if (!mpt->src_content.empty() && unlink(srcpath) == -1) { PLOG_W("unlink('%s')", srcpath); } return true; } -static bool remountRO(struct mounts_t* mpt) { - if (!mpt->mounted) { +static bool remountRO(const struct mount_t& mpt) { + if (!mpt.mounted) { return true; } - if (mpt->isSymlink) { + if (mpt.isSymlink) { return true; } - if ((mpt->flags & MS_RDONLY) == 0) { + if ((mpt.flags & MS_RDONLY) == 0) { return true; } struct statvfs vfs; - if (TEMP_FAILURE_RETRY(statvfs(mpt->dst, &vfs)) == -1) { - PLOG_W("statvfs('%s')", mpt->dst); + if (TEMP_FAILURE_RETRY(statvfs(mpt.dst.c_str(), &vfs)) == -1) { + PLOG_W("statvfs('%s')", mpt.dst.c_str()); return false; } @@ -262,9 +263,9 @@ static bool remountRO(struct mounts_t* mpt) { } } - LOG_D("Re-mounting R/O '%s' (flags:%s)", mpt->dst, flagsToStr(new_flags)); - if (mount(mpt->dst, mpt->dst, NULL, new_flags, 0) == -1) { - PLOG_W("mount('%s', flags:%s)", mpt->dst, flagsToStr(new_flags)); + LOG_D("Re-mounting R/O '%s' (flags:%s)", mpt.dst.c_str(), flagsToStr(new_flags)); + if (mount(mpt.dst.c_str(), mpt.dst.c_str(), NULL, new_flags, 0) == -1) { + PLOG_W("mount('%s', flags:%s)", mpt.dst.c_str(), flagsToStr(new_flags)); return false; } @@ -318,7 +319,7 @@ static bool initNsInternal(struct nsjconf_t* nsjconf) { * If CLONE_NEWNS is not used, we would be changing the global mount namespace, so simply * use --chroot in this case */ - if (nsjconf->clone_newns == false) { + if (!nsjconf->clone_newns) { if (nsjconf->chroot.empty()) { PLOG_E( "--chroot was not specified, and it's required when not using " @@ -342,7 +343,7 @@ static bool initNsInternal(struct nsjconf_t* nsjconf) { } char destdir[PATH_MAX]; - if (getDir(nsjconf, destdir, "root") == false) { + if (!getDir(nsjconf, destdir, "root")) { LOG_E("Couldn't obtain root mount directories"); return false; } @@ -358,7 +359,7 @@ static bool initNsInternal(struct nsjconf_t* nsjconf) { } char tmpdir[PATH_MAX]; - if (getDir(nsjconf, tmpdir, "tmp") == false) { + if (!getDir(nsjconf, tmpdir, "tmp")) { LOG_E("Couldn't obtain temporary mount directories"); return false; } @@ -367,9 +368,8 @@ static bool initNsInternal(struct nsjconf_t* nsjconf) { return false; } - struct mounts_t* p; - TAILQ_FOREACH(p, &nsjconf->mountpts, pointers) { - if (mountPt(p, destdir, tmpdir) == false && p->mandatory) { + for (auto& p : nsjconf->mountpts) { + if (!mountPt(&p, destdir, tmpdir) && p.mandatory) { return false; } } @@ -399,8 +399,8 @@ static bool initNsInternal(struct nsjconf_t* nsjconf) { return false; } - TAILQ_FOREACH(p, &nsjconf->mountpts, pointers) { - if (remountRO(p) == false && p->mandatory) { + for (const auto& p : nsjconf->mountpts) { + if (!remountRO(p) && p.mandatory) { return false; } } @@ -435,25 +435,19 @@ bool initNs(struct nsjconf_t* nsjconf) { return false; } -static bool addMountPt(struct nsjconf_t* nsjconf, bool head, const char* src, const char* dst, - const char* fstype, const char* options, uintptr_t flags, isDir_t isDir, bool mandatory, - const char* src_env, const char* dst_env, const char* src_content, size_t src_content_len, - bool is_symlink) { - struct mounts_t* p = - reinterpret_cast(util::clearAlloc(sizeof(struct mounts_t))); - +static bool addMountPt(struct mount_t* mnt, const char* src, const char* dst, const char* fstype, + const char* options, uintptr_t flags, isDir_t isDir, bool mandatory, const char* src_env, + const char* dst_env, const char* src_content, size_t src_content_len, bool is_symlink) { if (src_env) { const char* e = getenv(src_env); if (e == NULL) { LOG_W("No such envvar:'%s'", src_env); return false; } - if (asprintf((char**)&p->src, "%s%s", e, src ? src : "") == -1) { - PLOG_W("asprintf() failed"); - return false; - } - } else { - p->src = util::strDup(src); + mnt->src = e; + } + if (src) { + mnt->src.append(src); } if (dst_env) { @@ -462,38 +456,43 @@ static bool addMountPt(struct nsjconf_t* nsjconf, bool head, const char* src, co LOG_W("No such envvar:'%s'", dst_env); return false; } - if (asprintf((char**)&p->dst, "%s%s", e, dst ? dst : "") == -1) { - PLOG_W("asprintf() failed"); - return false; - } - } else { - p->dst = util::strDup(dst); + mnt->dst = e; + } + if (dst) { + mnt->dst.append(dst); } - p->fs_type = util::strDup(fstype); - p->options = util::strDup(options); - p->flags = flags; - p->isDir = true; - p->isSymlink = is_symlink; - p->mandatory = mandatory; - p->mounted = false; + if (fstype) { + mnt->fs_type = fstype; + } + if (options) { + mnt->options = options; + } + if (src_content) { + mnt->src_content.assign(src_content, src_content_len); + } + mnt->flags = flags; + mnt->isDir = true; + mnt->isSymlink = is_symlink; + mnt->mandatory = mandatory; + mnt->mounted = false; switch (isDir) { case NS_DIR_YES: - p->isDir = true; + mnt->isDir = true; break; case NS_DIR_NO: - p->isDir = false; + mnt->isDir = false; break; case NS_DIR_MAYBE: { if (src_content) { - p->isDir = false; - } else if (p->src == NULL) { - p->isDir = true; - } else if (p->flags & MS_BIND) { - p->isDir = mnt::isDir(p->src); + mnt->isDir = false; + } else if (mnt->src.empty()) { + mnt->isDir = true; + } else if (mnt->flags & MS_BIND) { + mnt->isDir = mnt::isDir(mnt->src.c_str()); } else { - p->isDir = true; + mnt->isDir = true; } } break; default: @@ -501,49 +500,49 @@ static bool addMountPt(struct nsjconf_t* nsjconf, bool head, const char* src, co break; } - p->src_content = util::memDup((const uint8_t*)src_content, src_content_len); - p->src_content_len = src_content_len; - - if (head) { - TAILQ_INSERT_HEAD(&nsjconf->mountpts, p, pointers); - } else { - TAILQ_INSERT_TAIL(&nsjconf->mountpts, p, pointers); - } - return true; } bool addMountPtHead(struct nsjconf_t* nsjconf, const char* src, const char* dst, const char* fstype, const char* options, uintptr_t flags, isDir_t isDir, bool mandatory, const char* src_env, const char* dst_env, const char* src_content, size_t src_content_len, bool is_symlink) { - return addMountPt(nsjconf, /* head= */ true, src, dst, fstype, options, flags, isDir, - mandatory, src_env, dst_env, src_content, src_content_len, is_symlink); + struct mount_t mnt; + if (!addMountPt(&mnt, src, dst, fstype, options, flags, isDir, mandatory, src_env, dst_env, + src_content, src_content_len, is_symlink)) { + return false; + } + nsjconf->mountpts.insert(nsjconf->mountpts.begin(), mnt); + return true; } bool addMountPtTail(struct nsjconf_t* nsjconf, const char* src, const char* dst, const char* fstype, const char* options, uintptr_t flags, isDir_t isDir, bool mandatory, const char* src_env, const char* dst_env, const char* src_content, size_t src_content_len, bool is_symlink) { - return addMountPt(nsjconf, /* head= */ false, src, dst, fstype, options, flags, isDir, - mandatory, src_env, dst_env, src_content, src_content_len, is_symlink); + struct mount_t mnt; + if (!addMountPt(&mnt, src, dst, fstype, options, flags, isDir, mandatory, src_env, dst_env, + src_content, src_content_len, is_symlink)) { + return false; + } + nsjconf->mountpts.push_back(mnt); + return true; } -const char* describeMountPt(struct mounts_t* mpt) { +const char* describeMountPt(const struct mount_t& mpt) { static __thread char mount_pt_descr[4096]; snprintf(mount_pt_descr, sizeof(mount_pt_descr), - "src:'%s' dst:'%s' type:'%s' flags:%s options:'%s' isDir:%s", - mpt->src ? mpt->src : "[NULL]", mpt->dst, mpt->fs_type ? mpt->fs_type : "[NULL]", - flagsToStr(mpt->flags), mpt->options ? mpt->options : "[NULL]", - mpt->isDir ? "true" : "false"); + "src:'%s' dst:'%s' type:'%s' flags:%s options:'%s' isDir:%s", mpt.src.c_str(), + mpt.dst.c_str(), mpt.fs_type.c_str(), flagsToStr(mpt.flags), mpt.options.c_str(), + mpt.isDir ? "true" : "false"); - if (mpt->mandatory == false) { + if (!mpt.mandatory) { util::sSnPrintf(mount_pt_descr, sizeof(mount_pt_descr), " mandatory:false"); } - if (mpt->src_content) { + if (!mpt.src_content.empty()) { util::sSnPrintf(mount_pt_descr, sizeof(mount_pt_descr), " src_content_len:%zu", - mpt->src_content_len); + mpt.src_content.length()); } - if (mpt->isSymlink) { + if (mpt.isSymlink) { util::sSnPrintf(mount_pt_descr, sizeof(mount_pt_descr), " symlink:true"); } diff --git a/mnt.h b/mnt.h index 76cfd95..e51cefa 100644 --- a/mnt.h +++ b/mnt.h @@ -43,7 +43,7 @@ bool addMountPtHead(struct nsjconf_t* nsjconf, const char* src, const char* dst, bool addMountPtTail(struct nsjconf_t* nsjconf, const char* src, const char* dst, const char* fstype, const char* options, uintptr_t flags, isDir_t isDir, bool mandatory, const char* src_env, const char* dst_env, const char* src_content, size_t src_content_len, bool is_symlink); -const char* describeMountPt(struct mounts_t* mpt); +const char* describeMountPt(const struct mount_t& mpt); } // namespace mnt diff --git a/net.cc b/net.cc index 2e046a0..efd3afd 100644 --- a/net.cc +++ b/net.cc @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include diff --git a/nsjail.h b/nsjail.h index 7b776a9..10d0d45 100644 --- a/nsjail.h +++ b/nsjail.h @@ -29,7 +29,6 @@ #include #include #include -#include #include #include @@ -53,19 +52,17 @@ struct pids_t { int pid_syscall_fd; }; -struct mounts_t { - const char* src; - const uint8_t* src_content; - size_t src_content_len; - const char* dst; - const char* fs_type; - const char* options; +struct mount_t { + std::string src; + std::string src_content; + std::string dst; + std::string fs_type; + std::string options; uintptr_t flags; bool isDir; bool isSymlink; bool mandatory; bool mounted; - TAILQ_ENTRY(mounts_t) pointers; }; struct idmap_t { @@ -157,8 +154,7 @@ struct nsjconf_t { struct sock_fprog seccomp_fprog; long num_cpus; uid_t orig_uid; - TAILQ_HEAD(mountptslist, mounts_t) - mountpts; + std::vector mountpts; std::vector pids; std::vector uids; std::vector gids; diff --git a/subproc.cc b/subproc.cc index 6f563cb..7be4a92 100644 --- a/subproc.cc +++ b/subproc.cc @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include diff --git a/user.cc b/user.cc index da499c3..9ff2bf1 100644 --- a/user.cc +++ b/user.cc @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include