Interceptor: add support for Command execution

That is necessary to execute modified commands. Otherwise the function
would return and fall back to the original execve. This change itself
does make sense together with a later change that actually modifies the
command.

Bug: 205577427
Signed-off-by: Matthias Maennich <maennich@google.com>
Change-Id: I6a9d7e31256063f3fe2883975fdc4cf486566b24
diff --git a/interceptor.cc b/interceptor.cc
index de3b2f3..71f67a2 100644
--- a/interceptor.cc
+++ b/interceptor.cc
@@ -44,6 +44,9 @@
 // log command if logging is enabled
 static void log(const interceptor::Command&, const std::string& prefix);
 
+// execute potentially modified command
+static void exec(const interceptor::Command&);
+
 // OVERLOADS for LD_PRELOAD USE
 
 // Intercept execve calls, for that capture the original execve call
@@ -134,6 +137,9 @@
 
   interceptor::Command command(filename, argv, envp);
   log(command, "");
+
+  // pass down the transformed command to execve
+  exec(command);
 }
 
 static void log(const interceptor::Command& command, const std::string& prefix) {
@@ -147,3 +153,16 @@
     }
   }
 }
+
+static void exec(const interceptor::Command& command) {
+  std::vector<const char*> c_args;
+  c_args.reserve(command.args().size() + 1);
+  c_args[command.args().size()] = nullptr;
+  for (const auto& arg : command.args()) {
+    c_args.push_back(arg.data());
+  }
+  // TODO: at this point, we could free some memory that is held in Command.
+  //       While the args vector is reused for args, we could free the EnvMap
+  //       and the original args.
+  old_execve(command.program().c_str(), const_cast<char**>(c_args.data()), command.envp());
+}