am da5b2d67: am 99bb69c1: am f41ee3d5: Merge "74602: Fixes adb not seeing connected devices on Mac."

* commit 'da5b2d67faeb2a85f042cf5b23d9875ad3413183':
  74602: Fixes adb not seeing connected devices on Mac.
diff --git a/adb/adb.h b/adb/adb.h
index 4f06800..135999d 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -338,6 +338,9 @@
 int check_header(apacket *p);
 int check_data(apacket *p);
 
+// Define it if you want to dump packets.
+#define DEBUG_PACKETS 0
+
 #if !DEBUG_PACKETS
 #define print_packet(tag,p) do {} while (0)
 #endif
diff --git a/adb/adb_client.c b/adb/adb_client.c
index eb1720d..ac5e15a 100644
--- a/adb/adb_client.c
+++ b/adb/adb_client.c
@@ -279,7 +279,7 @@
 
     fd = _adb_connect(service);
     if(fd == -1) {
-        D("_adb_connect error: %s\n", __adb_error);
+        D("_adb_connect error: %s", __adb_error);
     } else if(fd == -2) {
         fprintf(stderr,"** daemon still not running\n");
     }
diff --git a/adb/adb_trace.h b/adb/adb_trace.h
index 8a5d9f8..b8a2f4c 100644
--- a/adb/adb_trace.h
+++ b/adb/adb_trace.h
@@ -73,8 +73,9 @@
             if (ADB_TRACING) {                         \
                 int save_errno = errno;                \
                 adb_mutex_lock(&D_lock);               \
-                fprintf(stderr, "%s::%s():",           \
-                        __FILE__, __FUNCTION__);       \
+                fprintf(stderr, "%16s: %5d:%5lu | ",   \
+                        __FUNCTION__,                  \
+                        getpid(), adb_thread_id());    \
                 errno = save_errno;                    \
                 fprintf(stderr, __VA_ARGS__ );         \
                 fflush(stderr);                        \
@@ -96,15 +97,16 @@
         } while (0)
 #  define  DD(...)                                     \
         do {                                           \
-          int save_errno = errno;                      \
-          adb_mutex_lock(&D_lock);                     \
-          fprintf(stderr, "%s::%s():",                 \
-                  __FILE__, __FUNCTION__);             \
-          errno = save_errno;                          \
-          fprintf(stderr, __VA_ARGS__ );               \
-          fflush(stderr);                              \
-          adb_mutex_unlock(&D_lock);                   \
-          errno = save_errno;                          \
+            int save_errno = errno;                    \
+            adb_mutex_lock(&D_lock);                   \
+            fprintf(stderr, "%16s: %5d:%5lu | ",       \
+                    __FUNCTION__,                      \
+                    getpid(), adb_thread_id());        \
+            errno = save_errno;                        \
+            fprintf(stderr, __VA_ARGS__ );             \
+            fflush(stderr);                            \
+            adb_mutex_unlock(&D_lock);                 \
+            errno = save_errno;                        \
         } while (0)
 #else
 #  define  D(...)                                      \
diff --git a/adb/fdevent.c b/adb/fdevent.c
index 43e600c..f5ecd14 100644
--- a/adb/fdevent.c
+++ b/adb/fdevent.c
@@ -661,6 +661,8 @@
     if(adb_socketpair(s)) {
         FATAL("cannot create shell-exit socket-pair\n");
     }
+    D("socketpair: (%d,%d)", s[0], s[1]);
+
     SHELL_EXIT_NOTIFY_FD = s[0];
     fdevent *fde;
     fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
diff --git a/adb/jdwp_service.c b/adb/jdwp_service.c
index cd62b55..3074e42 100644
--- a/adb/jdwp_service.c
+++ b/adb/jdwp_service.c
@@ -415,6 +415,7 @@
               __FUNCTION__, strerror(errno));
             return -1;
         }
+        D("socketpair: (%d,%d)", fds[0], fds[1]);
 
         proc->out_fds[ proc->out_count ] = fds[1];
         if (++proc->out_count == 1)
