Merge "audio: added input device for front + back mic"
diff --git a/adb/adb.c b/adb/adb.c
index 8be5765..c57a875 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <time.h>
 #include <sys/time.h>
+#include <stdint.h>
 
 #include "sysdeps.h"
 #include "adb.h"
@@ -402,6 +403,8 @@
         return "device";
     case CS_OFFLINE:
         return "offline";
+    case CS_UNAUTHORIZED:
+        return "unauthorized";
     default:
         return "unknown";
     }
@@ -531,6 +534,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);
@@ -1018,6 +1022,7 @@
     /* message since the pipe handles must be inheritable, we use a     */
     /* security attribute                                               */
     HANDLE                pipe_read, pipe_write;
+    HANDLE                stdout_handle, stderr_handle;
     SECURITY_ATTRIBUTES   sa;
     STARTUPINFO           startup;
     PROCESS_INFORMATION   pinfo;
@@ -1037,6 +1042,26 @@
 
     SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
 
+    /* Some programs want to launch an adb command and collect its output by
+     * calling CreateProcess with inheritable stdout/stderr handles, then
+     * using read() to get its output. When this happens, the stdout/stderr
+     * handles passed to the adb client process will also be inheritable.
+     * When starting the adb server here, care must be taken to reset them
+     * to non-inheritable.
+     * Otherwise, something bad happens: even if the adb command completes,
+     * the calling process is stuck while read()-ing from the stdout/stderr
+     * descriptors, because they're connected to corresponding handles in the
+     * adb server process (even if the latter never uses/writes to them).
+     */
+    stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
+    stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
+    if (stdout_handle != INVALID_HANDLE_VALUE) {
+        SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
+    }
+    if (stderr_handle != INVALID_HANDLE_VALUE) {
+        SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
+    }
+
     ZeroMemory( &startup, sizeof(startup) );
     startup.cb = sizeof(startup);
     startup.hStdInput  = GetStdHandle( STD_INPUT_HANDLE );
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/sysdeps_win32.c b/adb/sysdeps_win32.c
index d41c42c..2105b16 100644
--- a/adb/sysdeps_win32.c
+++ b/adb/sysdeps_win32.c
@@ -781,7 +781,7 @@
 void  disable_tcp_nagle(int fd)
 {
     FH   fh = _fh_from_int(fd);
-    int  on;
+    int  on = 1;
 
     if ( !fh || fh->clazz != &_fh_socket_class )
         return;
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/fastboot/engine.c b/fastboot/engine.c
index 7a55260..8d46991 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -29,6 +29,7 @@
 #include "fastboot.h"
 #include "make_ext4fs.h"
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -45,8 +46,6 @@
 #include <sys/mman.h>
 #endif
 
-extern struct fs_info info;
-
 #define ARRAY_SIZE(x)           (sizeof(x)/sizeof(x[0]))
 
 double now()
@@ -302,10 +301,7 @@
 #else
     fd = fileno(tmpfile());
 #endif
-    /* reset ext4fs info so we can be called multiple times */
-    reset_ext4fs_info();
-    info.len = image->partition_size;
-    make_ext4fs_internal(fd, NULL, NULL, NULL, 0, 1, 0, 0, 0, NULL);
+    make_ext4fs_sparse_fd(fd, image->partition_size, NULL, NULL);
 
     fstat(fd, &st);
     image->image_size = st.st_size;
diff --git a/include/cutils/zygote.h b/include/cutils/zygote.h
index 22721a6..a7480d3 100644
--- a/include/cutils/zygote.h
+++ b/include/cutils/zygote.h
@@ -23,7 +23,6 @@
 
 int zygote_run_oneshot(int sendStdio, int argc, const char **argv);
 int zygote_run(int argc, const char **argv);
-int zygote_run_wait(int argc, const char **argv, void (*post_run_func)(int));
 
 #ifdef __cplusplus
 }
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index f90af81..746bec3 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -63,6 +63,7 @@
 #define AID_NFC           1027  /* nfc subsystem */
 #define AID_SDCARD_R      1028  /* external storage read access */
 #define AID_CLAT          1029  /* clat part of nat464 */
+#define AID_LOOP_RADIO    1030  /* loop radio devices */
 
 #define AID_SHELL         2000  /* adb and debug shell user */
 #define AID_CACHE         2001  /* cache access */
