blob: 2e840b97222f595c6b11dafa0d51976611009281 [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
47int UpdaterRuntime::Mount(const std::string_view location, const std::string_view mount_point,
48 const std::string_view fs_type, const std::string_view mount_options) {
49 std::string mount_point_string(mount_point);
50 char* secontext = nullptr;
51 if (sehandle_) {
52 selabel_lookup(sehandle_, &secontext, mount_point_string.c_str(), 0755);
53 setfscreatecon(secontext);
54 }
55
56 mkdir(mount_point_string.c_str(), 0755);
57
58 if (secontext) {
59 freecon(secontext);
60 setfscreatecon(nullptr);
61 }
62
63 return mount(std::string(location).c_str(), mount_point_string.c_str(),
64 std::string(fs_type).c_str(), MS_NOATIME | MS_NODEV | MS_NODIRATIME,
65 std::string(mount_options).c_str());
66}
67
68bool UpdaterRuntime::IsMounted(const std::string_view mount_point) const {
69 scan_mounted_volumes();
70 MountedVolume* vol = find_mounted_volume_by_mount_point(std::string(mount_point).c_str());
71 return vol != nullptr;
72}
73
74std::pair<bool, int> UpdaterRuntime::Unmount(const std::string_view mount_point) {
75 scan_mounted_volumes();
76 MountedVolume* vol = find_mounted_volume_by_mount_point(std::string(mount_point).c_str());
77 if (vol == nullptr) {
78 return { false, -1 };
79 }
80
81 int ret = unmount_mounted_volume(vol);
82 return { true, ret };
83}
84
85bool UpdaterRuntime::ReadFileToString(const std::string_view filename, std::string* content) const {
86 return android::base::ReadFileToString(std::string(filename), content);
87}
88
89bool UpdaterRuntime::WriteStringToFile(const std::string_view content,
90 const std::string_view filename) const {
91 return android::base::WriteStringToFile(std::string(content), std::string(filename));
92}
93
94int UpdaterRuntime::WipeBlockDevice(const std::string_view filename, size_t len) const {
95 android::base::unique_fd fd(open(std::string(filename).c_str(), O_WRONLY));
96 if (fd == -1) {
97 PLOG(ERROR) << "Failed to open " << filename;
98 return false;
99 }
100 // The wipe_block_device function in ext4_utils returns 0 on success and 1 for failure.
101 return wipe_block_device(fd, len);
102}
103
104int UpdaterRuntime::RunProgram(const std::vector<std::string>& args, bool is_vfork) const {
105 CHECK(!args.empty());
106 auto argv = StringVectorToNullTerminatedArray(args);
107 LOG(INFO) << "about to run program [" << args[0] << "] with " << argv.size() << " args";
108
109 pid_t child = is_vfork ? vfork() : fork();
110 if (child == 0) {
111 execv(argv[0], argv.data());
112 PLOG(ERROR) << "run_program: execv failed";
113 _exit(EXIT_FAILURE);
114 }
115
116 int status;
117 waitpid(child, &status, 0);
118 if (WIFEXITED(status)) {
119 if (WEXITSTATUS(status) != 0) {
120 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
121 }
122 } else if (WIFSIGNALED(status)) {
123 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
124 }
125
126 return status;
127}
128
129int UpdaterRuntime::Tune2Fs(const std::vector<std::string>& args) const {
130 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
131 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
132 return tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data());
133}
Yifan Hong0c328d02020-04-28 13:31:11 -0700134
135std::string UpdaterRuntime::AddSlotSuffix(const std::string_view arg) const {
136 return std::string(arg) + fs_mgr_get_slot_suffix();
137}