Merge "libusbhost:  add usb_device_get_unique_id_from_name()"
diff --git a/logcat/event.logtags b/logcat/event.logtags
index f146738..d41f36b 100644
--- a/logcat/event.logtags
+++ b/logcat/event.logtags
@@ -115,31 +115,20 @@
 50000 menu_item_selected (Menu type where 0 is options and 1 is context|1|5),(Menu item title|3)
 50001 menu_opened (Menu type where 0 is options and 1 is context|1|5)
 
-# Wi-Fi network state changed:
-# [31- 6] Reserved for future use
-# [ 5- 0] Detailed state ordinal (as defined by NetworkInfo.DetailedState)
-50021 wifi_network_state_changed (network_state|1|5)
-
-# Wi-Fi supplicant state changed:
-# [31- 6] Reserved for future use
-# [ 5- 0] Supplicant state ordinal (as defined by SupplicantState)
-50022 wifi_supplicant_state_changed (supplicant_state|1|5)
-
-# Wi-Fi driver state changed:
-# [31- 1] Reserved for future use
-# [ 0- 0] Driver start (1) or stopped (0)
-50023 wifi_driver_state_changed (driver_state|1|5)
-
-# Wi-Fi interface configuration state changed:
-# [31- 1] Reserved for future use
-# [ 0- 0] Interface configuration succeeded (1) or failed (0)
-50024 wifi_interface_configuration_state_changed (IP_configuration|1|5)
-
-# Wi-Fi supplicant connection state changed:
-# [31- 2] Reserved for future use
-# [ 1- 0] Connected to supplicant (1) or disconnected from supplicant (0),
-#         or supplicant died (2)
-50025 wifi_supplicant_connection_state_changed (connected|1|5)
+# HSM wifi state change
+# Hierarchical state class name (as defined in WifiStateTracker.java)
+# Logged on every state change in the hierarchical state machine
+50021 wifi_state_changed (wifi_state|3)
+# HSM wifi event
+# [31-16] Reserved for future use
+# [15 - 0] HSM event (as defined in WifiStateTracker.java)
+# Logged when an event is handled in a hierarchical state
+50022 wifi_event_handled (wifi_event|1|5)
+# Supplicant state change
+# [31-13] Reserved for future use
+# [8 - 0] Supplicant state (as defined in SupplicantState.java)
+# Logged when the supplicant switches to a new state
+50023 wifi_supplicant_state_changed (supplicant_state|1|5)
 
 # Do not change these names without updating tag in:
 #//device/dalvik/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.c
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 9349276..588dac0 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -53,7 +53,8 @@
 	uptime \
 	vmstat \
 	nandread \
