Move transport declarations into transport.h.

There are a few cloexec issues in here as an added bonus.

Change-Id: I1699d719d733f47878bdba0454230cf5ab6a60b6
diff --git a/adb.c b/adb.c
index d1f05e4..9c0bd8c 100644
--- a/adb.c
+++ b/adb.c
@@ -31,6 +31,7 @@
 #include "adb.h"
 #include "adb_auth.h"
 #include "adb_listeners.h"
+#include "transport.h"
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
 
diff --git a/adb.h b/adb.h
index 4181fbc..9a68871 100644
--- a/adb.h
+++ b/adb.h
@@ -18,10 +18,10 @@
 #define __ADB_H
 
 #include <limits.h>
+#include <sys/types.h>
 
 #include "adb_trace.h"
 #include "fdevent.h"
-#include "transport.h"  /* readx(), writex() */
 
 #ifdef __cplusplus
 extern "C" {
@@ -37,12 +37,15 @@
 #define A_WRTE 0x45545257
 #define A_AUTH 0x48545541
 
-#define A_VERSION 0x01000000        // ADB protocol version
+// ADB protocol version.
+#define A_VERSION 0x01000000
 
-#define ADB_VERSION_MAJOR 1         // Used for help/version information
-#define ADB_VERSION_MINOR 0         // Used for help/version information
+// Used for help/version information.
+#define ADB_VERSION_MAJOR 1
+#define ADB_VERSION_MINOR 0
 
-#define ADB_SERVER_VERSION    32    // Increment this when we want to force users to start a new adb server
+// Increment this when we want to force users to start a new adb server.
+#define ADB_SERVER_VERSION 32
 
 typedef struct amessage amessage;
 typedef struct apacket apacket;
@@ -263,33 +266,11 @@
 void fatal_errno(const char *fmt, ...);
 
 void handle_packet(apacket *p, atransport *t);
-void send_packet(apacket *p, atransport *t);
 
 void get_my_path(char *s, size_t maxLen);
 int launch_server(int server_port);
 int adb_main(int is_daemon, int server_port);
 
-
-/* transports are ref-counted
-** get_device_transport does an acquire on your behalf before returning
-*/
-void init_transport_registration(void);
-int  list_transports(char *buf, size_t  bufsize, int long_listing);
-void update_transports(void);
-
-asocket*  create_device_tracker(void);
-
-/* Obtain a transport from the available transports.
-** If state is != CS_ANY, only transports in that state are considered.
-** If serial is non-NULL then only the device with that serial will be chosen.
-** If no suitable transport is found, error is set.
-*/
-atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
-void   add_transport_disconnect( atransport*  t, adisconnect*  dis );
-void   remove_transport_disconnect( atransport*  t, adisconnect*  dis );
-void   run_transport_disconnects( atransport*  t );
-void   kick_transport( atransport*  t );
-
 /* initialize a transport object's func pointers and state */
 #if ADB_HOST
 int get_available_local_transport_index();
@@ -297,22 +278,6 @@
 int  init_socket_transport(atransport *t, int s, int port, int local);
 void init_usb_transport(atransport *t, usb_handle *usb, int state);
 
-/* for MacOS X cleanup */
-void close_usb_devices();
-
-/* cause new transports to be init'd and added to the list */
-int register_socket_transport(int s, const char *serial, int port, int local);
-
-/* these should only be used for the "adb disconnect" command */
-void unregister_transport(atransport *t);
-void unregister_all_tcp_transports();
-
-void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
-
-/* this should only be used for transports with connection_state == CS_NOPERM */
-void unregister_usb_transport(usb_handle *usb);
-
-atransport *find_transport(const char *serial);
 #if ADB_HOST
 atransport* find_emulator_transport_by_adb_port(int adb_port);
 #endif
@@ -343,9 +308,6 @@
 apacket *get_apacket(void);
 void put_apacket(apacket *p);
 
-int check_header(apacket *p);
-int check_data(apacket *p);
-
 // Define it if you want to dump packets.
 #define DEBUG_PACKETS 0
 
diff --git a/adb_auth.c b/adb_auth.c
index 11a89b0..c236b64 100644
--- a/adb_auth.c
+++ b/adb_auth.c
@@ -24,6 +24,7 @@
 
 #include "adb.h"
 #include "adb_auth.h"
+#include "transport.h"
 #include "sysdeps.h"
 
 int auth_enabled = 0;
diff --git a/adb_auth_client.c b/adb_auth_client.c
index 55e9dca..7be883c 100644
--- a/adb_auth_client.c
+++ b/adb_auth_client.c
@@ -14,18 +14,20 @@
  * limitations under the License.
  */
 
+#include <resolv.h>
 #include <stdio.h>
 #include <string.h>
-#include <resolv.h>
-#include <cutils/list.h>
-#include <cutils/sockets.h>
 
 #include "sysdeps.h"
+
 #include "adb.h"
 #include "adb_auth.h"
+#include "cutils/list.h"
+#include "cutils/sockets.h"
 #include "fdevent.h"
 #include "mincrypt/rsa.h"
 #include "mincrypt/sha.h"
+#include "transport.h"
 
 #define TRACE_TAG TRACE_AUTH
 
diff --git a/adb_client.c b/adb_client.c
index ac5e15a..9bf7b2b 100644
--- a/adb_client.c
+++ b/adb_client.c
@@ -1,17 +1,34 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * 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
+ *
+ *      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.
+ */
+
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
-#include <zipfile/zipfile.h>
-#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #include "sysdeps.h"
 
 #define  TRACE_TAG  TRACE_ADB
 #include "adb_client.h"
+#include "transport.h"
+#include "zipfile/zipfile.h"
 
 static transport_type __adb_transport = kTransportAny;
 static const char* __adb_serial = NULL;
diff --git a/adb_listeners.c b/adb_listeners.c
index cccc520..f68b876 100644
--- a/adb_listeners.c
+++ b/adb_listeners.c
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 
 #include "sysdeps.h"
+#include "transport.h"
 
 int gListenAll = 0; /* Not static because it is used in commandline.c. */
 
diff --git a/adb_main.c b/adb_main.c
index 57a15dd..f8475c7 100644
--- a/adb_main.c
+++ b/adb_main.c
@@ -21,10 +21,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "sysdeps.h"
+
 #include "adb.h"
 #include "adb_auth.h"
 #include "adb_listeners.h"
-#include "sysdeps.h"
+#include "transport.h"
 
 #if !ADB_HOST
 #include <getopt.h>
diff --git a/commandline.c b/commandline.c
index 830f290..2a02c38 100644
--- a/commandline.c
+++ b/commandline.c
@@ -14,30 +14,31 @@
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
+#include <assert.h>
+#include <ctype.h>
 #include <errno.h>
-#include <unistd.h>
 #include <limits.h>
 #include <stdarg.h>
-#include <sys/types.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
-#include <ctype.h>
-#include <assert.h>
-
-#include "sysdeps.h"
+#include <sys/types.h>
 
 #if !defined(_WIN32)
 #include <termios.h>
+#include <unistd.h>
 #endif
 
+#include "sysdeps.h"
+
 #define  TRACE_TAG  TRACE_ADB
 #include "adb.h"
 #include "adb_client.h"
 #include "adb_auth.h"
 #include "file_sync_service.h"
+#include "transport.h"
 
 static int do_cmd(transport_type ttype, char* serial, char *cmd, ...);
 
diff --git a/file_sync_client.c b/file_sync_client.c
index f7828a6..cf446e2 100644
--- a/file_sync_client.c
+++ b/file_sync_client.c
@@ -14,24 +14,25 @@
  * limitations under the License.
  */
 
+#include <dirent.h>
+#include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-#include <time.h>
-#include <dirent.h>
-#include <limits.h>
 #include <sys/types.h>
-#include <zipfile/zipfile.h>
+#include <time.h>
 #include <utime.h>
 
 #include "sysdeps.h"
+
 #include "adb.h"
 #include "adb_client.h"
 #include "file_sync_service.h"
-
+#include "transport.h"
+#include "zipfile/zipfile.h"
 
 static unsigned long long total_bytes;
 static long long start_time;
diff --git a/file_sync_service.c b/file_sync_service.c
index 7de82b7..0944e5d 100644
--- a/file_sync_service.c
+++ b/file_sync_service.c
@@ -14,24 +14,24 @@
  * limitations under the License.
  */
 
-#include <stdlib.h>
+#include <dirent.h>
+#include <errno.h>
+#include <selinux/android.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <dirent.h>
-#include <utime.h>
 #include <unistd.h>
+#include <utime.h>
 
-#include <errno.h>
-#include <private/android_filesystem_config.h>
-#include <selinux/android.h>
 #include "sysdeps.h"
 
 #define TRACE_TAG  TRACE_SYNC
 #include "adb.h"
 #include "file_sync_service.h"
+#include "private/android_filesystem_config.h"
+#include "transport.h"
 
 /* TODO: use fs_config to configure permissions on /data */
 static bool is_on_system(const char *name) {
diff --git a/framebuffer_service.c b/framebuffer_service.c
index 61578aa..5665a56 100644
--- a/framebuffer_service.c
+++ b/framebuffer_service.c
@@ -14,21 +14,23 @@
  * limitations under the License.
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
 #include <errno.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include "fdevent.h"
-#include "adb.h"
-
+#include <fcntl.h>
 #include <linux/fb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "sysdeps.h"
+
+#include "adb.h"
+#include "fdevent.h"
+#include "transport.h"
 
 /* TODO:
 ** - sync with vsync to avoid tearing
@@ -68,15 +70,15 @@
 
     if (pid == 0) {
         dup2(fds[1], STDOUT_FILENO);
-        close(fds[0]);
-        close(fds[1]);
+        adb_close(fds[0]);
+        adb_close(fds[1]);
         const char* command = "screencap";
         const char *args[2] = {command, NULL};
         execvp(command, (char**)args);
         exit(1);
     }
 
-    close(fds[1]);
+    adb_close(fds[1]);
     fd_screencap = fds[0];
 
     /* read w, h & format */
@@ -174,9 +176,9 @@
     }
 
 done:
-    close(fds[0]);
+    adb_close(fds[0]);
 
     TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
 pipefail:
-    close(fd);
+    adb_close(fd);
 }
