David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [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 | #include "fsck_unshare_blocks.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <spawn.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <memory> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/properties.h> |
| 36 | #include <android-base/unique_fd.h> |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 37 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 38 | #include "otautil/roots.h" |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 39 | |
| 40 | static constexpr const char* SYSTEM_E2FSCK_BIN = "/system/bin/e2fsck_static"; |
| 41 | static constexpr const char* TMP_E2FSCK_BIN = "/tmp/e2fsck.bin"; |
| 42 | |
| 43 | static bool copy_file(const char* source, const char* dest) { |
| 44 | android::base::unique_fd source_fd(open(source, O_RDONLY)); |
| 45 | if (source_fd < 0) { |
| 46 | PLOG(ERROR) << "open %s failed" << source; |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | android::base::unique_fd dest_fd(open(dest, O_CREAT | O_WRONLY, S_IRWXU)); |
| 51 | if (dest_fd < 0) { |
| 52 | PLOG(ERROR) << "open %s failed" << dest; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | for (;;) { |
| 57 | char buf[4096]; |
| 58 | ssize_t rv = read(source_fd, buf, sizeof(buf)); |
| 59 | if (rv < 0) { |
| 60 | PLOG(ERROR) << "read failed"; |
| 61 | return false; |
| 62 | } |
| 63 | if (rv == 0) { |
| 64 | break; |
| 65 | } |
| 66 | if (write(dest_fd, buf, rv) != rv) { |
| 67 | PLOG(ERROR) << "write failed"; |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | static bool run_e2fsck(const std::string& partition) { |
| 75 | Volume* volume = volume_for_mount_point(partition); |
| 76 | if (!volume) { |
| 77 | LOG(INFO) << "No fstab entry for " << partition << ", skipping."; |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | LOG(INFO) << "Running e2fsck on device " << volume->blk_device; |
| 82 | |
| 83 | std::vector<std::string> args = { TMP_E2FSCK_BIN, "-p", "-E", "unshare_blocks", |
| 84 | volume->blk_device }; |
| 85 | std::vector<char*> argv(args.size()); |
| 86 | std::transform(args.cbegin(), args.cend(), argv.begin(), |
| 87 | [](const std::string& arg) { return const_cast<char*>(arg.c_str()); }); |
| 88 | argv.push_back(nullptr); |
| 89 | |
| 90 | pid_t child; |
| 91 | char* env[] = { nullptr }; |
| 92 | if (posix_spawn(&child, argv[0], nullptr, nullptr, argv.data(), env)) { |
| 93 | PLOG(ERROR) << "posix_spawn failed"; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | int status = 0; |
| 98 | int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0)); |
| 99 | if (ret < 0) { |
| 100 | PLOG(ERROR) << "waitpid failed"; |
| 101 | return false; |
| 102 | } |
| 103 | if (!WIFEXITED(status)) { |
| 104 | LOG(ERROR) << "e2fsck exited abnormally: " << status; |
| 105 | return false; |
| 106 | } |
| 107 | int return_code = WEXITSTATUS(status); |
| 108 | if (return_code >= 8) { |
| 109 | LOG(ERROR) << "e2fsck could not unshare blocks: " << return_code; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | LOG(INFO) << "Successfully unshared blocks on " << partition; |
| 114 | return true; |
| 115 | } |
| 116 | |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 117 | bool do_fsck_unshare_blocks() { |
| 118 | // List of partitions we will try to e2fsck -E unshare_blocks. |
| 119 | std::vector<std::string> partitions = { "/odm", "/oem", "/product", "/vendor" }; |
| 120 | |
| 121 | // Temporarily mount system so we can copy e2fsck_static. |
Yifan Hong | 4932780 | 2018-11-26 14:59:09 -0800 | [diff] [blame] | 122 | std::string system_root = get_system_root(); |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 123 | bool mounted = ensure_path_mounted_at(system_root, "/mnt/system") != -1; |
Yifan Hong | 4932780 | 2018-11-26 14:59:09 -0800 | [diff] [blame] | 124 | partitions.push_back(system_root); |
| 125 | |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 126 | if (!mounted) { |
| 127 | LOG(ERROR) << "Failed to mount system image."; |
| 128 | return false; |
| 129 | } |
| 130 | if (!copy_file(SYSTEM_E2FSCK_BIN, TMP_E2FSCK_BIN)) { |
| 131 | LOG(ERROR) << "Could not copy e2fsck to /tmp."; |
| 132 | return false; |
| 133 | } |
Jiyong Park | 8b7af4c | 2018-06-01 11:58:54 +0900 | [diff] [blame] | 134 | if (umount("/mnt/system") < 0) { |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 135 | PLOG(ERROR) << "umount failed"; |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | bool ok = true; |
| 140 | for (const auto& partition : partitions) { |
| 141 | ok &= run_e2fsck(partition); |
| 142 | } |
| 143 | |
| 144 | if (ok) { |
| 145 | LOG(INFO) << "Finished running e2fsck."; |
| 146 | } else { |
| 147 | LOG(ERROR) << "Finished running e2fsck, but not all partitions succceeded."; |
| 148 | } |
| 149 | return ok; |
| 150 | } |