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