OSDN Git Service

Merge "Replace custom logwrapper implementation with liblogwrap"
authorRom Lemarchand <romlem@google.com>
Thu, 31 Jan 2013 20:31:23 +0000 (20:31 +0000)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Thu, 31 Jan 2013 20:31:23 +0000 (20:31 +0000)
Android.mk
BandwidthController.cpp
BandwidthController.h
logwrapper.c

index 69fd6d2..a8a6bb2 100644 (file)
@@ -39,7 +39,8 @@ LOCAL_C_INCLUDES := $(KERNEL_HEADERS) \
 LOCAL_CFLAGS := -Werror=format
 
 LOCAL_SHARED_LIBRARIES := libstlport libsysutils libcutils libnetutils \
-                          libcrypto libhardware_legacy libmdnssd libdl
+                          libcrypto libhardware_legacy libmdnssd libdl \
+                          liblogwrap
 
 include $(BUILD_EXECUTABLE)
 
index e5d3054..a836475 100644 (file)
@@ -40,9 +40,7 @@
 #define LOG_TAG "BandwidthController"
 #include <cutils/log.h>
 #include <cutils/properties.h>
-
-extern "C" int logwrap(int argc, const char **argv);
-extern "C" int system_nosh(const char *command);
+#include <logwrap/logwrap.h>
 
 #include "NetdConstants.h"
 #include "BandwidthController.h"
@@ -61,8 +59,6 @@ const int  BandwidthController::MAX_CMD_LEN = 1024;
 const int  BandwidthController::MAX_IFACENAME_LEN = 64;
 const int  BandwidthController::MAX_IPT_OUTPUT_LINE_LEN = 256;
 
-bool BandwidthController::useLogwrapCall = false;
-
 /**
  * Some comments about the rules:
  *  * Ordering
@@ -141,10 +137,6 @@ const char *BandwidthController::IPT_BASIC_ACCOUNTING_COMMANDS[] = {
 };
 
 BandwidthController::BandwidthController(void) {
-    char value[PROPERTY_VALUE_MAX];
-
-    property_get("persist.bandwidth.uselogwrap", value, "0");
-    useLogwrapCall = !strcmp(value, "1");
 }
 
 int BandwidthController::runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling,
@@ -172,6 +164,7 @@ int BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandl
     char *next = buffer;
     char *tmp;
     int res;
+    int status = 0;
 
     std::string fullCmd = cmd;
 
@@ -190,28 +183,28 @@ int BandwidthController::runIptablesCmd(const char *cmd, IptRejectOp rejectHandl
     fullCmd.insert(0, " ");
     fullCmd.insert(0, iptVer == IptIpV4 ? IPTABLES_PATH : IP6TABLES_PATH);
 
-    if (!useLogwrapCall) {
-        res = system_nosh(fullCmd.c_str());
-    } else {
-        if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
-            ALOGE("iptables command too long");
+    if (StrncpyAndCheck(buffer, fullCmd.c_str(), sizeof(buffer))) {
+        ALOGE("iptables command too long");
+        return -1;
+    }
+
+    argc = 0;
+    while ((tmp = strsep(&next, " "))) {
+        argv[argc++] = tmp;
+        if (argc >= MAX_CMD_ARGS) {
+            ALOGE("iptables argument overflow");
             return -1;
         }
+    }
 
-        argc = 0;
-        while ((tmp = strsep(&next, " "))) {
-            argv[argc++] = tmp;
-            if (argc >= MAX_CMD_ARGS) {
-                ALOGE("iptables argument overflow");
-                return -1;
-            }
-        }
+    argv[argc] = NULL;
+    res = android_fork_execvp(argc, (char **)argv, &status, false,
+            failureHandling == IptFailShow);
 
-        argv[argc] = NULL;
-        res = logwrap(argc, argv);
-    }
-    if (res && failureHandling == IptFailShow) {
-        ALOGE("runIptablesCmd(): failed %s res=%d", fullCmd.c_str(), res);
+    if ((res || !WIFEXITED(status) || WEXITSTATUS(status)) &&
+            failureHandling == IptFailShow) {
+        ALOGE("runIptablesCmd(): failed %s res=%d status=%d", fullCmd.c_str(),
+                res, status);
     }
     return res;
 }
index 17017d2..9e23b76 100644 (file)
@@ -175,11 +175,6 @@ private:
     static const int  MAX_CMD_LEN;
     static const int  MAX_IFACENAME_LEN;
     static const int  MAX_IPT_OUTPUT_LINE_LEN;
-
-    /*
-     * When false, it will directly use system() instead of logwrap()
-     */
-    static bool useLogwrapCall;
 };
 
 #endif
