util: remove file only of O_CREAT was specified
authorRobert Swiecki <robert@swiecki.net>
Sat, 17 Feb 2018 14:27:00 +0000 (15:27 +0100)
committerRobert Swiecki <robert@swiecki.net>
Sat, 17 Feb 2018 14:27:00 +0000 (15:27 +0100)
Makefile
net.cc
util.cc

index 6360c8c6ddc82b2d3a50ff2fe2e1eb0efcfa0fec..bf606064757774c15d22375126e275d410f3cf90 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -106,8 +106,8 @@ caps.o: caps.h nsjail.h logs.h macros.h util.h
 cgroup.o: cgroup.h nsjail.h logs.h util.h
 cmdline.o: cmdline.h nsjail.h logs.h caps.h config.h macros.h mnt.h user.h
 cmdline.o: util.h
-config.o: caps.h nsjail.h logs.h cmdline.h config.h config.pb.h macros.h
-config.o: mnt.h user.h util.h
+config.o: caps.h nsjail.h logs.h cmdline.h config.h macros.h mnt.h user.h
+config.o: util.h
 contain.o: contain.h nsjail.h logs.h caps.h cgroup.h cpu.h mnt.h net.h pid.h
 contain.o: user.h uts.h
 cpu.o: cpu.h nsjail.h logs.h util.h
@@ -122,4 +122,3 @@ subproc.o: sandbox.h user.h util.h
 uts.o: uts.h nsjail.h logs.h
 user.o: user.h nsjail.h logs.h macros.h subproc.h util.h
 util.o: util.h nsjail.h logs.h macros.h
-config.pb.o: config.pb.h
diff --git a/net.cc b/net.cc
index 1b290a9c172a6a48f0a734a978fa76365eea17db..8d76c12a92c413f75272c2922b78654cda1ce21f 100644 (file)
--- a/net.cc
+++ b/net.cc
@@ -135,9 +135,8 @@ bool initNsFromParent(nsjconf_t* nsjconf, int pid) {
        char pid_str[256];
        snprintf(pid_str, sizeof(pid_str), "%d", pid);
 
-       const std::vector<std::string> argv{"/sbin/ip", "link", "add", "link",
-           (char*)nsjconf->iface_vs.c_str(), "name", IFACE_NAME, "netns", pid_str, "type",
-           "macvlan", "mode", "bridge"};
+       const std::vector<std::string> argv{"/sbin/ip", "link", "add", "link", nsjconf->iface_vs,
+           "name", IFACE_NAME, "netns", pid_str, "type", "macvlan", "mode", "bridge"};
        if (subproc::systemExe(argv, environ) != 0) {
                LOG_E("Couldn't create MACVTAP interface for '%s'", nsjconf->iface_vs.c_str());
                return false;
diff --git a/util.cc b/util.cc
index 648138a2f93dfc8a5e87f473bc952b5ea152c345..9975c634f02916d4e6ece433065109ef5db6daca 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -100,7 +100,9 @@ bool writeBufToFile(const char* filename, const void* buf, size_t len, int open_
        if (!writeToFd(fd, buf, len)) {
                PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);
                close(fd);
-               unlink(filename);
+               if (open_flags & O_CREAT) {
+                       unlink(filename);
+               }
                return false;
        }