2015-02-25 06:26:58 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG RWX
|
2015-02-25 06:26:58 +01:00
|
|
|
|
|
|
|
#include "adb_io.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-12-12 19:48:50 +01:00
|
|
|
#if !ADB_HOST
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
2016-11-15 21:37:32 +01:00
|
|
|
#include <thread>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-05-02 02:04:38 +02:00
|
|
|
|
2016-01-15 23:35:54 +01:00
|
|
|
#include "adb.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_trace.h"
|
2015-05-01 02:32:03 +02:00
|
|
|
#include "adb_utils.h"
|
2015-05-02 02:04:38 +02:00
|
|
|
#include "sysdeps.h"
|
2015-05-01 02:32:03 +02:00
|
|
|
|
2019-02-21 02:16:37 +01:00
|
|
|
bool SendProtocolString(int fd, std::string_view s) {
|
2016-01-15 23:35:54 +01:00
|
|
|
unsigned int length = s.size();
|
2017-06-17 00:34:34 +02:00
|
|
|
if (length > MAX_PAYLOAD - 4) {
|
2016-01-15 23:35:54 +01:00
|
|
|
errno = EMSGSIZE;
|
|
|
|
return false;
|
2015-05-01 02:32:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 19:38:08 +02:00
|
|
|
// The cost of sending two strings outweighs the cost of formatting.
|
|
|
|
// "adb sync" performance is affected by this.
|
2018-12-20 19:09:43 +01:00
|
|
|
auto str = android::base::StringPrintf("%04x", length).append(s);
|
|
|
|
return WriteFdExactly(fd, str);
|
2015-08-03 19:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadProtocolString(int fd, std::string* s, std::string* error) {
|
|
|
|
char buf[5];
|
|
|
|
if (!ReadFdExactly(fd, buf, 4)) {
|
|
|
|
*error = perror_str("protocol fault (couldn't read status length)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buf[4] = 0;
|
|
|
|
|
2018-07-14 03:15:16 +02:00
|
|
|
unsigned long len = strtoul(buf, nullptr, 16);
|
2015-08-03 19:38:08 +02:00
|
|
|
s->resize(len, '\0');
|
|
|
|
if (!ReadFdExactly(fd, &(*s)[0], len)) {
|
|
|
|
*error = perror_str("protocol fault (couldn't read status message)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-05-01 02:32:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SendOkay(int fd) {
|
|
|
|
return WriteFdExactly(fd, "OKAY", 4);
|
|
|
|
}
|
|
|
|
|
2019-02-21 02:16:37 +01:00
|
|
|
bool SendFail(int fd, std::string_view reason) {
|
2015-05-01 02:32:03 +02:00
|
|
|
return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
|
|
|
|
}
|
2015-02-25 06:26:58 +01:00
|
|
|
|
|
|
|
bool ReadFdExactly(int fd, void* buf, size_t len) {
|
|
|
|
char* p = reinterpret_cast<char*>(buf);
|
|
|
|
|
|
|
|
size_t len0 = len;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("readx: fd=%d wanted=%zu", fd, len);
|
2015-02-25 06:26:58 +01:00
|
|
|
while (len > 0) {
|
2015-02-26 02:51:28 +01:00
|
|
|
int r = adb_read(fd, p, len);
|
2015-02-25 06:26:58 +01:00
|
|
|
if (r > 0) {
|
|
|
|
len -= r;
|
|
|
|
p += r;
|
|
|
|
} else if (r == -1) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("readx: fd=%d error %d: %s", fd, errno, strerror(errno));
|
2015-02-25 06:26:58 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("readx: fd=%d disconnected", fd);
|
2015-02-25 06:26:58 +01:00
|
|
|
errno = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len)
|
|
|
|
<< " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
|
2015-02-25 06:26:58 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFdExactly(int fd, const void* buf, size_t len) {
|
|
|
|
const char* p = reinterpret_cast<const char*>(buf);
|
|
|
|
int r;
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
VLOG(RWX) << "writex: fd=" << fd << " len=" << len
|
|
|
|
<< " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
|
2015-02-25 06:26:58 +01:00
|
|
|
|
|
|
|
while (len > 0) {
|
2015-02-26 02:51:28 +01:00
|
|
|
r = adb_write(fd, p, len);
|
2015-02-25 06:26:58 +01:00
|
|
|
if (r == -1) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
|
2015-02-25 06:26:58 +01:00
|
|
|
if (errno == EAGAIN) {
|
2016-11-15 21:37:32 +01:00
|
|
|
std::this_thread::yield();
|
2015-02-25 06:26:58 +01:00
|
|
|
continue;
|
|
|
|
} else if (errno == EPIPE) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("writex: fd=%d disconnected", fd);
|
2015-02-25 06:26:58 +01:00
|
|
|
errno = 0;
|
|
|
|
return false;
|
2015-03-20 02:09:59 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
2015-02-25 06:26:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len -= r;
|
|
|
|
p += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
bool WriteFdExactly(int fd, const char* str) {
|
|
|
|
return WriteFdExactly(fd, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFdExactly(int fd, const std::string& str) {
|
|
|
|
return WriteFdExactly(fd, str.c_str(), str.size());
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:04:38 +02:00
|
|
|
bool WriteFdFmt(int fd, const char* fmt, ...) {
|
|
|
|
std::string str;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
android::base::StringAppendV(&str, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return WriteFdExactly(fd, str);
|
2015-02-25 06:26:58 +01:00
|
|
|
}
|
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 02:32:44 +02:00
|
|
|
|
|
|
|
bool ReadOrderlyShutdown(int fd) {
|
|
|
|
char buf[16];
|
|
|
|
|
|
|
|
// Only call this function if you're sure that the peer does
|
|
|
|
// orderly/graceful shutdown of the socket, closing the socket so that
|
|
|
|
// adb_read() will return 0. If the peer keeps the socket open, adb_read()
|
|
|
|
// will never return.
|
|
|
|
int result = adb_read(fd, buf, sizeof(buf));
|
|
|
|
if (result == -1) {
|
|
|
|
// If errno is EAGAIN, that means this function was called on a
|
|
|
|
// nonblocking socket and it would have blocked (which would be bad
|
|
|
|
// because we'd probably block the main thread where nonblocking IO is
|
|
|
|
// done). Don't do that. If you have a nonblocking socket, use the
|
|
|
|
// fdevent APIs to get called on FDE_READ, and then call this function
|
|
|
|
// if you really need to, but it shouldn't be needed for server sockets.
|
|
|
|
CHECK_NE(errno, EAGAIN);
|
|
|
|
|
|
|
|
// Note that on Windows, orderly shutdown sometimes causes
|
|
|
|
// recv() == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET. That
|
|
|
|
// can be ignored.
|
|
|
|
return false;
|
|
|
|
} else if (result == 0) {
|
|
|
|
// Peer has performed an orderly/graceful shutdown.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Unexpectedly received data. This is essentially a protocol error
|
|
|
|
// because you should not call this function unless you expect no more
|
|
|
|
// data. We don't repeatedly call adb_read() until we get zero because
|
|
|
|
// we don't know how long that would take, but we do know that the
|
|
|
|
// caller wants to close the socket soon.
|
|
|
|
VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read "
|
|
|
|
<< dump_hex(buf, result);
|
|
|
|
// Shutdown the socket to prevent the caller from reading or writing to
|
|
|
|
// it which doesn't make sense if we just read and discarded some data.
|
|
|
|
adb_shutdown(fd);
|
|
|
|
errno = EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|