Merge "Delete frameworks/base/voip use voip-common from frameworks/opt/net/voip"
diff --git a/adb/adb.c b/adb/adb.c
index 1bf4b9e..a3cd281 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -402,6 +402,8 @@
         return "device";
     case CS_OFFLINE:
         return "offline";
+    case CS_UNAUTHORIZED:
+        return "unauthorized";
     default:
         return "unknown";
     }
@@ -531,6 +533,7 @@
 
     case A_AUTH:
         if (p->msg.arg0 == ADB_AUTH_TOKEN) {
+            t->connection_state = CS_UNAUTHORIZED;
             t->key = adb_auth_nextkey(t->key);
             if (t->key) {
                 send_auth_response(p->data, p->msg.data_length, t);
diff --git a/adb/adb.h b/adb/adb.h
index 9da8af8..a01d460 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -468,6 +468,7 @@
 #define CS_RECOVERY   4
 #define CS_NOPERM     5 /* Insufficient permissions to communicate with the device */
 #define CS_SIDELOAD   6
+#define CS_UNAUTHORIZED 7
 
 extern int HOST;
 extern int SHELL_EXIT_NOTIFY_FD;
diff --git a/adb/adb_auth.h b/adb/adb_auth.h
index 1fffa49..96f637b 100644
--- a/adb/adb_auth.h
+++ b/adb/adb_auth.h
@@ -36,7 +36,6 @@
 static inline int adb_auth_generate_token(void *token, size_t token_size) { return 0; }
 static inline int adb_auth_verify(void *token, void *sig, int siglen) { return 0; }
 static inline void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t) { }
-static inline void adb_auth_reload_keys(void) { }
 
 #else // !ADB_HOST
 
@@ -47,7 +46,6 @@
 int adb_auth_generate_token(void *token, size_t token_size);
 int adb_auth_verify(void *token, void *sig, int siglen);
 void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t);
-void adb_auth_reload_keys(void);
 
 #endif // ADB_HOST
 
diff --git a/adb/adb_auth_client.c b/adb/adb_auth_client.c
index 0b4913e..a4ad18f 100644
--- a/adb/adb_auth_client.c
+++ b/adb/adb_auth_client.c
@@ -34,8 +34,6 @@
     RSAPublicKey key;
 };
 
-static struct listnode key_list;
-
 static char *key_paths[] = {
     "/adb_keys",
     "/data/misc/adb/adb_keys",
@@ -102,18 +100,18 @@
     }
 }
 
-void adb_auth_reload_keys(void)
+static void load_keys(struct listnode *list)
 {
     char *path;
     char **paths = key_paths;
     struct stat buf;
 
-    free_keys(&key_list);
+    list_init(list);
 
     while ((path = *paths++)) {
         if (!stat(path, &buf)) {
             D("Loading keys from '%s'\n", path);
-            read_keys(path, &key_list);
+            read_keys(path, list);
         }
     }
 }
@@ -137,19 +135,24 @@
 {
     struct listnode *item;
     struct adb_public_key *key;
-    int ret;
+    struct listnode key_list;
+    int ret = 0;
 
     if (siglen != RSANUMBYTES)
         return 0;
 
+    load_keys(&key_list);
+
     list_for_each(item, &key_list) {
         key = node_to_item(item, struct adb_public_key, node);
         ret = RSA_verify(&key->key, sig, siglen, token);
         if (ret)
-            return 1;
+            break;
     }
 
-    return 0;
+    free_keys(&key_list);
+
+    return ret;
 }
 
 static void adb_auth_event(int fd, unsigned events, void *data)
@@ -166,7 +169,6 @@
             framework_fd = -1;
         }
         else if (ret == 2 && response[0] == 'O' && response[1] == 'K') {
-            adb_auth_reload_keys();
             adb_auth_verified(t);
         }
     }
