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 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_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 | |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 23 | #include <base/stringprintf.h> |
| 24 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 25 | #include "adb_trace.h" |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 26 | #include "adb_utils.h" |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 27 | #include "sysdeps.h" |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 28 | |
| 29 | bool SendProtocolString(int fd, const std::string& s) { |
| 30 | int length = s.size(); |
| 31 | if (length > 0xffff) { |
| 32 | length = 0xffff; |
| 33 | } |
| 34 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 35 | // 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 | |
| 40 | bool 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 Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool SendOkay(int fd) { |
| 59 | return WriteFdExactly(fd, "OKAY", 4); |
| 60 | } |
| 61 | |
| 62 | bool SendFail(int fd, const std::string& reason) { |
| 63 | return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason); |
| 64 | } |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 65 | |
| 66 | bool ReadFdExactly(int fd, void* buf, size_t len) { |
| 67 | char* p = reinterpret_cast<char*>(buf); |
| 68 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 69 | size_t len0 = len; |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 70 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame^] | 71 | D("readx: fd=%d wanted=%zu", fd, len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 72 | while (len > 0) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 73 | int r = adb_read(fd, p, len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 74 | if (r > 0) { |
| 75 | len -= r; |
| 76 | p += r; |
| 77 | } else if (r == -1) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame^] | 78 | D("readx: fd=%d error %d: %s", fd, errno, strerror(errno)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 79 | return false; |
| 80 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame^] | 81 | D("readx: fd=%d disconnected", fd); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 82 | errno = 0; |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame^] | 87 | D("readx: fd=%d wanted=%zu got=%zu", fd, len0, len0 - len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 88 | if (ADB_TRACING) { |
| 89 | dump_hex(reinterpret_cast<const unsigned char*>(buf), len0); |
| 90 | } |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool WriteFdExactly(int fd, const void* buf, size_t len) { |
| 96 | const char* p = reinterpret_cast<const char*>(buf); |
| 97 | int r; |
| 98 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 99 | D("writex: fd=%d len=%d: ", fd, (int)len); |
| 100 | if (ADB_TRACING) { |
| 101 | dump_hex(reinterpret_cast<const unsigned char*>(buf), len); |
| 102 | } |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 103 | |
| 104 | while (len > 0) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 105 | r = adb_write(fd, p, len); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 106 | if (r == -1) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame^] | 107 | D("writex: fd=%d error %d: %s", fd, errno, strerror(errno)); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 108 | if (errno == EAGAIN) { |
| 109 | adb_sleep_ms(1); // just yield some cpu time |
| 110 | continue; |
| 111 | } else if (errno == EPIPE) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame^] | 112 | D("writex: fd=%d disconnected", fd); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 113 | errno = 0; |
| 114 | return false; |
Dan Albert | 08f66bc | 2015-03-19 18:09:59 -0700 | [diff] [blame] | 115 | } else { |
| 116 | return false; |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 117 | } |
| 118 | } else { |
| 119 | len -= r; |
| 120 | p += r; |
| 121 | } |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 126 | bool WriteFdExactly(int fd, const char* str) { |
| 127 | return WriteFdExactly(fd, str, strlen(str)); |
| 128 | } |
| 129 | |
| 130 | bool WriteFdExactly(int fd, const std::string& str) { |
| 131 | return WriteFdExactly(fd, str.c_str(), str.size()); |
| 132 | } |
| 133 | |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 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); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 143 | } |