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" |
| 20 | |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 21 | #include <libgen.h> |
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> |
| 28 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame^] | 30 | #include <android-base/parseint.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
| 32 | #include <android-base/strings.h> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 33 | |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 34 | #include "adb.h" |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 35 | #include "adb_trace.h" |
Elliott Hughes | df5bb43 | 2015-04-19 13:17:01 -0700 | [diff] [blame] | 36 | #include "sysdeps.h" |
| 37 | |
Josh Gao | f462e0b | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 38 | ADB_MUTEX_DEFINE(basename_lock); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 39 | ADB_MUTEX_DEFINE(dirname_lock); |
| 40 | |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 41 | #if defined(_WIN32) |
| 42 | constexpr char kNullFileName[] = "NUL"; |
| 43 | #else |
| 44 | constexpr char kNullFileName[] = "/dev/null"; |
| 45 | #endif |
| 46 | |
| 47 | void close_stdin() { |
| 48 | int fd = unix_open(kNullFileName, O_RDONLY); |
| 49 | if (fd == -1) { |
| 50 | fatal_errno("failed to open %s", kNullFileName); |
| 51 | } |
| 52 | |
| 53 | if (TEMP_FAILURE_RETRY(dup2(fd, STDIN_FILENO)) == -1) { |
| 54 | fatal_errno("failed to redirect stdin to %s", kNullFileName); |
| 55 | } |
| 56 | unix_close(fd); |
| 57 | } |
| 58 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 59 | bool getcwd(std::string* s) { |
| 60 | char* cwd = getcwd(nullptr, 0); |
| 61 | if (cwd != nullptr) *s = cwd; |
| 62 | free(cwd); |
| 63 | return (cwd != nullptr); |
| 64 | } |
| 65 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 66 | bool directory_exists(const std::string& path) { |
| 67 | struct stat sb; |
| 68 | return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); |
| 69 | } |
| 70 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 71 | std::string escape_arg(const std::string& s) { |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 72 | std::string result = s; |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 74 | // Escape any ' in the string (before we single-quote the whole thing). |
| 75 | // The correct way to do this for the shell is to replace ' with '\'' --- that is, |
| 76 | // close the existing single-quoted string, escape a single single-quote, and start |
| 77 | // a new single-quoted string. Like the C preprocessor, the shell will concatenate |
| 78 | // these pieces into one string. |
| 79 | for (size_t i = 0; i < s.size(); ++i) { |
| 80 | if (s[i] == '\'') { |
| 81 | result.insert(i, "'\\'"); |
| 82 | i += 2; |
| 83 | } |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 84 | } |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 85 | |
| 86 | // Prefix and suffix the whole string with '. |
| 87 | result.insert(result.begin(), '\''); |
| 88 | result.push_back('\''); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 89 | return result; |
| 90 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 91 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 92 | std::string adb_basename(const std::string& path) { |
Josh Gao | f462e0b | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 93 | // Copy path because basename may modify the string passed in. |
| 94 | std::string result(path); |
| 95 | |
| 96 | // Use lock because basename() may write to a process global and return a |
| 97 | // pointer to that. Note that this locking strategy only works if all other |
| 98 | // callers to dirname in the process also grab this same lock. |
| 99 | adb_mutex_lock(&basename_lock); |
| 100 | |
| 101 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 102 | // the copy to be made, so there is no chance of us accidentally writing to |
| 103 | // the storage for 'path'. |
| 104 | char* name = basename(&result[0]); |
| 105 | |
| 106 | // In case dirname returned a pointer to a process global, copy that string |
| 107 | // before leaving the lock. |
| 108 | result.assign(name); |
| 109 | |
| 110 | adb_mutex_unlock(&basename_lock); |
| 111 | |
| 112 | return result; |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 115 | std::string adb_dirname(const std::string& path) { |
| 116 | // Copy path because dirname may modify the string passed in. |
Josh Gao | f462e0b | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 117 | std::string result(path); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 118 | |
| 119 | // Use lock because dirname() may write to a process global and return a |
| 120 | // pointer to that. Note that this locking strategy only works if all other |
| 121 | // callers to dirname in the process also grab this same lock. |
| 122 | adb_mutex_lock(&dirname_lock); |
| 123 | |
| 124 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 125 | // the copy to be made, so there is no chance of us accidentally writing to |
| 126 | // the storage for 'path'. |
Josh Gao | f462e0b | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 127 | char* parent = dirname(&result[0]); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 128 | |
| 129 | // In case dirname returned a pointer to a process global, copy that string |
| 130 | // before leaving the lock. |
Josh Gao | f462e0b | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 131 | result.assign(parent); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 132 | |
| 133 | adb_mutex_unlock(&dirname_lock); |
| 134 | |
| 135 | return result; |
Alex Vallée | e916315 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 138 | // Given a relative or absolute filepath, create the directory hierarchy |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 139 | // as needed. Returns true if the hierarchy is/was setup. |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 140 | bool mkdirs(const std::string& path) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 141 | // TODO: all the callers do unlink && mkdirs && adb_creat --- |
| 142 | // that's probably the operation we should expose. |
| 143 | |
| 144 | // Implementation Notes: |
| 145 | // |
| 146 | // Pros: |
| 147 | // - Uses dirname, so does not need to deal with OS_PATH_SEPARATOR. |
| 148 | // - On Windows, uses mingw dirname which accepts '/' and '\\', drive letters |
| 149 | // (C:\foo), UNC paths (\\server\share\dir\dir\file), and Unicode (when |
| 150 | // combined with our adb_mkdir() which takes UTF-8). |
| 151 | // - Is optimistic wrt thinking that a deep directory hierarchy will exist. |
| 152 | // So it does as few stat()s as possible before doing mkdir()s. |
| 153 | // Cons: |
| 154 | // - Recursive, so it uses stack space relative to number of directory |
| 155 | // components. |
| 156 | |
Josh Gao | 49726bc | 2016-02-26 13:26:55 -0800 | [diff] [blame] | 157 | // If path points to a symlink to a directory, that's fine. |
| 158 | struct stat sb; |
| 159 | if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 163 | const std::string parent(adb_dirname(path)); |
| 164 | |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 165 | // If dirname returned the same path as what we passed in, don't go recursive. |
| 166 | // This can happen on Windows when walking up the directory hierarchy and not |
| 167 | // finding anything that already exists (unlike POSIX that will eventually |
| 168 | // find . or /). |
| 169 | if (parent == path) { |
| 170 | errno = ENOENT; |
| 171 | return false; |
| 172 | } |
| 173 | |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 174 | // Recursively make parent directories of 'path'. |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 175 | if (!mkdirs(parent)) { |
| 176 | return false; |
| 177 | } |
| 178 | |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 179 | // Now that the parent directory hierarchy of 'path' has been ensured, |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 180 | // create path itself. |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 181 | if (adb_mkdir(path, 0775) == -1) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 182 | const int saved_errno = errno; |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 183 | // If someone else created the directory, that is ok. |
| 184 | if (directory_exists(path)) { |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 185 | return true; |
| 186 | } |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 187 | // 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] | 188 | errno = saved_errno; |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | return true; |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 195 | std::string dump_hex(const void* data, size_t byte_count) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 196 | byte_count = std::min(byte_count, size_t(16)); |
| 197 | |
| 198 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 199 | |
| 200 | std::string line; |
| 201 | for (size_t i = 0; i < byte_count; ++i) { |
| 202 | android::base::StringAppendF(&line, "%02x", p[i]); |
| 203 | } |
| 204 | line.push_back(' '); |
| 205 | |
| 206 | for (size_t i = 0; i < byte_count; ++i) { |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 207 | int ch = p[i]; |
| 208 | line.push_back(isprint(ch) ? ch : '.'); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 211 | return line; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 212 | } |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 213 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 214 | std::string perror_str(const char* msg) { |
| 215 | return android::base::StringPrintf("%s: %s", msg, strerror(errno)); |
| 216 | } |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 217 | |
| 218 | #if !defined(_WIN32) |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 219 | // Windows version provided in sysdeps_win32.cpp |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 220 | bool set_file_block_mode(int fd, bool block) { |
| 221 | int flags = fcntl(fd, F_GETFL, 0); |
| 222 | if (flags == -1) { |
| 223 | PLOG(ERROR) << "failed to fcntl(F_GETFL) for fd " << fd; |
| 224 | return false; |
| 225 | } |
| 226 | flags = block ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); |
| 227 | if (fcntl(fd, F_SETFL, flags) != 0) { |
| 228 | PLOG(ERROR) << "failed to fcntl(F_SETFL) for fd " << fd << ", flags " << flags; |
| 229 | return false; |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | #endif |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame^] | 234 | |
| 235 | bool forward_targets_are_valid(const std::string& source, const std::string& dest, |
| 236 | std::string* error) { |
| 237 | if (android::base::StartsWith(source, "tcp:")) { |
| 238 | // The source port may be 0 to allow the system to select an open port. |
| 239 | int port; |
| 240 | if (!android::base::ParseInt(&source[4], &port) || port < 0) { |
| 241 | *error = android::base::StringPrintf("Invalid source port: '%s'", &source[4]); |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (android::base::StartsWith(dest, "tcp:")) { |
| 247 | // The destination port must be > 0. |
| 248 | int port; |
| 249 | if (!android::base::ParseInt(&dest[4], &port) || port <= 0) { |
| 250 | *error = android::base::StringPrintf("Invalid destination port: '%s'", &dest[4]); |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return true; |
| 256 | } |