diff --git a/adb/services.c b/adb/services.c
index e61371a..bf2f2c3 100644
--- a/adb/services.c
+++ b/adb/services.c
@@ -164,6 +164,7 @@
         printf("cannot create service socket pair\n");
         return -1;
     }
+    D("socketpair: (%d,%d)", s[0], s[1]);
 
     sti = malloc(sizeof(stinfo));
     if(sti == 0) fatal("cannot allocate stinfo");
@@ -264,10 +265,11 @@
 
     // 0 is parent socket, 1 is child socket
     int sv[2];
-    if (unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
+    if (adb_socketpair(sv) < 0) {
         printf("[ cannot create socket pair - %s ]\n", strerror(errno));
         return -1;
     }
+    D("socketpair: (%d,%d)", sv[0], sv[1]);
 
     *pid = fork();
     if (*pid < 0) {
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index ba4306f..11de548 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -76,6 +76,11 @@
     return 0;
 }
 
+static __inline__  unsigned long adb_thread_id()
+{
+    return GetCurrentThreadId();
+}
+
 static __inline__ void  close_on_exec(int  fd)
 {
     /* nothing really */
@@ -515,6 +520,12 @@
 {
     return strtok_r(str, delim, saveptr);
 }
+
+static __inline__ unsigned long adb_thread_id()
+{
+    return (unsigned long)pthread_self();
+}
+
 #undef   strtok_r
 #define  strtok_r  ___xxx_strtok_r
 
diff --git a/adb/transport.c b/adb/transport.c
index f35880c..7db6a47 100644
--- a/adb/transport.c
+++ b/adb/transport.c
@@ -629,7 +629,7 @@
             fatal_errno("cannot open transport socketpair");
         }
 
-        D("transport: %s (%d,%d) starting\n", t->serial, s[0], s[1]);
+        D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
 
         t->transport_socket = s[0];
         t->fd = s[1];
@@ -673,6 +673,7 @@
     if(adb_socketpair(s)){
         fatal_errno("cannot open transport registration socketpair");
     }
+    D("socketpair: (%d,%d)", s[0], s[1]);
 
     transport_registration_send = s[0];
     transport_registration_recv = s[1];
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 03b3177..64a52b5 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -298,9 +298,8 @@
             "                                           Can override the fs type and/or\n"
             "                                           size the bootloader reports.\n"
             "  getvar <variable>                        display a bootloader variable\n"
-            "  boot <kernel> [ <ramdisk> [ <second> ] ] download and boot kernel\n"
-            "  flash:raw boot <kernel> [ <ramdisk> [ <second> ] ] create bootimage and \n"
-            "                                           flash it\n"
+            "  boot <kernel> [ <ramdisk> ]              download and boot kernel\n"
+            "  flash:raw boot <kernel> [ <ramdisk> ]    create bootimage and flash it\n"
             "  devices                                  list all connected devices\n"
             "  continue                                 continue with autoboot\n"
             "  reboot                                   reboot device normally\n"
@@ -328,11 +327,10 @@
 }
 
 void *load_bootable_image(const char *kernel, const char *ramdisk,
-                          const char *secondstage, unsigned *sz,
-                          const char *cmdline)
+                          unsigned *sz, const char *cmdline)
 {
-    void *kdata = 0, *rdata = 0, *sdata = 0;
-    unsigned ksize = 0, rsize = 0, ssize = 0;
+    void *kdata = 0, *rdata = 0;
+    unsigned ksize = 0, rsize = 0;
     void *bdata;
     unsigned bsize;
 
@@ -368,18 +366,10 @@
         }
     }
 
