mnt: replace sys/queue with std::vector
authorRobert Swiecki <robert@swiecki.net>
Sat, 10 Feb 2018 13:38:01 +0000 (14:38 +0100)
committerRobert Swiecki <robert@swiecki.net>
Sat, 10 Feb 2018 13:38:01 +0000 (14:38 +0100)
Makefile
cmdline.cc
contain.cc
mnt.cc
mnt.h
net.cc
nsjail.h
subproc.cc
user.cc

index 3176b20933880022c3bffb3c0c7d9c254c6cf861..2f48990157e0ba1310ee7c81157c0e60addfaf68 100644 (file)
--- 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
index a9835cb6e6c80e04edca3c2118bdec6e0c9a51b5..d14e63bece7931e3e57793a991d0b4e702847b23 100644 (file)
@@ -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<struct nsjconf_t> 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.
index e0eb2936056247f1dfa2a36b7fe174433055919b..ff0eb8957e3f7ae99a33875122b2bd62a5ff7f0c 100644 (file)
@@ -33,7 +33,6 @@
 #include <string.h>
 #include <sys/personality.h>
 #include <sys/prctl.h>
-#include <sys/queue.h>
 #include <sys/resource.h>
 #include <unistd.h>
 
diff --git a/mnt.cc b/mnt.cc
index 35b4c82520e6814f4bdb603df73b78f55b8d9d91..25389e8c107c9bd527d0e415e17bafbe03dfde5e 100644 (file)
--- a/mnt.cc
+++ b/mnt.cc
@@ -32,7 +32,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mount.h>
-#include <sys/queue.h>
 #include <sys/stat.h>
 #include <sys/statvfs.h>
 #include <sys/syscall.h>
@@ -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<struct mounts_t*>(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 76cfd955e3a3beac5a15fd758ff6fef2cda8b634..e51cefa60004bc5a2febf7841474dc2701fd39ad 100644 (file)
--- 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 2e046a0ed9b6c350c4e3104f2adbf0cb335d9e89..efd3afd3dca245063a1e5f467a6512791e2cf90e 100644 (file)
--- a/net.cc
+++ b/net.cc
@@ -34,7 +34,6 @@
 #include <string.h>
 #include <strings.h>
 #include <sys/ioctl.h>
-#include <sys/queue.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/types.h>
index 7b776a93add87152e053f7f4efe1df25b76f26f8..10d0d452cd9f225f0ee8d6ce75fbb3d9a4b1904b 100644 (file)
--- a/nsjail.h
+++ b/nsjail.h
@@ -29,7 +29,6 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
-#include <sys/queue.h>
 #include <time.h>
 #include <unistd.h>
 
@@ -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<mount_t> mountpts;
        std::vector<pids_t> pids;
        std::vector<idmap_t> uids;
        std::vector<idmap_t> gids;
index 6f563cb0f2c04a9725c1228d97536616595d666e..7be4a92bacfac869e78c20d60b5ffb06ab7ef89a 100644 (file)
@@ -34,7 +34,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/queue.h>
 #include <sys/socket.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
diff --git a/user.cc b/user.cc
index da499c38da2ddaa30d16c430caefbbf214d1809c..9ff2bf1a59c19eac1beb9824b4ce769556529030 100644 (file)
--- a/user.cc
+++ b/user.cc
@@ -34,7 +34,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/prctl.h>
-#include <sys/queue.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
 #include <unistd.h>