Merge "Update eventlog tags for WifiStateTracker"
diff --git a/adb/adb.c b/adb/adb.c
index 95dc001..ee78688 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -302,8 +302,10 @@
 {
     asocket *s;
 
-    D("handle_packet() %d\n", p->msg.command);
-
+    D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
+            ((char*) (&(p->msg.command)))[1],
+            ((char*) (&(p->msg.command)))[2],
+            ((char*) (&(p->msg.command)))[3]);
     print_packet("recv", p);
 
     switch(p->msg.command){
diff --git a/adb/jdwp_service.c b/adb/jdwp_service.c
index 0c26f7b..296f718 100644
--- a/adb/jdwp_service.c
+++ b/adb/jdwp_service.c
@@ -5,6 +5,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 
 /* here's how these things work.
 
@@ -320,6 +321,7 @@
             struct iovec     iov;
             char             dummy = '!';
             char             buffer[sizeof(struct cmsghdr) + sizeof(int)];
+            int flags;
 
             iov.iov_base       = &dummy;
             iov.iov_len        = 1;
@@ -337,10 +339,27 @@
             cmsg->cmsg_type  = SCM_RIGHTS;
             ((int*)CMSG_DATA(cmsg))[0] = fd;
 
+            flags = fcntl(proc->socket,F_GETFL,0);
+
+            if (flags == -1) {
+                D("failed to get cntl flags for socket %d: %s\n",
+                  proc->pid, strerror(errno));
+                goto CloseProcess;
+
+            }
+
+            if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) {
+                D("failed to remove O_NONBLOCK flag for socket %d: %s\n",
+                  proc->pid, strerror(errno));
+                goto CloseProcess;
+            }
+
             for (;;) {
                 ret = sendmsg(proc->socket, &msg, 0);
-                if (ret >= 0)
+                if (ret >= 0) {
+                    adb_close(fd);
                     break;
+                }
                 if (errno == EINTR)
                     continue;
                 D("sending new file descriptor to JDWP %d failed: %s\n",
@@ -354,6 +373,12 @@
             for (n = 1; n < proc->out_count; n++)
                 proc->out_fds[n-1] = proc->out_fds[n];
 
+            if (fcntl(proc->socket, F_SETFL, flags) == -1) {
+                D("failed to set O_NONBLOCK flag for socket %d: %s\n",
+                  proc->pid, strerror(errno));
+                goto CloseProcess;
+            }
+
             if (--proc->out_count == 0)
                 fdevent_del( proc->fde, FDE_WRITE );
         }
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
index 993206f..efeb2a0 100644
--- a/adb/usb_vendors.c
+++ b/adb/usb_vendors.c
@@ -75,6 +75,8 @@
 #define VENDOR_ID_NEC           0x0409
 // Panasonic Mobile Communication's USB Vendor ID
 #define VENDOR_ID_PMC           0x04DA
+// Toshiba's USB Vendor ID
+#define VENDOR_ID_TOSHIBA       0x0930
 
 
 /** built-in vendor list */
@@ -99,6 +101,7 @@
     VENDOR_ID_OTGV,
     VENDOR_ID_NEC,
     VENDOR_ID_PMC,
+    VENDOR_ID_TOSHIBA,
 };
 
 #define BUILT_IN_VENDOR_COUNT    (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
diff --git a/fastboot/usb_osx.c b/fastboot/usb_osx.c
index 9488687..570a456 100644
--- a/fastboot/usb_osx.c
+++ b/fastboot/usb_osx.c
@@ -231,8 +231,7 @@
                 kr = (*interface)->ClearPipeStallBothEnds(interface,
                         handle->bulkIn);
                 if (kr != 0) {
-                    ERR("could not clear input pipe; result %d", kr);
-                    return -1;
+                    ERR("could not clear input pipe; result %x, ignoring...\n", kr);
                 }
             }
 
