• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/simulator_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 <unordered_set>
26 
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/properties.h>
30 #include <android-base/strings.h>
31 #include <android-base/unique_fd.h>
32 #include <ext4_utils/wipe.h>
33 #include <selinux/label.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 SimulatorRuntime::GetProperty(const std::string_view key,
39                                           const std::string_view default_value) const {
40   return source_->GetProperty(key, default_value);
41 }
42 
Mount(const std::string_view location,const std::string_view mount_point,const std::string_view,const std::string_view)43 int SimulatorRuntime::Mount(const std::string_view location, const std::string_view mount_point,
44                             const std::string_view /* fs_type */,
45                             const std::string_view /* mount_options */) {
46   if (auto mounted_location = mounted_partitions_.find(mount_point);
47       mounted_location != mounted_partitions_.end() && mounted_location->second != location) {
48     LOG(ERROR) << mount_point << " has been mounted at " << mounted_location->second;
49     return -1;
50   }
51 
52   mounted_partitions_.emplace(mount_point, location);
53   return 0;
54 }
55 
IsMounted(const std::string_view mount_point) const56 bool SimulatorRuntime::IsMounted(const std::string_view mount_point) const {
57   return mounted_partitions_.find(mount_point) != mounted_partitions_.end();
58 }
59 
Unmount(const std::string_view mount_point)60 std::pair<bool, int> SimulatorRuntime::Unmount(const std::string_view mount_point) {
61   if (!IsMounted(mount_point)) {
62     return { false, -1 };
63   }
64 
65   mounted_partitions_.erase(std::string(mount_point));
66   return { true, 0 };
67 }
68 
FindBlockDeviceName(const std::string_view name) const69 std::string SimulatorRuntime::FindBlockDeviceName(const std::string_view name) const {
70   return source_->FindBlockDeviceName(name);
71 }
72 
73 // TODO(xunchang) implement the utility functions in simulator.
RunProgram(const std::vector<std::string> & args,bool) const74 int SimulatorRuntime::RunProgram(const std::vector<std::string>& args, bool /* is_vfork */) const {
75   LOG(INFO) << "Running program with args " << android::base::Join(args, " ");
76   return 0;
77 }
78 
Tune2Fs(const std::vector<std::string> & args) const79 int SimulatorRuntime::Tune2Fs(const std::vector<std::string>& args) const {
80   LOG(INFO) << "Running Tune2Fs with args " << android::base::Join(args, " ");
81   return 0;
82 }
83 
WipeBlockDevice(const std::string_view filename,size_t) const84 int SimulatorRuntime::WipeBlockDevice(const std::string_view filename, size_t /* len */) const {
85   LOG(INFO) << "SKip wiping block device " << filename;
86   return 0;
87 }
88 
ReadFileToString(const std::string_view filename,std::string * content) const89 bool SimulatorRuntime::ReadFileToString(const std::string_view filename,
90                                         std::string* content) const {
91   if (android::base::EndsWith(filename, "oem.prop")) {
92     return android::base::ReadFileToString(source_->GetOemSettings(), content);
93   }
94 
95   LOG(INFO) << "SKip reading filename " << filename;
96   return true;
97 }
98 
WriteStringToFile(const std::string_view content,const std::string_view filename) const99 bool SimulatorRuntime::WriteStringToFile(const std::string_view content,
100                                          const std::string_view filename) const {
101   LOG(INFO) << "SKip writing " << content.size() << " bytes to file " << filename;
102   return true;
103 }
104 
MapPartitionOnDeviceMapper(const std::string & partition_name,std::string * path)105 bool SimulatorRuntime::MapPartitionOnDeviceMapper(const std::string& partition_name,
106                                                   std::string* path) {
107   *path = partition_name;
108   return true;
109 }
110 
UnmapPartitionOnDeviceMapper(const std::string & partition_name)111 bool SimulatorRuntime::UnmapPartitionOnDeviceMapper(const std::string& partition_name) {
112   LOG(INFO) << "Skip unmapping " << partition_name;
113   return true;
114 }
115 
UpdateDynamicPartitions(const std::string_view op_list_value)116 bool SimulatorRuntime::UpdateDynamicPartitions(const std::string_view op_list_value) {
117   const std::unordered_set<std::string> commands{
118     "resize",    "remove",       "add",          "move",
119     "add_group", "resize_group", "remove_group", "remove_all_groups",
120   };
121 
122   std::vector<std::string> lines = android::base::Split(std::string(op_list_value), "\n");
123   for (const auto& line : lines) {
124     if (line.empty() || line[0] == '#') continue;
125     auto tokens = android::base::Split(line, " ");
126     if (commands.find(tokens[0]) == commands.end()) {
127       LOG(ERROR) << "Unknown operation in op_list: " << line;
128       return false;
129     }
130   }
131   return true;
132 }
133 
AddSlotSuffix(const std::string_view arg) const134 std::string SimulatorRuntime::AddSlotSuffix(const std::string_view arg) const {
135   LOG(INFO) << "Skip adding slot suffix to " << arg;
136   return std::string(arg);
137 }
138