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> |
Nikita Ioffe | 31dc34d | 2019-02-23 15:59:04 +0000 | [diff] [blame] | 33 | #include <cutils/android_reboot.h> |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 34 | |
Jooyung Han | 451cc34 | 2019-04-12 04:52:42 +0900 | [diff] [blame] | 35 | #include "status_or.h" |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 36 | #include "string_log.h" |
| 37 | |
| 38 | namespace android { |
| 39 | namespace apex { |
| 40 | |
| 41 | inline int WaitChild(pid_t pid) { |
| 42 | int status; |
| 43 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 44 | |
| 45 | if (got_pid != pid) { |
| 46 | PLOG(WARNING) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 51 | return 0; |
| 52 | } else { |
| 53 | return status; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | inline int ForkAndRun(const std::vector<std::string>& args, |
| 58 | std::string* error_msg) { |
| 59 | std::vector<const char*> argv; |
| 60 | argv.resize(args.size() + 1, nullptr); |
| 61 | std::transform(args.begin(), args.end(), argv.begin(), |
| 62 | [](const std::string& in) { return in.c_str(); }); |
| 63 | |
| 64 | // 3) Fork. |
| 65 | pid_t pid = fork(); |
| 66 | if (pid == -1) { |
| 67 | // Fork failed. |
| 68 | *error_msg = PStringLog() << "Unable to fork"; |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | if (pid == 0) { |
| 73 | execv(argv[0], const_cast<char**>(argv.data())); |
| 74 | PLOG(ERROR) << "execv failed"; |
| 75 | _exit(1); |
| 76 | } |
| 77 | |
| 78 | int rc = WaitChild(pid); |
| 79 | if (rc != 0) { |
| 80 | *error_msg = StringLog() << "Failed run: status=" << rc; |
| 81 | } |
| 82 | return rc; |
| 83 | } |
| 84 | |
Jooyung Han | aed3a03 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 85 | template <typename Fn> |
| 86 | Status WalkDir(const std::string& path, Fn fn) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 87 | namespace fs = std::filesystem; |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 88 | std::error_code ec; |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 89 | auto it = fs::directory_iterator(path, ec); |
| 90 | auto end = fs::directory_iterator(); |
| 91 | while (!ec && it != end) { |
Jooyung Han | aed3a03 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 92 | fn(*it); |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 93 | it.increment(ec); |
| 94 | } |
Martijn Coenen | 809c363 | 2019-05-01 09:20:42 +0200 | [diff] [blame] | 95 | if (ec) { |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 96 | return Status::Fail(StringLog() << "Can't open " << path |
| 97 | << " for reading : " << ec.message()); |
Martijn Coenen | 809c363 | 2019-05-01 09:20:42 +0200 | [diff] [blame] | 98 | } |
Jooyung Han | aed3a03 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 99 | return Status::Success(); |
| 100 | } |
| 101 | |
| 102 | template <typename FilterFn> |
| 103 | StatusOr<std::vector<std::string>> ReadDir(const std::string& path, |
| 104 | FilterFn fn) { |
| 105 | namespace fs = std::filesystem; |
| 106 | using Status = StatusOr<std::vector<std::string>>; |
| 107 | |
| 108 | std::vector<std::string> ret; |
| 109 | auto status = WalkDir(path, [&](const fs::directory_entry& entry) { |
| 110 | if (fn(entry)) { |
| 111 | ret.push_back(entry.path()); |
| 112 | } |
| 113 | }); |
| 114 | if (!status.Ok()) { |
| 115 | return Status::Fail(status.ErrorMessage()); |
| 116 | } |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 117 | return Status(std::move(ret)); |
Jooyung Han | aed3a03 | 2019-05-03 00:42:31 +0900 | [diff] [blame] | 118 | } |
Martijn Coenen | 610909b | 2019-01-18 13:49:38 +0100 | [diff] [blame] | 119 | |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 120 | inline bool IsEmptyDirectory(const std::string& path) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 121 | auto res = ReadDir(path, [](auto _) { return true; }); |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 122 | return res.Ok() && res->empty(); |
| 123 | } |
| 124 | |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 125 | inline Status createDirIfNeeded(const std::string& path, mode_t mode) { |
| 126 | struct stat stat_data; |
| 127 | |
| 128 | if (stat(path.c_str(), &stat_data) != 0) { |
| 129 | if (errno == ENOENT) { |
| 130 | if (mkdir(path.c_str(), mode) != 0) { |
| 131 | return Status::Fail(PStringLog() << "Could not mkdir " << path); |
| 132 | } |
| 133 | } else { |
| 134 | return Status::Fail(PStringLog() << "Could not stat " << path); |
| 135 | } |
| 136 | } else { |
| 137 | if (!S_ISDIR(stat_data.st_mode)) { |
| 138 | return Status::Fail(path + " exists and is not a directory."); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Need to manually call chmod because mkdir will create a folder with |
| 143 | // permissions mode & ~umask. |
| 144 | if (chmod(path.c_str(), mode) != 0) { |
| 145 | return Status::Fail(PStringLog() << "Could not chmod " << path); |
| 146 | } |
| 147 | |
| 148 | return Status::Success(); |
| 149 | } |
| 150 | |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 151 | inline Status DeleteDirContent(const std::string& path) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 152 | auto files = ReadDir(path, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 153 | if (!files.Ok()) { |
| 154 | return Status::Fail(StringLog() << "Failed to delete " << path << " : " |
| 155 | << files.ErrorMessage()); |
| 156 | } |
| 157 | for (const std::string& file : *files) { |
| 158 | if (unlink(file.c_str()) != 0) { |
| 159 | return Status::Fail(PStringLog() << "Failed to delete " << file); |
| 160 | } |
| 161 | } |
| 162 | return Status::Success(); |
| 163 | } |
| 164 | |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 165 | inline StatusOr<bool> PathExists(const std::string& path) { |
| 166 | namespace fs = std::filesystem; |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 167 | using Status = StatusOr<bool>; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 168 | |
| 169 | std::error_code ec; |
| 170 | if (!fs::exists(fs::path(path), ec)) { |
| 171 | if (ec) { |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 172 | return Status::Fail(StringLog() << "Failed to access " << path << " : " |
| 173 | << ec.message()); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 174 | } else { |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 175 | return Status(false); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Jooyung Han | 86b0e38 | 2019-04-24 10:11:56 +0900 | [diff] [blame] | 178 | return Status(true); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Nikita Ioffe | 31dc34d | 2019-02-23 15:59:04 +0000 | [diff] [blame] | 181 | inline void Reboot() { |
| 182 | LOG(INFO) << "Rebooting device"; |
| 183 | if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) != 0) { |
| 184 | LOG(ERROR) << "Failed to reboot device"; |
| 185 | } |
| 186 | } |
| 187 | |
Jiyong Park | d8a82ce | 2019-02-25 02:21:18 +0900 | [diff] [blame] | 188 | inline Status WaitForFile(const std::string& path, |
| 189 | std::chrono::nanoseconds timeout) { |
| 190 | android::base::Timer t; |
| 191 | bool has_slept = false; |
| 192 | while (t.duration() < timeout) { |
| 193 | struct stat sb; |
| 194 | if (stat(path.c_str(), &sb) != -1) { |
| 195 | if (has_slept) { |
| 196 | LOG(INFO) << "wait for '" << path << "' took " << t; |
| 197 | } |
| 198 | return Status::Success(); |
| 199 | } |
| 200 | std::this_thread::sleep_for(5ms); |
| 201 | has_slept = true; |
| 202 | } |
| 203 | return Status::Fail(PStringLog() |
| 204 | << "wait for '" << path << "' timed out and took " << t); |
| 205 | } |
| 206 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 207 | } // namespace apex |
| 208 | } // namespace android |
| 209 | |
| 210 | #endif // ANDROID_APEXD_APEXD_UTILS_H_ |