blob: bac078cf98fc8ebfeb74363ce88105ba368d2dcd [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 Hong0c328d02020-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 Chen586565f2020-03-15 21:09:21 -070047static bool setMountFlag(const std::string& flag, unsigned* mount_flags) {
Alessandro Astone1dfb0862021-02-16 20:03:54 +010048 static constexpr std::pair<const char*, unsigned> mount_flags_list[] = {
49 { "noatime", MS_NOATIME },
50 { "noexec", MS_NOEXEC },
51 { "nosuid", MS_NOSUID },
52 { "nodev", MS_NODEV },
53 { "nodiratime", MS_NODIRATIME },
54 { "ro", MS_RDONLY },
55 { "rw", 0 },
56 { "remount", MS_REMOUNT },
57 { "bind", MS_BIND },
58 { "rec", MS_REC },
59 { "unbindable", MS_UNBINDABLE },
60 { "private", MS_PRIVATE },
61 { "slave", MS_SLAVE },
62 { "shared", MS_SHARED },
63 { "defaults", 0 },
64 };
65
Hongguang Chen586565f2020-03-15 21:09:21 -070066 for (const auto& [name, value] : mount_flags_list) {
67 if (flag == name) {
68 *mount_flags |= value;
69 return true;
70 }
71 }
72 return false;
73}
74
75static bool parseMountFlags(const std::string& flags, unsigned* mount_flags,
76 std::string* fs_options) {
77 bool is_flag_set = false;
78 std::vector<std::string> flag_list;
79 for (const auto& flag : android::base::Split(flags, ",")) {
80 if (!setMountFlag(flag, mount_flags)) {
81 // Unknown flag, so it must be a filesystem specific option.
82 flag_list.push_back(flag);
83 } else {
84 is_flag_set = true;
85 }
86 }
87 *fs_options = android::base::Join(flag_list, ',');
88 return is_flag_set;
89}
90
Tianjie Xu1536db82019-05-14 10:54:43 -070091int UpdaterRuntime::Mount(const std::string_view location, const std::string_view mount_point,
92 const std::string_view fs_type, const std::string_view mount_options) {
93 std::string mount_point_string(mount_point);
Hongguang Chen586565f2020-03-15 21:09:21 -070094 std::string mount_options_string(mount_options);
Tianjie Xu1536db82019-05-14 10:54:43 -070095 char* secontext = nullptr;
Hongguang Chen586565f2020-03-15 21:09:21 -070096 unsigned mount_flags = 0;
97 std::string fs_options;
98
Tianjie Xu1536db82019-05-14 10:54:43 -070099 if (sehandle_) {
100 selabel_lookup(sehandle_, &secontext, mount_point_string.c_str(), 0755);
101 setfscreatecon(secontext);
102 }
103
104 mkdir(mount_point_string.c_str(), 0755);
105
106 if (secontext) {
107 freecon(secontext);
108 setfscreatecon(nullptr);
109 }
110
Hongguang Chen586565f2020-03-15 21:09:21 -0700111 if (!parseMountFlags(mount_options_string, &mount_flags, &fs_options)) {
112 // Fall back to default
113 mount_flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
114 }
115
Tianjie Xu1536db82019-05-14 10:54:43 -0700116 return mount(std::string(location).c_str(), mount_point_string.c_str(),
Hongguang Chen586565f2020-03-15 21:09:21 -0700117 std::string(fs_type).c_str(), mount_flags, fs_options.c_str());
Tianjie Xu1536db82019-05-14 10:54:43 -0700118}
119
120bool UpdaterRuntime::IsMounted(const std::string_view mount_point) const {
121 scan_mounted_volumes();
122 MountedVolume* vol = find_mounted_volume_by_mount_point(std::string(mount_point).c_str());
123 return vol != nullptr;
124}
125
126std::pair<bool, int> UpdaterRuntime::Unmount(const std::string_view mount_point) {
127 scan_mounted_volumes();
128 MountedVolume* vol = find_mounted_volume_by_mount_point(std::string(mount_point).c_str());
129 if (vol == nullptr) {
130 return { false, -1 };
131 }
132
133 int ret = unmount_mounted_volume(vol);
134 return { true, ret };
135}
136
137bool UpdaterRuntime::ReadFileToString(const std::string_view filename, std::string* content) const {
138 return android::base::ReadFileToString(std::string(filename), content);
139}
140
141bool UpdaterRuntime::WriteStringToFile(const std::string_view content,
142 const std::string_view filename) const {
143 return android::base::WriteStringToFile(std::string(content), std::string(filename));
144}
145
146int UpdaterRuntime::WipeBlockDevice(const std::string_view filename, size_t len) const {
147 android::base::unique_fd fd(open(std::string(filename).c_str(), O_WRONLY));
148 if (fd == -1) {
149 PLOG(ERROR) << "Failed to open " << filename;
150 return false;
151 }
152 // The wipe_block_device function in ext4_utils returns 0 on success and 1 for failure.
153 return wipe_block_device(fd, len);
154}
155
156int UpdaterRuntime::RunProgram(const std::vector<std::string>& args, bool is_vfork) const {
157 CHECK(!args.empty());
158 auto argv = StringVectorToNullTerminatedArray(args);
159 LOG(INFO) << "about to run program [" << args[0] << "] with " << argv.size() << " args";
160
161 pid_t child = is_vfork ? vfork() : fork();
162 if (child == 0) {
163 execv(argv[0], argv.data());
164 PLOG(ERROR) << "run_program: execv failed";
165 _exit(EXIT_FAILURE);
166 }
167
168 int status;
169 waitpid(child, &status, 0);
170 if (WIFEXITED(status)) {
171 if (WEXITSTATUS(status) != 0) {
172 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
173 }
174 } else if (WIFSIGNALED(status)) {
175 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
176 }
177
178 return status;
179}
180
181int UpdaterRuntime::Tune2Fs(const std::vector<std::string>& args) const {
182 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
183 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
184 return tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data());
185}
Yifan Hong0c328d02020-04-28 13:31:11 -0700186
187std::string UpdaterRuntime::AddSlotSuffix(const std::string_view arg) const {
188 return std::string(arg) + fs_mgr_get_slot_suffix();
189}