Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 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 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG RWX |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 18 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 19 | #include "adb_io.h" |
| 20 | |
| 21 | #include <unistd.h> |
| 22 | |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame^] | 23 | #if !ADB_HOST |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/un.h> |
| 26 | #endif |
| 27 | |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 28 | #include <thread> |
| 29 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 31 | |
Josh Gao | 7e6683c | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 32 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 33 | #include "adb_trace.h" |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 34 | #include "adb_utils.h" |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 35 | #include "sysdeps.h" |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 36 | |
| 37 | bool SendProtocolString(int fd, const std::string& s) { |
Josh Gao | 7e6683c | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 38 | unsigned int length = s.size(); |
Josh Gao | a019f78 | 2017-06-16 15:34:34 -0700 | [diff] [blame] | 39 | if (length > MAX_PAYLOAD - 4) { |
Josh Gao | 7e6683c | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 40 | errno = EMSGSIZE; |
| 41 | return false; |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 44 | // The cost of sending two strings outweighs the cost of formatting. |
| 45 | // "adb sync" performance is affected by this. |
Alex Buynytskyy | a9e7948 | 2018-12-20 10:09:43 -0800 | [diff] [blame] | 46 | auto str = android::base::StringPrintf("%04x", length).append(s); |
| 47 | return WriteFdExactly(fd, str); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool ReadProtocolString(int fd, std::string* s, std::string* error) { |
| 51 | char buf[5]; |
| 52 | if (!ReadFdExactly(fd, buf, 4)) { |
| 53 | *error = perror_str("protocol fault (couldn't read status length)"); |
| 54 | return false; |
| 55 | } |
| 56 | buf[4] = 0; |
| 57 | |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 58 | unsigned long len = strtoul(buf, nullptr, 16); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 59 | s->resize(len, '\0'); |
| 60 | if (!ReadFdExactly(fd, &(*s)[0], len)) { |
| 61 | *error = perror_str("protocol fault (couldn't read status message)"); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return true; |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool SendOkay(int fd) { |
| 69 | return WriteFdExactly(fd, "OKAY", 4); |
| 70 | } |
| 71 | |
| 72 | bool SendFail(int fd, const std::string& reason) { |
| 73 | return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason); |
| 74 | } |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 75 | |
| 76 | bool ReadFdExactly(int fd, void* buf, size_t len) { |
| 77 | char* p = reinterpret_cast<char*>(buf); |
| 78 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 79 | size_t len0 = len; |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 80 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 81 | D("readx: fd=%d wanted=%zu", fd, len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 82 | while (len > 0) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 83 | int r = adb_read(fd, p, len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 84 | if (r > 0) { |
| 85 | len -= r; |
| 86 | p += r; |
| 87 | } else if (r == -1) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 88 | D("readx: fd=%d error %d: %s", fd, errno, strerror(errno)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 89 | return false; |
| 90 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 91 | D("readx: fd=%d disconnected", fd); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 92 | errno = 0; |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 97 | VLOG(RWX) << "readx: fd=" << fd << " wanted=" << len0 << " got=" << (len0 - len) |
| 98 | << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len0); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool WriteFdExactly(int fd, const void* buf, size_t len) { |
| 104 | const char* p = reinterpret_cast<const char*>(buf); |
| 105 | int r; |
| 106 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 107 | VLOG(RWX) << "writex: fd=" << fd << " len=" << len |
| 108 | << " " << dump_hex(reinterpret_cast<const unsigned char*>(buf), len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 109 | |
| 110 | while (len > 0) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 111 | r = adb_write(fd, p, len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 112 | if (r == -1) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 113 | D("writex: fd=%d error %d: %s", fd, errno, strerror(errno)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 114 | if (errno == EAGAIN) { |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 115 | std::this_thread::yield(); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 116 | continue; |
| 117 | } else if (errno == EPIPE) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 118 | D("writex: fd=%d disconnected", fd); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 119 | errno = 0; |
| 120 | return false; |
Dan Albert | 08f66bc | 2015-03-19 18:09:59 -0700 | [diff] [blame] | 121 | } else { |
| 122 | return false; |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 123 | } |
| 124 | } else { |
| 125 | len -= r; |
| 126 | p += r; |
| 127 | } |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 132 | bool WriteFdExactly(int fd, const char* str) { |
| 133 | return WriteFdExactly(fd, str, strlen(str)); |
| 134 | } |
| 135 | |
| 136 | bool WriteFdExactly(int fd, const std::string& str) { |
| 137 | return WriteFdExactly(fd, str.c_str(), str.size()); |
| 138 | } |
| 139 | |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 140 | bool WriteFdFmt(int fd, const char* fmt, ...) { |
| 141 | std::string str; |
| 142 | |
| 143 | va_list ap; |
| 144 | va_start(ap, fmt); |
| 145 | android::base::StringAppendV(&str, fmt, ap); |
| 146 | va_end(ap); |
| 147 | |
| 148 | return WriteFdExactly(fd, str); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 149 | } |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 150 | |
| 151 | bool ReadOrderlyShutdown(int fd) { |
| 152 | char buf[16]; |
| 153 | |
| 154 | // Only call this function if you're sure that the peer does |
| 155 | // orderly/graceful shutdown of the socket, closing the socket so that |
| 156 | // adb_read() will return 0. If the peer keeps the socket open, adb_read() |
| 157 | // will never return. |
| 158 | int result = adb_read(fd, buf, sizeof(buf)); |
| 159 | if (result == -1) { |
| 160 | // If errno is EAGAIN, that means this function was called on a |
| 161 | // nonblocking socket and it would have blocked (which would be bad |
| 162 | // because we'd probably block the main thread where nonblocking IO is |
| 163 | // done). Don't do that. If you have a nonblocking socket, use the |
| 164 | // fdevent APIs to get called on FDE_READ, and then call this function |
| 165 | // if you really need to, but it shouldn't be needed for server sockets. |
| 166 | CHECK_NE(errno, EAGAIN); |
| 167 | |
| 168 | // Note that on Windows, orderly shutdown sometimes causes |
| 169 | // recv() == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET. That |
| 170 | // can be ignored. |
| 171 | return false; |
| 172 | } else if (result == 0) { |
| 173 | // Peer has performed an orderly/graceful shutdown. |
| 174 | return true; |
| 175 | } else { |
| 176 | // Unexpectedly received data. This is essentially a protocol error |
| 177 | // because you should not call this function unless you expect no more |
| 178 | // data. We don't repeatedly call adb_read() until we get zero because |
| 179 | // we don't know how long that would take, but we do know that the |
| 180 | // caller wants to close the socket soon. |
| 181 | VLOG(RWX) << "ReadOrderlyShutdown(" << fd << ") unexpectedly read " |
| 182 | << dump_hex(buf, result); |
| 183 | // Shutdown the socket to prevent the caller from reading or writing to |
| 184 | // it which doesn't make sense if we just read and discarded some data. |
| 185 | adb_shutdown(fd); |
| 186 | errno = EINVAL; |
| 187 | return false; |
| 188 | } |
| 189 | } |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame^] | 190 | |
| 191 | #if defined(__linux__) |
| 192 | bool SendFileDescriptor(int socket_fd, int fd) { |
| 193 | struct msghdr msg; |
| 194 | struct iovec iov; |
| 195 | char dummy = '!'; |
| 196 | union { |
| 197 | cmsghdr cm; |
| 198 | char buffer[CMSG_SPACE(sizeof(int))]; |
| 199 | } cm_un; |
| 200 | |
| 201 | iov.iov_base = &dummy; |
| 202 | iov.iov_len = 1; |
| 203 | msg.msg_name = nullptr; |
| 204 | msg.msg_namelen = 0; |
| 205 | msg.msg_iov = &iov; |
| 206 | msg.msg_iovlen = 1; |
| 207 | msg.msg_flags = 0; |
| 208 | msg.msg_control = cm_un.buffer; |
| 209 | msg.msg_controllen = sizeof(cm_un.buffer); |
| 210 | |
| 211 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 212 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 213 | cmsg->cmsg_level = SOL_SOCKET; |
| 214 | cmsg->cmsg_type = SCM_RIGHTS; |
| 215 | ((int*)CMSG_DATA(cmsg))[0] = fd; |
| 216 | |
| 217 | int ret = TEMP_FAILURE_RETRY(sendmsg(socket_fd, &msg, 0)); |
| 218 | if (ret < 0) { |
| 219 | D("sending file descriptor via socket %d failed: %s", socket_fd, strerror(errno)); |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | D("sent file descriptor %d to via socket %d", fd, socket_fd); |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | bool ReceiveFileDescriptor(int socket_fd, unique_fd* fd, std::string* error) { |
| 228 | char dummy = '!'; |
| 229 | union { |
| 230 | cmsghdr cm; |
| 231 | char buffer[CMSG_SPACE(sizeof(int))]; |
| 232 | } cm_un; |
| 233 | |
| 234 | iovec iov; |
| 235 | iov.iov_base = &dummy; |
| 236 | iov.iov_len = 1; |
| 237 | |
| 238 | msghdr msg; |
| 239 | msg.msg_name = nullptr; |
| 240 | msg.msg_namelen = 0; |
| 241 | msg.msg_iov = &iov; |
| 242 | msg.msg_iovlen = 1; |
| 243 | msg.msg_flags = 0; |
| 244 | msg.msg_control = cm_un.buffer; |
| 245 | msg.msg_controllen = sizeof(cm_un.buffer); |
| 246 | |
| 247 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 248 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 249 | cmsg->cmsg_level = SOL_SOCKET; |
| 250 | cmsg->cmsg_type = SCM_RIGHTS; |
| 251 | ((int*)(CMSG_DATA(cmsg)))[0] = -1; |
| 252 | |
| 253 | int rc = TEMP_FAILURE_RETRY(recvmsg(socket_fd, &msg, 0)); |
| 254 | if (rc <= 0) { |
| 255 | *error = perror_str("receiving file descriptor via socket failed"); |
| 256 | D("receiving file descriptor via socket %d failed: %s", socket_fd, strerror(errno)); |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | fd->reset(((int*)(CMSG_DATA(cmsg)))[0]); |
| 261 | D("received file descriptor %d to via socket %d", fd->get(), socket_fd); |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | #endif |