netd: all: use system() instead of logwrap() for now.

The logwrapper uses a blocking read() which does not always
correctly detect when the child process at the other end is gone.
This is a quick workaround for http://b/5144246
A cleaner logwrapper parent() will follow.

Add support for BandwidthController() to use either system() or
logwrap(). It looks at "persist.bandwidth.uselogwrap" to be 0 or 1.

Change-Id: I2d17732214f1a7fef6838eee05d827695b707ab0
Signed-off-by: JP Abgrall <jpa@google.com>
diff --git a/BandwidthController.cpp b/BandwidthController.cpp
index 441818d..233c104 100644
--- a/BandwidthController.cpp
+++ b/BandwidthController.cpp
@@ -43,6 +43,7 @@
 const char BandwidthController::IP6TABLES_PATH[] = "/system/bin/ip6tables";
 const char BandwidthController::ALERT_IPT_TEMPLATE[] = "%s %s -m quota2 ! --quota %lld --name %s";
 const int BandwidthController::ALERT_RULE_POS_IN_COSTLY_CHAIN = 4;
+bool BandwidthController::useLogwrapCall = false;
 
 /**
  * Some comments about the rules:
@@ -124,6 +125,8 @@
         enableBandwidthControl();
     }
 
+    property_get("persist.bandwidth.uselogwrap", value, "0");
+    useLogwrapCall = !strcmp(value, "1");
 }
 
 int BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling) {
@@ -149,6 +152,7 @@
     int argc = 0;
     char *next = buffer;
     char *tmp;
+    int res;
 
     std::string fullCmd = cmd;
 
@@ -164,28 +168,33 @@
         }
     }
 
-    argc = 0;
-    argv[argc++] = iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH;
+    fullCmd.insert(0, " ");
+    fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
 
-    LOGD("runIptablesCmd(): %s %s", argv[0], fullCmd.c_str());
-    if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
-        LOGE("iptables command too long");
-        return -1;
-    }
-
-    while ((tmp = strsep(&next, " "))) {
-        argv[argc++] = tmp;
-        if (argc >= MAX_CMD_ARGS) {
-            LOGE("iptables argument overflow");
+    if (!useLogwrapCall) {
+        res = system(fullCmd.c_str());
+    } else {
+        if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
+            LOGE("iptables command too long");
             return -1;
         }
-    }
 
-    argv[argc] = NULL;
-    /* TODO(jpa): Once this stabilizes, remove logwrap() as it tends to wedge netd
-     * Then just talk directly to the kernel via rtnetlink.
-     */
-    return logwrap(argc, argv, 0);
+        argc = 0;
+        while ((tmp = strsep(&next, " "))) {
+            argv[argc++] = tmp;
+            if (argc >= MAX_CMD_ARGS) {
+                LOGE("iptables argument overflow");
+                return -1;
+            }
+        }
+
+        argv[argc] = NULL;
+        res = logwrap(argc, argv, 0);
+    }
+    if (res) {
+        LOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res);
+    }
+    return res;
 }
 
 int BandwidthController::enableBandwidthControl(void) {