Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef ANDROID_APEXD_APEXD_UTILS_H_ |
| 18 | #define ANDROID_APEXD_APEXD_UTILS_H_ |
| 19 | |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 20 | #include <chrono> |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 21 | #include <filesystem> |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 22 | #include <string> |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 23 | #include <thread> |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Martijn Coenen | 610909b | 2019-01-18 13:49:38 +0100 | [diff] [blame] | 26 | #include <dirent.h> |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 31 | #include <android-base/chrono_utils.h> |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 33 | #include <android-base/result.h> |
Oli Lan | 013148f | 2020-01-08 13:36:03 +0000 | [diff] [blame] | 34 | #include <android-base/scopeguard.h> |
Nikita Ioffe | 264c421 | 2019-09-13 16:30:17 +0100 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Nikita Ioffe | 31dc34d | 2019-02-23 15:59:04 +0000 | [diff] [blame] | 36 | #include <cutils/android_reboot.h> |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 37 | |
Oli Lan | 042fbcf | 2020-01-17 11:14:16 +0000 | [diff] [blame] | 38 | #include "apex_constants.h" |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 39 | #include "string_log.h" |
| 40 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 41 | using android::base::ErrnoError; |
| 42 | using android::base::Error; |
| 43 | using android::base::Result; |
| 44 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace apex { |
| 47 | |
| 48 | inline int WaitChild(pid_t pid) { |
| 49 | int status; |
| 50 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 51 | |
| 52 | if (got_pid != pid) { |
| 53 | PLOG(WARNING) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 58 | return 0; |
| 59 | } else { |
| 60 | return status; |
| 61 | } |
| 62 | } |
| 63 | |
Nikita Ioffe | 264c421 | 2019-09-13 16:30:17 +0100 | [diff] [blame] | 64 | // TODO(ioffe): change to Result<void>? |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 65 | inline int ForkAndRun(const std::vector<std::string>& args, |
| 66 | std::string* error_msg) { |
Nikita Ioffe | 264c421 | 2019-09-13 16:30:17 +0100 | [diff] [blame] | 67 | LOG(DEBUG) << "Forking : " << android::base::Join(args, " "); |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 68 | std::vector<const char*> argv; |
| 69 | argv.resize(args.size() + 1, nullptr); |
| 70 | std::transform(args.begin(), args.end(), argv.begin(), |
| 71 | [](const std::string& in) { return in.c_str(); }); |
| 72 | |
| 73 | // 3) Fork. |
| 74 | pid_t pid = fork(); |
| 75 | if (pid == -1) { |
| 76 | // Fork failed. |
| 77 | *error_msg = PStringLog() << "Unable to fork"; |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if (pid == 0) { |
| 82 | execv(argv[0], const_cast<char**>(argv.data())); |
| 83 | PLOG(ERROR) << "execv failed"; |
| 84 | _exit(1); |
| 85 | } |
| 86 | |
| 87 | int rc = WaitChild(pid); |
| 88 | if (rc != 0) { |
| 89 | *error_msg = StringLog() << "Failed run: status=" << rc; |
| 90 | } |
| 91 | return rc; |
| 92 | } |
| 93 | |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 94 | template <typename Fn> |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 95 | Result<void> WalkDir(const std::string& path, Fn fn) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 96 | namespace fs = std::filesystem; |
Jooyung Han | 50011d5 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 97 | std::error_code ec; |
Jooyung Han | 50011d5 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 98 | auto it = fs::directory_iterator(path, ec); |
| 99 | auto end = fs::directory_iterator(); |
| 100 | while (!ec && it != end) { |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 101 | fn(*it); |
Jooyung Han | 50011d5 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 102 | it.increment(ec); |
| 103 | } |
Martijn Coenen | 320baff | 2019-05-01 09:20:42 +0200 | [diff] [blame] | 104 | if (ec) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 105 | return Error() << "Can't open " << path |
| 106 | << " for reading : " << ec.message(); |
Martijn Coenen | 320baff | 2019-05-01 09:20:42 +0200 | [diff] [blame] | 107 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 108 | return {}; |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | template <typename FilterFn> |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 112 | Result<std::vector<std::string>> ReadDir(const std::string& path, FilterFn fn) { |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 113 | namespace fs = std::filesystem; |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 114 | |
| 115 | std::vector<std::string> ret; |
| 116 | auto status = WalkDir(path, [&](const fs::directory_entry& entry) { |
| 117 | if (fn(entry)) { |
| 118 | ret.push_back(entry.path()); |
| 119 | } |
| 120 | }); |
Bernie Innocenti | d04d5d0 | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 121 | if (!status.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 122 | return status.error(); |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 123 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 124 | return ret; |
Jooyung Han | 80d8c86 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 125 | } |
Martijn Coenen | 610909b | 2019-01-18 13:49:38 +0100 | [diff] [blame] | 126 | |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 127 | inline bool IsEmptyDirectory(const std::string& path) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 128 | auto res = ReadDir(path, [](auto _) { return true; }); |
Bernie Innocenti | d04d5d0 | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 129 | return res.ok() && res->empty(); |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 132 | inline Result<void> createDirIfNeeded(const std::string& path, mode_t mode) { |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 133 | struct stat stat_data; |
| 134 | |
| 135 | if (stat(path.c_str(), &stat_data) != 0) { |
| 136 | if (errno == ENOENT) { |
| 137 | if (mkdir(path.c_str(), mode) != 0) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 138 | return ErrnoError() << "Could not mkdir " << path; |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 139 | } |
| 140 | } else { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 141 | return ErrnoError() << "Could not stat " << path; |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 142 | } |
| 143 | } else { |
| 144 | if (!S_ISDIR(stat_data.st_mode)) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 145 | return Error() << path << " exists and is not a directory."; |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | // Need to manually call chmod because mkdir will create a folder with |
| 150 | // permissions mode & ~umask. |
| 151 | if (chmod(path.c_str(), mode) != 0) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 152 | return ErrnoError() << "Could not chmod " << path; |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 155 | return {}; |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 158 | inline Result<void> DeleteDirContent(const std::string& path) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 159 | auto files = ReadDir(path, [](auto _) { return true; }); |
Bernie Innocenti | d04d5d0 | 2020-02-06 22:01:51 +0900 | [diff] [blame] | 160 | if (!files.ok()) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 161 | return Error() << "Failed to delete " << path << " : " << files.error(); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 162 | } |
| 163 | for (const std::string& file : *files) { |
| 164 | if (unlink(file.c_str()) != 0) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 165 | return ErrnoError() << "Failed to delete " << file; |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 168 | return {}; |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Oli Lan | f2083e3 | 2020-01-30 10:25:49 +0000 | [diff] [blame] | 171 | inline Result<void> DeleteDir(const std::string& path) { |
| 172 | namespace fs = std::filesystem; |
| 173 | std::error_code ec; |
| 174 | fs::remove_all(path, ec); |
| 175 | if (ec) { |
| 176 | return Error() << "Failed to delete path " << path << " : " << ec.message(); |
| 177 | } |
| 178 | return {}; |
| 179 | } |
| 180 | |
Oli Lan | 2d59dfa | 2020-01-14 20:25:09 +0000 | [diff] [blame] | 181 | inline Result<ino_t> get_path_inode(const std::string& path) { |
| 182 | struct stat buf; |
| 183 | memset(&buf, 0, sizeof(buf)); |
| 184 | if (stat(path.c_str(), &buf) != 0) { |
| 185 | return ErrnoError() << "Failed to stat " << path; |
| 186 | } else { |
| 187 | return buf.st_ino; |
| 188 | } |
| 189 | } |
| 190 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 191 | inline Result<bool> PathExists(const std::string& path) { |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 192 | namespace fs = std::filesystem; |
| 193 | |
| 194 | std::error_code ec; |
| 195 | if (!fs::exists(fs::path(path), ec)) { |
| 196 | if (ec) { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 197 | return Error() << "Failed to access " << path << " : " << ec.message(); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 198 | } else { |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 199 | return false; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 202 | return true; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Nikita Ioffe | 31dc34d | 2019-02-23 15:59:04 +0000 | [diff] [blame] | 205 | inline void Reboot() { |
| 206 | LOG(INFO) << "Rebooting device"; |
| 207 | if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) != 0) { |
| 208 | LOG(ERROR) << "Failed to reboot device"; |
| 209 | } |
| 210 | } |
| 211 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 212 | inline Result<void> WaitForFile(const std::string& path, |
| 213 | std::chrono::nanoseconds timeout) { |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 214 | android::base::Timer t; |
| 215 | bool has_slept = false; |
| 216 | while (t.duration() < timeout) { |
| 217 | struct stat sb; |
| 218 | if (stat(path.c_str(), &sb) != -1) { |
| 219 | if (has_slept) { |
| 220 | LOG(INFO) << "wait for '" << path << "' took " << t; |
| 221 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 222 | return {}; |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 223 | } |
| 224 | std::this_thread::sleep_for(5ms); |
| 225 | has_slept = true; |
| 226 | } |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 227 | return ErrnoError() << "wait for '" << path << "' timed out and took " << t; |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 228 | } |
| 229 | |
Oli Lan | 2993ccc | 2020-03-06 18:06:40 +0000 | [diff] [blame] | 230 | inline Result<std::vector<std::string>> GetSubdirs(const std::string& path) { |
Oli Lan | 042fbcf | 2020-01-17 11:14:16 +0000 | [diff] [blame] | 231 | namespace fs = std::filesystem; |
| 232 | auto filter_fn = [](const std::filesystem::directory_entry& entry) { |
| 233 | std::error_code ec; |
| 234 | bool result = entry.is_directory(ec); |
| 235 | if (ec) { |
| 236 | LOG(ERROR) << "Failed to check is_directory : " << ec.message(); |
| 237 | return false; |
| 238 | } |
| 239 | return result; |
| 240 | }; |
Oli Lan | 2993ccc | 2020-03-06 18:06:40 +0000 | [diff] [blame] | 241 | return ReadDir(path, filter_fn); |
| 242 | } |
| 243 | |
| 244 | inline Result<std::vector<std::string>> GetDeUserDirs() { |
| 245 | return GetSubdirs(kDeNDataDir); |
Oli Lan | 042fbcf | 2020-01-17 11:14:16 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 248 | } // namespace apex |
| 249 | } // namespace android |
| 250 | |
| 251 | #endif // ANDROID_APEXD_APEXD_UTILS_H_ |