Use %zd for size_t.

Change-Id: I0139243c309e85236eb00bb471736bdd99eeec84
Fix overflow in adb_client

Change-Id: I7f8582bd8a69d61ce47d11a9ffd0e901fc371037
Credit: Joshua Drake
Bug: 12060953
Free file handle for /firmware/image after use

Change-Id: Id59e25486b45dffc87a89a961970a6e8128a5237
init: Fix several memory leaks

1. Fix fd leak in do_mount function
2. Fix file1 and file2 leak in process_firmware_event function

Change-Id: Ic4271f2cf5e1df3cd32265544764a22b42271213
Signed-off-by: Hong-Mei Li <a21834@motorola.com>
Reviewed-on: http://gerrit.pcs.mot.com/563988
SLTApproved: Slta Waiver <sltawvr@motorola.com>
Tested-by: Jira Key <jirakey@motorola.com>
Reviewed-by: Yi-Wei Zhao <gbjc64@motorola.com>
SLTApproved: Yi-Wei Zhao <gbjc64@motorola.com>
Reviewed-by: Sheng-Zhe Zhao <a18689@motorola.com>
Submit-Approved: Jira Key <jirakey@motorola.com>
(cherry picked from commit 0f13608cf4d753e494ea5ba848bdd67dcf59c317)
init: device: fix memory leak issue

Fix some memory leak and null pointer issues reported by KW.

Change-Id: I61df734380498b68cdb4c3f66dcf5771e9e191eb
Signed-off-by: Hong-Mei Li <a21834@motorola.com>
Reviewed-on: http://gerrit.pcs.mot.com/549753
SLT-Approved: Slta Waiver <sltawvr@motorola.com>
Tested-by: Jira Key <jirakey@motorola.com>
Reviewed-by: Patrick Auchter <auchter@motorola.com>
Reviewed-by: Jeffrey Carlyle <jeff.carlyle@motorola.com>
Submit-Approved: Jira Key <jirakey@motorola.com>
(cherry picked from commit dab2f49656556281f8e8ac313c7023355f705489)
adb: Handle adb connect in a thread

adb connect calls connect() in the event loop. If you pass a wrong ip
address or the server is slow to respond, this will block the event loop
and you can't even kill the adb server with adb kill-server. Handle connect
requests in a service thread instead.

Change-Id: I2ee732869a3dc22a6d3b87cf8ac80acaa7790037
adb: Cleanup dead code

dns_service is unused and recover_service has been replaced by adb
sideload

Change-Id: Ie90000d7f672e8299ee1622a9690c7371b214dc1
fix up adb to prefer /sbin/sh if it exists

Change-Id: Ifa68efec387e6c12dc4bb2d0e764b8ecf96c0ad5

Conflicts:
	adb/services.c
adb: Check sender's socket id when receiving packets.

handle_packet() in adb.c didn't check that when an A_WRTE packet is
received, the sender's local-id matches the socket's peer id.

This meant that a compromised adbd server could sent packets to
the host adb server, spoofing the identity of another connected
device if it could "guess" the right host socket id.

This patch gets rid of the issue by enforcing even more checks
to ensure that all packets comply with the description in
protocol.txt.

+ Fix a bug where closing a local socket associated with a
  remote one would always send an A_CLSE(0, remote-id, "")
  message, though protocol.txt says that should only happen
  for failed opens.

  The issue was that local_socket_close() called
  remote_socket_close() after clearing the remote socket's
  'peer' field.

  The fix introduces a new asocket optional callback,
  named 'shutdown' that is called before that, and is
  used to send the A_CLSE() message with the right ID
  in remote_socket_shutdown().

  Also add some code in handle_packet() to detect
  invalid close commands.

Change-Id: I9098bc8c6e81f8809334b060e5dca4fc92e6fbc9
Fix adb forward --list when forwarding a lot

The list action had some problems with large numbers of forwards:
 * adb_query() limited replies to 1024 B (and the print was useless)
 * the reply header's length could overflow (also in other commands)
 * ...and the client had no way of detecting it
 * writex() didn't retry on EAGAIN ("Resource temporarily unavailable")

This patch makes all "OKAY%04x" replies use a common function which
checks the length and limits it to 0xffff. This means that the client
can easily check for truncated replies.

Before: forward --list starts failing at 15-30 forwards (depending on
device serial and forward spec lengths).

After: no problems with forward --list.

Change-Id: Ie1e82c4d622f5c56e51abb26533ba17d40459914
adbd: Don't close/reopen FFS ep0 on disconnect

In case of bulk read/write failure or disconnect, transport_usb
calls usb_ffs_kick in order to force the daemon to re-open the
FunctionFS ep files as a way to soft restart the kernel driver.

However, there's no reason to always close the ep0 file, as it
would need to be reopened and the descriptors rewritten--this
is unnecessary, especially in the simple/frequent case of cable
disconnect, and causes the kernel driver to unbind and bind *all*
of the function drivers. This is causing USB Chapter 9 tests to fail.

Thus, try to reuse the same ep0 file handle across reconnects. In
case there is a failure, the ep1/ep2 opens would fail and all the
file handles get dropped. For the adb root case, the entire daemon
gets restarted anyway.

Change-Id: I0840085c52a0795dcb2d751c39aa4a436c039ee2
adb: fix windows build after "forward --list" fix.

EWOULDBLOCK is not there on windows.
It gets translated by the adb_write() wrapper into EGAIN.
But the Linux one does not translate EWOULDBLOCK into EGAIN.
Without EWOULDBLOCK, it works just fine.

Change-Id: Ic293be392aa4364637428ea94ef87890ffa09b9b
Signed-off-by: JP Abgrall <jpa@google.com>