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