blob: ae168340802ff372f1d16a886a3686fc5ab8d765 [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
Josh Gao7e6683c2016-01-15 14:35:54 -080025#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080026#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070027#include "adb_utils.h"
Elliott Hughesab52c182015-05-01 17:04:38 -070028#include "sysdeps.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070029
30bool SendProtocolString(int fd, const std::string& s) {
Josh Gao7e6683c2016-01-15 14:35:54 -080031 unsigned int length = s.size();
32 if (length > MAX_PAYLOAD_V1 - 4) {
33 errno = EMSGSIZE;
34 return false;
Elliott Hughese67f1f82015-04-30 17:32:03 -070035 }
36
Elliott Hughesaa245492015-08-03 10:38:08 -070037 // The cost of sending two strings outweighs the cost of formatting.
38 // "adb sync" performance is affected by this.
39 return WriteFdFmt(fd, "%04x%.*s", length, length, s.c_str());
40}
41
42bool ReadProtocolString(int fd, std::string* s, std::string* error) {
43 char buf[5];
44 if (!ReadFdExactly(fd, buf, 4)) {
45 *error = perror_str("protocol fault (couldn't read status length)");
46 return false;
47 }
48 buf[4] = 0;
49
50 unsigned long len = strtoul(buf, 0, 16);
51 s->resize(len, '\0');
52 if (!ReadFdExactly(fd, &(*s)[0], len)) {
53 *error = perror_str("protocol fault (couldn't read status message)");
54 return false;
55 }
56
57 return true;
Elliott Hughese67f1f82015-04-30 17:32:03 -070058}
59
60bool SendOkay(int fd) {
61 return WriteFdExactly(fd, "OKAY", 4);
62}
63
64bool SendFail(int fd, const std::string& reason) {
65 return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
66}
Dan Albertcc731cc2015-02-24 21:26:58 -080067
68bool ReadFdExactly(int fd, void* buf, size_t len) {
69 char* p = reinterpret_cast<char*>(buf);
70
Dan Albertcc731cc2015-02-24 21:26:58 -080071 size_t len0 = len;
Dan Albertcc731cc2015-02-24 21:26:58 -080072
Yabin Cui7a3f8d62015-09-02 17:44:28 -070073 D("readx: fd=%d wanted=%zu", fd, len);
Dan Albertcc731cc2015-02-24 21:26:58 -080074 while (len > 0) {
Dan Albertbac34742015-02-25 17:51:28 -080075 int r = adb_read(fd, p, len);
Dan Albertcc731cc2015-02-24 21:26:58 -080076 if (r > 0) {
77 len -= r;
78 p += r;
79 } else if (r == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070080 D("readx: fd=%d error %d: %s", fd, errno, strerror(errno));
Dan Albertcc731cc2015-02-24 21:26:58 -080081 return false;
82 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070083 D("readx: fd=%d disconnected", fd);
Dan Albertcc731cc2015-02-24 21:26:58 -080084 errno = 0;
85 return false;
86 }
87 }
88
Yabin Cuiaed3c612015-09-22 15:52:57 -070089 VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len)
90 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
Dan Albertcc731cc2015-02-24 21:26:58 -080091
92 return true;
93}
94
95bool WriteFdExactly(int fd, const void* buf, size_t len) {
96 const char* p = reinterpret_cast<const char*>(buf);
97 int r;
98
Yabin Cuiaed3c612015-09-22 15:52:57 -070099 VLOG(RWX) << "writex: fd=" << fd << " len=" << len
100 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
Dan Albertcc731cc2015-02-24 21:26:58 -0800101
102 while (len > 0) {
Dan Albertbac34742015-02-25 17:51:28 -0800103 r = adb_write(fd, p, len);
Dan Albertcc731cc2015-02-24 21:26:58 -0800104 if (r == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700105 D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
Dan Albertcc731cc2015-02-24 21:26:58 -0800106 if (errno == EAGAIN) {
107 adb_sleep_ms(1); // just yield some cpu time
108 continue;
109 } else if (errno == EPIPE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700110 D("writex: fd=%d disconnected", fd);
Dan Albertcc731cc2015-02-24 21:26:58 -0800111 errno = 0;
112 return false;
Dan Albert08f66bc2015-03-19 18:09:59 -0700113 } else {
114 return false;
Dan Albertcc731cc2015-02-24 21:26:58 -0800115 }
116 } else {
117 len -= r;
118 p += r;
119 }
120 }
121 return true;
122}
123
Elliott Hughese67f1f82015-04-30 17:32:03 -0700124bool WriteFdExactly(int fd, const char* str) {
125 return WriteFdExactly(fd, str, strlen(str));
126}
127
128bool WriteFdExactly(int fd, const std::string& str) {
129 return WriteFdExactly(fd, str.c_str(), str.size());
130}
131
Elliott Hughesab52c182015-05-01 17:04:38 -0700132bool WriteFdFmt(int fd, const char* fmt, ...) {
133 std::string str;
134
135 va_list ap;
136 va_start(ap, fmt);
137 android::base::StringAppendV(&str, fmt, ap);
138 va_end(ap);
139
140 return WriteFdExactly(fd, str);
Dan Albertcc731cc2015-02-24 21:26:58 -0800141}
Spencer Low351ecd12015-10-14 17:32:44 -0700142
143bool ReadOrderlyShutdown(int fd) {
144 char buf[16];
145
146 // Only call this function if you're sure that the peer does
147 // orderly/graceful shutdown of the socket, closing the socket so that
148 // adb_read() will return 0. If the peer keeps the socket open, adb_read()
149 // will never return.
150 int result = adb_read(fd, buf, sizeof(buf));
151 if (result == -1) {
152 // If errno is EAGAIN, that means this function was called on a
153 // nonblocking socket and it would have blocked (which would be bad
154 // because we'd probably block the main thread where nonblocking IO is
155 // done). Don't do that. If you have a nonblocking socket, use the
156 // fdevent APIs to get called on FDE_READ, and then call this function
157 // if you really need to, but it shouldn't be needed for server sockets.
158 CHECK_NE(errno, EAGAIN);
159
160 // Note that on Windows, orderly shutdown sometimes causes
161 // recv() == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET. That
162 // can be ignored.
163 return false;
164 } else if (result == 0) {
165 // Peer has performed an orderly/graceful shutdown.
166 return true;
167 } else {
168 // Unexpectedly received data. This is essentially a protocol error
169 // because you should not call this function unless you expect no more
170 // data. We don't repeatedly call adb_read() until we get zero because
171 // we don't know how long that would take, but we do know that the
172 // caller wants to close the socket soon.
173 VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read "
174 << dump_hex(buf, result);
175 // Shutdown the socket to prevent the caller from reading or writing to
176 // it which doesn't make sense if we just read and discarded some data.
177 adb_shutdown(fd);
178 errno = EINVAL;
179 return false;
180 }
181}