@@ -240,8 +239,7 @@
                 kr = (*interface)->ClearPipeStallBothEnds(interface,
                         handle->bulkOut);
                 if (kr != 0) {
-                    ERR("could not clear output pipe; result %d", kr);
-                    return -1;
+                    ERR("could not clear output pipe; result %x, ignoring....\n", kr);
                 }
             }
             
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 1dbe171..903f99f 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -165,6 +165,7 @@
     { 00640, AID_SYSTEM,    AID_SYSTEM,    "system/etc/bluetooth/auto_pairing.conf" },
     { 00444, AID_RADIO,     AID_AUDIO,     "system/etc/AudioPara4.csv" },
     { 00555, AID_ROOT,      AID_ROOT,      "system/etc/ppp/*" },
+    { 00555, AID_ROOT,      AID_ROOT,      "system/etc/rc.*" },
     { 00644, AID_SYSTEM,    AID_SYSTEM,    "data/app/*" },
     { 00644, AID_SYSTEM,    AID_SYSTEM,    "data/app-private/*" },
     { 00644, AID_APP,       AID_APP,       "data/data/*" },
diff --git a/include/usbhost/usbhost.h b/include/usbhost/usbhost.h
index c6a443c..04c494d 100644
--- a/include/usbhost/usbhost.h
+++ b/include/usbhost/usbhost.h
@@ -42,6 +42,11 @@
  */
 typedef int (* usb_device_removed_cb)(const char *dev_name, void *client_data);
 
+/* Callback indicating that initial device discovery is done.
+ * Return true to exit from usb_host_run.
+ */
+typedef int (* usb_discovery_done_cb)(void *client_data);
+
 /* Call this to initialize the USB host library. */
 struct usb_host_context *usb_host_init(void);
 
@@ -54,10 +59,13 @@
  * added_cb will be called immediately for each existing USB device,
  * and subsequently each time a new device is added.
  * removed_cb is called when USB devices are removed from the bus.
+ * discovery_done_cb is called after the initial discovery of already
+ * connected devices is complete.
  */
 void usb_host_run(struct usb_host_context *context,
                   usb_device_added_cb added_cb,
                   usb_device_removed_cb removed_cb,
+                  usb_discovery_done_cb discovery_done_cb,
                   void *client_data);
 
 /* Creates a usb_device object for a USB device */
@@ -82,6 +90,15 @@
 /* Returns the USB product ID from the device descriptor for the USB device */
 uint16_t usb_device_get_product_id(struct usb_device *device);
 
+/* Sends a control message to the specified device on endpoint zero */
+int usb_device_send_control(struct usb_device *device,
+                            int requestType,
+                            int request,
+                            int value,
+                            int index,
+                            int length,
+                            void* buffer);
+
 /* Returns a USB descriptor string for the given string ID.
  * Used to implement usb_device_get_manufacturer_name,
  * usb_device_get_product_name and usb_device_get_serial.
diff --git a/init/devices.c b/init/devices.c
index 155a41e..8e912de 100644
--- a/init/devices.c
+++ b/init/devices.c
@@ -58,6 +58,7 @@
 {
     struct sockaddr_nl addr;
     int sz = 64*1024; // XXX larger? udev uses 16MB!
+    int on = 1;
     int s;
 
     memset(&addr, 0, sizeof(addr));
@@ -70,6 +71,7 @@
         return -1;
 
     setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
+    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
 
     if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
         close(s);
@@ -562,18 +564,42 @@
 #define UEVENT_MSG_LEN  1024
 void handle_device_fd()
 {
-    char msg[UEVENT_MSG_LEN+2];
-    int n;
+    for(;;) {
+        char msg[UEVENT_MSG_LEN+2];
+        char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
+        struct iovec iov = {msg, sizeof(msg)};
+        struct sockaddr_nl snl;
+        struct msghdr hdr = {&snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0};
 
-    while((n = recv(device_fd, msg, UEVENT_MSG_LEN, 0)) > 0) {
-        struct uevent uevent;
+        ssize_t n = recvmsg(device_fd, &hdr, 0);
+        if (n <= 0) {
+            break;
+        }
 
-        if(n == UEVENT_MSG_LEN)   /* overflow -- discard */
+        if ((snl.nl_groups != 1) || (snl.nl_pid != 0)) {
+            /* ignoring non-kernel netlink multicast message */
+            continue;
+        }
+
+        struct cmsghdr * cmsg = CMSG_FIRSTHDR(&hdr);
+        if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
+            /* no sender credentials received, ignore message */
+            continue;
+        }
+
+        struct ucred * cred = (struct ucred *)CMSG_DATA(cmsg);
+        if (cred->uid != 0) {
+            /* message from non-root user, ignore */
+            continue;
+        }
+
+        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
             continue;
 
         msg[n] = '\0';
         msg[n+1] = '\0';
 
