blob: fe2ece50eb81f5237898d8c0627886721c1206d0 [file] [log] [blame]
Dan Albertcc731cc2015-02-24 21:26:58 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG RWX
Dan Albertcc731cc2015-02-24 21:26:58 -080018
Dan Albertcc731cc2015-02-24 21:26:58 -080019#include "adb_io.h"
20
21#include <unistd.h>
22
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080023#include <chrono>
24#include <thread>
25
Elliott Hughes4f713192015-12-04 22:00:26 -080026#include <android-base/stringprintf.h>
Elliott Hughesab52c182015-05-01 17:04:38 -070027
Josh Gao7e6683c2016-01-15 14:35:54 -080028#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080029#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070030#include "adb_utils.h"
Elliott Hughesab52c182015-05-01 17:04:38 -070031#include "sysdeps.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070032
33bool SendProtocolString(int fd, const std::string& s) {
Josh Gao7e6683c2016-01-15 14:35:54 -080034 unsigned int length = s.size();
35 if (length > MAX_PAYLOAD_V1 - 4) {
36 errno = EMSGSIZE;
37 return false;
Elliott Hughese67f1f82015-04-30 17:32:03 -070038 }
39
Elliott Hughesaa245492015-08-03 10:38:08 -070040 // The cost of sending two strings outweighs the cost of formatting.
41 // "adb sync" performance is affected by this.
42 return WriteFdFmt(fd, "%04x%.*s", length, length, s.c_str());
43}
44
45bool ReadProtocolString(int fd, std::string* s, std::string* error) {
46 char buf[5];
47 if (!ReadFdExactly(fd, buf, 4)) {
48 *error = perror_str("protocol fault (couldn't read status length)");
49 return false;
50 }
51 buf[4] = 0;
52
53 unsigned long len = strtoul(buf, 0, 16);
54 s->resize(len, '\0');
55 if (!ReadFdExactly(fd, &(*s)[0], len)) {
56 *error = perror_str("protocol fault (couldn't read status message)");
57 return false;
58 }
59
60 return true;
Elliott Hughese67f1f82015-04-30 17:32:03 -070061}
62
63bool SendOkay(int fd) {
64 return WriteFdExactly(fd, "OKAY", 4);
65}
66
67bool SendFail(int fd, const std::string& reason) {
68 return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
69}
Dan Albertcc731cc2015-02-24 21:26:58 -080070
71bool ReadFdExactly(int fd, void* buf, size_t len) {
72 char* p = reinterpret_cast<char*>(buf);
73
Dan Albertcc731cc2015-02-24 21:26:58 -080074 size_t len0 = len;
Dan Albertcc731cc2015-02-24 21:26:58 -080075
Yabin Cui7a3f8d62015-09-02 17:44:28 -070076 D("readx: fd=%d wanted=%zu", fd, len);
Dan Albertcc731cc2015-02-24 21:26:58 -080077 while (len > 0) {
Dan Albertbac34742015-02-25 17:51:28 -080078 int r = adb_read(fd, p, len);
Dan Albertcc731cc2015-02-24 21:26:58 -080079 if (r > 0) {
80 len -= r;
81 p += r;
82 } else if (r == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070083 D("readx: fd=%d error %d: %s", fd, errno, strerror(errno));
Dan Albertcc731cc2015-02-24 21:26:58 -080084 return false;
85 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070086 D("readx: fd=%d disconnected", fd);
Dan Albertcc731cc2015-02-24 21:26:58 -080087 errno = 0;
88 return false;
89 }
90 }
91
Yabin Cuiaed3c612015-09-22 15:52:57 -070092 VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len)
93 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
Dan Albertcc731cc2015-02-24 21:26:58 -080094
95 return true;
96}
97
98bool WriteFdExactly(int fd, const void* buf, size_t len) {
99 const char* p = reinterpret_cast<const char*>(buf);
100 int r;
101
Yabin Cuiaed3c612015-09-22 15:52:57 -0700102 VLOG(RWX) << "writex: fd=" << fd << " len=" << len
103 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
Dan Albertcc731cc2015-02-24 21:26:58 -0800104
105 while (len > 0) {
Dan Albertbac34742015-02-25 17:51:28 -0800106 r = adb_write(fd, p, len);
Dan Albertcc731cc2015-02-24 21:26:58 -0800107 if (r == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700108 D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
Dan Albertcc731cc2015-02-24 21:26:58 -0800109 if (errno == EAGAIN) {
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800110 std::this_thread::yield();
Dan Albertcc731cc2015-02-24 21:26:58 -0800111 continue;
112 } else if (errno == EPIPE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700113 D("writex: fd=%d disconnected", fd);
Dan Albertcc731cc2015-02-24 21:26:58 -0800114 errno = 0;
115 return false;
Dan Albert08f66bc2015-03-19 18:09:59 -0700116 } else {
117 return false;
Dan Albertcc731cc2015-02-24 21:26:58 -0800118 }
119 } else {
120 len -= r;
121 p += r;
122 }
123 }
124 return true;
125}
126
Elliott Hughese67f1f82015-04-30 17:32:03 -0700127bool WriteFdExactly(int fd, const char* str) {
128 return WriteFdExactly(fd, str, strlen(str));
129}
130
131bool WriteFdExactly(int fd, const std::string& str) {
132 return WriteFdExactly(fd, str.c_str(), str.size());
133}
134
Elliott Hughesab52c182015-05-01 17:04:38 -0700135bool WriteFdFmt(int fd, const char* fmt, ...) {
136 std::string str;
137
138 va_list ap;
139 va_start(ap, fmt);
140 android::base::StringAppendV(&str, fmt, ap);
141 va_end(ap);
142
143 return WriteFdExactly(fd, str);
Dan Albertcc731cc2015-02-24 21:26:58 -0800144}
Spencer Low351ecd12015-10-14 17:32:44 -0700145
146bool ReadOrderlyShutdown(int fd) {
147 char buf[16];
148
149 // Only call this function if you're sure that the peer does
150 // orderly/graceful shutdown of the socket, closing the socket so that
151 // adb_read() will return 0. If the peer keeps the socket open, adb_read()
152 // will never return.
153 int result = adb_read(fd, buf, sizeof(buf));
154 if (result == -1) {
155 // If errno is EAGAIN, that means this function was called on a
156 // nonblocking socket and it would have blocked (which would be bad
157 // because we'd probably block the main thread where nonblocking IO is
158 // done). Don't do that. If you have a nonblocking socket, use the
159 // fdevent APIs to get called on FDE_READ, and then call this function
160 // if you really need to, but it shouldn't be needed for server sockets.
161 CHECK_NE(errno, EAGAIN);
162
163 // Note that on Windows, orderly shutdown sometimes causes
164 // recv() == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET. That
165 // can be ignored.
166 return false;
167 } else if (result == 0) {
168 // Peer has performed an orderly/graceful shutdown.
169 return true;
170 } else {
171 // Unexpectedly received data. This is essentially a protocol error
172 // because you should not call this function unless you expect no more
173 // data. We don't repeatedly call adb_read() until we get zero because
174 // we don't know how long that would take, but we do know that the
175 // caller wants to close the socket soon.
176 VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read "
177 << dump_hex(buf, result);
178 // Shutdown the socket to prevent the caller from reading or writing to
179 // it which doesn't make sense if we just read and discarded some data.
180 adb_shutdown(fd);
181 errno = EINVAL;
182 return false;
183 }
184}