index e84ae89..ce22850 100644 (file)
 #include "private/android_filesystem_config.h"
 #include "cutils/log.h"
 
-int parent(const char *tag, int parent_read) {
-    int status;
-    char buffer[4096];
-
-    int a = 0;  // start index of unprocessed data
-    int b = 0;  // end index of unprocessed data
-    int sz;
-    while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
-
-        sz += b;
-        // Log one line at a time
-        for (b = 0; b < sz; b++) {
-            if (buffer[b] == '\r') {
-                buffer[b] = '\0';
-            } else if (buffer[b] == '\n') {
-                buffer[b] = '\0';
-
-                ALOG(LOG_INFO, tag, "%s", &buffer[a]);
-                a = b + 1;
-            }
-        }
-
-        if (a == 0 && b == sizeof(buffer) - 1) {
-            // buffer is full, flush
-            buffer[b] = '\0';
-            ALOG(LOG_INFO, tag, "%s", &buffer[a]);
-            b = 0;
-        } else if (a != b) {
-            // Keep left-overs
-            b -= a;
-            memmove(buffer, &buffer[a], b);
-            a = 0;
-        } else {
-            a = 0;
-            b = 0;
-        }
-
-    }
-    // Flush remaining data
-    if (a != b) {
-        buffer[b] = '\0';
-        ALOG(LOG_INFO, tag, "%s", &buffer[a]);
-    }
-    status = 0xAAAA;
-    if (wait(&status) != -1) {  // Wait for child
-        if (WIFEXITED(status)) {
-            if (WEXITSTATUS(status) != 0) {
-                ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
-                        WEXITSTATUS(status));
-            }
-            return WEXITSTATUS(status);
-        } else if (WIFSIGNALED(status))
-            ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
-                    WTERMSIG(status));
-        else if (WIFSTOPPED(status))
-            ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
-                    WSTOPSIG(status));
-    } else
-        ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
-                strerror(errno), errno);
-    return -EAGAIN;
-}
-
-void child(int argc, const char**argv) {
-    // create null terminated argv_child array
-    char* argv_child[argc + 1];
-    memcpy(argv_child, argv, argc * sizeof(char *));
-    argv_child[argc] = NULL;
-
-    // XXX: PROTECT FROM VIKING KILLER
-    if (execv(argv_child[0], argv_child)) {
-        ALOG(LOG_ERROR, "logwrapper",
-            "executing %s failed: %s", argv_child[0], strerror(errno));
-    }
-    _exit(1);
-}
-
-int logwrap(int argc, const char* argv[])
-{
-    pid_t pid;
-
-    int parent_ptty;
-    int child_ptty;
-    char child_devname[64];  // same size as libc/unistd/ptsname_r.c
-
-    /* Use ptty instead of socketpair so that STDOUT is not buffered */
-    parent_ptty = open("/dev/ptmx", O_RDWR);
-    if (parent_ptty < 0) {
-        ALOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty");
-        return -errno;
-    }
-
-    if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
-            ptsname_r(parent_ptty, child_devname, sizeof(child_devname))) {
-        close(parent_ptty);
-        ALOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx");
-        return -1;
-    }
-
-    pid = fork();
-    if (pid < 0) {
-        close(parent_ptty);
-        ALOG(LOG_ERROR, "logwrapper", "Failed to fork");
-        return -errno;
-    } else if (pid == 0) {
-        /*
-         * Child
-         */
-        child_ptty = open(child_devname, O_RDWR);
-        if (child_ptty < 0) {
-            close(parent_ptty);
-            ALOG(LOG_ERROR, "logwrapper", "Problem with child ptty");
-            _exit(errno < 128 ? errno : 1);  // XXX lame
-        }
-
-        // redirect stdout and stderr
-        close(parent_ptty);
-        dup2(child_ptty, 1);
-        dup2(child_ptty, 2);
-        close(child_ptty);
-
-        child(argc, argv);
-    } else {
-        /*
-         * Parent
-         */
-        int rc = parent(argv[0], parent_ptty);
-        close(parent_ptty);
-        return rc;
-    }
-
-    return 0;
-}
-
 int fork_and_execve(const char*, char*[]);
 
 /*