Support socket activation of adb server

Socket activation allows adb to be run as a system daemon that starts
only as needed and supports race-free transparent restarts of the adb
server after a client issues an "adb kill-server" command.

Test: see SOCKET-ACTIVATION.txt
Change-Id: Ieabf08710ce4365e5513102f3aa578560aa7355e
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 466c2ce..979413a 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -349,8 +349,15 @@
     return c == '/';
 }
 
+static __inline__ int get_fd_flags(borrowed_fd fd) {
+    return fcntl(fd.get(), F_GETFD);
+}
+
 static __inline__ void close_on_exec(borrowed_fd fd) {
-    fcntl(fd.get(), F_SETFD, FD_CLOEXEC);
+    int flags = get_fd_flags(fd);
+    if (flags >= 0 && (flags & O_CLOEXEC) == 0) {
+        fcntl(fd.get(), F_SETFD, flags | O_CLOEXEC);
+    }
 }
 
 // Open a file and return a file descriptor that may be used with unix_read(),