1 /*
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>
31 #include <fs_mgr.h>
32 #include <selinux/label.h>
33 #include <tune2fs.h>
34
35 #include "mounts.h"
36 #include "otautil/sysutil.h"
37
GetProperty(const std::string_view key,const std::string_view default_value) const38 std::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
FindBlockDeviceName(const std::string_view name) const43 std::string UpdaterRuntime::FindBlockDeviceName(const std::string_view name) const {
44 return std::string(name);
45 }
46
setMountFlag(const std::string & flag,unsigned * mount_flags)47 static bool setMountFlag(const std::string& flag, unsigned* mount_flags) {
48 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
66 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
parseMountFlags(const std::string & flags,unsigned * mount_flags,std::string * fs_options)75 static 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
Mount(const std::string_view location,const std::string_view mount_point,const std::string_view fs_type,const std::string_view mount_options)91 int 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);
94 std::string mount_options_string(mount_options);
95 char* secontext = nullptr;
96 unsigned mount_flags = 0;
97 std::string fs_options;
98
99 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
111 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
116 return mount(std::string(location).c_str(), mount_point_string.c_str(),
117 std::string(fs_type).c_str(), mount_flags, fs_options.c_str());
118 }
119
IsMounted(const std::string_view mount_point) const120 bool 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
Unmount(const std::string_view mount_point)126 std::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
ReadFileToString(const std::string_view filename,std::string * content) const137 bool UpdaterRuntime::ReadFileToString(const std::string_view filename, std::string* content) const {
138 return android::base::ReadFileToString(std::string(filename), content);
139 }
140
WriteStringToFile(const std::string_view content,const std::string_view filename) const141 bool 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
WipeBlockDevice(const std::string_view filename,size_t len) const146 int 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
RunProgram(const std::vector<std::string> & args,bool is_vfork) const156 int 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
Tune2Fs(const std::vector<std::string> & args) const181 int 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 }
186
AddSlotSuffix(const std::string_view arg) const187 std::string UpdaterRuntime::AddSlotSuffix(const std::string_view arg) const {
188 return std::string(arg) + fs_mgr_get_slot_suffix();
189 }
190