+        struct uevent uevent;
         parse_event(msg, &uevent);
 
         handle_device_event(&uevent);
diff --git a/libacc/Android.mk b/libacc/Android.mk
index 5101e16..67661b4 100644
--- a/libacc/Android.mk
+++ b/libacc/Android.mk
@@ -4,7 +4,8 @@
 # Shared library for target
 # ========================================================
 
-LOCAL_MODULE:= libacc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := libacc
 LOCAL_SRC_FILES := acc.cpp
 
 LOCAL_SHARED_LIBRARIES := libdl libcutils
diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp
index c2ba647..86c1f42 100644
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -93,13 +93,11 @@
 }
 
 const char *NetlinkEvent::findParam(const char *paramName) {
-    int i;
-
-    for (i = 0; i < NL_PARAMS_MAX; i++) {
-        if (!mParams[i])
-            break;
-        if (!strncmp(mParams[i], paramName, strlen(paramName)))
-            return &mParams[i][strlen(paramName) + 1];
+    size_t len = strlen(paramName);
+    for (int i = 0; mParams[i] && i < NL_PARAMS_MAX; ++i) {
+        const char *ptr = mParams[i] + len;
+        if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
+            return ++ptr;
     }
 
     SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
index efd3103..613dbaa 100644
--- a/libusbhost/usbhost.c
+++ b/libusbhost/usbhost.c
@@ -144,6 +144,7 @@
 void usb_host_run(struct usb_host_context *context,
                   usb_device_added_cb added_cb,
                   usb_device_removed_cb removed_cb,
+                  usb_discovery_done_cb discovery_done_cb,
                   void *client_data)
 {
     struct inotify_event* event;
@@ -161,6 +162,8 @@
     wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
     if (wds[0] < 0) {
         fprintf(stderr, "inotify_add_watch failed\n");
+        if (discovery_done_cb)
+            discovery_done_cb(client_data);
         return;
     }
 
@@ -174,6 +177,8 @@
 
     /* check for existing devices first, after we have inotify set up */
     done = find_existing_devices(added_cb, removed_cb, client_data);
+    if (discovery_done_cb)
+        done |= discovery_done_cb(client_data);
 
     while (!done) {
         ret = read(context->fd, event_buf, sizeof(event_buf));
@@ -277,18 +282,17 @@
     return __le16_to_cpu(desc->idProduct);
 }
 
-char* usb_device_get_string(struct usb_device *device, int id)
+int usb_device_send_control(struct usb_device *device,
+                            int requestType,
+                            int request,
+                            int value,
+                            int index,
+                            int length,
+                            void* buffer)
 {
-    char string[256];
     struct usbdevfs_ctrltransfer  ctrl;
-    __u16 buffer[128];
-    __u16 languages[128];
-    int i, result;
-    int languageCount = 0;
 
-    string[0] = 0;
-
-    // reading the string requires read/write permission
+    // this usually requires read/write permission
     if (!device->writeable) {
         int fd = open(device->dev_name, O_RDWR);
         if (fd > 0) {
@@ -296,37 +300,44 @@
             device->fd = fd;
             device->writeable = 1;
         } else {
-            return NULL;
+            return -1;
         }
     }
 
-    memset(languages, 0, sizeof(languages));
     memset(&ctrl, 0, sizeof(ctrl));
+    ctrl.bRequestType = requestType;
+    ctrl.bRequest = request;
+    ctrl.wValue = value;
+    ctrl.wIndex = index;
+    ctrl.wLength = length;
+    ctrl.data = buffer;
+    return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
+}
+
+char* usb_device_get_string(struct usb_device *device, int id)
+{
+    char string[256];
+    __u16 buffer[128];
+    __u16 languages[128];
+    int i, result;
+    int languageCount = 0;
+
+    string[0] = 0;
+    memset(languages, 0, sizeof(languages));
 
     // read list of supported languages
-    ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
-    ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
-    ctrl.wValue = (USB_DT_STRING << 8) | 0;
-    ctrl.wIndex = 0;
-    ctrl.wLength = sizeof(languages);
-    ctrl.data = languages;
-
-    result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
+    result = usb_device_send_control(device,
+            USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
+            (USB_DT_STRING << 8) | 0, 0, sizeof(languages), languages);
     if (result > 0)
         languageCount = (result - 2) / 2;
 
     for (i = 1; i <= languageCount; i++) {
         memset(buffer, 0, sizeof(buffer));
-        memset(&ctrl, 0, sizeof(ctrl));
 
-        ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
-        ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
-        ctrl.wValue = (USB_DT_STRING << 8) | id;
-        ctrl.wIndex = languages[i];
-        ctrl.wLength = sizeof(buffer);
-        ctrl.data = buffer;
-
-        result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
+        result = usb_device_send_control(device,
+                USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
+                (USB_DT_STRING << 8) | id, languages[i], sizeof(buffer), buffer);
         if (result > 0) {
             int i;
             // skip first word, and copy the rest to the string, changing shorts to bytes.
diff --git a/patch.txt b/patch.txt
new file mode 100644
index 0000000..258965d
--- /dev/null
+++ b/patch.txt
@@ -0,0 +1,16 @@
+diff --git a/init/util.c b/init/util.c
+index 4d98cc2..0667593 100755
+--- a/init/util.c
++++ b/init/util.c
+@@ -657,8 +657,9 @@ static void get_hardware_name(void)
+         if (x) {
+             x += 2;
+             n = 0;
+-            while (*x && !isspace(*x)) {
+-                hardware[n++] = tolower(*x);
++            while (*x && *x != '\n') {
++                if (!isspace(*x))
++                    hardware[n++] = tolower(*x);
+                 x++;
+                 if (n == 31) break;
+             }
diff --git a/toolbox/NOTICE b/toolbox/NOTICE
index 12f28b9..895b49a 100644
--- a/toolbox/NOTICE
+++ b/toolbox/NOTICE
@@ -1,5 +1,67 @@
 
-Copyright (c) 2008, The Android Open Source Project
+Copyright (c) 2010, The Android Open Source Project.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in
+   the documentation and/or other materials provided with the 
+   distribution.
+ * Neither the name of The Android Open Source Project nor the names
+   of its contributors may be used to endorse or promote products
+   derived from this software without specific prior written
+   permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+
+Copyright (c) 2009, The Android Open Source Project.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in
+   the documentation and/or other materials provided with the 
+   distribution.
+ * Neither the name of The Android Open Source Project nor the names
+   of its contributors may be used to endorse or promote products
+   derived from this software without specific prior written
+   permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+
+Copyright (c) 2008, The Android Open Source Project.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
diff --git a/toolbox/uptime.c b/toolbox/uptime.c
index 8b1983d..3d8061c 100644
--- a/toolbox/uptime.c
+++ b/toolbox/uptime.c
@@ -1,17 +1,32 @@
 /*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (c) 2010, The Android Open Source Project
+ * All rights reserved.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *  * Neither the name of Google, Inc. nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #include <sys/time.h>