@@ -138,6 +139,7 @@
     { "net_admin", AID_NET_ADMIN, },
     { "net_bw_stats", AID_NET_BW_STATS, },
     { "net_bw_acct", AID_NET_BW_ACCT, },
+    { "loop_radio", AID_LOOP_RADIO, },
     { "misc",      AID_MISC, },
     { "nobody",    AID_NOBODY, },
     { "clat",      AID_CLAT, },
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/libcorkscrew/backtrace.c b/libcorkscrew/backtrace.c
index 3697d18..03dbd53 100644
--- a/libcorkscrew/backtrace.c
+++ b/libcorkscrew/backtrace.c
@@ -35,10 +35,8 @@
 #include <cutils/atomic.h>
 #include <elf.h>
 
-#if HAVE_DLADDR
 #define __USE_GNU // For dladdr(3) in glibc.
 #include <dlfcn.h>
-#endif
 
 #if defined(__BIONIC__)
 
@@ -256,7 +254,6 @@
             if (mi->name[0]) {
                 symbol->map_name = strdup(mi->name);
             }
-#if HAVE_DLADDR
             Dl_info info;
             if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
                 symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
@@ -264,7 +261,6 @@
                 symbol->symbol_name = strdup(info.dli_sname);
                 symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
             }
-#endif
         }
     }
     release_my_map_info_list(milist);
diff --git a/libcutils/zygote.c b/libcutils/zygote.c
index 75ce3ba..37236e8 100644
--- a/libcutils/zygote.c
+++ b/libcutils/zygote.c
@@ -159,44 +159,6 @@
 #endif /* HAVE_ANDROID_OS */
 }
 
