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
47 static 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
setMountFlag(const std::string & flag,unsigned * mount_flags)69 static 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
parseMountFlags(const std::string & flags,unsigned * mount_flags,std::string * fs_options)79 static 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
Mount(const std::string_view location,const std::string_view mount_point,const std::string_view fs_type,const std::string_view mount_options)95 int 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);
98 std::string mount_options_string(mount_options);
99 char* secontext = nullptr;
100 unsigned mount_flags = 0;
101 std::string fs_options;
102
103 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
115 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
120 return mount(std::string(location).c_str(), mount_point_string.c_str(),
121 std::string(fs_type).c_str(), mount_flags, fs_options.c_str());
122 }
123
IsMounted(const std::string_view mount_point) const124 bool 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
Unmount(const std::string_view mount_point)130 std::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
ReadFileToString(const std::string_view filename,std::string * content) const141 bool UpdaterRuntime::ReadFileToString(const std::string_view filename, std::string* content) const {
142 return android::base::ReadFileToString(std::string(filename), content);
143 }
144
WriteStringToFile(const std::string_view content,const std::string_view filename) const145 bool 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
WipeBlockDevice(const std::string_view filename,size_t len) const150 int 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
RunProgram(const std::vector<std::string> & args,bool is_vfork) const160 int 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
Tune2Fs(const std::vector<std::string> & args) const185 int 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 }
190
AddSlotSuffix(const std::string_view arg) const191 std::string UpdaterRuntime::AddSlotSuffix(const std::string_view arg) const {
192 return std::string(arg) + fs_mgr_get_slot_suffix();
193 }
194