blob: 176b7bd44de38bce3e3c63005e1d0658f9a8615c [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 Hughes4f713192015-12-04 22:00:26 -080023#include <android-base/stringprintf.h>
Elliott Hughesab52c182015-05-01 17:04:38 -070024
Dan Albertcc731cc2015-02-24 21:26:58 -080025#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070026#include "adb_utils.h"
Elliott Hughesab52c182015-05-01 17:04:38 -070027#include "sysdeps.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070028
29bool SendProtocolString(int fd, const std::string& s) {
30 int length = s.size();
31 if (length > 0xffff) {
32 length = 0xffff;
33 }
34
Elliott Hughesaa245492015-08-03 10:38:08 -070035 // The cost of sending two strings outweighs the cost of formatting.
36 // "adb sync" performance is affected by this.
37 return WriteFdFmt(fd, "%04x%.*s", length, length, s.c_str());
38}
39
40bool ReadProtocolString(int fd, std::string* s, std::string* error) {
41 char buf[5];
42 if (!ReadFdExactly(fd, buf, 4)) {
43 *error = perror_str("protocol fault (couldn't read status length)");
44 return false;
45 }
46 buf[4] = 0;
47
48 unsigned long len = strtoul(buf, 0, 16);
49 s->resize(len, '\0');
50 if (!ReadFdExactly(fd, &(*s)[0], len)) {
51 *error = perror_str("protocol fault (couldn't read status message)");
52 return false;
53 }
54
55 return true;
Elliott Hughese67f1f82015-04-30 17:32:03 -070056}
57
58bool SendOkay(int fd) {
59 return WriteFdExactly(fd, "OKAY", 4);
60}
61
62bool SendFail(int fd, const std::string& reason) {
63 return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
64}
Dan Albertcc731cc2015-02-24 21:26:58 -080065
66bool ReadFdExactly(int fd, void* buf, size_t len) {
67 char* p = reinterpret_cast<char*>(buf);
68
Dan Albertcc731cc2015-02-24 21:26:58 -080069 size_t len0 = len;
Dan Albertcc731cc2015-02-24 21:26:58 -080070
Yabin Cui7a3f8d62015-09-02 17:44:28 -070071 D("readx: fd=%d wanted=%zu", fd, len);
Dan Albertcc731cc2015-02-24 21:26:58 -080072 while (len > 0) {
Dan Albertbac34742015-02-25 17:51:28 -080073 int r = adb_read(fd, p, len);
Dan Albertcc731cc2015-02-24 21:26:58 -080074 if (r > 0) {
75 len -= r;
76 p += r;
77 } else if (r == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070078 D("readx: fd=%d error %d: %s", fd, errno, strerror(errno));
Dan Albertcc731cc2015-02-24 21:26:58 -080079 return false;
80 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070081 D("readx: fd=%d disconnected", fd);
Dan Albertcc731cc2015-02-24 21:26:58 -080082 errno = 0;
83 return false;
84 }
85 }
86
Yabin Cuiaed3c612015-09-22 15:52:57 -070087 VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len)
88 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
Dan Albertcc731cc2015-02-24 21:26:58 -080089
90 return true;
91}
92
93bool WriteFdExactly(int fd, const void* buf, size_t len) {
94 const char* p = reinterpret_cast<const char*>(buf);
95 int r;
96
Yabin Cuiaed3c612015-09-22 15:52:57 -070097 VLOG(RWX) << "writex: fd=" << fd << " len=" << len
98 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
Dan Albertcc731cc2015-02-24 21:26:58 -080099
100 while (len > 0) {
Dan Albertbac34742015-02-25 17:51:28 -0800101 r = adb_write(fd, p, len);
Dan Albertcc731cc2015-02-24 21:26:58 -0800102 if (r == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700103 D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
Dan Albertcc731cc2015-02-24 21:26:58 -0800104 if (errno == EAGAIN) {
105 adb_sleep_ms(1); // just yield some cpu time
106 continue;
107 } else if (errno == EPIPE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700108 D("writex: fd=%d disconnected", fd);
Dan Albertcc731cc2015-02-24 21:26:58 -0800109 errno = 0;
110 return false;
Dan Albert08f66bc2015-03-19 18:09:59 -0700111 } else {
112 return false;
Dan Albertcc731cc2015-02-24 21:26:58 -0800113 }
114 } else {
115 len -= r;
116 p += r;
117 }
118 }
119 return true;
120}
121
Elliott Hughese67f1f82015-04-30 17:32:03 -0700122bool WriteFdExactly(int fd, const char* str) {
123 return WriteFdExactly(fd, str, strlen(str));
124}
125
126bool WriteFdExactly(int fd, const std::string& str) {
127 return WriteFdExactly(fd, str.c_str(), str.size());
128}
129
Elliott Hughesab52c182015-05-01 17:04:38 -0700130bool WriteFdFmt(int fd, const char* fmt, ...) {
131 std::string str;
132
133 va_list ap;
134 va_start(ap, fmt);
135 android::base::StringAppendV(&str, fmt, ap);
136 va_end(ap);
137
138 return WriteFdExactly(fd, str);
Dan Albertcc731cc2015-02-24 21:26:58 -0800139}
Spencer Low351ecd12015-10-14 17:32:44 -0700140
141bool ReadOrderlyShutdown(int fd) {
142 char buf[16];
143
144 // Only call this function if you're sure that the peer does
145 // orderly/graceful shutdown of the socket, closing the socket so that
146 // adb_read() will return 0. If the peer keeps the socket open, adb_read()
147 // will never return.
148 int result = adb_read(fd, buf, sizeof(buf));
149 if (result == -1) {
150 // If errno is EAGAIN, that means this function was called on a
151 // nonblocking socket and it would have blocked (which would be bad
152 // because we'd probably block the main thread where nonblocking IO is
153 // done). Don't do that. If you have a nonblocking socket, use the
154 // fdevent APIs to get called on FDE_READ, and then call this function
155 // if you really need to, but it shouldn't be needed for server sockets.
156 CHECK_NE(errno, EAGAIN);
157
158 // Note that on Windows, orderly shutdown sometimes causes
159 // recv() == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET. That
160 // can be ignored.
161 return false;
162 } else if (result == 0) {
163 // Peer has performed an orderly/graceful shutdown.
164 return true;
165 } else {
166 // Unexpectedly received data. This is essentially a protocol error
167 // because you should not call this function unless you expect no more
168 // data. We don't repeatedly call adb_read() until we get zero because
169 // we don't know how long that would take, but we do know that the
170 // caller wants to close the socket soon.
171 VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read "
172 << dump_hex(buf, result);
173 // Shutdown the socket to prevent the caller from reading or writing to
174 // it which doesn't make sense if we just read and discarded some data.
175 adb_shutdown(fd);
176 errno = EINVAL;
177 return false;
178 }
179}