Merge changes I8d0c60e5,Ib8ad0ad6
am: a6bb369cb0

Change-Id: I1f6da82e3d8cc4646eca6403bf1f1b7266665fbf
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index bd9ad01..62798cd 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -125,6 +125,11 @@
     });
 #endif
 
+    if (is_daemon) {
+        close_stdin();
+        setup_daemon_logging();
+    }
+
     android::base::at_quick_exit(adb_server_cleanup);
 
     init_transport_registration();
@@ -148,11 +153,6 @@
         std::this_thread::sleep_for(100ms);
     }
 
-    if (is_daemon) {
-        close_stdin();
-        setup_daemon_logging();
-    }
-
     adb_auth_init();
 
     if (is_daemon) {
diff --git a/adb/transport.cpp b/adb/transport.cpp
index 13cfaa1..24e90cf 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -1050,27 +1050,24 @@
         [usb](atransport* t) { return t->usb == usb && t->GetConnectionState() == kCsNoPerm; });
 }
 
-int check_header(apacket* p, atransport* t) {
+bool check_header(apacket* p, atransport* t) {
     if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
         VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
                   << ", magic = " << p->msg.magic;
-        return -1;
+        return false;
     }
 
     if (p->msg.data_length > t->get_max_payload()) {
         VLOG(RWX) << "check_header(): " << p->msg.data_length
                   << " atransport::max_payload = " << t->get_max_payload();
-        return -1;
+        return false;
     }
 
-    return 0;
+    return true;
 }
 
-int check_data(apacket* p) {
-    if (calculate_apacket_checksum(p) != p->msg.data_check) {
-        return -1;
-    }
-    return 0;
+bool check_data(apacket* p) {
+    return calculate_apacket_checksum(p) == p->msg.data_check;
 }
 
 #if ADB_HOST
diff --git a/adb/transport.h b/adb/transport.h
index 006aaf4..7304404 100644
--- a/adb/transport.h
+++ b/adb/transport.h
@@ -221,8 +221,8 @@
 // This should only be used for transports with connection_state == kCsNoPerm.
 void unregister_usb_transport(usb_handle* usb);
 
-int check_header(apacket* p, atransport* t);
-int check_data(apacket* p);
+bool check_header(apacket* p, atransport* t);
+bool check_data(apacket* p);
 
 void close_usb_devices();
 void close_usb_devices(std::function<bool(const atransport*)> predicate);
diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp
index 3ee286a..a9e583f 100644
--- a/adb/transport_local.cpp
+++ b/adb/transport_local.cpp
@@ -62,22 +62,22 @@
 
 static int remote_read(apacket *p, atransport *t)
 {
-    if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
+    if (!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))) {
         D("remote local: read terminated (message)");
         return -1;
     }
 
-    if(check_header(p, t)) {
+    if (!check_header(p, t)) {
         D("bad header: terminated (data)");
         return -1;
     }
 
-    if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
+    if (!ReadFdExactly(t->sfd, p->data, p->msg.data_length)) {
         D("remote local: terminated (data)");
         return -1;
     }
 
-    if(check_data(p)) {
+    if (!check_data(p)) {
         D("bad data: terminated (data)");
         return -1;
     }
diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp
index 885d723..47094b8 100644
--- a/adb/transport_usb.cpp
+++ b/adb/transport_usb.cpp
@@ -72,7 +72,7 @@
         D("remote usb: read terminated (message)");
         return -1;
     }
-    if (static_cast<size_t>(n) != sizeof(p->msg) || check_header(p, t)) {
+    if (static_cast<size_t>(n) != sizeof(p->msg) || !check_header(p, t)) {
         D("remote usb: check_header failed, skip it");
         goto err_msg;
     }
@@ -95,7 +95,7 @@
             goto err_msg;
         }
     }
-    if (check_data(p)) {
+    if (!check_data(p)) {
         D("remote usb: check_data failed, skip it");
         goto err_msg;
     }
@@ -124,19 +124,19 @@
         return -1;
     }
 
-    if(check_header(p, t)) {
+    if (!check_header(p, t)) {
         D("remote usb: check_header failed");
         return -1;
     }
 
-    if(p->msg.data_length) {
+    if (p->msg.data_length) {
         if (usb_read(t->usb, p->data, p->msg.data_length)) {
             D("remote usb: terminated (data)");
             return -1;
         }
     }
 
-    if(check_data(p)) {
+    if (!check_data(p)) {
         D("remote usb: check_data failed");
         return -1;
     }