platform_system_core/adb/adb_io.h

77 lines
3.0 KiB
C
Raw Normal View History

/*
* 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.
*/
#ifndef ADB_IO_H
#define ADB_IO_H
#include <sys/types.h>
#include <string>
// Sends the protocol "OKAY" message.
bool SendOkay(int fd);
// Sends the protocol "FAIL" message, with the given failure reason.
bool SendFail(int fd, const std::string& reason);
// Writes a protocol-format string; a four hex digit length followed by the string data.
bool SendProtocolString(int fd, const std::string& s);
// Reads a protocol-format string; a four hex digit length followed by the string data.
bool ReadProtocolString(int fd, std::string* s, std::string* error);
// Reads exactly len bytes from fd into buf.
//
// Returns false if there is an error or if EOF was reached before len bytes
// were read. If EOF was found, errno will be set to 0.
//
// If this function fails, the contents of buf are undefined.
bool ReadFdExactly(int fd, void* buf, size_t len);
adb: fix adb client running out of sockets on Windows Background ========== On Windows, if you run "adb shell exit" in a loop in two windows, eventually the adb client will be unable to connect to the adb server. I think connect() is returning WSAEADDRINUSE: "Only one usage of each socket address (protocol/network address/port) is normally permitted. (10048)". The Windows System Event Log may also show Event 4227, Tcpip. Netstat output is filled with: # for the adb server TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT # for the adb client TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT The error probably means that the client is running out of free address:port pairs. The first netstat line is unavoidable, but the second line exists because the adb client is not waiting for orderly/graceful shutdown of the socket, and that is apparently required on Windows to get rid of the second line. For more info, see https://github.com/CompareAndSwap/SocketCloseTest . This is exacerbated by the fact that "adb shell exit" makes 4 socket connections to the adb server: 1) host:version, 2) host:features, 3) host:version (again), 4) shell:exit. Also exacerbating is the fact that the adb protocol is length-prefixed so the client typically does not have to 'read() until zero' which effectively waits for orderly/graceful shutdown. The Fix ======= Introduce a function, ReadOrderlyShutdown(), that should be called in the adb client to wait for the server to close its socket, before closing the client socket. I reviewed all code where the adb client makes a connection to the adb server and added ReadOrderlyShutdown() when it made sense. I wasn't able to add it to the following: * interactive_shell: this doesn't matter because this is interactive and thus can't be run fast enough to use up ports. * adb sideload: I couldn't get enough test coverage and I don't think this is being called frequently enough to be a problem. * send_shell_command, backup, adb_connect_command, adb shell, adb exec-out, install_multiple_app, adb_send_emulator_command: These already wait for server socket shutdown since they already call recv() until zero. * restore, adb exec-in: protocol design can't have the server close first. * adb start-server: no fd is actually returned * create_local_service_socket, local_connect_arbitrary_ports, connect_device: probably called rarely enough not to be a problem. Also in this change =================== * Clarify comments in when adb_shutdown() is called before exit(). * add some missing adb_close() in adb sideload. * Fixup error handling and comments in adb_send_emulator_command(). * Make SyncConnection::SendQuit return a success boolean. * Add unittest for adb emu kill command. This gets code coverage over this very careful piece of code. Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 08:32:44 +08:00
// Given a client socket, wait for orderly/graceful shutdown. Call this:
//
// * Before closing a client socket.
// * Only when no more data is expected to come in.
// * Only when the server is not waiting for data from the client (because then
// the client and server will deadlock waiting for each other).
// * Only when the server is expected to close its socket right now.
// * Don't call shutdown(SHUT_WR) before calling this because that will shutdown
// the client socket early, defeating the purpose of calling this.
//
// Waiting for orderly/graceful shutdown of the server socket will cause the
// server socket to close before the client socket. That prevents the client
// socket from staying in TIME_WAIT which eventually causes subsequent
// connect()s from the client to fail with WSAEADDRINUSE on Windows.
// Returns true if it is sure that orderly/graceful shutdown has occurred with
// no additional data read from the server.
bool ReadOrderlyShutdown(int fd);
// Writes exactly len bytes from buf to fd.
//
// Returns false if there is an error or if the fd was closed before the write
// completed. If the other end of the fd (such as in a socket, pipe, or fifo),
// is closed, errno will be set to 0.
bool WriteFdExactly(int fd, const void* buf, size_t len);
// Same as above, but for strings.
bool WriteFdExactly(int fd, const char* s);
bool WriteFdExactly(int fd, const std::string& s);
// Same as above, but formats the string to send.
bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
#endif /* ADB_IO_H */