-int zygote_run_wait(int argc, const char **argv, void (*post_run_func)(int))
-{
-    int fd;
-    int pid;
-    int err;
-    const char *newargv[argc + 1];
-
-    fd = socket_local_client(ZYGOTE_SOCKET, 
-            ANDROID_SOCKET_NAMESPACE_RESERVED, AF_LOCAL);
-
-    if (fd < 0) {
-        return -1;
-    }
-
-    // The command socket is passed to the peer as close-on-exec
-    // and will close when the peer dies
-    newargv[0] = "--peer-wait";
-    memcpy(newargv + 1, argv, argc * sizeof(*argv)); 
-
-    pid = send_request(fd, 1, argc + 1, newargv);
-
-    if (pid > 0 && post_run_func != NULL) {
-        post_run_func(pid);
-    }
-
-    // Wait for socket to close
-    do {
-        int dummy;
-        err = read(fd, &dummy, sizeof(dummy));
-    } while ((err < 0 && errno == EINTR) || err != 0);
-
-    do {
-        err = close(fd);
-    } while (err < 0 && errno == EINTR);
-
-    return 0;
-}
-
 /**
  * Spawns a new dalvik instance via the Zygote process. The non-zygote
  * arguments are passed to com.android.internal.os.RuntimeInit(). The
diff --git a/logwrapper/include/logwrap/logwrap.h b/logwrapper/include/logwrap/logwrap.h
index 722dda2..44523e3 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)
+ *   logwrap: when true, log messages from the child
  *
  * 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 android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
+        bool logwrap);
 
 __END_DECLS
 
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index c2b36be..a04097d 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,206 @@
 
 #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, args...)                                                   \
+do {                                                                          \
+    fprintf(stderr, fmt, ## args);                                            \
+    ALOG(LOG_ERROR, "logwrapper", fmt, ## args);                              \
+} while(0)
+
+#define FATAL_CHILD(fmt, args...)                                             \
+do {                                                                          \
+    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) {
-    int status;
+        int *chld_sts, bool logwrap) {
+    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");
+            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 (logwrap)
+                            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 (logwrap)
+                        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 (logwrap)
+            ALOG(LOG_INFO, btag, "%s", &buffer[a]);
     }
 
     if (WIFEXITED(status)) {
         if (WEXITSTATUS(status))
-            ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
+            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", tag,
+        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", tag,
+        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 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)) {
-        ALOG(LOG_ERROR, "logwrapper",
-            "executing %s failed: %s\n", argv_child[0], strerror(errno));
-        exit(-1);
+        FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
+                strerror(errno));
     }
 }
 
-int logwrap(int argc, char* argv[], int *status) {
-    pid_t pid;
+static void sigchld_handler(int sig) {
+    write(signal_fd_write, &sig, 1);
+}
 
+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;
     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");
+        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");
+        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");
+        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");
+            return -1;
         }
 
         // redirect stdout and stderr
@@ -190,36 +241,59 @@
         dup2(child_ptty, 2);
         close(child_ptty);
 
-        child(argc - 1, &argv[1]);
-        return fatal("This should never happen\n");
-
+        child(argc, argv, logwrap);
     } 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", 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, logwrap);
     }
+
+    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..ed71a29 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -59,7 +59,7 @@
         usage();
     }
 
-    rc = logwrap(argc, argv, &status);
+    rc = android_fork_execvp(argc - 1, &argv[1], &status, true, true);
     if (!rc) {
         if (WIFEXITED(status))
             rc = WEXITSTATUS(status);
diff --git a/rootdir/init.rc b/rootdir/init.rc
index bee0729..f866ad1 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -37,7 +37,7 @@
     export ANDROID_STORAGE /storage
     export ASEC_MOUNTPOINT /mnt/asec
     export LOOP_MOUNTPOINT /mnt/obb
-    export BOOTCLASSPATH /system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar
+    export BOOTCLASSPATH /system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar
 
 # Backward compatibility
     symlink /system/etc /etc
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}
diff --git a/toolbox/renice.c b/toolbox/renice.c
index 978b329..9dfeb51 100644
--- a/toolbox/renice.c
+++ b/toolbox/renice.c
@@ -35,11 +35,12 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sched.h>
+#include <getopt.h>
 
 static void
 usage(const char *s)
 {
-    fprintf(stderr, "USAGE: %s [[-r] priority pids ...] [-g pid]\n", s);
+    fprintf(stderr, "USAGE: %s [[-r] [-t TYPE] priority pids ...] [-g pid]\n", s);
     exit(EXIT_FAILURE);
 }
 
@@ -74,32 +75,49 @@
            sched_get_priority_min(sched), sched_get_priority_max(sched));
 }
 
+int get_sched(char *str)
+{
+    if (strcasecmp(str, "RR") == 0)
+        return SCHED_RR;
+    else if (strcasecmp(str, "FIFO") == 0)
+        return SCHED_FIFO;
+    else if (strcasecmp(str, "NORMAL") == 0)
+        return SCHED_OTHER;
+    else if (strcasecmp(str, "OTHER") == 0)
+        return SCHED_OTHER;
+    return SCHED_RR;
+}
+
 int renice_main(int argc, char *argv[])
 {
     int prio;
     int realtime = 0;
+    int opt;
+    int sched = SCHED_RR;
     char *cmd = argv[0];
 
-    // consume command name
-    argc--;
-    argv++;
-
-    if (argc < 1)
-        usage(cmd);
-
-    if(strcmp("-r", argv[0]) == 0) {
-        // do realtime priority adjustment
-        realtime = 1;
-        argc--;
-        argv++;
-    }
-
-	if(strcmp("-g", argv[0]) == 0) {
-        if (argc < 2)
+    do {
+        opt = getopt(argc, argv, "rt:g:");
+        if (opt == -1)
+            break;
+        switch (opt) {
+        case 'r':
+            // do realtime priority adjustment
+            realtime = 1;
+            break;
+        case 't':
+            sched = get_sched(optarg);
+            break;
+        case 'g':
+            print_prio(atoi(optarg));
+            return 0;
+        default:
             usage(cmd);
-        print_prio(atoi(argv[1]));
-        return 0;
-    }
+        }
+    } while (1);
+
+    argc -= optind;
+    argv += optind;
 
     if (argc < 1)
         usage(cmd);
@@ -122,7 +140,7 @@
             struct sched_param sp = { .sched_priority = prio };
             int ret;
 
-            ret = sched_setscheduler(pid, SCHED_RR, &sp);
+            ret = sched_setscheduler(pid, sched, &sp);
             if (ret) {
                 perror("sched_set_scheduler");
                 exit(EXIT_FAILURE);
@@ -137,8 +155,6 @@
             }
         }
     }
-   
+
     return 0;
 }
-
-