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 19 #include <algorithm> 20 #include <climits> 21 #include <cstdlib> 22 #include <cstring> 23 #include <fstream> 24 #include <iomanip> 25 #include <iterator> 26 #include <sstream> 27 #include <string> 28 29 #include <glog/logging.h> 30 #include <json/json.h> 31 32 #include "common/libs/utils/environment.h" 33 #include "common/libs/utils/files.h" 34 #include "host/libs/vm_manager/qemu_manager.h" 35 36 37 namespace { 38 39 int InstanceFromEnvironment() { 40 static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE"; 41 static constexpr int kDefaultInstance = 1; 42 43 // CUTTLEFISH_INSTANCE environment variable 44 const char* instance_str = std::getenv(kInstanceEnvironmentVariable); 45 if (!instance_str) { 46 // Try to get it from the user instead 47 instance_str = std::getenv("USER"); 48 49 if (!instance_str || std::strncmp(instance_str, vsoc::kVsocUserPrefix, 50 sizeof(vsoc::kVsocUserPrefix) - 1)) { 51 // No user or we don't recognize this user 52 LOG(WARNING) << "No user or non-vsoc user, returning default config"; 53 return kDefaultInstance; 54 } 55 instance_str += sizeof(vsoc::kVsocUserPrefix) - 1; 56 57 // Set the environment variable so that child processes see it 58 setenv(kInstanceEnvironmentVariable, instance_str, 0); 59 } 60 61 int instance = std::atoi(instance_str); 62 if (instance <= 0) { 63 instance = kDefaultInstance; 64 } 65 66 return instance; 67 } 68 69 const char* kSerialNumber = "serial_number"; 70 const char* kInstanceDir = "instance_dir"; 71 const char* kVmManager = "vm_manager"; 72 const char* kHardwareName = "hardware_name"; 73 const char* kDeviceTitle = "device_title"; 74 75 const char* kCpus = "cpus"; 76 const char* kMemoryMb = "memory_mb"; 77 const char* kDpi = "dpi"; 78 const char* kXRes = "x_res"; 79 const char* kYRes = "y_res"; 80 const char* kNumScreenBuffers = "num_screen_buffers"; 81 const char* kRefreshRateHz = "refresh_rate_hz"; 82 83 const char* kKernelImagePath = "kernel_image_path"; 84 const char* kUseUnpackedKernel = "use_unpacked_kernel"; 85 const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path"; 86 const char* kDecompressKernel = "decompress_kernel"; 87 const char* kGdbFlag = "gdb_flag"; 88 const char* kKernelCmdline = "kernel_cmdline"; 89 const char* kRamdiskImagePath = "ramdisk_image_path"; 90 91 const char* kSystemImagePath = "system_image_path"; 92 const char* kCacheImagePath = "cache_image_path"; 93 const char* kDataImagePath = "data_image_path"; 94 const char* kVendorImagePath = "vendor_image_path"; 95 const char* kMetadataImagePath = "metadata_image_path"; 96 const char* kProductImagePath = "product_image_path"; 97 const char* kSuperImagePath = "super_image_path"; 98 const char* kUsbV1SocketName = "usb_v1_socket_name"; 99 const char* kVhciPort = "vhci_port"; 100 const char* kUsbIpSocketName = "usb_ip_socket_name"; 101 const char* kKernelLogSocketName = "kernel_log_socket_name"; 102 const char* kDeprecatedBootCompleted = "deprecated_boot_completed"; 103 const char* kConsolePath = "console_path"; 104 const char* kLogcatPath = "logcat_path"; 105 const char* kLauncherLogPath = "launcher_log_path"; 106 const char* kLauncherMonitorPath = "launcher_monitor_socket"; 107 const char* kDtbPath = "dtb_path"; 108 const char* kGsiFstabPath = "gsi.fstab_path"; 109 110 const char* kMempath = "mempath"; 111 const char* kIvshmemQemuSocketPath = "ivshmem_qemu_socket_path"; 112 const char* kIvshmemClientSocketPath = "ivshmem_client_socket_path"; 113 const char* kIvshmemVectorCount = "ivshmem_vector_count"; 114 115 const char* kMobileBridgeName = "mobile_bridge_name"; 116 const char* kMobileTapName = "mobile_tap_name"; 117 const char* kWifiTapName = "wifi_tap_name"; 118 const char* kWifiGuestMacAddr = "wifi_guest_mac_addr"; 119 const char* kWifiHostMacAddr = "wifi_host_mac_addr"; 120 const char* kEntropySource = "entropy_source"; 121 const char* kVsockGuestCid = "vsock_guest_cid"; 122 123 const char* kUuid = "uuid"; 124 const char* kCuttlefishEnvPath = "cuttlefish_env_path"; 125 126 const char* kAdbMode = "adb_mode"; 127 const char* kAdbIPAndPort = "adb_ip_and_port"; 128 const char* kSetupWizardMode = "setupwizard_mode"; 129 130 const char* kQemuBinary = "qemu_binary"; 131 const char* kCrosvmBinary = "crosvm_binary"; 132 const char* kIvServerBinary = "ivserver_binary"; 133 const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary"; 134 135 const char* kEnableVncServer = "enable_vnc_server"; 136 const char* kVncServerBinary = "vnc_server_binary"; 137 const char* kVncServerPort = "vnc_server_port"; 138 139 const char* kEnableStreamAudio = "enable_stream_audio"; 140 const char* kStreamAudioBinary = "stream_audio_binary"; 141 const char* kStreamAudioPort = "stream_audio_port"; 142 143 const char* kRestartSubprocesses = "restart_subprocesses"; 144 const char* kRunAdbConnector = "run_adb_connector"; 145 const char* kAdbConnectorBinary = "adb_connector_binary"; 146 const char* kVirtualUsbManagerBinary = "virtual_usb_manager_binary"; 147 const char* kSocketForwardProxyBinary = "socket_forward_proxy_binary"; 148 const char* kSocketVsockProxyBinary = "socket_vsock_proxy_binary"; 149 150 const char* kRunAsDaemon = "run_as_daemon"; 151 const char* kRunE2eTest = "run_e2e_test"; 152 const char* kE2eTestBinary = "e2e_test_binary"; 153 154 const char* kDataPolicy = "data_policy"; 155 const char* kBlankDataImageMb = "blank_data_image_mb"; 156 const char* kBlankDataImageFmt = "blank_data_image_fmt"; 157 158 const char* kLogcatMode = "logcat_mode"; 159 const char* kLogcatVsockPort = "logcat_vsock_port"; 160 const char* kFramesVsockPort = "frames_vsock_port"; 161 const char* kLogcatReceiverBinary = "logcat_receiver_binary"; 162 } // namespace 163 164 namespace vsoc { 165 166 std::string CuttlefishConfig::instance_dir() const { 167 return (*dictionary_)[kInstanceDir].asString(); 168 } 169 void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) { 170 (*dictionary_)[kInstanceDir] = instance_dir; 171 } 172 173 std::string CuttlefishConfig::vm_manager() const { 174 return (*dictionary_)[kVmManager].asString(); 175 } 176 void CuttlefishConfig::set_vm_manager(const std::string& name) { 177 (*dictionary_)[kVmManager] = name; 178 } 179 180 std::string CuttlefishConfig::hardware_name() const { 181 return (*dictionary_)[kHardwareName].asString(); 182 } 183 void CuttlefishConfig::set_hardware_name(const std::string& name) { 184 (*dictionary_)[kHardwareName] = name; 185 } 186 187 std::string CuttlefishConfig::serial_number() const { 188 return (*dictionary_)[kSerialNumber].asString(); 189 } 190 void CuttlefishConfig::set_serial_number(const std::string& serial_number) { 191 (*dictionary_)[kSerialNumber] = serial_number; 192 } 193 194 int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); } 195 void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; } 196 197 int CuttlefishConfig::memory_mb() const { 198 return (*dictionary_)[kMemoryMb].asInt(); 199 } 200 void CuttlefishConfig::set_memory_mb(int memory_mb) { 201 (*dictionary_)[kMemoryMb] = memory_mb; 202 } 203 204 int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); } 205 void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; } 206 207 int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); } 208 void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; } 209 210 int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); } 211 void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; } 212 213 int CuttlefishConfig::num_screen_buffers() const { 214 return (*dictionary_)[kNumScreenBuffers].asInt(); 215 } 216 void CuttlefishConfig::set_num_screen_buffers(int num_screen_buffers) { 217 (*dictionary_)[kNumScreenBuffers] = num_screen_buffers; 218 } 219 220 int CuttlefishConfig::refresh_rate_hz() const { 221 return (*dictionary_)[kRefreshRateHz].asInt(); 222 } 223 void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) { 224 (*dictionary_)[kRefreshRateHz] = refresh_rate_hz; 225 } 226 227 std::string CuttlefishConfig::kernel_image_path() const { 228 return (*dictionary_)[kKernelImagePath].asString(); 229 } 230 231 void CuttlefishConfig::SetPath(const std::string& key, 232 const std::string& path) { 233 if (!path.empty()) { 234 (*dictionary_)[key] = cvd::AbsolutePath(path); 235 } 236 } 237 238 void CuttlefishConfig::set_kernel_image_path( 239 const std::string& kernel_image_path) { 240 SetPath(kKernelImagePath, kernel_image_path); 241 } 242 243 bool CuttlefishConfig::use_unpacked_kernel() const { 244 return (*dictionary_)[kUseUnpackedKernel].asBool(); 245 } 246 247 void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) { 248 (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel; 249 } 250 251 bool CuttlefishConfig::decompress_kernel() const { 252 return (*dictionary_)[kDecompressKernel].asBool(); 253 } 254 void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) { 255 (*dictionary_)[kDecompressKernel] = decompress_kernel; 256 } 257 258 std::string CuttlefishConfig::decompressed_kernel_image_path() const { 259 return (*dictionary_)[kDecompressedKernelImagePath].asString(); 260 } 261 void CuttlefishConfig::set_decompressed_kernel_image_path( 262 const std::string& path) { 263 SetPath(kDecompressedKernelImagePath, path); 264 } 265 266 std::string CuttlefishConfig::gdb_flag() const { 267 return (*dictionary_)[kGdbFlag].asString(); 268 } 269 270 void CuttlefishConfig::set_gdb_flag(const std::string& device) { 271 (*dictionary_)[kGdbFlag] = device; 272 } 273 274 std::set<std::string> CuttlefishConfig::kernel_cmdline() const { 275 std::set<std::string> args_set; 276 auto args_json_obj = (*dictionary_)[kKernelCmdline]; 277 std::transform(args_json_obj.begin(), args_json_obj.end(), 278 std::inserter(args_set, args_set.begin()), 279 [](const Json::Value& it) { return it.asString(); }); 280 return args_set; 281 } 282 void CuttlefishConfig::set_kernel_cmdline( 283 const std::set<std::string>& kernel_cmdline) { 284 Json::Value args_json_obj(Json::arrayValue); 285 for (const auto& arg : kernel_cmdline) { 286 args_json_obj.append(arg); 287 } 288 (*dictionary_)[kKernelCmdline] = args_json_obj; 289 } 290 void CuttlefishConfig::add_kernel_cmdline( 291 const std::set<std::string>& extra_args) { 292 std::set<std::string> cmdline = kernel_cmdline(); 293 for (const auto& arg : extra_args) { 294 if (cmdline.count(arg)) { 295 LOG(ERROR) << "Kernel argument " << arg << " is duplicated"; 296 } 297 cmdline.insert(arg); 298 } 299 set_kernel_cmdline(cmdline); 300 } 301 void CuttlefishConfig::add_kernel_cmdline(const std::string& kernel_cmdline) { 302 std::stringstream args_stream(kernel_cmdline); 303 std::set<std::string> kernel_cmdline_set; 304 using is_iter = std::istream_iterator<std::string>; 305 std::copy(is_iter(args_stream), is_iter(), 306 std::inserter(kernel_cmdline_set, kernel_cmdline_set.begin())); 307 add_kernel_cmdline(kernel_cmdline_set); 308 } 309 std::string CuttlefishConfig::kernel_cmdline_as_string() const { 310 auto args_set = kernel_cmdline(); 311 std::stringstream output; 312 std::copy(args_set.begin(), args_set.end(), 313 std::ostream_iterator<std::string>(output, " ")); 314 return output.str(); 315 } 316 317 std::string CuttlefishConfig::ramdisk_image_path() const { 318 return (*dictionary_)[kRamdiskImagePath].asString(); 319 } 320 void CuttlefishConfig::set_ramdisk_image_path( 321 const std::string& ramdisk_image_path) { 322 SetPath(kRamdiskImagePath, ramdisk_image_path); 323 } 324 325 std::string CuttlefishConfig::system_image_path() const { 326 return (*dictionary_)[kSystemImagePath].asString(); 327 } 328 void CuttlefishConfig::set_system_image_path( 329 const std::string& system_image_path) { 330 SetPath(kSystemImagePath, system_image_path); 331 } 332 333 std::string CuttlefishConfig::cache_image_path() const { 334 return (*dictionary_)[kCacheImagePath].asString(); 335 } 336 void CuttlefishConfig::set_cache_image_path( 337 const std::string& cache_image_path) { 338 SetPath(kCacheImagePath, cache_image_path); 339 } 340 341 std::string CuttlefishConfig::data_image_path() const { 342 return (*dictionary_)[kDataImagePath].asString(); 343 } 344 void CuttlefishConfig::set_data_image_path(const std::string& data_image_path) { 345 SetPath(kDataImagePath, data_image_path); 346 } 347 348 std::string CuttlefishConfig::vendor_image_path() const { 349 return (*dictionary_)[kVendorImagePath].asString(); 350 } 351 void CuttlefishConfig::set_vendor_image_path( 352 const std::string& vendor_image_path) { 353 SetPath(kVendorImagePath, vendor_image_path); 354 } 355 356 std::string CuttlefishConfig::metadata_image_path() const { 357 return (*dictionary_)[kMetadataImagePath].asString(); 358 } 359 void CuttlefishConfig::set_metadata_image_path( 360 const std::string& metadata_image_path) { 361 SetPath(kMetadataImagePath, metadata_image_path); 362 } 363 364 std::string CuttlefishConfig::super_image_path() const { 365 return (*dictionary_)[kSuperImagePath].asString(); 366 } 367 void CuttlefishConfig::set_super_image_path( 368 const std::string& super_image_path) { 369 SetPath(kSuperImagePath, super_image_path); 370 } 371 372 std::string CuttlefishConfig::product_image_path() const { 373 return (*dictionary_)[kProductImagePath].asString(); 374 } 375 void CuttlefishConfig::set_product_image_path( 376 const std::string& product_image_path) { 377 SetPath(kProductImagePath, product_image_path); 378 } 379 380 std::string CuttlefishConfig::dtb_path() const { 381 return (*dictionary_)[kDtbPath].asString(); 382 } 383 void CuttlefishConfig::set_dtb_path(const std::string& dtb_path) { 384 SetPath(kDtbPath, dtb_path); 385 } 386 387 std::string CuttlefishConfig::gsi_fstab_path() const { 388 return (*dictionary_)[kGsiFstabPath].asString(); 389 } 390 void CuttlefishConfig::set_gsi_fstab_path(const std::string& path){ 391 SetPath(kGsiFstabPath, path); 392 } 393 394 std::string CuttlefishConfig::mempath() const { 395 return (*dictionary_)[kMempath].asString(); 396 } 397 void CuttlefishConfig::set_mempath(const std::string& mempath) { 398 SetPath(kMempath, mempath); 399 } 400 401 std::string CuttlefishConfig::ivshmem_qemu_socket_path() const { 402 return (*dictionary_)[kIvshmemQemuSocketPath].asString(); 403 } 404 void CuttlefishConfig::set_ivshmem_qemu_socket_path( 405 const std::string& ivshmem_qemu_socket_path) { 406 SetPath(kIvshmemQemuSocketPath, ivshmem_qemu_socket_path); 407 } 408 409 std::string CuttlefishConfig::ivshmem_client_socket_path() const { 410 return (*dictionary_)[kIvshmemClientSocketPath].asString(); 411 } 412 void CuttlefishConfig::set_ivshmem_client_socket_path( 413 const std::string& ivshmem_client_socket_path) { 414 SetPath(kIvshmemClientSocketPath, ivshmem_client_socket_path); 415 } 416 417 int CuttlefishConfig::ivshmem_vector_count() const { 418 return (*dictionary_)[kIvshmemVectorCount].asInt(); 419 } 420 void CuttlefishConfig::set_ivshmem_vector_count(int ivshmem_vector_count) { 421 (*dictionary_)[kIvshmemVectorCount] = ivshmem_vector_count; 422 } 423 424 std::string CuttlefishConfig::usb_v1_socket_name() const { 425 return (*dictionary_)[kUsbV1SocketName].asString(); 426 } 427 void CuttlefishConfig::set_usb_v1_socket_name( 428 const std::string& usb_v1_socket_name) { 429 (*dictionary_)[kUsbV1SocketName] = usb_v1_socket_name; 430 } 431 432 int CuttlefishConfig::vhci_port() const { 433 return (*dictionary_)[kVhciPort].asInt(); 434 } 435 void CuttlefishConfig::set_vhci_port(int vhci_port) { 436 (*dictionary_)[kVhciPort] = vhci_port; 437 } 438 439 std::string CuttlefishConfig::usb_ip_socket_name() const { 440 return (*dictionary_)[kUsbIpSocketName].asString(); 441 } 442 void CuttlefishConfig::set_usb_ip_socket_name( 443 const std::string& usb_ip_socket_name) { 444 (*dictionary_)[kUsbIpSocketName] = usb_ip_socket_name; 445 } 446 447 std::string CuttlefishConfig::kernel_log_socket_name() const { 448 return (*dictionary_)[kKernelLogSocketName].asString(); 449 } 450 void CuttlefishConfig::set_kernel_log_socket_name( 451 const std::string& kernel_log_socket_name) { 452 (*dictionary_)[kKernelLogSocketName] = kernel_log_socket_name; 453 } 454 455 bool CuttlefishConfig::deprecated_boot_completed() const { 456 return (*dictionary_)[kDeprecatedBootCompleted].asBool(); 457 } 458 void CuttlefishConfig::set_deprecated_boot_completed( 459 bool deprecated_boot_completed) { 460 (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed; 461 } 462 463 std::string CuttlefishConfig::console_path() const { 464 return (*dictionary_)[kConsolePath].asString(); 465 } 466 void CuttlefishConfig::set_console_path(const std::string& console_path) { 467 SetPath(kConsolePath, console_path); 468 } 469 470 std::string CuttlefishConfig::logcat_path() const { 471 return (*dictionary_)[kLogcatPath].asString(); 472 } 473 void CuttlefishConfig::set_logcat_path(const std::string& logcat_path) { 474 SetPath(kLogcatPath, logcat_path); 475 } 476 477 std::string CuttlefishConfig::launcher_monitor_socket_path() const { 478 return (*dictionary_)[kLauncherMonitorPath].asString(); 479 } 480 void CuttlefishConfig::set_launcher_monitor_socket_path( 481 const std::string& launcher_monitor_path) { 482 SetPath(kLauncherMonitorPath, launcher_monitor_path); 483 } 484 485 std::string CuttlefishConfig::launcher_log_path() const { 486 return (*dictionary_)[kLauncherLogPath].asString(); 487 } 488 void CuttlefishConfig::set_launcher_log_path( 489 const std::string& launcher_log_path) { 490 (*dictionary_)[kLauncherLogPath] = launcher_log_path; 491 } 492 493 std::string CuttlefishConfig::mobile_bridge_name() const { 494 return (*dictionary_)[kMobileBridgeName].asString(); 495 } 496 void CuttlefishConfig::set_mobile_bridge_name( 497 const std::string& mobile_bridge_name) { 498 (*dictionary_)[kMobileBridgeName] = mobile_bridge_name; 499 } 500 501 std::string CuttlefishConfig::wifi_guest_mac_addr() const { 502 return (*dictionary_)[kWifiGuestMacAddr].asString(); 503 } 504 void CuttlefishConfig::set_wifi_guest_mac_addr( 505 const std::string& wifi_guest_mac_addr) { 506 (*dictionary_)[kWifiGuestMacAddr] = wifi_guest_mac_addr; 507 } 508 509 std::string CuttlefishConfig::wifi_host_mac_addr() const { 510 return (*dictionary_)[kWifiHostMacAddr].asString(); 511 } 512 void CuttlefishConfig::set_wifi_host_mac_addr( 513 const std::string& wifi_host_mac_addr) { 514 (*dictionary_)[kWifiHostMacAddr] = wifi_host_mac_addr; 515 } 516 517 std::string CuttlefishConfig::mobile_tap_name() const { 518 return (*dictionary_)[kMobileTapName].asString(); 519 } 520 void CuttlefishConfig::set_mobile_tap_name(const std::string& mobile_tap_name) { 521 (*dictionary_)[kMobileTapName] = mobile_tap_name; 522 } 523 524 std::string CuttlefishConfig::wifi_tap_name() const { 525 return (*dictionary_)[kWifiTapName].asString(); 526 } 527 void CuttlefishConfig::set_wifi_tap_name(const std::string& wifi_tap_name) { 528 (*dictionary_)[kWifiTapName] = wifi_tap_name; 529 } 530 531 std::string CuttlefishConfig::entropy_source() const { 532 return (*dictionary_)[kEntropySource].asString(); 533 } 534 void CuttlefishConfig::set_entropy_source(const std::string& entropy_source) { 535 (*dictionary_)[kEntropySource] = entropy_source; 536 } 537 538 int CuttlefishConfig::vsock_guest_cid() const { 539 return (*dictionary_)[kVsockGuestCid].asInt(); 540 } 541 542 void CuttlefishConfig::set_vsock_guest_cid(int vsock_guest_cid) { 543 (*dictionary_)[kVsockGuestCid] = vsock_guest_cid; 544 } 545 546 std::string CuttlefishConfig::uuid() const { 547 return (*dictionary_)[kUuid].asString(); 548 } 549 void CuttlefishConfig::set_uuid(const std::string& uuid) { 550 (*dictionary_)[kUuid] = uuid; 551 } 552 553 void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) { 554 SetPath(kCuttlefishEnvPath, path); 555 } 556 std::string CuttlefishConfig::cuttlefish_env_path() const { 557 return (*dictionary_)[kCuttlefishEnvPath].asString(); 558 } 559 560 std::set<std::string> CuttlefishConfig::adb_mode() const { 561 std::set<std::string> args_set; 562 for (auto& mode : (*dictionary_)[kAdbMode]) { 563 args_set.insert(mode.asString()); 564 } 565 return args_set; 566 } 567 568 void CuttlefishConfig::set_adb_mode(const std::set<std::string>& mode) { 569 Json::Value mode_json_obj(Json::arrayValue); 570 for (const auto& arg : mode) { 571 mode_json_obj.append(arg); 572 } 573 (*dictionary_)[kAdbMode] = mode_json_obj; 574 } 575 576 std::string CuttlefishConfig::adb_ip_and_port() const { 577 return (*dictionary_)[kAdbIPAndPort].asString(); 578 } 579 580 void CuttlefishConfig::set_adb_ip_and_port(const std::string& ip_port) { 581 (*dictionary_)[kAdbIPAndPort] = ip_port; 582 } 583 584 std::string CuttlefishConfig::adb_device_name() const { 585 // TODO(schuffelen): Deal with duplication between here and launch.cc 586 bool tunnelMode = adb_mode().count("tunnel") > 0; 587 bool vsockTunnel = adb_mode().count("vsock_tunnel") > 0; 588 bool vsockHalfProxy = adb_mode().count("vsock_half_tunnel") > 0; 589 bool nativeVsock = adb_mode().count("native_vsock") > 0; 590 if (tunnelMode || vsockTunnel || vsockHalfProxy || nativeVsock) { 591 return adb_ip_and_port(); 592 } else if (adb_mode().count("usb") > 0) { 593 return serial_number(); 594 } 595 LOG(ERROR) << "no adb_mode found, returning bad device name"; 596 return "NO_ADB_MODE_SET_NO_VALID_DEVICE_NAME"; 597 } 598 599 std::string CuttlefishConfig::device_title() const { 600 return (*dictionary_)[kDeviceTitle].asString(); 601 } 602 603 void CuttlefishConfig::set_device_title(const std::string& title) { 604 (*dictionary_)[kDeviceTitle] = title; 605 } 606 607 std::string CuttlefishConfig::setupwizard_mode() const { 608 return (*dictionary_)[kSetupWizardMode].asString(); 609 } 610 611 void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) { 612 (*dictionary_)[kSetupWizardMode] = mode; 613 } 614 615 std::string CuttlefishConfig::qemu_binary() const { 616 return (*dictionary_)[kQemuBinary].asString(); 617 } 618 619 void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) { 620 (*dictionary_)[kQemuBinary] = qemu_binary; 621 } 622 623 std::string CuttlefishConfig::crosvm_binary() const { 624 return (*dictionary_)[kCrosvmBinary].asString(); 625 } 626 627 void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) { 628 (*dictionary_)[kCrosvmBinary] = crosvm_binary; 629 } 630 631 std::string CuttlefishConfig::ivserver_binary() const { 632 return (*dictionary_)[kIvServerBinary].asString(); 633 } 634 635 void CuttlefishConfig::set_ivserver_binary(const std::string& ivserver_binary) { 636 (*dictionary_)[kIvServerBinary] = ivserver_binary; 637 } 638 639 std::string CuttlefishConfig::kernel_log_monitor_binary() const { 640 return (*dictionary_)[kKernelLogMonitorBinary].asString(); 641 } 642 643 void CuttlefishConfig::set_kernel_log_monitor_binary( 644 const std::string& kernel_log_monitor_binary) { 645 (*dictionary_)[kKernelLogMonitorBinary] = kernel_log_monitor_binary; 646 } 647 648 bool CuttlefishConfig::enable_vnc_server() const { 649 return (*dictionary_)[kEnableVncServer].asBool(); 650 } 651 652 void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) { 653 (*dictionary_)[kEnableVncServer] = enable_vnc_server; 654 } 655 656 std::string CuttlefishConfig::vnc_server_binary() const { 657 return (*dictionary_)[kVncServerBinary].asString(); 658 } 659 660 void CuttlefishConfig::set_vnc_server_binary( 661 const std::string& vnc_server_binary) { 662 (*dictionary_)[kVncServerBinary] = vnc_server_binary; 663 } 664 665 int CuttlefishConfig::vnc_server_port() const { 666 return (*dictionary_)[kVncServerPort].asInt(); 667 } 668 669 void CuttlefishConfig::set_vnc_server_port(int vnc_server_port) { 670 (*dictionary_)[kVncServerPort] = vnc_server_port; 671 } 672 673 bool CuttlefishConfig::enable_stream_audio() const { 674 return (*dictionary_)[kEnableStreamAudio].asBool(); 675 } 676 677 void CuttlefishConfig::set_enable_stream_audio(bool enable_stream_audio) { 678 (*dictionary_)[kEnableStreamAudio] = enable_stream_audio; 679 } 680 681 std::string CuttlefishConfig::stream_audio_binary() const { 682 return (*dictionary_)[kStreamAudioBinary].asString(); 683 } 684 685 void CuttlefishConfig::set_stream_audio_binary( 686 const std::string& stream_audio_binary) { 687 (*dictionary_)[kStreamAudioBinary] = stream_audio_binary; 688 } 689 690 int CuttlefishConfig::stream_audio_port() const { 691 return (*dictionary_)[kStreamAudioPort].asInt(); 692 } 693 694 void CuttlefishConfig::set_stream_audio_port(int stream_audio_port) { 695 (*dictionary_)[kStreamAudioPort] = stream_audio_port; 696 } 697 698 bool CuttlefishConfig::restart_subprocesses() const { 699 return (*dictionary_)[kRestartSubprocesses].asBool(); 700 } 701 702 void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) { 703 (*dictionary_)[kRestartSubprocesses] = restart_subprocesses; 704 } 705 706 bool CuttlefishConfig::run_adb_connector() const { 707 return (*dictionary_)[kRunAdbConnector].asBool(); 708 } 709 710 void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) { 711 (*dictionary_)[kRunAdbConnector] = run_adb_connector; 712 } 713 714 std::string CuttlefishConfig::adb_connector_binary() const { 715 return (*dictionary_)[kAdbConnectorBinary].asString(); 716 } 717 718 void CuttlefishConfig::set_adb_connector_binary( 719 const std::string& adb_connector_binary) { 720 (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary; 721 } 722 723 std::string CuttlefishConfig::virtual_usb_manager_binary() const { 724 return (*dictionary_)[kVirtualUsbManagerBinary].asString(); 725 } 726 727 void CuttlefishConfig::set_virtual_usb_manager_binary( 728 const std::string& virtual_usb_manager_binary) { 729 (*dictionary_)[kVirtualUsbManagerBinary] = virtual_usb_manager_binary; 730 } 731 732 std::string CuttlefishConfig::socket_forward_proxy_binary() const { 733 return (*dictionary_)[kSocketForwardProxyBinary].asString(); 734 } 735 736 void CuttlefishConfig::set_socket_forward_proxy_binary( 737 const std::string& socket_forward_proxy_binary) { 738 (*dictionary_)[kSocketForwardProxyBinary] = socket_forward_proxy_binary; 739 } 740 741 std::string CuttlefishConfig::socket_vsock_proxy_binary() const { 742 return (*dictionary_)[kSocketVsockProxyBinary].asString(); 743 } 744 745 void CuttlefishConfig::set_socket_vsock_proxy_binary( 746 const std::string& socket_vsock_proxy_binary) { 747 (*dictionary_)[kSocketVsockProxyBinary] = socket_vsock_proxy_binary; 748 } 749 750 bool CuttlefishConfig::run_as_daemon() const { 751 return (*dictionary_)[kRunAsDaemon].asBool(); 752 } 753 754 void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) { 755 (*dictionary_)[kRunAsDaemon] = run_as_daemon; 756 } 757 758 bool CuttlefishConfig::run_e2e_test() const { 759 return (*dictionary_)[kRunE2eTest].asBool(); 760 } 761 762 void CuttlefishConfig::set_run_e2e_test(bool run_e2e_test) { 763 (*dictionary_)[kRunE2eTest] = run_e2e_test; 764 } 765 766 std::string CuttlefishConfig::e2e_test_binary() const { 767 return (*dictionary_)[kE2eTestBinary].asString(); 768 } 769 770 void CuttlefishConfig::set_e2e_test_binary(const std::string& e2e_test_binary) { 771 (*dictionary_)[kE2eTestBinary] = e2e_test_binary; 772 } 773 774 std::string CuttlefishConfig::data_policy() const { 775 return (*dictionary_)[kDataPolicy].asString(); 776 } 777 778 void CuttlefishConfig::set_data_policy(const std::string& data_policy) { 779 (*dictionary_)[kDataPolicy] = data_policy; 780 } 781 782 int CuttlefishConfig::blank_data_image_mb() const { 783 return (*dictionary_)[kBlankDataImageMb].asInt(); 784 } 785 786 void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) { 787 (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb; 788 } 789 790 std::string CuttlefishConfig::blank_data_image_fmt() const { 791 return (*dictionary_)[kBlankDataImageFmt].asString(); 792 } 793 794 void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) { 795 (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt; 796 } 797 798 799 void CuttlefishConfig::set_logcat_mode(const std::string& mode) { 800 (*dictionary_)[kLogcatMode] = mode; 801 } 802 803 std::string CuttlefishConfig::logcat_mode() const { 804 return (*dictionary_)[kLogcatMode].asString(); 805 } 806 807 void CuttlefishConfig::set_logcat_vsock_port(int port) { 808 (*dictionary_)[kLogcatVsockPort] = port; 809 } 810 811 int CuttlefishConfig::logcat_vsock_port() const { 812 return (*dictionary_)[kLogcatVsockPort].asInt(); 813 } 814 815 void CuttlefishConfig::set_frames_vsock_port(int port) { 816 (*dictionary_)[kFramesVsockPort] = port; 817 } 818 819 int CuttlefishConfig::frames_vsock_port() const { 820 return (*dictionary_)[kFramesVsockPort].asInt(); 821 } 822 823 void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) { 824 SetPath(kLogcatReceiverBinary, binary); 825 } 826 827 std::string CuttlefishConfig::logcat_receiver_binary() const { 828 return (*dictionary_)[kLogcatReceiverBinary].asString(); 829 } 830 831 bool CuttlefishConfig::enable_ivserver() const { 832 return hardware_name() == "cutf_ivsh"; 833 } 834 835 std::string CuttlefishConfig::touch_socket_path() const { 836 return PerInstancePath("touch.sock"); 837 } 838 839 std::string CuttlefishConfig::keyboard_socket_path() const { 840 return PerInstancePath("keyboard.sock"); 841 } 842 843 // Creates the (initially empty) config object and populates it with values from 844 // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present. 845 // Returns nullptr if there was an error loading from file 846 /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() { 847 auto config_file_path = cvd::StringFromEnv(kCuttlefishConfigEnvVarName, 848 vsoc::GetGlobalConfigFileLink()); 849 auto ret = new CuttlefishConfig(); 850 if (ret) { 851 auto loaded = ret->LoadFromFile(config_file_path.c_str()); 852 if (!loaded) { 853 delete ret; 854 return nullptr; 855 } 856 } 857 return ret; 858 } 859 860 /*static*/ CuttlefishConfig* CuttlefishConfig::Get() { 861 static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl()); 862 return config.get(); 863 } 864 865 CuttlefishConfig::CuttlefishConfig() : dictionary_(new Json::Value()) {} 866 // Can't use '= default' on the header because the compiler complains of 867 // Json::Value being an incomplete type 868 CuttlefishConfig::~CuttlefishConfig() {} 869 870 bool CuttlefishConfig::LoadFromFile(const char* file) { 871 auto real_file_path = cvd::AbsolutePath(file); 872 if (real_file_path.empty()) { 873 LOG(ERROR) << "Could not get real path for file " << file; 874 return false; 875 } 876 Json::Reader reader; 877 std::ifstream ifs(real_file_path); 878 if (!reader.parse(ifs, *dictionary_)) { 879 LOG(ERROR) << "Could not read config file " << file << ": " 880 << reader.getFormattedErrorMessages(); 881 return false; 882 } 883 return true; 884 } 885 bool CuttlefishConfig::SaveToFile(const std::string& file) const { 886 std::ofstream ofs(file); 887 if (!ofs.is_open()) { 888 LOG(ERROR) << "Unable to write to file " << file; 889 return false; 890 } 891 ofs << *dictionary_; 892 return !ofs.fail(); 893 } 894 895 std::string CuttlefishConfig::PerInstancePath(const char* file_name) const { 896 return (instance_dir() + "/") + file_name; 897 } 898 899 std::string CuttlefishConfig::instance_name() const { 900 return GetPerInstanceDefault("cvd-"); 901 } 902 903 int GetInstance() { 904 static int instance_id = InstanceFromEnvironment(); 905 return instance_id; 906 } 907 908 std::string GetGlobalConfigFileLink() { 909 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json"; 910 } 911 912 std::string GetDomain() { 913 return CuttlefishConfig::Get()->ivshmem_client_socket_path(); 914 } 915 916 std::string GetPerInstanceDefault(const char* prefix) { 917 std::ostringstream stream; 918 stream << prefix << std::setfill('0') << std::setw(2) << GetInstance(); 919 return stream.str(); 920 } 921 int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; } 922 923 std::string GetDefaultPerInstanceDir() { 924 std::ostringstream stream; 925 stream << std::getenv("HOME") << "/cuttlefish_runtime"; 926 return stream.str(); 927 } 928 929 std::string GetDefaultMempath() { 930 return GetPerInstanceDefault("/var/run/shm/cvd-"); 931 } 932 933 int GetDefaultPerInstanceVsockCid() { 934 constexpr int kFirstGuestCid = 3; 935 return vsoc::HostSupportsVsock() ? GetPerInstanceDefault(kFirstGuestCid) : 0; 936 } 937 938 std::string DefaultHostArtifactsPath(const std::string& file_name) { 939 return (cvd::StringFromEnv("ANDROID_HOST_OUT", 940 cvd::StringFromEnv("HOME", ".")) + 941 "/") + 942 file_name; 943 } 944 945 std::string DefaultGuestImagePath(const std::string& file_name) { 946 return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT", 947 cvd::StringFromEnv("HOME", ".")) + 948 "/") + 949 file_name; 950 } 951 952 bool HostSupportsQemuCli() { 953 static bool supported = 954 std::system( 955 "/usr/lib/cuttlefish-common/bin/capability_query.py qemu_cli") == 0; 956 return supported; 957 } 958 959 bool HostSupportsVsock() { 960 static bool supported = 961 std::system( 962 "/usr/lib/cuttlefish-common/bin/capability_query.py vsock") == 0; 963 return supported; 964 } 965 } // namespace vsoc 966