logwrapper: rename logwrap() to android_fork_execvp()

Also change the quiet flag to a logwrap flag (inverses the meaning of
the boolean).

Change-Id: I76047a7b460f4c28d52f26bfe3f65889d96047f8
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index 5af1671..a04097d 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.c
@@ -36,22 +36,20 @@
 
 static int signal_fd_write;
 
-#define ERROR(fmt, quiet, args...)                                            \
+#define ERROR(fmt, args...)                                                   \
 do {                                                                          \
-    if (!quiet) {                                                             \
-        fprintf(stderr, fmt, ## args);                                        \
-        ALOG(LOG_ERROR, "logwrapper", fmt, ## args);                          \
-    }                                                                         \
+    fprintf(stderr, fmt, ## args);                                            \
+    ALOG(LOG_ERROR, "logwrapper", fmt, ## args);                              \
 } while(0)
 
-#define FATAL_CHILD(fmt, quiet, args...)                                      \
+#define FATAL_CHILD(fmt, args...)                                             \
 do {                                                                          \
-    ERROR(fmt, quiet, ## args);                                               \
+    ERROR(fmt, ## args);                                                      \
     _exit(-1);                                                                \
 } while(0)
 
 static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
-        int *chld_sts, bool quiet) {
+        int *chld_sts, bool logwrap) {
     int status = 0;
     char buffer[4096];
     struct pollfd poll_fds[] = {
@@ -84,7 +82,7 @@
         if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
             if (errno == EINTR)
                 continue;
-            ERROR("poll failed\n", quiet);
+            ERROR("poll failed\n");
             rc = -1;
             goto err_poll;
         }
@@ -100,7 +98,7 @@
                         buffer[b] = '\0';
                     } else if (buffer[b] == '\n') {
                         buffer[b] = '\0';
-                        if (!quiet)
+                        if (logwrap)
                             ALOG(LOG_INFO, btag, "%s", &buffer[a]);
                         a = b + 1;
                     }
@@ -109,7 +107,7 @@
                 if (a == 0 && b == sizeof(buffer) - 1) {
                     // buffer is full, flush
                     buffer[b] = '\0';
-                    if (!quiet)
+                    if (logwrap)
                         ALOG(LOG_INFO, btag, "%s", &buffer[a]);
                     b = 0;
                 } else if (a != b) {
@@ -149,22 +147,20 @@
     // Flush remaining data
     if (a != b) {
         buffer[b] = '\0';
-        if (!quiet)
+        if (logwrap)
             ALOG(LOG_INFO, btag, "%s", &buffer[a]);
     }
 
-    if (!quiet) {
-        if (WIFEXITED(status)) {
-            if (WEXITSTATUS(status))
-                ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
-                        WEXITSTATUS(status));
-        } else if (WIFSIGNALED(status)) {
-            ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
-                    WTERMSIG(status));
-        } else if (WIFSTOPPED(status)) {
-            ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
-                    WSTOPSIG(status));
-        }
+    if (WIFEXITED(status)) {
+        if (WEXITSTATUS(status))
+            ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
+                    WEXITSTATUS(status));
+    } else if (WIFSIGNALED(status)) {
+        ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
+                WTERMSIG(status));
+    } else if (WIFSTOPPED(status)) {
+        ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
+                WSTOPSIG(status));
     }
     if (chld_sts != NULL)
         *chld_sts = status;
@@ -173,24 +169,24 @@
     return rc;
 }
 
-static void child(int argc, char* argv[], bool quiet) {
+static void child(int argc, char* argv[], bool logwrap) {
     // create null terminated argv_child array
     char* argv_child[argc + 1];
     memcpy(argv_child, argv, argc * sizeof(char *));
     argv_child[argc] = NULL;
 
     if (execvp(argv_child[0], argv_child)) {
-        FATAL_CHILD("executing %s failed: %s\n", quiet, argv_child[0],
+        FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
                 strerror(errno));
     }
 }
 
-void sigchld_handler(int sig) {
+static void sigchld_handler(int sig) {
     write(signal_fd_write, &sig, 1);
 }
 
-int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
-        bool quiet) {
+int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
+        bool logwrap) {
     pid_t pid;
     int parent_ptty;
     int child_ptty;
@@ -207,14 +203,14 @@
     /* Use ptty instead of socketpair so that STDOUT is not buffered */
     parent_ptty = open("/dev/ptmx", O_RDWR);
     if (parent_ptty < 0) {
-        ERROR("Cannot create parent ptty\n", quiet);
+        ERROR("Cannot create parent ptty\n");
         rc = -1;
         goto err_open;
     }
 
     if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
             ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
-        ERROR("Problem with /dev/ptmx\n", quiet);
+        ERROR("Problem with /dev/ptmx\n");
         rc = -1;
         goto err_ptty;
     }
@@ -227,7 +223,7 @@
 
     pid = fork();
     if (pid < 0) {
-        ERROR("Failed to fork\n", quiet);
+        ERROR("Failed to fork\n");
         rc = -1;
         goto err_fork;
     } else if (pid == 0) {
@@ -236,7 +232,7 @@
 
         child_ptty = open(child_devname, O_RDWR);
         if (child_ptty < 0) {
-            FATAL_CHILD("Problem with child ptty\n", quiet);
+            FATAL_CHILD("Problem with child ptty\n");
             return -1;
         }
 
@@ -245,7 +241,7 @@
         dup2(child_ptty, 2);
         close(child_ptty);
 
-        child(argc, argv, quiet);
+        child(argc, argv, logwrap);
     } else {
         struct sigaction ignact;
 
@@ -272,7 +268,7 @@
 
         rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
         if (rc == -1) {
-            ERROR("socketpair failed: %s\n", quiet, strerror(errno));
+            ERROR("socketpair failed: %s\n", strerror(errno));
             goto err_socketpair;
         }
 
@@ -283,7 +279,7 @@
 
         signal_fd_write = sockets[0];
 
-        rc = parent(argv[0], parent_ptty, sockets[1], pid, status, quiet);
+        rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
     }
 
     close(sockets[0]);