diff --git a/remount_service.c b/remount_service.c
index d7b0dd1..7e5613a 100644
--- a/remount_service.c
+++ b/remount_service.c
@@ -14,8 +14,6 @@
  * limitations under the License.
  */
 
-#include "sysdeps.h"
-
 #include <errno.h>
 #include <fcntl.h>
 #include <mntent.h>
@@ -25,11 +23,12 @@
 #include <sys/mount.h>
 #include <unistd.h>
 
-#include "cutils/properties.h"
+#include "sysdeps.h"
 
 #define  TRACE_TAG  TRACE_ADB
 #include "adb.h"
-
+#include "cutils/properties.h"
+#include "transport.h"
 
 static int system_ro = 1;
 static int vendor_ro = 1;
diff --git a/services.c b/services.c
index bd210a8..2325c0f 100644
--- a/services.c
+++ b/services.c
@@ -14,29 +14,30 @@
  * limitations under the License.
  */
 
-#include <stddef.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
 #include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef _WIN32
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#endif
+
+#if !ADB_HOST
+#include "cutils/android_reboot.h"
+#include "cutils/properties.h"
+#endif
 
 #include "sysdeps.h"
 
 #define  TRACE_TAG  TRACE_SERVICES
 #include "adb.h"
 #include "file_sync_service.h"
