Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "host/libs/config/cuttlefish_config.h" |
| 18 | |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 20 | #include <climits> |
| 21 | #include <cstdlib> |
| 22 | #include <cstring> |
| 23 | #include <fstream> |
| 24 | #include <iomanip> |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 25 | #include <iterator> |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 30 | #include <glog/logging.h> |
| 31 | #include <json/json.h> |
| 32 | |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 33 | #include "common/libs/utils/environment.h" |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 34 | #include "common/libs/utils/files.h" |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 35 | #include "host/libs/vm_manager/qemu_manager.h" |
| 36 | |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 37 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 38 | namespace { |
| 39 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 40 | int InstanceFromEnvironment() { |
| 41 | static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 42 | static constexpr int kDefaultInstance = 1; |
| 43 | |
| 44 | // CUTTLEFISH_INSTANCE environment variable |
| 45 | const char* instance_str = std::getenv(kInstanceEnvironmentVariable); |
| 46 | if (!instance_str) { |
| 47 | // Try to get it from the user instead |
| 48 | instance_str = std::getenv("USER"); |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 49 | |
| 50 | if (!instance_str || std::strncmp(instance_str, vsoc::kVsocUserPrefix, |
| 51 | sizeof(vsoc::kVsocUserPrefix) - 1)) { |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 52 | // No user or we don't recognize this user |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 53 | LOG(WARNING) << "No user or non-vsoc user, returning default config"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 54 | return kDefaultInstance; |
| 55 | } |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 56 | instance_str += sizeof(vsoc::kVsocUserPrefix) - 1; |
| 57 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 58 | // Set the environment variable so that child processes see it |
| 59 | setenv(kInstanceEnvironmentVariable, instance_str, 0); |
| 60 | } |
| 61 | |
| 62 | int instance = std::atoi(instance_str); |
| 63 | if (instance <= 0) { |
| 64 | instance = kDefaultInstance; |
| 65 | } |
| 66 | |
| 67 | return instance; |
| 68 | } |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 69 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 70 | const char* kSerialNumber = "serial_number"; |
| 71 | const char* kInstanceDir = "instance_dir"; |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 72 | const char* kVmManager = "vm_manager"; |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 73 | const char* const kGpuMode = "gpu_mode"; |
| 74 | const char* const kWaylandSocket = "wayland_socket"; |
Greg Hartman | eb04ac5 | 2019-07-16 16:44:18 -0700 | [diff] [blame] | 75 | const char* const kXDisplay = "x_display"; |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 76 | const char* kDeviceTitle = "device_title"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 77 | |
| 78 | const char* kCpus = "cpus"; |
| 79 | const char* kMemoryMb = "memory_mb"; |
| 80 | const char* kDpi = "dpi"; |
| 81 | const char* kXRes = "x_res"; |
| 82 | const char* kYRes = "y_res"; |
| 83 | const char* kRefreshRateHz = "refresh_rate_hz"; |
| 84 | |
| 85 | const char* kKernelImagePath = "kernel_image_path"; |
Cody Schuffelen | e13a78a | 2019-01-29 14:51:26 -0800 | [diff] [blame] | 86 | const char* kUseUnpackedKernel = "use_unpacked_kernel"; |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 87 | const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path"; |
| 88 | const char* kDecompressKernel = "decompress_kernel"; |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 89 | const char* kGdbFlag = "gdb_flag"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 90 | const char* kRamdiskImagePath = "ramdisk_image_path"; |
Cody Schuffelen | 9fd9547 | 2019-10-04 13:49:57 -0700 | [diff] [blame] | 91 | const char* kInitramfsPath = "initramfs_path"; |
| 92 | const char* kFinalRamdiskPath = "final_ramdisk_path"; |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 93 | const char* kVendorRamdiskImagePath = "vendor_ramdisk_image_path"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 94 | |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 95 | const char* kVirtualDiskPaths = "virtual_disk_paths"; |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 96 | const char* kDeprecatedBootCompleted = "deprecated_boot_completed"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 97 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 98 | const char* kMobileBridgeName = "mobile_bridge_name"; |
| 99 | const char* kMobileTapName = "mobile_tap_name"; |
Cody Schuffelen | 975175a | 2018-06-14 18:20:02 -0700 | [diff] [blame] | 100 | const char* kWifiTapName = "wifi_tap_name"; |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 101 | const char* kVsockGuestCid = "vsock_guest_cid"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 102 | |
| 103 | const char* kUuid = "uuid"; |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 104 | const char* kCuttlefishEnvPath = "cuttlefish_env_path"; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 105 | |
| 106 | const char* kAdbMode = "adb_mode"; |
Cody Schuffelen | e0d0c46 | 2019-09-09 14:13:01 -0700 | [diff] [blame] | 107 | const char* kHostPort = "host_port"; |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 108 | const char* kAdbIPAndPort = "adb_ip_and_port"; |
Greg Hartman | f1f28c5 | 2018-09-20 17:19:19 -0700 | [diff] [blame] | 109 | const char* kSetupWizardMode = "setupwizard_mode"; |
Cody Schuffelen | 5898003 | 2018-10-11 17:11:13 -0700 | [diff] [blame] | 110 | |
Cody Schuffelen | 5898003 | 2018-10-11 17:11:13 -0700 | [diff] [blame] | 111 | const char* kQemuBinary = "qemu_binary"; |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 112 | const char* kCrosvmBinary = "crosvm_binary"; |
Jorge E. Moreira | 13cbd98 | 2019-06-06 16:06:34 -0700 | [diff] [blame] | 113 | const char* kConsoleForwarderBinary = "console_forwarder_binary"; |
Cody Schuffelen | 55676ca | 2019-01-28 22:00:05 -0800 | [diff] [blame] | 114 | const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary"; |
Cody Schuffelen | 17b3472 | 2019-01-28 22:57:14 -0800 | [diff] [blame] | 115 | |
| 116 | const char* kEnableVncServer = "enable_vnc_server"; |
| 117 | const char* kVncServerBinary = "vnc_server_binary"; |
| 118 | const char* kVncServerPort = "vnc_server_port"; |
| 119 | |
Cody Schuffelen | d12e0c9 | 2019-01-29 15:44:45 -0800 | [diff] [blame] | 120 | const char* kRestartSubprocesses = "restart_subprocesses"; |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 121 | const char* kRunAdbConnector = "run_adb_connector"; |
| 122 | const char* kAdbConnectorBinary = "adb_connector_binary"; |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 123 | const char* kSocketVsockProxyBinary = "socket_vsock_proxy_binary"; |
Cody Schuffelen | 5024973 | 2019-01-29 16:42:55 -0800 | [diff] [blame] | 124 | |
| 125 | const char* kRunAsDaemon = "run_as_daemon"; |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 126 | |
| 127 | const char* kDataPolicy = "data_policy"; |
| 128 | const char* kBlankDataImageMb = "blank_data_image_mb"; |
| 129 | const char* kBlankDataImageFmt = "blank_data_image_fmt"; |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 130 | |
Cody Schuffelen | 5de4865 | 2019-09-16 19:49:52 +0000 | [diff] [blame] | 131 | const char* kLogcatMode = "logcat_mode"; |
Cody Schuffelen | 5de4865 | 2019-09-16 19:49:52 +0000 | [diff] [blame] | 132 | const char* kLogcatReceiverBinary = "logcat_receiver_binary"; |
Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 133 | const char* kConfigServerBinary = "config_server_binary"; |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 134 | |
| 135 | const char* kRunTombstoneReceiver = "enable_tombstone_logger"; |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 136 | const char* kTombstoneReceiverBinary = "tombstone_receiver_binary"; |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 137 | |
| 138 | const char* kBootloader = "bootloader"; |
| 139 | const char* kUseBootloader = "use_bootloader"; |
David Anderson | 637bfd6 | 2019-09-26 13:23:21 -0700 | [diff] [blame] | 140 | |
| 141 | const char* kBootSlot = "boot_slot"; |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 142 | |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 143 | const char* kLoopMaxPart = "loop_max_part"; |
| 144 | const char* kGuestEnforceSecurity = "guest_enforce_security"; |
| 145 | const char* kGuestAuditSecurity = "guest_audit_security"; |
| 146 | const char* kBootImageKernelCmdline = "boot_image_kernel_cmdline"; |
| 147 | const char* kExtraKernelCmdline = "extra_kernel_cmdline"; |
| 148 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 149 | } // namespace |
| 150 | |
| 151 | namespace vsoc { |
| 152 | |
Greg Hartman | bc1bed4 | 2019-04-05 20:02:00 -0700 | [diff] [blame] | 153 | const char* const kGpuModeGuestSwiftshader = "guest_swiftshader"; |
| 154 | const char* const kGpuModeDrmVirgl = "drm_virgl"; |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 155 | |
| 156 | std::string DefaultEnvironmentPath(const char* environment_key, |
| 157 | const char* default_value, |
| 158 | const char* subpath) { |
| 159 | return cvd::StringFromEnv(environment_key, default_value) + "/" + subpath; |
| 160 | } |
| 161 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 162 | std::string CuttlefishConfig::instance_dir() const { |
| 163 | return (*dictionary_)[kInstanceDir].asString(); |
| 164 | } |
| 165 | void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) { |
| 166 | (*dictionary_)[kInstanceDir] = instance_dir; |
| 167 | } |
| 168 | |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 169 | std::string CuttlefishConfig::instance_internal_dir() const { |
| 170 | return PerInstancePath(kInternalDirName); |
| 171 | } |
| 172 | |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 173 | std::string CuttlefishConfig::vm_manager() const { |
| 174 | return (*dictionary_)[kVmManager].asString(); |
| 175 | } |
| 176 | void CuttlefishConfig::set_vm_manager(const std::string& name) { |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 177 | (*dictionary_)[kVmManager] = name; |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 180 | std::string CuttlefishConfig::gpu_mode() const { |
| 181 | return (*dictionary_)[kGpuMode].asString(); |
| 182 | } |
| 183 | void CuttlefishConfig::set_gpu_mode(const std::string& name) { |
| 184 | (*dictionary_)[kGpuMode] = name; |
| 185 | } |
| 186 | |
| 187 | std::string CuttlefishConfig::wayland_socket() const { |
| 188 | // Don't use SetPath here: the path is already fully formed. |
| 189 | return (*dictionary_)[kWaylandSocket].asString(); |
| 190 | } |
| 191 | void CuttlefishConfig::set_wayland_socket(const std::string& path) { |
| 192 | (*dictionary_)[kWaylandSocket] = path; |
| 193 | } |
| 194 | |
Greg Hartman | eb04ac5 | 2019-07-16 16:44:18 -0700 | [diff] [blame] | 195 | std::string CuttlefishConfig::x_display() const { |
| 196 | return (*dictionary_)[kXDisplay].asString(); |
| 197 | } |
| 198 | void CuttlefishConfig::set_x_display(const std::string& address) { |
| 199 | (*dictionary_)[kXDisplay] = address; |
| 200 | } |
| 201 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 202 | std::string CuttlefishConfig::serial_number() const { |
| 203 | return (*dictionary_)[kSerialNumber].asString(); |
| 204 | } |
| 205 | void CuttlefishConfig::set_serial_number(const std::string& serial_number) { |
| 206 | (*dictionary_)[kSerialNumber] = serial_number; |
| 207 | } |
| 208 | |
| 209 | int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); } |
| 210 | void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; } |
| 211 | |
| 212 | int CuttlefishConfig::memory_mb() const { |
| 213 | return (*dictionary_)[kMemoryMb].asInt(); |
| 214 | } |
| 215 | void CuttlefishConfig::set_memory_mb(int memory_mb) { |
| 216 | (*dictionary_)[kMemoryMb] = memory_mb; |
| 217 | } |
| 218 | |
| 219 | int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); } |
| 220 | void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; } |
| 221 | |
| 222 | int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); } |
| 223 | void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; } |
| 224 | |
| 225 | int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); } |
| 226 | void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; } |
| 227 | |
| 228 | int CuttlefishConfig::refresh_rate_hz() const { |
| 229 | return (*dictionary_)[kRefreshRateHz].asInt(); |
| 230 | } |
| 231 | void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) { |
| 232 | (*dictionary_)[kRefreshRateHz] = refresh_rate_hz; |
| 233 | } |
| 234 | |
| 235 | std::string CuttlefishConfig::kernel_image_path() const { |
| 236 | return (*dictionary_)[kKernelImagePath].asString(); |
| 237 | } |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 238 | |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 239 | void CuttlefishConfig::SetPath(const std::string& key, |
| 240 | const std::string& path) { |
| 241 | if (!path.empty()) { |
| 242 | (*dictionary_)[key] = cvd::AbsolutePath(path); |
| 243 | } |
| 244 | } |
| 245 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 246 | void CuttlefishConfig::set_kernel_image_path( |
| 247 | const std::string& kernel_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 248 | SetPath(kKernelImagePath, kernel_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Cody Schuffelen | e13a78a | 2019-01-29 14:51:26 -0800 | [diff] [blame] | 251 | bool CuttlefishConfig::use_unpacked_kernel() const { |
| 252 | return (*dictionary_)[kUseUnpackedKernel].asBool(); |
| 253 | } |
| 254 | |
| 255 | void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) { |
| 256 | (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel; |
| 257 | } |
| 258 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 259 | bool CuttlefishConfig::decompress_kernel() const { |
| 260 | return (*dictionary_)[kDecompressKernel].asBool(); |
| 261 | } |
| 262 | void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) { |
| 263 | (*dictionary_)[kDecompressKernel] = decompress_kernel; |
| 264 | } |
| 265 | |
| 266 | std::string CuttlefishConfig::decompressed_kernel_image_path() const { |
| 267 | return (*dictionary_)[kDecompressedKernelImagePath].asString(); |
| 268 | } |
| 269 | void CuttlefishConfig::set_decompressed_kernel_image_path( |
| 270 | const std::string& path) { |
| 271 | SetPath(kDecompressedKernelImagePath, path); |
| 272 | } |
| 273 | |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 274 | std::string CuttlefishConfig::gdb_flag() const { |
| 275 | return (*dictionary_)[kGdbFlag].asString(); |
| 276 | } |
| 277 | |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 278 | void CuttlefishConfig::set_gdb_flag(const std::string& device) { |
Matthias Maennich | ac14e1e | 2019-02-06 16:25:42 +0000 | [diff] [blame] | 279 | (*dictionary_)[kGdbFlag] = device; |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 282 | std::string CuttlefishConfig::ramdisk_image_path() const { |
| 283 | return (*dictionary_)[kRamdiskImagePath].asString(); |
| 284 | } |
| 285 | void CuttlefishConfig::set_ramdisk_image_path( |
| 286 | const std::string& ramdisk_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 287 | SetPath(kRamdiskImagePath, ramdisk_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Cody Schuffelen | 9fd9547 | 2019-10-04 13:49:57 -0700 | [diff] [blame] | 290 | std::string CuttlefishConfig::initramfs_path() const { |
| 291 | return (*dictionary_)[kInitramfsPath].asString(); |
| 292 | } |
| 293 | void CuttlefishConfig::set_initramfs_path(const std::string& initramfs_path) { |
| 294 | SetPath(kInitramfsPath, initramfs_path); |
| 295 | } |
| 296 | |
| 297 | std::string CuttlefishConfig::final_ramdisk_path() const { |
| 298 | return (*dictionary_)[kFinalRamdiskPath].asString(); |
| 299 | } |
| 300 | void CuttlefishConfig::set_final_ramdisk_path( |
| 301 | const std::string& final_ramdisk_path) { |
| 302 | SetPath(kFinalRamdiskPath, final_ramdisk_path); |
| 303 | } |
| 304 | |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 305 | std::string CuttlefishConfig::vendor_ramdisk_image_path() const { |
| 306 | return (*dictionary_)[kVendorRamdiskImagePath].asString(); |
| 307 | } |
| 308 | void CuttlefishConfig::set_vendor_ramdisk_image_path( |
| 309 | const std::string& vendor_ramdisk_image_path) { |
| 310 | SetPath(kVendorRamdiskImagePath, vendor_ramdisk_image_path); |
| 311 | } |
| 312 | |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 313 | std::vector<std::string> CuttlefishConfig::virtual_disk_paths() const { |
| 314 | std::vector<std::string> virtual_disks; |
| 315 | auto virtual_disks_json_obj = (*dictionary_)[kVirtualDiskPaths]; |
| 316 | for (const auto& disk : virtual_disks_json_obj) { |
| 317 | virtual_disks.push_back(disk.asString()); |
| 318 | } |
| 319 | return virtual_disks; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 320 | } |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 321 | void CuttlefishConfig::set_virtual_disk_paths( |
| 322 | const std::vector<std::string>& virtual_disk_paths) { |
| 323 | Json::Value virtual_disks_json_obj(Json::arrayValue); |
| 324 | for (const auto& arg : virtual_disk_paths) { |
| 325 | virtual_disks_json_obj.append(arg); |
| 326 | } |
| 327 | (*dictionary_)[kVirtualDiskPaths] = virtual_disks_json_obj; |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 330 | std::string CuttlefishConfig::kernel_log_pipe_name() const { |
Cody Schuffelen | 4a8c1ef | 2019-12-11 17:36:03 -0800 | [diff] [blame] | 331 | return cvd::AbsolutePath(PerInstanceInternalPath("kernel-log-pipe")); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Jorge E. Moreira | 7207f4b | 2019-09-25 10:30:29 -0700 | [diff] [blame] | 334 | std::string CuttlefishConfig::console_pipe_name() const { |
Cody Schuffelen | 4a8c1ef | 2019-12-11 17:36:03 -0800 | [diff] [blame] | 335 | return cvd::AbsolutePath(PerInstanceInternalPath("console-pipe")); |
Jorge E. Moreira | 7207f4b | 2019-09-25 10:30:29 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 338 | bool CuttlefishConfig::deprecated_boot_completed() const { |
| 339 | return (*dictionary_)[kDeprecatedBootCompleted].asBool(); |
| 340 | } |
| 341 | void CuttlefishConfig::set_deprecated_boot_completed( |
| 342 | bool deprecated_boot_completed) { |
| 343 | (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed; |
| 344 | } |
| 345 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 346 | std::string CuttlefishConfig::console_path() const { |
Cody Schuffelen | 4a8c1ef | 2019-12-11 17:36:03 -0800 | [diff] [blame] | 347 | return cvd::AbsolutePath(PerInstancePath("console")); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | std::string CuttlefishConfig::logcat_path() const { |
Cody Schuffelen | 4a8c1ef | 2019-12-11 17:36:03 -0800 | [diff] [blame] | 351 | return cvd::AbsolutePath(PerInstancePath("logcat")); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 354 | std::string CuttlefishConfig::launcher_monitor_socket_path() const { |
Cody Schuffelen | 4a8c1ef | 2019-12-11 17:36:03 -0800 | [diff] [blame] | 355 | return cvd::AbsolutePath(PerInstancePath("launcher_monitor.sock")); |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Jorge E. Moreira | 66f6ec8 | 2018-07-16 16:43:15 -0700 | [diff] [blame] | 358 | std::string CuttlefishConfig::launcher_log_path() const { |
Cody Schuffelen | 4a8c1ef | 2019-12-11 17:36:03 -0800 | [diff] [blame] | 359 | return cvd::AbsolutePath(PerInstancePath("launcher.log")); |
Jorge E. Moreira | 66f6ec8 | 2018-07-16 16:43:15 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 362 | std::string CuttlefishConfig::mobile_bridge_name() const { |
| 363 | return (*dictionary_)[kMobileBridgeName].asString(); |
| 364 | } |
| 365 | void CuttlefishConfig::set_mobile_bridge_name( |
| 366 | const std::string& mobile_bridge_name) { |
| 367 | (*dictionary_)[kMobileBridgeName] = mobile_bridge_name; |
| 368 | } |
| 369 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 370 | std::string CuttlefishConfig::mobile_tap_name() const { |
| 371 | return (*dictionary_)[kMobileTapName].asString(); |
| 372 | } |
| 373 | void CuttlefishConfig::set_mobile_tap_name(const std::string& mobile_tap_name) { |
| 374 | (*dictionary_)[kMobileTapName] = mobile_tap_name; |
| 375 | } |
| 376 | |
Cody Schuffelen | 975175a | 2018-06-14 18:20:02 -0700 | [diff] [blame] | 377 | std::string CuttlefishConfig::wifi_tap_name() const { |
| 378 | return (*dictionary_)[kWifiTapName].asString(); |
| 379 | } |
| 380 | void CuttlefishConfig::set_wifi_tap_name(const std::string& wifi_tap_name) { |
| 381 | (*dictionary_)[kWifiTapName] = wifi_tap_name; |
| 382 | } |
| 383 | |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 384 | int CuttlefishConfig::vsock_guest_cid() const { |
| 385 | return (*dictionary_)[kVsockGuestCid].asInt(); |
| 386 | } |
| 387 | |
| 388 | void CuttlefishConfig::set_vsock_guest_cid(int vsock_guest_cid) { |
| 389 | (*dictionary_)[kVsockGuestCid] = vsock_guest_cid; |
| 390 | } |
| 391 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 392 | std::string CuttlefishConfig::uuid() const { |
| 393 | return (*dictionary_)[kUuid].asString(); |
| 394 | } |
| 395 | void CuttlefishConfig::set_uuid(const std::string& uuid) { |
| 396 | (*dictionary_)[kUuid] = uuid; |
| 397 | } |
| 398 | |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 399 | void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 400 | SetPath(kCuttlefishEnvPath, path); |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 401 | } |
| 402 | std::string CuttlefishConfig::cuttlefish_env_path() const { |
| 403 | return (*dictionary_)[kCuttlefishEnvPath].asString(); |
| 404 | } |
| 405 | |
Cody Schuffelen | b6fa736 | 2019-05-08 11:58:14 -0700 | [diff] [blame] | 406 | static AdbMode stringToAdbMode(std::string mode) { |
| 407 | std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower); |
Cody Schuffelen | d3d6a23 | 2019-11-14 17:28:46 -0800 | [diff] [blame] | 408 | if (mode == "vsock_tunnel") { |
Cody Schuffelen | b6fa736 | 2019-05-08 11:58:14 -0700 | [diff] [blame] | 409 | return AdbMode::VsockTunnel; |
| 410 | } else if (mode == "vsock_half_tunnel") { |
| 411 | return AdbMode::VsockHalfTunnel; |
| 412 | } else if (mode == "native_vsock") { |
| 413 | return AdbMode::NativeVsock; |
Cody Schuffelen | b6fa736 | 2019-05-08 11:58:14 -0700 | [diff] [blame] | 414 | } else { |
| 415 | return AdbMode::Unknown; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | std::set<AdbMode> CuttlefishConfig::adb_mode() const { |
| 420 | std::set<AdbMode> args_set; |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 421 | for (auto& mode : (*dictionary_)[kAdbMode]) { |
Cody Schuffelen | b6fa736 | 2019-05-08 11:58:14 -0700 | [diff] [blame] | 422 | args_set.insert(stringToAdbMode(mode.asString())); |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 423 | } |
| 424 | return args_set; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 427 | void CuttlefishConfig::set_adb_mode(const std::set<std::string>& mode) { |
| 428 | Json::Value mode_json_obj(Json::arrayValue); |
| 429 | for (const auto& arg : mode) { |
| 430 | mode_json_obj.append(arg); |
| 431 | } |
| 432 | (*dictionary_)[kAdbMode] = mode_json_obj; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Cody Schuffelen | e0d0c46 | 2019-09-09 14:13:01 -0700 | [diff] [blame] | 435 | int CuttlefishConfig::host_port() const { |
| 436 | return (*dictionary_)[kHostPort].asInt(); |
| 437 | } |
| 438 | |
| 439 | void CuttlefishConfig::set_host_port(int host_port) { |
| 440 | (*dictionary_)[kHostPort] = host_port; |
| 441 | } |
| 442 | |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 443 | std::string CuttlefishConfig::adb_ip_and_port() const { |
| 444 | return (*dictionary_)[kAdbIPAndPort].asString(); |
| 445 | } |
| 446 | |
| 447 | void CuttlefishConfig::set_adb_ip_and_port(const std::string& ip_port) { |
| 448 | (*dictionary_)[kAdbIPAndPort] = ip_port; |
| 449 | } |
| 450 | |
| 451 | std::string CuttlefishConfig::adb_device_name() const { |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 452 | // TODO(schuffelen): Deal with duplication between here and launch.cc |
Cody Schuffelen | b6fa736 | 2019-05-08 11:58:14 -0700 | [diff] [blame] | 453 | bool vsockTunnel = adb_mode().count(AdbMode::VsockTunnel) > 0; |
| 454 | bool vsockHalfProxy = adb_mode().count(AdbMode::VsockHalfTunnel) > 0; |
| 455 | bool nativeVsock = adb_mode().count(AdbMode::NativeVsock) > 0; |
Cody Schuffelen | d3d6a23 | 2019-11-14 17:28:46 -0800 | [diff] [blame] | 456 | if (vsockTunnel || vsockHalfProxy || nativeVsock) { |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 457 | return adb_ip_and_port(); |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 458 | } |
| 459 | LOG(ERROR) << "no adb_mode found, returning bad device name"; |
| 460 | return "NO_ADB_MODE_SET_NO_VALID_DEVICE_NAME"; |
| 461 | } |
| 462 | |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 463 | std::string CuttlefishConfig::device_title() const { |
| 464 | return (*dictionary_)[kDeviceTitle].asString(); |
| 465 | } |
| 466 | |
| 467 | void CuttlefishConfig::set_device_title(const std::string& title) { |
| 468 | (*dictionary_)[kDeviceTitle] = title; |
| 469 | } |
| 470 | |
Greg Hartman | f1f28c5 | 2018-09-20 17:19:19 -0700 | [diff] [blame] | 471 | std::string CuttlefishConfig::setupwizard_mode() const { |
| 472 | return (*dictionary_)[kSetupWizardMode].asString(); |
| 473 | } |
| 474 | |
| 475 | void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) { |
| 476 | (*dictionary_)[kSetupWizardMode] = mode; |
| 477 | } |
| 478 | |
Cody Schuffelen | 5898003 | 2018-10-11 17:11:13 -0700 | [diff] [blame] | 479 | std::string CuttlefishConfig::qemu_binary() const { |
| 480 | return (*dictionary_)[kQemuBinary].asString(); |
| 481 | } |
| 482 | |
| 483 | void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) { |
| 484 | (*dictionary_)[kQemuBinary] = qemu_binary; |
| 485 | } |
| 486 | |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 487 | std::string CuttlefishConfig::crosvm_binary() const { |
| 488 | return (*dictionary_)[kCrosvmBinary].asString(); |
| 489 | } |
| 490 | |
| 491 | void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) { |
| 492 | (*dictionary_)[kCrosvmBinary] = crosvm_binary; |
| 493 | } |
| 494 | |
Jorge E. Moreira | 13cbd98 | 2019-06-06 16:06:34 -0700 | [diff] [blame] | 495 | std::string CuttlefishConfig::console_forwarder_binary() const { |
| 496 | return (*dictionary_)[kConsoleForwarderBinary].asString(); |
| 497 | } |
| 498 | |
| 499 | void CuttlefishConfig::set_console_forwarder_binary( |
| 500 | const std::string& binary) { |
| 501 | (*dictionary_)[kConsoleForwarderBinary] = binary; |
| 502 | } |
| 503 | |
Cody Schuffelen | 55676ca | 2019-01-28 22:00:05 -0800 | [diff] [blame] | 504 | std::string CuttlefishConfig::kernel_log_monitor_binary() const { |
| 505 | return (*dictionary_)[kKernelLogMonitorBinary].asString(); |
| 506 | } |
| 507 | |
| 508 | void CuttlefishConfig::set_kernel_log_monitor_binary( |
| 509 | const std::string& kernel_log_monitor_binary) { |
| 510 | (*dictionary_)[kKernelLogMonitorBinary] = kernel_log_monitor_binary; |
| 511 | } |
| 512 | |
Cody Schuffelen | 17b3472 | 2019-01-28 22:57:14 -0800 | [diff] [blame] | 513 | bool CuttlefishConfig::enable_vnc_server() const { |
| 514 | return (*dictionary_)[kEnableVncServer].asBool(); |
| 515 | } |
| 516 | |
| 517 | void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) { |
| 518 | (*dictionary_)[kEnableVncServer] = enable_vnc_server; |
| 519 | } |
| 520 | |
| 521 | std::string CuttlefishConfig::vnc_server_binary() const { |
| 522 | return (*dictionary_)[kVncServerBinary].asString(); |
| 523 | } |
| 524 | |
| 525 | void CuttlefishConfig::set_vnc_server_binary( |
| 526 | const std::string& vnc_server_binary) { |
| 527 | (*dictionary_)[kVncServerBinary] = vnc_server_binary; |
| 528 | } |
| 529 | |
| 530 | int CuttlefishConfig::vnc_server_port() const { |
| 531 | return (*dictionary_)[kVncServerPort].asInt(); |
| 532 | } |
| 533 | |
| 534 | void CuttlefishConfig::set_vnc_server_port(int vnc_server_port) { |
| 535 | (*dictionary_)[kVncServerPort] = vnc_server_port; |
| 536 | } |
| 537 | |
Cody Schuffelen | d12e0c9 | 2019-01-29 15:44:45 -0800 | [diff] [blame] | 538 | bool CuttlefishConfig::restart_subprocesses() const { |
| 539 | return (*dictionary_)[kRestartSubprocesses].asBool(); |
| 540 | } |
| 541 | |
| 542 | void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) { |
| 543 | (*dictionary_)[kRestartSubprocesses] = restart_subprocesses; |
| 544 | } |
| 545 | |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 546 | bool CuttlefishConfig::run_adb_connector() const { |
Jorge E. Moreira | b86922b | 2019-02-04 10:52:58 -0800 | [diff] [blame] | 547 | return (*dictionary_)[kRunAdbConnector].asBool(); |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) { |
| 551 | (*dictionary_)[kRunAdbConnector] = run_adb_connector; |
| 552 | } |
| 553 | |
| 554 | std::string CuttlefishConfig::adb_connector_binary() const { |
| 555 | return (*dictionary_)[kAdbConnectorBinary].asString(); |
| 556 | } |
| 557 | |
| 558 | void CuttlefishConfig::set_adb_connector_binary( |
| 559 | const std::string& adb_connector_binary) { |
| 560 | (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary; |
| 561 | } |
| 562 | |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 563 | std::string CuttlefishConfig::socket_vsock_proxy_binary() const { |
| 564 | return (*dictionary_)[kSocketVsockProxyBinary].asString(); |
| 565 | } |
| 566 | |
| 567 | void CuttlefishConfig::set_socket_vsock_proxy_binary( |
| 568 | const std::string& socket_vsock_proxy_binary) { |
| 569 | (*dictionary_)[kSocketVsockProxyBinary] = socket_vsock_proxy_binary; |
| 570 | } |
| 571 | |
Cody Schuffelen | 5024973 | 2019-01-29 16:42:55 -0800 | [diff] [blame] | 572 | bool CuttlefishConfig::run_as_daemon() const { |
| 573 | return (*dictionary_)[kRunAsDaemon].asBool(); |
| 574 | } |
| 575 | |
| 576 | void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) { |
| 577 | (*dictionary_)[kRunAsDaemon] = run_as_daemon; |
| 578 | } |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 579 | std::string CuttlefishConfig::data_policy() const { |
| 580 | return (*dictionary_)[kDataPolicy].asString(); |
| 581 | } |
| 582 | |
| 583 | void CuttlefishConfig::set_data_policy(const std::string& data_policy) { |
| 584 | (*dictionary_)[kDataPolicy] = data_policy; |
| 585 | } |
| 586 | |
| 587 | int CuttlefishConfig::blank_data_image_mb() const { |
Cody Schuffelen | e3269ed | 2019-02-11 18:38:42 -0800 | [diff] [blame] | 588 | return (*dictionary_)[kBlankDataImageMb].asInt(); |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) { |
| 592 | (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb; |
| 593 | } |
| 594 | |
| 595 | std::string CuttlefishConfig::blank_data_image_fmt() const { |
| 596 | return (*dictionary_)[kBlankDataImageFmt].asString(); |
| 597 | } |
| 598 | |
| 599 | void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) { |
| 600 | (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt; |
| 601 | } |
| 602 | |
Cody Schuffelen | 5de4865 | 2019-09-16 19:49:52 +0000 | [diff] [blame] | 603 | |
| 604 | void CuttlefishConfig::set_logcat_mode(const std::string& mode) { |
| 605 | (*dictionary_)[kLogcatMode] = mode; |
| 606 | } |
| 607 | |
| 608 | std::string CuttlefishConfig::logcat_mode() const { |
| 609 | return (*dictionary_)[kLogcatMode].asString(); |
| 610 | } |
| 611 | |
Cody Schuffelen | 5de4865 | 2019-09-16 19:49:52 +0000 | [diff] [blame] | 612 | void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) { |
| 613 | SetPath(kLogcatReceiverBinary, binary); |
| 614 | } |
| 615 | |
| 616 | std::string CuttlefishConfig::logcat_receiver_binary() const { |
| 617 | return (*dictionary_)[kLogcatReceiverBinary].asString(); |
| 618 | } |
| 619 | |
Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 620 | void CuttlefishConfig::set_config_server_binary(const std::string& binary) { |
| 621 | SetPath(kConfigServerBinary, binary); |
| 622 | } |
| 623 | |
| 624 | std::string CuttlefishConfig::config_server_binary() const { |
| 625 | return (*dictionary_)[kConfigServerBinary].asString(); |
| 626 | } |
| 627 | |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 628 | bool CuttlefishConfig::enable_tombstone_receiver() const { |
| 629 | return (*dictionary_)[kRunTombstoneReceiver].asBool(); |
| 630 | } |
| 631 | |
| 632 | void CuttlefishConfig::set_enable_tombstone_receiver(bool enable_tombstone_receiver) { |
| 633 | (*dictionary_)[kRunTombstoneReceiver] = enable_tombstone_receiver; |
| 634 | } |
| 635 | |
| 636 | std::string CuttlefishConfig::tombstone_receiver_binary() const { |
| 637 | return (*dictionary_)[kTombstoneReceiverBinary].asString(); |
| 638 | } |
| 639 | |
| 640 | void CuttlefishConfig::set_tombstone_receiver_binary(const std::string& e2e_test_binary) { |
| 641 | (*dictionary_)[kTombstoneReceiverBinary] = e2e_test_binary; |
| 642 | } |
| 643 | |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 644 | bool CuttlefishConfig::use_bootloader() const { |
| 645 | return (*dictionary_)[kUseBootloader].asBool(); |
| 646 | } |
| 647 | |
| 648 | void CuttlefishConfig::set_use_bootloader(bool use_bootloader) { |
| 649 | (*dictionary_)[kUseBootloader] = use_bootloader; |
| 650 | } |
| 651 | |
| 652 | std::string CuttlefishConfig::bootloader() const { |
| 653 | return (*dictionary_)[kBootloader].asString(); |
| 654 | } |
| 655 | |
| 656 | void CuttlefishConfig::set_bootloader(const std::string& bootloader) { |
| 657 | SetPath(kBootloader, bootloader); |
| 658 | } |
| 659 | |
David Anderson | 637bfd6 | 2019-09-26 13:23:21 -0700 | [diff] [blame] | 660 | void CuttlefishConfig::set_boot_slot(const std::string& boot_slot) { |
| 661 | (*dictionary_)[kBootSlot] = boot_slot; |
| 662 | } |
| 663 | |
| 664 | std::string CuttlefishConfig::boot_slot() const { |
| 665 | return (*dictionary_)[kBootSlot].asString(); |
| 666 | } |
| 667 | |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 668 | std::string CuttlefishConfig::touch_socket_path() const { |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 669 | return PerInstanceInternalPath("touch.sock"); |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | std::string CuttlefishConfig::keyboard_socket_path() const { |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 673 | return PerInstanceInternalPath("keyboard.sock"); |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 676 | void CuttlefishConfig::set_loop_max_part(int loop_max_part) { |
| 677 | (*dictionary_)[kLoopMaxPart] = loop_max_part; |
| 678 | } |
| 679 | int CuttlefishConfig::loop_max_part() const { |
| 680 | return (*dictionary_)[kLoopMaxPart].asInt(); |
| 681 | } |
| 682 | |
| 683 | void CuttlefishConfig::set_guest_enforce_security(bool guest_enforce_security) { |
| 684 | (*dictionary_)[kGuestEnforceSecurity] = guest_enforce_security; |
| 685 | } |
| 686 | bool CuttlefishConfig::guest_enforce_security() const { |
| 687 | return (*dictionary_)[kGuestEnforceSecurity].asBool(); |
| 688 | } |
| 689 | |
| 690 | void CuttlefishConfig::set_guest_audit_security(bool guest_audit_security) { |
| 691 | (*dictionary_)[kGuestAuditSecurity] = guest_audit_security; |
| 692 | } |
| 693 | bool CuttlefishConfig::guest_audit_security() const { |
| 694 | return (*dictionary_)[kGuestAuditSecurity].asBool(); |
| 695 | } |
| 696 | |
| 697 | void CuttlefishConfig::set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline) { |
| 698 | Json::Value args_json_obj(Json::arrayValue); |
| 699 | for (const auto& arg : android::base::Split(boot_image_kernel_cmdline, " ")) { |
| 700 | args_json_obj.append(arg); |
| 701 | } |
| 702 | (*dictionary_)[kBootImageKernelCmdline] = args_json_obj; |
| 703 | } |
| 704 | std::vector<std::string> CuttlefishConfig::boot_image_kernel_cmdline() const { |
| 705 | std::vector<std::string> cmdline; |
| 706 | for (const Json::Value& arg : (*dictionary_)[kBootImageKernelCmdline]) { |
| 707 | cmdline.push_back(arg.asString()); |
| 708 | } |
| 709 | return cmdline; |
| 710 | } |
| 711 | |
| 712 | void CuttlefishConfig::set_extra_kernel_cmdline(std::string extra_cmdline) { |
| 713 | Json::Value args_json_obj(Json::arrayValue); |
| 714 | for (const auto& arg : android::base::Split(extra_cmdline, " ")) { |
| 715 | args_json_obj.append(arg); |
| 716 | } |
| 717 | (*dictionary_)[kExtraKernelCmdline] = extra_cmdline; |
| 718 | } |
| 719 | std::vector<std::string> CuttlefishConfig::extra_kernel_cmdline() const { |
| 720 | std::vector<std::string> cmdline; |
| 721 | for (const Json::Value& arg : (*dictionary_)[kExtraKernelCmdline]) { |
| 722 | cmdline.push_back(arg.asString()); |
| 723 | } |
| 724 | return cmdline; |
| 725 | } |
| 726 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 727 | // Creates the (initially empty) config object and populates it with values from |
Cody Schuffelen | f219a16 | 2018-10-11 19:01:57 -0700 | [diff] [blame] | 728 | // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present. |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 729 | // Returns nullptr if there was an error loading from file |
| 730 | /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() { |
Jorge E. Moreira | a00584e | 2018-10-25 15:07:52 -0700 | [diff] [blame] | 731 | auto config_file_path = cvd::StringFromEnv(kCuttlefishConfigEnvVarName, |
| 732 | vsoc::GetGlobalConfigFileLink()); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 733 | auto ret = new CuttlefishConfig(); |
Jorge E. Moreira | a00584e | 2018-10-25 15:07:52 -0700 | [diff] [blame] | 734 | if (ret) { |
| 735 | auto loaded = ret->LoadFromFile(config_file_path.c_str()); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 736 | if (!loaded) { |
Cody Schuffelen | f219a16 | 2018-10-11 19:01:57 -0700 | [diff] [blame] | 737 | delete ret; |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 738 | return nullptr; |
| 739 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 740 | } |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 741 | return ret; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Cody Schuffelen | 1fcdbc0 | 2019-10-04 14:02:23 -0700 | [diff] [blame] | 744 | /*static*/ const CuttlefishConfig* CuttlefishConfig::Get() { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 745 | static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl()); |
| 746 | return config.get(); |
| 747 | } |
| 748 | |
| 749 | CuttlefishConfig::CuttlefishConfig() : dictionary_(new Json::Value()) {} |
| 750 | // Can't use '= default' on the header because the compiler complains of |
| 751 | // Json::Value being an incomplete type |
| 752 | CuttlefishConfig::~CuttlefishConfig() {} |
| 753 | |
| 754 | bool CuttlefishConfig::LoadFromFile(const char* file) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 755 | auto real_file_path = cvd::AbsolutePath(file); |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 756 | if (real_file_path.empty()) { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 757 | LOG(ERROR) << "Could not get real path for file " << file; |
| 758 | return false; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 759 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 760 | Json::Reader reader; |
| 761 | std::ifstream ifs(real_file_path); |
| 762 | if (!reader.parse(ifs, *dictionary_)) { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 763 | LOG(ERROR) << "Could not read config file " << file << ": " |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 764 | << reader.getFormattedErrorMessages(); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 765 | return false; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 766 | } |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 767 | return true; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 768 | } |
| 769 | bool CuttlefishConfig::SaveToFile(const std::string& file) const { |
| 770 | std::ofstream ofs(file); |
| 771 | if (!ofs.is_open()) { |
| 772 | LOG(ERROR) << "Unable to write to file " << file; |
| 773 | return false; |
| 774 | } |
| 775 | ofs << *dictionary_; |
| 776 | return !ofs.fail(); |
| 777 | } |
| 778 | |
| 779 | std::string CuttlefishConfig::PerInstancePath(const char* file_name) const { |
| 780 | return (instance_dir() + "/") + file_name; |
| 781 | } |
| 782 | |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 783 | std::string CuttlefishConfig::PerInstanceInternalPath( |
| 784 | const char* file_name) const { |
| 785 | if (file_name[0] == '\0') { |
| 786 | // Don't append a / if file_name is empty. |
| 787 | return PerInstancePath(kInternalDirName); |
| 788 | } |
| 789 | auto relative_path = (std::string(kInternalDirName) + "/") + file_name; |
| 790 | return PerInstancePath(relative_path.c_str()); |
| 791 | } |
| 792 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 793 | std::string CuttlefishConfig::instance_name() const { |
| 794 | return GetPerInstanceDefault("cvd-"); |
| 795 | } |
| 796 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 797 | int GetInstance() { |
| 798 | static int instance_id = InstanceFromEnvironment(); |
| 799 | return instance_id; |
| 800 | } |
| 801 | |
Jorge E. Moreira | 015c000 | 2018-07-19 11:01:16 -0700 | [diff] [blame] | 802 | std::string GetGlobalConfigFileLink() { |
| 803 | return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json"; |
| 804 | } |
| 805 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 806 | std::string GetPerInstanceDefault(const char* prefix) { |
| 807 | std::ostringstream stream; |
| 808 | stream << prefix << std::setfill('0') << std::setw(2) << GetInstance(); |
| 809 | return stream.str(); |
| 810 | } |
| 811 | int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; } |
| 812 | |
| 813 | std::string GetDefaultPerInstanceDir() { |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 814 | std::ostringstream stream; |
Jorge E. Moreira | d966837 | 2019-01-28 22:40:42 -0800 | [diff] [blame] | 815 | stream << std::getenv("HOME") << "/cuttlefish_runtime"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 816 | return stream.str(); |
| 817 | } |
| 818 | |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 819 | int GetDefaultPerInstanceVsockCid() { |
| 820 | constexpr int kFirstGuestCid = 3; |
| 821 | return vsoc::HostSupportsVsock() ? GetPerInstanceDefault(kFirstGuestCid) : 0; |
| 822 | } |
| 823 | |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 824 | std::string DefaultHostArtifactsPath(const std::string& file_name) { |
| 825 | return (cvd::StringFromEnv("ANDROID_HOST_OUT", |
| 826 | cvd::StringFromEnv("HOME", ".")) + |
| 827 | "/") + |
| 828 | file_name; |
| 829 | } |
| 830 | |
| 831 | std::string DefaultGuestImagePath(const std::string& file_name) { |
| 832 | return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT", |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 833 | cvd::StringFromEnv("HOME", ".")) + |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 834 | "/") + |
| 835 | file_name; |
| 836 | } |
| 837 | |
| 838 | bool HostSupportsQemuCli() { |
| 839 | static bool supported = |
| 840 | std::system( |
| 841 | "/usr/lib/cuttlefish-common/bin/capability_query.py qemu_cli") == 0; |
| 842 | return supported; |
| 843 | } |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 844 | |
| 845 | bool HostSupportsVsock() { |
| 846 | static bool supported = |
| 847 | std::system( |
| 848 | "/usr/lib/cuttlefish-common/bin/capability_query.py vsock") == 0; |
| 849 | return supported; |
| 850 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 851 | } // namespace vsoc |