-    if (secondstage) {
-        sdata = load_file(secondstage, &ssize);
-        if(sdata == 0) {
-            fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
-            return  0;
-        }
-    }
-
     fprintf(stderr,"creating boot image...\n");
     bdata = mkbootimg(kdata, ksize, kernel_offset,
                       rdata, rsize, ramdisk_offset,
-                      sdata, ssize, second_offset,
+                      0, 0, second_offset,
                       page_size, base_addr, tags_offset, &bsize);
     if(bdata == 0) {
         fprintf(stderr,"failed to create boot.img\n");
@@ -1157,7 +1147,6 @@
         } else if(!strcmp(*argv, "boot")) {
             char *kname = 0;
             char *rname = 0;
-            char *sname = 0;
             skip(1);
             if (argc > 0) {
                 kname = argv[0];
@@ -1167,11 +1156,7 @@
                 rname = argv[0];
                 skip(1);
             }
-            if (argc > 0) {
-                sname = argv[0];
-                skip(1);
-            }
-            data = load_bootable_image(kname, rname, sname, &sz, cmdline);
+            data = load_bootable_image(kname, rname, &sz, cmdline);
             if (data == 0) return 1;
             fb_queue_download("boot.img", data, sz);
             fb_queue_command("boot", "booting");
@@ -1195,18 +1180,14 @@
             char *pname = argv[1];
             char *kname = argv[2];
             char *rname = 0;
-            char *sname = 0;
             require(3);
-            skip(3);
-            if (argc > 0) {
-                rname = argv[0];
-                skip(1);
+            if(argc > 3) {
+                rname = argv[3];
+                skip(4);
+            } else {
+                skip(3);
             }
-            if (argc > 0) {
-                sname = argv[0];
-                skip(1);
-            }
-            data = load_bootable_image(kname, rname, sname, &sz, cmdline);
+            data = load_bootable_image(kname, rname, &sz, cmdline);
             if (data == 0) die("cannot load bootable image");
             fb_queue_flash(pname, data, sz);
         } else if(!strcmp(*argv, "flashall")) {
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc
index fa80e1e..6602d3f 100644
--- a/libnativebridge/native_bridge.cc
+++ b/libnativebridge/native_bridge.cc
@@ -29,7 +29,6 @@
 enum class NativeBridgeState {
   kNotSetup,                        // Initial state.
   kOpened,                          // After successful dlopen.
-                                    // Temporary meaning: string copied. TODO: remove. b/17440362
   kInitialized,                     // After successful initialization.
   kClosed                           // Closed or errors.
 };
@@ -61,9 +60,6 @@
 // Whether we had an error at some point.
 static bool had_error = false;
 
-// Native bridge filename. TODO: Temporary, remove. b/17440362
-static const char* native_bridge_filename;
-
 // Handle of the loaded library.
 static void* native_bridge_handle = nullptr;
 // Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
@@ -135,32 +131,28 @@
       state = NativeBridgeState::kClosed;
       had_error = true;
     } else {
-      // Save the name. TODO: Remove this, return to old flow. b/17440362
-      native_bridge_filename = nb_library_filename;
-      runtime_callbacks = runtime_cbs;
-      state = NativeBridgeState::kOpened;
-//       // Try to open the library.
-//       void* handle = dlopen(nb_library_filename, RTLD_LAZY);
-//       if (handle != nullptr) {
-//         callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
-//                                                                    kNativeBridgeInterfaceSymbol));
-//         if (callbacks != nullptr) {
-//           // Store the handle for later.
-//           native_bridge_handle = handle;
-//         } else {
-//           dlclose(handle);
-//         }
-//       }
-//
-//       // Two failure conditions: could not find library (dlopen failed), or could not find native
-//       // bridge interface (dlsym failed). Both are an error and close the native bridge.
-//       if (callbacks == nullptr) {
-//         had_error = true;
-//         state = NativeBridgeState::kClosed;
-//       } else {
-//         runtime_callbacks = runtime_cbs;
-//         state = NativeBridgeState::kOpened;
-//       }
+      // Try to open the library.
+      void* handle = dlopen(nb_library_filename, RTLD_LAZY);
+      if (handle != nullptr) {
+        callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
+                                                                   kNativeBridgeInterfaceSymbol));
+        if (callbacks != nullptr) {
+          // Store the handle for later.
+          native_bridge_handle = handle;
+        } else {
+          dlclose(handle);
+        }
+      }
+
+      // Two failure conditions: could not find library (dlopen failed), or could not find native
+      // bridge interface (dlsym failed). Both are an error and close the native bridge.
+      if (callbacks == nullptr) {
+        had_error = true;
+        state = NativeBridgeState::kClosed;
+      } else {
+        runtime_callbacks = runtime_cbs;
+        state = NativeBridgeState::kOpened;
+      }
     }
     return state == NativeBridgeState::kOpened;
   }