-
-#if ADB_HOST
-#  ifndef HAVE_WINSOCK
-#    include <netinet/in.h>
-#    include <netdb.h>
-#    include <sys/ioctl.h>
-#  endif
-#else
-#  include <cutils/android_reboot.h>
-#  include <cutils/properties.h>
-#endif
+#include "transport.h"
 
 typedef struct stinfo stinfo;
 
diff --git a/sockets.c b/sockets.c
index 1598c3c..7da6d57 100644
--- a/sockets.c
+++ b/sockets.c
@@ -14,21 +14,21 @@
  * limitations under the License.
  */
 
+#include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
 #include <string.h>
-#include <ctype.h>
+#include <unistd.h>
 
 #include "sysdeps.h"
 
-#if !ADB_HOST
-#include <cutils/properties.h>
-#endif
-
 #define  TRACE_TAG  TRACE_SOCKETS
 #include "adb.h"
+#if !ADB_HOST
+#include "cutils/properties.h"
+#endif
+#include "transport.h"
 
 ADB_MUTEX_DEFINE( socket_list_lock );
 
diff --git a/transport.c b/transport.c
index ffe59da..4904cd0 100644
--- a/transport.c
+++ b/transport.c
@@ -14,13 +14,15 @@
  * limitations under the License.
  */
 
+#include "sysdeps.h"
+
+#include "transport.h"
+
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
-#include <errno.h>
-
-#include "sysdeps.h"
+#include <unistd.h>
 
 #define   TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
@@ -494,8 +496,7 @@
 
 
 /* call this function each time the transport list has changed */
