blob: e93830505c52eae9ec0ff1ec026e718726b8e929 [file] [log] [blame]
Tianjie Xu1536db82019-05-14 10:54:43 -07001/*
2 * Copyright (C) 2019 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 "updater/updater_runtime.h"
18
19#include <string.h>
20#include <sys/mount.h>
21#include <sys/stat.h>
22#include <sys/wait.h>
23#include <unistd.h>
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/properties.h>
28#include <android-base/strings.h>
29#include <android-base/unique_fd.h>
30#include <ext4_utils/wipe.h>
Yifan Hongdff80042020-04-28 13:31:11 -070031#include <fs_mgr.h>
Tianjie Xu1536db82019-05-14 10:54:43 -070032#include <selinux/label.h>
33#include <tune2fs.h>
34
Tao Baod628cfc2019-10-01 12:08:33 -070035#include "mounts.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070036#include "otautil/sysutil.h"
37
38std::string UpdaterRuntime::GetProperty(const std::string_view key,
39 const std::string_view default_value) const {
40 return android::base::GetProperty(std::string(key), std::string(default_value));
41}
42
43std::string UpdaterRuntime::FindBlockDeviceName(const std::string_view name) const {
44 return std::string(name);
45}
46
Hongguang Chenb37d7942020-03-15 21:09:21 -070047static struct {
48 const char* name;
49 unsigned flag;
50} mount_flags_list[] = {
51 { "noatime", MS_NOATIME },
52 { "noexec", MS_NOEXEC },
53 { "nosuid", MS_NOSUID },
54 { "nodev", MS_NODEV },
55 { "nodiratime", MS_NODIRATIME },
56 { "ro", MS_RDONLY },
57 { "rw", 0 },
58 { "remount", MS_REMOUNT },
59 { "bind", MS_BIND },
60 { "rec", MS_REC },
61 { "unbindable", MS_UNBINDABLE },
62 { "private", MS_PRIVATE },
63 { "slave", MS_SLAVE },
64 { "shared", MS_SHARED },
65 { "defaults", 0 },
66 { 0, 0 },
67};
68
69static bool setMountFlag(const std::string& flag, unsigned* mount_flags) {
70 for (const auto& [name, value] : mount_flags_list) {
71 if (flag == name) {
72 *mount_flags |= value;
73 return true;
74 }
75 }
76 return false;
77}
78
79static bool parseMountFlags(const std::string& flags, unsigned* mount_flags,
80 std::string* fs_options) {
81 bool is_flag_set = false;
82 std::vector<std::string> flag_list;
83 for (const auto& flag : android::base::Split(flags, ",")) {
84 if (!setMountFlag(flag, mount_flags)) {
85 // Unknown flag, so it must be a filesystem specific option.
86 flag_list.push_back(flag);
87 } else {
88 is_flag_set = true;
89 }
90 }
91 *fs_options = android::base::Join(flag_list, ',');
92 return is_flag_set;
93}
94
Tianjie Xu1536db82019-05-14 10:54:43 -070095int UpdaterRuntime::Mount(const std::string_view location, const std::string_view mount_point,
96 const std::string_view fs_type, const std::string_view mount_options) {
97 std::string mount_point_string(mount_point);
Hongguang Chenb37d7942020-03-15 21:09:21 -070098 std::string mount_options_string(mount_options);
Tianjie Xu1536db82019-05-14 10:54:43 -070099 char* secontext = nullptr;
Hongguang Chenb37d7942020-03-15 21:09:21 -0700100 unsigned mount_flags = 0;
101 std::string fs_options;
102
Tianjie Xu1536db82019-05-14 10:54:43 -0700103 if (sehandle_) {
104 selabel_lookup(sehandle_, &secontext, mount_point_string.c_str(), 0755);
105 setfscreatecon(secontext);
106 }
107
108 mkdir(mount_point_string.c_str(), 0755);
109
110 if (secontext) {
111 freecon(secontext);
112 setfscreatecon(nullptr);
113 }
114
Hongguang Chenb37d7942020-03-15 21:09:21 -0700115 if (!parseMountFlags(mount_options_string, &mount_flags, &fs_options)) {
116 // Fall back to default
117 mount_flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
118 }
119
Tianjie Xu1536db82019-05-14 10:54:43 -0700120 return mount(std::string(location).c_str(), mount_point_string.c_str(),
Hongguang Chenb37d7942020-03-15 21:09:21 -0700121 std::string(fs_type).c_str(), mount_flags, fs_options.c_str());
Tianjie Xu1536db82019-05-14 10:54:43 -0700122}
123
124bool UpdaterRuntime::IsMounted(const std::string_view mount_point) const {
125 scan_mounted_volumes();
126 MountedVolume* vol = find_mounted_volume_by_mount_point(std::string(mount_point).c_str());
127 return vol != nullptr;
128}
129
130std::pair<bool, int> UpdaterRuntime::Unmount(const std::string_view mount_point) {
131 scan_mounted_volumes();
132 MountedVolume* vol = find_mounted_volume_by_mount_point(std::string(mount_point).c_str());
133 if (vol == nullptr) {
134 return { false, -1 };
135 }
136
137 int ret = unmount_mounted_volume(vol);
138 return { true, ret };
139}
140
141bool UpdaterRuntime::ReadFileToString(const std::string_view filename, std::string* content) const {
142 return android::base::ReadFileToString(std::string(filename), content);
143}
144
145bool UpdaterRuntime::WriteStringToFile(const std::string_view content,
146 const std::string_view filename) const {
147 return android::base::WriteStringToFile(std::string(content), std::string(filename));
148}
149
150int UpdaterRuntime::WipeBlockDevice(const std::string_view filename, size_t len) const {
151 android::base::unique_fd fd(open(std::string(filename).c_str(), O_WRONLY));
152 if (fd == -1) {
153 PLOG(ERROR) << "Failed to open " << filename;
154 return false;
155 }
156 // The wipe_block_device function in ext4_utils returns 0 on success and 1 for failure.
157 return wipe_block_device(fd, len);
158}
159
160int UpdaterRuntime::RunProgram(const std::vector<std::string>& args, bool is_vfork) const {
161 CHECK(!args.empty());
162 auto argv = StringVectorToNullTerminatedArray(args);
163 LOG(INFO) << "about to run program [" << args[0] << "] with " << argv.size() << " args";
164
165 pid_t child = is_vfork ? vfork() : fork();
166 if (child == 0) {
167 execv(argv[0], argv.data());
168 PLOG(ERROR) << "run_program: execv failed";
169 _exit(EXIT_FAILURE);
170 }
171
172 int status;
173 waitpid(child, &status, 0);
174 if (WIFEXITED(status)) {
175 if (WEXITSTATUS(status) != 0) {
176 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
177 }
178 } else if (WIFSIGNALED(status)) {
179 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
180 }
181
182 return status;
183}
184
185int UpdaterRuntime::Tune2Fs(const std::vector<std::string>& args) const {
186 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
187 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
188 return tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data());
189}
Yifan Hongdff80042020-04-28 13:31:11 -0700190
191std::string UpdaterRuntime::AddSlotSuffix(const std::string_view arg) const {
192 return std::string(arg) + fs_mgr_get_slot_suffix();
193}