@@ -171,38 +163,15 @@
   // point we are not multi-threaded, so we do not need locking here.
 
   if (state == NativeBridgeState::kOpened) {
-    // Open and initialize. TODO: Temporary, remove. b/17440362
-    void* handle = dlopen(native_bridge_filename, RTLD_LAZY);
-    if (handle != nullptr) {
-      callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
-                                                                 kNativeBridgeInterfaceSymbol));
-      if (callbacks != nullptr) {
-        if (callbacks->initialize(runtime_callbacks)) {
-          state = NativeBridgeState::kInitialized;
-          native_bridge_handle = handle;
-        } else {
-          callbacks = nullptr;
-        }
-      }
-
-      if (callbacks == nullptr) {
-        state = NativeBridgeState::kClosed;
-        had_error = true;
-        dlclose(handle);
-      }
+    // Try to initialize.
+    if (callbacks->initialize(runtime_callbacks)) {
+      state = NativeBridgeState::kInitialized;
     } else {
-      state = NativeBridgeState::kClosed;
+      // Unload the library.
+      dlclose(native_bridge_handle);
       had_error = true;
+      state = NativeBridgeState::kClosed;
     }
-//     // Try to initialize.
-//     if (callbacks->initialize(runtime_callbacks)) {
-//       state = NativeBridgeState::kInitialized;
-//     } else {
-//       // Unload the library.
-//       dlclose(native_bridge_handle);
-//       had_error = true;
-//       state = NativeBridgeState::kClosed;
-//     }
   } else {
     had_error = true;
     state = NativeBridgeState::kClosed;
@@ -216,7 +185,7 @@
   // point we are not multi-threaded, so we do not need locking here.
 
   switch(state) {
-    // case NativeBridgeState::kOpened:  // TODO: Re-add this. b/17440362
+    case NativeBridgeState::kOpened:
     case NativeBridgeState::kInitialized:
       // Unload.
       dlclose(native_bridge_handle);
@@ -227,7 +196,6 @@
       had_error = true;
       break;
 
-    case NativeBridgeState::kOpened:  // TODO: Remove this. b/17440362
     case NativeBridgeState::kClosed:
       // Ignore.
       break;
diff --git a/libnativebridge/tests/ValidNameNativeBridge_test.cpp b/libnativebridge/tests/ValidNameNativeBridge_test.cpp
index b28f5b2..690be4a 100644
--- a/libnativebridge/tests/ValidNameNativeBridge_test.cpp
+++ b/libnativebridge/tests/ValidNameNativeBridge_test.cpp
@@ -27,12 +27,9 @@
     // Now check what happens on LoadNativeBridge.
     EXPECT_EQ(false, NativeBridgeError());
     LoadNativeBridge(kTestName, nullptr);
-    // TODO: Remove this call. b/17440362
-    InitializeNativeBridge();
     // This will lead to an error as the library doesn't exist.
     EXPECT_EQ(true, NativeBridgeError());
-    // TODO: Test again. b/17440362
-//     EXPECT_EQ(false, NativeBridgeAvailable());
+    EXPECT_EQ(false, NativeBridgeAvailable());
 }
 
 }  // namespace android
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 681324a..09f7297 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -525,6 +525,7 @@
     onrestart restart zygote
     onrestart restart media
     onrestart restart surfaceflinger
+    onrestart restart inputflinger
     onrestart restart drm
 
 service vold /system/bin/vold
@@ -558,6 +559,12 @@
     group graphics drmrpc
     onrestart restart zygote
 
+service inputflinger /system/bin/inputflinger
+    class main
+    user system
+    group input
+    onrestart restart zygote
+
 service drm /system/bin/drmserver
     class main
     user drm