-void  update_transports(void)
-{
+void update_transports(void) {
     char             buffer[1024];
     int              len;
     device_tracker*  tracker;
diff --git a/transport.h b/transport.h
index d95ad32..9802b91 100644
--- a/transport.h
+++ b/transport.h
@@ -19,17 +19,64 @@
 
 #include <sys/types.h>
 
+#include "adb.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/* convenience wrappers around read/write that will retry on
-** EINTR and/or short read/write.  Returns 0 on success, -1
-** on error or EOF.
-*/
+/*
+ * Convenience wrappers around read/write that will retry on
+ * EINTR and/or short read/write.  Returns 0 on success, -1
+ * on error or EOF.
+ */
 int readx(int fd, void *ptr, size_t len);
 int writex(int fd, const void *ptr, size_t len);
 
+/*
+ * Obtain a transport from the available transports.
+ * If state is != CS_ANY, only transports in that state are considered.
+ * If serial is non-NULL then only the device with that serial will be chosen.
+ * If no suitable transport is found, error is set.
+ */
+atransport* acquire_one_transport(int state, transport_type ttype,
+                                  const char* serial, char** error_out);
+void add_transport_disconnect(atransport* t, adisconnect* dis);
+void remove_transport_disconnect(atransport* t, adisconnect* dis);
+void kick_transport(atransport* t);
+void run_transport_disconnects(atransport* t);
+void update_transports(void);
+
+/* transports are ref-counted
+** get_device_transport does an acquire on your behalf before returning
+*/
+void init_transport_registration(void);
+int list_transports(char* buf, size_t bufsize, int long_listing);
+atransport* find_transport(const char* serial);
+
+void register_usb_transport(usb_handle* h, const char* serial,
+                            const char* devpath, unsigned writeable);
+
+/* cause new transports to be init'd and added to the list */
+int register_socket_transport(int s, const char* serial, int port, int local);
+
+/* this should only be used for transports with connection_state == CS_NOPERM */
+void unregister_usb_transport(usb_handle* usb);
+
+/* these should only be used for the "adb disconnect" command */
+void unregister_transport(atransport* t);
+void unregister_all_tcp_transports();
+
+int check_header(apacket* p);
+int check_data(apacket* p);
+
+/* for MacOS X cleanup */
+void close_usb_devices();
+
+void send_packet(apacket* p, atransport* t);
+
+asocket* create_device_tracker(void);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/transport_local.c b/transport_local.c
index 6c4e220..e07f2f7 100644
--- a/transport_local.c
+++ b/transport_local.c
@@ -14,19 +14,20 @@
  * limitations under the License.
  */
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
+#include <sys/types.h>
 
 #include "sysdeps.h"
-#include <sys/types.h>
-#if !ADB_HOST
-#include <cutils/properties.h>
-#endif
 
 #define  TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
+#if !ADB_HOST
+#include "cutils/properties.h"
+#endif
+#include "transport.h"
 
 #if ADB_HOST
 /* we keep a list of opened transports. The atransport struct knows to which
diff --git a/transport_usb.c b/transport_usb.c
index 1138ddd..37a8219 100644
--- a/transport_usb.c
+++ b/transport_usb.c
@@ -22,6 +22,7 @@
 
 #define  TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
+#include "transport.h"
 
 static int remote_read(apacket *p, atransport *t)
 {
diff --git a/usb_linux.c b/usb_linux.c
index d242c8a..d03f8be 100644
--- a/usb_linux.c
+++ b/usb_linux.c
@@ -14,33 +14,30 @@
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <errno.h>
 #include <ctype.h>
-
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <linux/usbdevice_fs.h>
 #include <linux/version.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
 #include <linux/usb/ch9.h>
 #else
 #include <linux/usb_ch9.h>
 #endif
-#include <asm/byteorder.h>
 
 #include "sysdeps.h"
 
 #define   TRACE_TAG  TRACE_USB
 #include "adb.h"
-
+#include "transport.h"
 
 /* usb scan debugging is waaaay too verbose */
 #define DBGX(x...)
diff --git a/usb_linux_client.c b/usb_linux_client.c
index ee6b37c..c88b258 100644
--- a/usb_linux_client.c
+++ b/usb_linux_client.c
@@ -14,22 +14,22 @@
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#include <linux/usb/ch9.h>
-#include <linux/usb/functionfs.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
 #include <dirent.h>
 #include <errno.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/functionfs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "sysdeps.h"
 
 #define   TRACE_TAG  TRACE_USB
 #include "adb.h"
+#include "transport.h"
 
 #define MAX_PACKET_SIZE_FS	64
 #define MAX_PACKET_SIZE_HS	512
diff --git a/usb_windows.c b/usb_windows.c
index a2d7226..3c5533b 100644
--- a/usb_windows.c
+++ b/usb_windows.c
@@ -14,19 +14,20 @@
  * limitations under the License.
  */
 
-#include <winsock2.h>
-#include <windows.h>
-#include <winerror.h>
-#include <errno.h>
-#include <usb100.h>
+#include <winsock2.h>  // winsock.h *must* be included before windows.h.
 #include <adb_api.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <usb100.h>
+#include <windows.h>
+#include <winerror.h>
 
 #include "sysdeps.h"
 
 #define   TRACE_TAG  TRACE_USB
 #include "adb.h"
+#include "transport.h"
 
 /** Structure usb_handle describes our connection to the usb device via
   AdbWinApi.dll. This structure is returned from usb_open() routine and