]> git.saurik.com Git - apt.git/commitdiff
Implement Popen() execv helper to avoid popen()
authorMichael Vogt <mvo@ubuntu.com>
Mon, 28 Apr 2014 14:43:58 +0000 (16:43 +0200)
committerMichael Vogt <mvo@ubuntu.com>
Mon, 28 Apr 2014 14:43:58 +0000 (16:43 +0200)
apt-pkg/contrib/fileutl.cc
apt-pkg/contrib/fileutl.h
test/libapt/fileutl_test.cc

index de73a7fd8a8f9e593d3301e6db76aff30bab5e22..8d15baad2eb6f0af6ddf67ea21743cfca4aa0047 100644 (file)
@@ -2057,3 +2057,58 @@ bool Rename(std::string From, std::string To)
    }   
    return true;
 }
+
+bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode)
+{
+   int fd;
+   if (Mode != FileFd::ReadOnly && Mode != FileFd::WriteOnly)
+      return _error->Error("Popen supports ReadOnly (x)or WriteOnly mode only");
+
+   int Pipe[2] = {-1, -1};
+   if(pipe(Pipe) != 0)
+   {
+      return _error->Errno("pipe", _("Failed to create subprocess IPC"));
+      return NULL;
+   }
+   std::set<int> keep_fds;
+   keep_fds.insert(Pipe[0]);
+   keep_fds.insert(Pipe[1]);
+   Child = ExecFork(keep_fds);
+   if(Child < 0)
+      return _error->Errno("fork", "Failed to fork");
+   if(Child == 0)
+   {
+      if(Mode == FileFd::ReadOnly)
+      {
+         close(Pipe[0]);
+         fd = Pipe[1];
+      }
+      else if(Mode == FileFd::WriteOnly)
+      {
+         close(Pipe[1]);
+         fd = Pipe[0];
+      }
+
+      if(Mode == FileFd::ReadOnly)
+      {
+         dup2(fd, 1);
+         dup2(fd, 2);
+      } else if(Mode == FileFd::WriteOnly)
+         dup2(fd, 0);
+
+      execv(Args[0], (char**)Args);
+      _exit(100);
+   }
+   if(Mode == FileFd::ReadOnly)
+   {
+      close(Pipe[1]);
+      fd = Pipe[0];
+   } else if(Mode == FileFd::WriteOnly)
+   {
+      close(Pipe[0]);
+      fd = Pipe[1];
+   }
+   Fd.OpenDescriptor(fd, Mode, FileFd::None, true);
+
+   return true;
+}
index cc1a98eae02c227b47962dff72421baf0e3f5e6d..b7812ac5677e770e9f30546632a17a9939888542 100644 (file)
@@ -201,4 +201,16 @@ std::string flCombine(std::string Dir,std::string File);
 // simple c++ glob
 std::vector<std::string> Glob(std::string const &pattern, int flags=0);
 
+/** \brief Popen() implementation that execv() instead of using a shell
+ *
+ * \param Args the execv style command to run
+ * \param FileFd is a referenz to the FileFd to use for input or output
+ * \param Child a reference to the integer that stores the child pid
+ *        Note that you must call ExecWait() or similar to cleanup
+ * \param Mode is either FileFd::ReadOnly or FileFd::WriteOnly
+ * \return true on success, false on failure with _error set
+ */
+bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode);
+
+
 #endif
index 643c02297fbc2e8851775cceb84fb36582beea9f..c2a43eda7d1db6a395242c1e587e6c96d0578a8b 100644 (file)
@@ -224,3 +224,51 @@ TEST(FileUtlTest, GetTempDir)
    if (old_tmpdir.empty() == false)
       setenv("TMPDIR", old_tmpdir.c_str(), 1);
 }
+
+TEST(FileUtlTest, Popen)
+{
+   FileFd Fd;
+   pid_t Child;
+   char buf[1024];
+   std::string s;
+   unsigned long long n = 0;
+   std::vector<std::string> OpenFds;
+
+   // count Fds to ensure we don't have a resource leak
+   if(FileExists("/proc/self/fd"))
+      OpenFds = Glob("/proc/self/fd/*");
+
+   // output something
+   const char* Args[10] = {"/bin/echo", "meepmeep", NULL};
+   bool res = Popen(Args, Fd, Child, FileFd::ReadOnly);
+   Fd.Read(buf, sizeof(buf)-1, &n);
+   buf[n] = 0;
+   EXPECT_NE(n, 0);
+   EXPECT_EQ(res, true);
+   EXPECT_STREQ(buf, "meepmeep\n");
+
+   // wait for the child to exit and cleanup
+   ExecWait(Child, "PopenRead");
+   Fd.Close();
+
+   // ensure that after a close all is good again
+   if(FileExists("/proc/self/fd"))
+      EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds.size());
+
+
+   // ReadWrite is not supported
+   res = Popen(Args, Fd, Child, FileFd::ReadWrite);
+   EXPECT_EQ(res, false);
+   _error->Discard();
+
+   // write something
+   Args[0] = "/bin/bash";
+   Args[1] = "-c";
+   Args[2] = "read";
+   Args[3] = NULL;
+   res = Popen(Args, Fd, Child, FileFd::WriteOnly);
+   s = "\n";
+   Fd.Write(s.c_str(), s.size());
+   Fd.Close();
+   ExecWait(Child, "PopenWrite");
+}