-	ionice
+	ionice \
+	lsof
 
 LOCAL_SRC_FILES:= \
 	toolbox.c \
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/lsof.c b/toolbox/lsof.c
new file mode 100644
index 0000000..99891db
--- /dev/null
+++ b/toolbox/lsof.c
@@ -0,0 +1,223 @@
+/*
+ * 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 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.
+ *
+ * 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 <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define BUF_MAX 1024
+#define CMD_DISPLAY_MAX 10
+
+struct pid_info_t {
+    pid_t pid;
+
+    char cmdline[CMD_DISPLAY_MAX];
+
+    char path[PATH_MAX];
+    ssize_t parent_length;
+};
+
+void print_header()
+{
+    printf("%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
+            "COMMAND",
+            "PID",
+            "USER",
+            "FD",
+            "TYPE",
+            "DEVICE",
+            "SIZE/OFF",
+            "NODE",
+            "NAME");
+}
+
+void print_type(char *type, struct pid_info_t* info)
+{
+    static ssize_t link_dest_size;
+    static char link_dest[PATH_MAX];
+
+    strncat(info->path, type, sizeof(info->path));
+    if ((link_dest_size = readlink(info->path, link_dest, sizeof(link_dest)-1)) < 0) {
+        if (errno == ENOENT)
+            goto out;
+
+        snprintf(link_dest, sizeof(link_dest), "%s (readlink: %s)", info->path, strerror(errno));
+    } else {
+        link_dest[link_dest_size] = '\0';
+    }
+
+    // Things that are just the root filesystem are uninteresting (we already know)
+    if (!strcmp(link_dest, "/"))
+        goto out;
+
+    printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n", info->cmdline, info->pid, "???", type,
+            "???", "???", "???", "???", link_dest);
+
+out:
+    info->path[info->parent_length] = '\0';
+}
+
+// Prints out all file that have been memory mapped
+void print_maps(struct pid_info_t* info)
+{
+    FILE *maps;
+    char buffer[PATH_MAX + 100];
+
+    size_t offset;
+    int major, minor;
+    char device[10];
+    long int inode;
+    char file[PATH_MAX];
+
+    strncat(info->path, "maps", sizeof(info->path));
+
+    maps = fopen(info->path, "r");
+    if (!maps)
+        goto out;
+
+    while (fscanf(maps, "%*x-%*x %*s %zx %5s %ld %s\n", &offset, device, &inode,
+            file) == 4) {
+        // We don't care about non-file maps
+        if (inode == 0 || !strcmp(device, "00:00"))
+            continue;
+
+        printf("%-9s %5d %10s %4s %9s %18s %9zd %10ld %s\n", info->cmdline, info->pid, "???", "mem",
+                "???", device, offset, inode, file);
+    }
+
+    fclose(maps);
+
+out:
+    info->path[info->parent_length] = '\0';
+}
+
+// Prints out all open file descriptors
+void print_fds(struct pid_info_t* info)
+{
+    static char* fd_path = "fd/";
+    strncat(info->path, fd_path, sizeof(info->path));
+
+    int previous_length = info->parent_length;
+    info->parent_length += strlen(fd_path);
+
+    DIR *dir = opendir(info->path);
+    if (dir == NULL) {
+        char msg[BUF_MAX];
+        snprintf(msg, sizeof(msg), "%s (opendir: %s)", info->path, strerror(errno));
+        printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n", info->cmdline, info->pid, "???", "FDS",
+                "", "", "", "", msg);
+        goto out;
+    }
+
+    struct dirent* de;
+    while ((de = readdir(dir))) {
+        if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
+            continue;
+
+        print_type(de->d_name, info);
+    }
+    closedir(dir);
+
+out:
+    info->parent_length = previous_length;
+    info->path[info->parent_length] = '\0';
+}
+
+void lsof_dumpinfo(pid_t pid)
+{
+    int fd;
+    struct pid_info_t info;
+    info.pid = pid;
+
+    snprintf(info.path, sizeof(info.path), "/proc/%d/", pid);
+
+    info.parent_length = strlen(info.path);
+
+    // Read the command line information; each argument is terminated with NULL.
+    strncat(info.path, "cmdline", sizeof(info.path));
+    fd = open(info.path, O_RDONLY);
+    if (fd < 0) {
+        fprintf(stderr, "Couldn't read %s\n", info.path);
+        return;
+    }
+    char cmdline[PATH_MAX];
+    if (read(fd, cmdline, sizeof(cmdline)) < 0) {
+        fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno));
+        close(fd);
+        return;
+    }
+    close(fd);
+    info.path[info.parent_length] = '\0';
+
+    // We only want the basename of the cmdline
+    strncpy(info.cmdline, basename(cmdline), sizeof(info.cmdline));
+    info.cmdline[sizeof(info.cmdline)-1] = '\0';
+
+    // Read each of these symlinks
+    print_type("cwd", &info);
+    print_type("exe", &info);
+    print_type("root", &info);
+
+    print_fds(&info);
+    print_maps(&info);
+}
+
+int lsof_main(int argc, char *argv[])
+{
+    DIR *dir = opendir("/proc");
+    if (dir == NULL) {
+        fprintf(stderr, "Couldn't open /proc\n");
+        return -1;
+    }
+
+    print_header();
+
+    struct dirent* de;
+    while ((de = readdir(dir))) {
+        if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
+            continue;
+
+        // Only inspect directories that are PID numbers
+        char* endptr;
+        long int pid = strtol(de->d_name, &endptr, 10);
+        if (*endptr != '\0')
+            continue;
+
+        lsof_dumpinfo(pid);
+    }
+    closedir(dir);
+
+    return 0;
+}
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>