Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 19 | #include "adb_utils.h" |
Josh Gao | ea7457b | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 20 | #include "adb_unique_fd.h" |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 21 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 28 | #include <vector> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 29 | |
Colin Cross | e679407 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 31 | #include <android-base/logging.h> |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 32 | #include <android-base/parseint.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| 34 | #include <android-base/strings.h> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 35 | |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 36 | #include "adb.h" |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 37 | #include "adb_trace.h" |
Elliott Hughes | df5bb43 | 2015-04-19 13:17:01 -0700 | [diff] [blame] | 38 | #include "sysdeps.h" |
| 39 | |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 40 | #ifdef _WIN32 |
| 41 | # ifndef WIN32_LEAN_AND_MEAN |
| 42 | # define WIN32_LEAN_AND_MEAN |
| 43 | # endif |
| 44 | # include "windows.h" |
| 45 | # include "shlobj.h" |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 46 | #else |
| 47 | #include <pwd.h> |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 48 | #endif |
| 49 | |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 50 | |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 51 | #if defined(_WIN32) |
| 52 | constexpr char kNullFileName[] = "NUL"; |
| 53 | #else |
| 54 | constexpr char kNullFileName[] = "/dev/null"; |
| 55 | #endif |
| 56 | |
| 57 | void close_stdin() { |
| 58 | int fd = unix_open(kNullFileName, O_RDONLY); |
| 59 | if (fd == -1) { |
| 60 | fatal_errno("failed to open %s", kNullFileName); |
| 61 | } |
| 62 | |
| 63 | if (TEMP_FAILURE_RETRY(dup2(fd, STDIN_FILENO)) == -1) { |
| 64 | fatal_errno("failed to redirect stdin to %s", kNullFileName); |
| 65 | } |
| 66 | unix_close(fd); |
| 67 | } |
| 68 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 69 | bool getcwd(std::string* s) { |
| 70 | char* cwd = getcwd(nullptr, 0); |
| 71 | if (cwd != nullptr) *s = cwd; |
| 72 | free(cwd); |
| 73 | return (cwd != nullptr); |
| 74 | } |
| 75 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 76 | bool directory_exists(const std::string& path) { |
| 77 | struct stat sb; |
Josh Gao | a0e185b | 2017-03-23 16:05:12 -0700 | [diff] [blame] | 78 | return stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 81 | std::string escape_arg(const std::string& s) { |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 82 | std::string result = s; |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 83 | |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 84 | // Escape any ' in the string (before we single-quote the whole thing). |
| 85 | // The correct way to do this for the shell is to replace ' with '\'' --- that is, |
| 86 | // close the existing single-quoted string, escape a single single-quote, and start |
| 87 | // a new single-quoted string. Like the C preprocessor, the shell will concatenate |
| 88 | // these pieces into one string. |
| 89 | for (size_t i = 0; i < s.size(); ++i) { |
| 90 | if (s[i] == '\'') { |
| 91 | result.insert(i, "'\\'"); |
| 92 | i += 2; |
| 93 | } |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 94 | } |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 95 | |
| 96 | // Prefix and suffix the whole string with '. |
| 97 | result.insert(result.begin(), '\''); |
| 98 | result.push_back('\''); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 99 | return result; |
| 100 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 101 | |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 102 | // Given a relative or absolute filepath, create the directory hierarchy |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 103 | // as needed. Returns true if the hierarchy is/was setup. |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 104 | bool mkdirs(const std::string& path) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 105 | // TODO: all the callers do unlink && mkdirs && adb_creat --- |
| 106 | // that's probably the operation we should expose. |
| 107 | |
| 108 | // Implementation Notes: |
| 109 | // |
| 110 | // Pros: |
| 111 | // - Uses dirname, so does not need to deal with OS_PATH_SEPARATOR. |
| 112 | // - On Windows, uses mingw dirname which accepts '/' and '\\', drive letters |
| 113 | // (C:\foo), UNC paths (\\server\share\dir\dir\file), and Unicode (when |
| 114 | // combined with our adb_mkdir() which takes UTF-8). |
| 115 | // - Is optimistic wrt thinking that a deep directory hierarchy will exist. |
| 116 | // So it does as few stat()s as possible before doing mkdir()s. |
| 117 | // Cons: |
| 118 | // - Recursive, so it uses stack space relative to number of directory |
| 119 | // components. |
| 120 | |
Josh Gao | 49726bc | 2016-02-26 13:26:55 -0800 | [diff] [blame] | 121 | // If path points to a symlink to a directory, that's fine. |
| 122 | struct stat sb; |
| 123 | if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
Colin Cross | e679407 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 127 | const std::string parent(android::base::Dirname(path)); |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 128 | |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 129 | // If dirname returned the same path as what we passed in, don't go recursive. |
| 130 | // This can happen on Windows when walking up the directory hierarchy and not |
| 131 | // finding anything that already exists (unlike POSIX that will eventually |
| 132 | // find . or /). |
| 133 | if (parent == path) { |
| 134 | errno = ENOENT; |
| 135 | return false; |
| 136 | } |
| 137 | |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 138 | // Recursively make parent directories of 'path'. |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 139 | if (!mkdirs(parent)) { |
| 140 | return false; |
| 141 | } |
| 142 | |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 143 | // Now that the parent directory hierarchy of 'path' has been ensured, |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 144 | // create path itself. |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 145 | if (adb_mkdir(path, 0775) == -1) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 146 | const int saved_errno = errno; |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 147 | // If someone else created the directory, that is ok. |
| 148 | if (directory_exists(path)) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 149 | return true; |
| 150 | } |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 151 | // There might be a pre-existing file at 'path', or there might have been some other error. |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 152 | errno = saved_errno; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 159 | std::string dump_hex(const void* data, size_t byte_count) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 160 | byte_count = std::min(byte_count, size_t(16)); |
| 161 | |
| 162 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 163 | |
| 164 | std::string line; |
| 165 | for (size_t i = 0; i < byte_count; ++i) { |
| 166 | android::base::StringAppendF(&line, "%02x", p[i]); |
| 167 | } |
| 168 | line.push_back(' '); |
| 169 | |
| 170 | for (size_t i = 0; i < byte_count; ++i) { |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 171 | int ch = p[i]; |
| 172 | line.push_back(isprint(ch) ? ch : '.'); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 175 | return line; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 176 | } |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 177 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 178 | std::string perror_str(const char* msg) { |
| 179 | return android::base::StringPrintf("%s: %s", msg, strerror(errno)); |
| 180 | } |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 181 | |
| 182 | #if !defined(_WIN32) |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 183 | // Windows version provided in sysdeps_win32.cpp |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 184 | bool set_file_block_mode(int fd, bool block) { |
| 185 | int flags = fcntl(fd, F_GETFL, 0); |
| 186 | if (flags == -1) { |
| 187 | PLOG(ERROR) << "failed to fcntl(F_GETFL) for fd " << fd; |
| 188 | return false; |
| 189 | } |
| 190 | flags = block ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); |
| 191 | if (fcntl(fd, F_SETFL, flags) != 0) { |
| 192 | PLOG(ERROR) << "failed to fcntl(F_SETFL) for fd " << fd << ", flags " << flags; |
| 193 | return false; |
| 194 | } |
| 195 | return true; |
| 196 | } |
| 197 | #endif |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 198 | |
| 199 | bool forward_targets_are_valid(const std::string& source, const std::string& dest, |
| 200 | std::string* error) { |
| 201 | if (android::base::StartsWith(source, "tcp:")) { |
| 202 | // The source port may be 0 to allow the system to select an open port. |
| 203 | int port; |
| 204 | if (!android::base::ParseInt(&source[4], &port) || port < 0) { |
| 205 | *error = android::base::StringPrintf("Invalid source port: '%s'", &source[4]); |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (android::base::StartsWith(dest, "tcp:")) { |
| 211 | // The destination port must be > 0. |
| 212 | int port; |
| 213 | if (!android::base::ParseInt(&dest[4], &port) || port <= 0) { |
| 214 | *error = android::base::StringPrintf("Invalid destination port: '%s'", &dest[4]); |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return true; |
| 220 | } |
Yurii Zubrytskyi | cc3a895 | 2016-05-27 11:07:40 -0700 | [diff] [blame] | 221 | |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 222 | std::string adb_get_homedir_path() { |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 223 | #ifdef _WIN32 |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 224 | WCHAR path[MAX_PATH]; |
| 225 | const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path); |
| 226 | if (FAILED(hr)) { |
| 227 | D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str()); |
| 228 | return {}; |
| 229 | } |
| 230 | std::string home_str; |
| 231 | if (!android::base::WideToUTF8(path, &home_str)) { |
| 232 | return {}; |
| 233 | } |
| 234 | return home_str; |
| 235 | #else |
| 236 | if (const char* const home = getenv("HOME")) { |
| 237 | return home; |
| 238 | } |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 239 | |
| 240 | struct passwd pwent; |
| 241 | struct passwd* result; |
| 242 | int pwent_max = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 243 | std::vector<char> buf(pwent_max); |
| 244 | int rc = getpwuid_r(getuid(), &pwent, buf.data(), buf.size(), &result); |
| 245 | if (rc == 0 && result) { |
| 246 | return result->pw_dir; |
| 247 | } |
| 248 | |
| 249 | LOG(FATAL) << "failed to get user home directory"; |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 250 | return {}; |
| 251 | #endif |
| 252 | } |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 253 | |
| 254 | std::string adb_get_android_dir_path() { |
| 255 | std::string user_dir = adb_get_homedir_path(); |
| 256 | std::string android_dir = user_dir + OS_PATH_SEPARATOR + ".android"; |
| 257 | struct stat buf; |
| 258 | if (stat(android_dir.c_str(), &buf) == -1) { |
| 259 | if (adb_mkdir(android_dir.c_str(), 0750) == -1) { |
| 260 | PLOG(FATAL) << "Cannot mkdir '" << android_dir << "'"; |
| 261 | } |
| 262 | } |
| 263 | return android_dir; |
| 264 | } |
Josh Gao | ea7457b | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 265 | |
| 266 | void AdbCloser::Close(int fd) { |
| 267 | adb_close(fd); |
| 268 | } |
Elliott Hughes | 97e74bb | 2017-02-06 16:20:30 -0800 | [diff] [blame] | 269 | |
| 270 | int usage(const char* fmt, ...) { |
| 271 | fprintf(stderr, "adb: "); |
| 272 | |
| 273 | va_list ap; |
| 274 | va_start(ap, fmt); |
| 275 | vfprintf(stderr, fmt, ap); |
| 276 | va_end(ap); |
| 277 | |
| 278 | fprintf(stderr, "\n"); |
| 279 | return 1; |
| 280 | } |