Replacing fork_and_execve with liblogwrap call

Using the android_fork_exec call from liblogwrap rather than a
custom fork_and_execve.

Change-Id: If8f9a5442e5e3cfc887010268c360e55e733aef0
diff --git a/NetdConstants.cpp b/NetdConstants.cpp
index db1d4cd..9b83125 100644
--- a/NetdConstants.cpp
+++ b/NetdConstants.cpp
@@ -15,10 +15,12 @@
  */
 
 #include <string.h>
+#include <sys/wait.h>
 
 #define LOG_TAG "Netd"
 
 #include <cutils/log.h>
+#include <logwrap/logwrap.h>
 
 #include "NetdConstants.h"
 
@@ -30,7 +32,7 @@
 const char * const ADD = "add";
 const char * const DEL = "del";
 
-static void logExecError(const char* argv[], int res) {
+static void logExecError(const char* argv[], int res, int status) {
     const char** argp = argv;
     std::string args = "";
     while (*argp) {
@@ -38,7 +40,25 @@
         args += ' ';
         argp++;
     }
-    ALOGE("exec() res=%d for %s", res, args.c_str());
+    ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
+}
+
+static int execIptablesCommand(int argc, const char *argv[], bool silent) {
+    int res;
+    int status;
+
+    res = android_fork_execvp(argc, (char **)argv, &status, false,
+        !silent);
+    if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
+        if (!silent) {
+            logExecError(argv, res, status);
+        }
+        if (res)
+            return res;
+        if (!WIFEXITED(status))
+            return ECHILD;
+    }
+    return WEXITSTATUS(status);
 }
 
 static int execIptables(IptablesTarget target, bool silent, va_list args) {
@@ -61,23 +81,11 @@
     int res = 0;
     if (target == V4 || target == V4V6) {
         argv[0] = IPTABLES_PATH;
-        int localRes = fork_and_execve(argv[0], argv);
-        if (localRes) {
-            if (!silent) {
-                logExecError(argv, localRes);
-            }
-            res |= localRes;
-        }
+        res |= execIptablesCommand(argsList.size(), argv, silent);
     }
     if (target == V6 || target == V4V6) {
         argv[0] = IP6TABLES_PATH;
-        int localRes = fork_and_execve(argv[0], argv);
-        if (localRes) {
-            if (!silent) {
-                logExecError(argv, localRes);
-            }
-            res |= localRes;
-        }
+        res |= execIptablesCommand(argsList.size(), argv, silent);
     }
     return res;
 }