@@ -225,9 +227,6 @@
 {
     int fd, ret;
 
-    list_init(&key_list);
-    adb_auth_reload_keys();
-
     fd = android_get_control_socket("adbd");
     if (fd < 0) {
         D("Failed to get adbd socket\n");
diff --git a/adb/transport.c b/adb/transport.c
index 9fd6cc2..b4abb66 100644
--- a/adb/transport.c
+++ b/adb/transport.c
@@ -851,6 +851,12 @@
     adb_mutex_unlock(&transport_lock);
 
     if (result) {
+        if (result->connection_state == CS_UNAUTHORIZED) {
+            if (error_out)
+                *error_out = "device unauthorized. Please check the confirmation dialog on your device.";
+            result = NULL;
+        }
+
          /* offline devices are ignored -- they are either being born or dying */
         if (result && result->connection_state == CS_OFFLINE) {
             if (error_out)
@@ -888,6 +894,7 @@
     case CS_RECOVERY: return "recovery";
     case CS_SIDELOAD: return "sideload";
     case CS_NOPERM: return "no permissions";
+    case CS_UNAUTHORIZED: return "unauthorized";
     default: return "unknown";
     }
 }
diff --git a/init/property_service.c b/init/property_service.c
index 61dd86f..5780001 100755
--- a/init/property_service.c
+++ b/init/property_service.c
@@ -123,7 +123,7 @@
         /* dev is a tmpfs that we can use to carve a shared workspace
          * out of, so let's do that...
          */
-    fd = open("/dev/__properties__", O_RDWR | O_CREAT | O_NOFOLLOW, 0600);
+    fd = open(PROP_FILENAME, O_RDWR | O_CREAT | O_NOFOLLOW, 0644);
     if (fd < 0)
         return -1;
 
@@ -136,12 +136,10 @@
 
     close(fd);
 
-    fd = open("/dev/__properties__", O_RDONLY | O_NOFOLLOW);
+    fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
     if (fd < 0)
         return -1;
 
-    unlink("/dev/__properties__");
-
     w->data = data;
     w->size = size;
     w->fd = fd;
diff --git a/logwrapper/include/logwrap/logwrap.h b/logwrapper/include/logwrap/logwrap.h
index 722dda2..6597ef5 100644
--- a/logwrapper/include/logwrap/logwrap.h
+++ b/logwrapper/include/logwrap/logwrap.h
@@ -18,6 +18,8 @@
 #ifndef __LIBS_LOGWRAP_H
 #define __LIBS_LOGWRAP_H
 
+#include <stdbool.h>
+
 __BEGIN_DECLS
 
 /*
@@ -36,13 +38,19 @@
  *           NULL-terminated
  *   status: the equivalent child status as populated by wait(status). This
  *           value is only valid when logwrap successfully completes
+ *   ignore_int_quit: set to true if you want to completely ignore SIGINT and
+ *           SIGQUIT while logwrap is running. This may force the end-user to
+ *           send a signal twice to signal the caller (once for the child, and
+ *           once for the caller)
+ *   quiet: when true, don't display log messages
  *
  * Return value:
  *   0 when logwrap successfully run the child process and captured its status
  *   -1 when an internal error occurred
  *
  */
-int logwrap(int argc, char* argv[], int *status);
+int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
+        bool quiet);
 
 __END_DECLS
 
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index c2b36be..5af1671 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.c
@@ -16,7 +16,7 @@
 
 #include <string.h>
 #include <sys/types.h>
-#include <sys/signalfd.h>
+#include <sys/socket.h>
 #include <signal.h>
 #include <poll.h>
 #include <sys/wait.h>
@@ -26,6 +26,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <libgen.h>
+#include <stdbool.h>
 
 #include <logwrap/logwrap.h>
 #include "private/android_filesystem_config.h"
@@ -33,156 +34,210 @@
 
 #define ARRAY_SIZE(x)	(sizeof(x) / sizeof(*(x)))
 
-static int fatal(const char *msg) {
-    fprintf(stderr, "%s", msg);
-    ALOG(LOG_ERROR, "logwrapper", "%s", msg);
-    return -1;
-}
+static int signal_fd_write;
+
+#define ERROR(fmt, quiet, args...)                                            \
+do {                                                                          \
+    if (!quiet) {                                                             \
+        fprintf(stderr, fmt, ## args);                                        \
+        ALOG(LOG_ERROR, "logwrapper", fmt, ## args);                          \
+    }                                                                         \
+} while(0)
+
+#define FATAL_CHILD(fmt, quiet, args...)                                      \
+do {                                                                          \
+    ERROR(fmt, quiet, ## args);                                               \
+    _exit(-1);                                                                \
+} while(0)
 
 static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
-        int *chld_sts) {
-    int status;
+        int *chld_sts, bool quiet) {
+    int status = 0;
     char buffer[4096];
     struct pollfd poll_fds[] = {
         [0] = {
-            .fd = parent_read,
-            .events = POLLIN,
-        },
-        [1] = {
             .fd = signal_fd,
             .events = POLLIN,
         },
+        [1] = {
+            .fd = parent_read,
+            .events = POLLIN,
+        },
     };
+    int rc = 0;
+    sigset_t chldset;
 
     int a = 0;  // start index of unprocessed data
     int b = 0;  // end index of unprocessed data
     int sz;
+    bool remote_hung = false;
+    bool found_child = false;
 
     char *btag = basename(tag);
     if (!btag) btag = (char*) tag;
 
-    while (1) {
-        if (poll(poll_fds, ARRAY_SIZE(poll_fds), -1) <= 0) {
-            return fatal("poll failed\n");
+    sigemptyset(&chldset);
+    sigaddset(&chldset, SIGCHLD);
+    pthread_sigmask(SIG_UNBLOCK, &chldset, NULL);
+
+    while (!found_child) {
+        if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
+            if (errno == EINTR)
+                continue;
+            ERROR("poll failed\n", quiet);
+            rc = -1;
+            goto err_poll;
         }
 
-        if (poll_fds[0].revents & POLLIN) {
-            sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
+        if (!remote_hung) {
+            if (poll_fds[1].revents & POLLIN) {
+                sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
 
-            sz += b;
-            // Log one line at a time
-            for (b = 0; b < sz; b++) {
-                if (buffer[b] == '\r') {
+                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';
+                        if (!quiet)
+                            ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+                        a = b + 1;
+                    }
+                }
+
+                if (a == 0 && b == sizeof(buffer) - 1) {
+                    // buffer is full, flush
                     buffer[b] = '\0';
-                } else if (buffer[b] == '\n') {
-                    buffer[b] = '\0';
-                    ALOG(LOG_INFO, btag, "%s", &buffer[a]);
-                    a = b + 1;
+                    if (!quiet)
+                        ALOG(LOG_INFO, btag, "%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;
                 }
             }
 
-            if (a == 0 && b == sizeof(buffer) - 1) {
-                // buffer is full, flush
-                buffer[b] = '\0';
-                ALOG(LOG_INFO, btag, "%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;
+            if (poll_fds[1].revents & POLLHUP) {
+                remote_hung = true;
             }
         }
 
-        if (poll_fds[1].revents & POLLIN) {
-            struct signalfd_siginfo sfd_info;
-            pid_t wpid;
+        if (poll_fds[0].revents & POLLIN) {
+            char tmp[32];
+            int ret;
 
-            // Clear all pending signals before reading the child's status
-            while (read(signal_fd, &sfd_info, sizeof(sfd_info)) > 0) {
-                if ((pid_t)sfd_info.ssi_pid != pid)
-                    ALOG(LOG_WARN, "logwrapper", "cleared SIGCHLD for pid %u\n",
-                            sfd_info.ssi_pid);
+            read(signal_fd, tmp, sizeof(tmp));
+            while (!found_child) {
+                do {
+                    ret = waitpid(-1, &status, WNOHANG);
+                } while (ret < 0 && errno == EINTR);
+
+                if (ret <= 0)
+                    break;
+
+                found_child = (pid == ret);
             }
-            wpid = waitpid(pid, &status, WNOHANG);
-            if (wpid > 0)
-                break;
         }
     }
 
     // Flush remaining data
     if (a != b) {
         buffer[b] = '\0';
-        ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+        if (!quiet)
+            ALOG(LOG_INFO, btag, "%s", &buffer[a]);
     }
 
-    if (WIFEXITED(status)) {
-        if (WEXITSTATUS(status))
-            ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
-                    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));
+    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 (chld_sts != NULL)
         *chld_sts = status;
 
-    return 0;
+err_poll:
+    return rc;
 }
 
-static void child(int argc, char* argv[]) {
+static void child(int argc, char* argv[], bool quiet) {
     // 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)) {
-        ALOG(LOG_ERROR, "logwrapper",
-            "executing %s failed: %s\n", argv_child[0], strerror(errno));
-        exit(-1);
+        FATAL_CHILD("executing %s failed: %s\n", quiet, argv_child[0],
+                strerror(errno));
     }
 }
 
-int logwrap(int argc, char* argv[], int *status) {
-    pid_t pid;
+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) {
+    pid_t pid;
     int parent_ptty;
     int child_ptty;
     char *child_devname = NULL;
-    sigset_t chldset;
+    struct sigaction chldact;
+    struct sigaction oldchldact;
+    struct sigaction intact;
+    struct sigaction quitact;
+    sigset_t blockset;
+    sigset_t oldset;
+    int sockets[2];
+    int rc = 0;
 
     /* Use ptty instead of socketpair so that STDOUT is not buffered */
     parent_ptty = open("/dev/ptmx", O_RDWR);
     if (parent_ptty < 0) {
-        return fatal("Cannot create parent ptty\n");
+        ERROR("Cannot create parent ptty\n", quiet);
+        rc = -1;
+        goto err_open;
     }
 
     if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
             ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
-        return fatal("Problem with /dev/ptmx\n");
+        ERROR("Problem with /dev/ptmx\n", quiet);
+        rc = -1;
+        goto err_ptty;
     }
 
-    sigemptyset(&chldset);
-    sigaddset(&chldset, SIGCHLD);
-    sigprocmask(SIG_BLOCK, &chldset, NULL);
+    sigemptyset(&blockset);
+    sigaddset(&blockset, SIGINT);
+    sigaddset(&blockset, SIGQUIT);
+    sigaddset(&blockset, SIGCHLD);
+    pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
 
     pid = fork();
     if (pid < 0) {
-        close(parent_ptty);
-        sigprocmask(SIG_UNBLOCK, &chldset, NULL);
-        return fatal("Failed to fork\n");
+        ERROR("Failed to fork\n", quiet);
+        rc = -1;
+        goto err_fork;
     } else if (pid == 0) {
+        pthread_sigmask(SIG_SETMASK, &oldset, NULL);
         close(parent_ptty);
-        sigprocmask(SIG_UNBLOCK, &chldset, NULL);
+
         child_ptty = open(child_devname, O_RDWR);
         if (child_ptty < 0) {
-            return fatal("Problem with child ptty\n");
+            FATAL_CHILD("Problem with child ptty\n", quiet);
+            return -1;
         }
 
         // redirect stdout and stderr
@@ -190,36 +245,59 @@
         dup2(child_ptty, 2);
         close(child_ptty);
 
-        child(argc - 1, &argv[1]);
-        return fatal("This should never happen\n");
-
+        child(argc, argv, quiet);
     } else {
-        int rc;
-        int fd;
+        struct sigaction ignact;
 
-        fd = signalfd(-1, &chldset, SFD_NONBLOCK);
-        if (fd == -1) {
-            char msg[40];
+        memset(&chldact, 0, sizeof(chldact));
+        chldact.sa_handler = sigchld_handler;
+        chldact.sa_flags = SA_NOCLDSTOP;
 
-            snprintf(msg, sizeof(msg), "signalfd failed: %d\n", errno);
-
-            close(parent_ptty);
-            sigprocmask(SIG_UNBLOCK, &chldset, NULL);
-            return fatal(msg);
+        sigaction(SIGCHLD, &chldact, &oldchldact);
+        if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
+                oldchldact.sa_handler != SIG_DFL &&
+                oldchldact.sa_handler != SIG_IGN) ||
+                ((oldchldact.sa_flags & SA_SIGINFO) &&
+                oldchldact.sa_sigaction != NULL)) {
+            ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
+                    "handler and might cause interaction issues");
         }
 
-        // switch user and group to "log"
-        // this may fail if we are not root,
-        // but in that case switching user/group is unnecessary
-        setgid(AID_LOG);
-        setuid(AID_LOG);
+        if (ignore_int_quit) {
+            memset(&ignact, 0, sizeof(ignact));
+            ignact.sa_handler = SIG_IGN;
+            sigaction(SIGINT, &ignact, &intact);
+            sigaction(SIGQUIT, &ignact, &quitact);
+        }
 
-        rc = parent(argv[1], parent_ptty, fd, pid, status);
-        close(parent_ptty);
-        close(fd);
+        rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
+        if (rc == -1) {
+            ERROR("socketpair failed: %s\n", quiet, strerror(errno));
+            goto err_socketpair;
+        }
 
-        sigprocmask(SIG_UNBLOCK, &chldset, NULL);
+        fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
+        fcntl(sockets[0], F_SETFL, O_NONBLOCK);
+        fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
+        fcntl(sockets[1], F_SETFL, O_NONBLOCK);
 
-        return rc;
+        signal_fd_write = sockets[0];
+
+        rc = parent(argv[0], parent_ptty, sockets[1], pid, status, quiet);
     }
+
+    close(sockets[0]);
+    close(sockets[1]);
+err_socketpair:
+    if (ignore_int_quit) {
+        sigaction(SIGINT, &intact, NULL);
+        sigaction(SIGQUIT, &quitact, NULL);
+    }
+    sigaction(SIGCHLD, &oldchldact, NULL);
+err_fork:
+    pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+err_ptty:
+    close(parent_ptty);
+err_open:
+    return rc;
 }
diff --git a/logwrapper/logwrapper.c b/logwrapper/logwrapper.c
index 4f1bff9..cba69d9 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -59,7 +59,7 @@
         usage();
     }
 
-    rc = logwrap(argc, argv, &status);
+    rc = logwrap(argc - 1, &argv[1], &status, true, false);
     if (!rc) {
         if (WIFEXITED(status))
             rc = WEXITSTATUS(status);
diff --git a/rootdir/init.usb.rc b/rootdir/init.usb.rc
index f37b630..15467cc 100644
--- a/rootdir/init.usb.rc
+++ b/rootdir/init.usb.rc
@@ -88,5 +88,4 @@
 # Used to set USB configuration at boot and to switch the configuration
 # when changing the default configuration
 on property:persist.sys.usb.config=*
-    setprop sys.usb.config none
     setprop sys.usb.config ${persist.sys.usb.config}