1 /*
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
17 #define TRACE_TAG RWX
18
19 #include "adb_io.h"
20
21 #include <unistd.h>
22
23 #include <thread>
24
25 #include <android-base/stringprintf.h>
26
27 #include "adb.h"
28 #include "adb_trace.h"
29 #include "adb_utils.h"
30 #include "sysdeps.h"
31
SendProtocolString(int fd,const std::string & s)32 bool SendProtocolString(int fd, const std::string& s) {
33 unsigned int length = s.size();
34 if (length > MAX_PAYLOAD_V1 - 4) {
35 errno = EMSGSIZE;
36 return false;
37 }
38
39 // The cost of sending two strings outweighs the cost of formatting.
40 // "adb sync" performance is affected by this.
41 return WriteFdFmt(fd, "%04x%.*s", length, length, s.c_str());
42 }
43
ReadProtocolString(int fd,std::string * s,std::string * error)44 bool ReadProtocolString(int fd, std::string* s, std::string* error) {
45 char buf[5];
46 if (!ReadFdExactly(fd, buf, 4)) {
47 *error = perror_str("protocol fault (couldn't read status length)");
48 return false;
49 }
50 buf[4] = 0;
51
52 unsigned long len = strtoul(buf, 0, 16);
53 s->resize(len, '\0');
54 if (!ReadFdExactly(fd, &(*s)[0], len)) {
55 *error = perror_str("protocol fault (couldn't read status message)");
56 return false;
57 }
58
59 return true;
60 }
61
SendOkay(int fd)62 bool SendOkay(int fd) {
63 return WriteFdExactly(fd, "OKAY", 4);
64 }
65
SendFail(int fd,const std::string & reason)66 bool SendFail(int fd, const std::string& reason) {
67 return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
68 }
69
ReadFdExactly(int fd,void * buf,size_t len)70 bool ReadFdExactly(int fd, void* buf, size_t len) {
71 char* p = reinterpret_cast<char*>(buf);
72
73 size_t len0 = len;
74
75 D("readx: fd=%d wanted=%zu", fd, len);
76 while (len > 0) {
77 int r = adb_read(fd, p, len);
78 if (r > 0) {
79 len -= r;
80 p += r;
81 } else if (r == -1) {
82 D("readx: fd=%d error %d: %s", fd, errno, strerror(errno));
83 return false;
84 } else {
85 D("readx: fd=%d disconnected", fd);
86 errno = 0;
87 return false;
88 }
89 }
90
91 VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len)
92 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
93
94 return true;
95 }
96
WriteFdExactly(int fd,const void * buf,size_t len)97 bool WriteFdExactly(int fd, const void* buf, size_t len) {
98 const char* p = reinterpret_cast<const char*>(buf);
99 int r;
100
101 VLOG(RWX) << "writex: fd=" << fd << " len=" << len
102 << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
103
104 while (len > 0) {
105 r = adb_write(fd, p, len);
106 if (r == -1) {
107 D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
108 if (errno == EAGAIN) {
109 std::this_thread::yield();
110 continue;
111 } else if (errno == EPIPE) {
112 D("writex: fd=%d disconnected", fd);
113 errno = 0;
114 return false;
115 } else {
116 return false;
117 }
118 } else {
119 len -= r;
120 p += r;
121 }
122 }
123 return true;
124 }
125
WriteFdExactly(int fd,const char * str)126 bool WriteFdExactly(int fd, const char* str) {
127 return WriteFdExactly(fd, str, strlen(str));
128 }
129
WriteFdExactly(int fd,const std::string & str)130 bool WriteFdExactly(int fd, const std::string& str) {
131 return WriteFdExactly(fd, str.c_str(), str.size());
132 }
133
WriteFdFmt(int fd,const char * fmt,...)134 bool WriteFdFmt(int fd, const char* fmt, ...) {
135 std::string str;
136
137 va_list ap;
138 va_start(ap, fmt);
139 android::base::StringAppendV(&str, fmt, ap);
140 va_end(ap);
141
142 return WriteFdExactly(fd, str);
143 }
144
ReadOrderlyShutdown(int fd)145 bool ReadOrderlyShutdown(int fd) {
146 char buf[16];
147
148 // Only call this function if you're sure that the peer does
149 // orderly/graceful shutdown of the socket, closing the socket so that
150 // adb_read() will return 0. If the peer keeps the socket open, adb_read()
151 // will never return.
152 int result = adb_read(fd, buf, sizeof(buf));
153 if (result == -1) {
154 // If errno is EAGAIN, that means this function was called on a
155 // nonblocking socket and it would have blocked (which would be bad
156 // because we'd probably block the main thread where nonblocking IO is
157 // done). Don't do that. If you have a nonblocking socket, use the
158 // fdevent APIs to get called on FDE_READ, and then call this function
159 // if you really need to, but it shouldn't be needed for server sockets.
160 CHECK_NE(errno, EAGAIN);
161
162 // Note that on Windows, orderly shutdown sometimes causes
163 // recv() == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET. That
164 // can be ignored.
165 return false;
166 } else if (result == 0) {
167 // Peer has performed an orderly/graceful shutdown.
168 return true;
169 } else {
170 // Unexpectedly received data. This is essentially a protocol error
171 // because you should not call this function unless you expect no more
172 // data. We don't repeatedly call adb_read() until we get zero because
173 // we don't know how long that would take, but we do know that the
174 // caller wants to close the socket soon.
175 VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read "
176 << dump_hex(buf, result);
177 // Shutdown the socket to prevent the caller from reading or writing to
178 // it which doesn't make sense if we just read and discarded some data.
179 adb_shutdown(fd);
180 errno = EINVAL;
181 return false;
182 }
183 }
184