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 | |
| 19 | #include <climits> |
| 20 | #include <cstdlib> |
| 21 | #include <cstring> |
| 22 | #include <fstream> |
| 23 | #include <iomanip> |
| 24 | #include <sstream> |
| 25 | #include <string> |
| 26 | |
| 27 | #include <gflags/gflags.h> |
| 28 | #include <glog/logging.h> |
| 29 | #include <json/json.h> |
| 30 | |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 31 | #include "common/libs/utils/environment.h" |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 32 | #include "common/libs/utils/files.h" |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 33 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 34 | DEFINE_string(config_file, |
Jorge E. Moreira | 015c000 | 2018-07-19 11:01:16 -0700 | [diff] [blame] | 35 | vsoc::GetGlobalConfigFileLink(), |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 36 | "A file from where to load the config values. This flag is " |
| 37 | "ignored by the launcher"); |
| 38 | |
| 39 | namespace { |
| 40 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 41 | int InstanceFromEnvironment() { |
| 42 | static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE"; |
| 43 | static constexpr char kVsocUserPrefix[] = "vsoc-"; |
| 44 | static constexpr int kDefaultInstance = 1; |
| 45 | |
| 46 | // CUTTLEFISH_INSTANCE environment variable |
| 47 | const char* instance_str = std::getenv(kInstanceEnvironmentVariable); |
| 48 | if (!instance_str) { |
| 49 | // Try to get it from the user instead |
| 50 | instance_str = std::getenv("USER"); |
| 51 | if (!instance_str || std::strncmp(instance_str, kVsocUserPrefix, |
| 52 | sizeof(kVsocUserPrefix) - 1)) { |
| 53 | // No user or we don't recognize this user |
| 54 | return kDefaultInstance; |
| 55 | } |
| 56 | instance_str += sizeof(kVsocUserPrefix) - 1; |
| 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 | } |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 68 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 69 | const char* kSerialNumber = "serial_number"; |
| 70 | const char* kInstanceDir = "instance_dir"; |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 71 | const char* kVmManager = "vm_manager"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 72 | |
| 73 | const char* kCpus = "cpus"; |
| 74 | const char* kMemoryMb = "memory_mb"; |
| 75 | const char* kDpi = "dpi"; |
| 76 | const char* kXRes = "x_res"; |
| 77 | const char* kYRes = "y_res"; |
| 78 | const char* kRefreshRateHz = "refresh_rate_hz"; |
| 79 | |
| 80 | const char* kKernelImagePath = "kernel_image_path"; |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 81 | const char* kGdbFlag = "gdb_flag"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 82 | const char* kKernelArgs = "kernel_args"; |
| 83 | const char* kRamdiskImagePath = "ramdisk_image_path"; |
| 84 | |
| 85 | const char* kSystemImagePath = "system_image_path"; |
| 86 | const char* kCacheImagePath = "cache_image_path"; |
| 87 | const char* kDataImagePath = "data_image_path"; |
| 88 | const char* kVendorImagePath = "vendor_image_path"; |
| 89 | const char* kUsbV1SocketName = "usb_v1_socket_name"; |
| 90 | const char* kVhciPort = "vhci_port"; |
| 91 | const char* kUsbIpSocketName = "usb_ip_socket_name"; |
| 92 | const char* kKernelLogSocketName = "kernel_log_socket_name"; |
| 93 | const char* kConsolePath = "console_path"; |
| 94 | const char* kLogcatPath = "logcat_path"; |
Jorge E. Moreira | 66f6ec8 | 2018-07-16 16:43:15 -0700 | [diff] [blame] | 95 | const char* kLauncherLogPath = "launcher_log_path"; |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 96 | const char* kLauncherMonitorPath = "launcher_monitor_socket"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 97 | const char* kDtbPath = "dtb_path"; |
| 98 | |
| 99 | const char* kMempath = "mempath"; |
| 100 | const char* kIvshmemQemuSocketPath = "ivshmem_qemu_socket_path"; |
| 101 | const char* kIvshmemClientSocketPath = "ivshmem_client_socket_path"; |
| 102 | const char* kIvshmemVectorCount = "ivshmem_vector_count"; |
| 103 | |
| 104 | const char* kMobileBridgeName = "mobile_bridge_name"; |
| 105 | const char* kMobileTapName = "mobile_tap_name"; |
Cody Schuffelen | 975175a | 2018-06-14 18:20:02 -0700 | [diff] [blame] | 106 | const char* kWifiBridgeName = "wifi_bridge_name"; |
| 107 | const char* kWifiTapName = "wifi_tap_name"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 108 | const char* kWifiGuestMacAddr = "wifi_guest_mac_addr"; |
| 109 | const char* kWifiHostMacAddr = "wifi_host_mac_addr"; |
| 110 | const char* kEntropySource = "entropy_source"; |
| 111 | |
| 112 | const char* kUuid = "uuid"; |
| 113 | const char* kDisableDacSecurity = "disable_dac_security"; |
| 114 | const char* kDisableAppArmorSecurity = "disable_app_armor_security"; |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 115 | const char* kCuttlefishEnvPath = "cuttlefish_env_path"; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 116 | |
| 117 | const char* kAdbMode = "adb_mode"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 118 | } // namespace |
| 119 | |
| 120 | namespace vsoc { |
| 121 | |
| 122 | std::string CuttlefishConfig::instance_dir() const { |
| 123 | return (*dictionary_)[kInstanceDir].asString(); |
| 124 | } |
| 125 | void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) { |
| 126 | (*dictionary_)[kInstanceDir] = instance_dir; |
| 127 | } |
| 128 | |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 129 | std::string CuttlefishConfig::vm_manager() const { |
| 130 | return (*dictionary_)[kVmManager].asString(); |
| 131 | } |
| 132 | void CuttlefishConfig::set_vm_manager(const std::string& name) { |
| 133 | (*dictionary_)[kVmManager] = name; |
| 134 | } |
| 135 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 136 | std::string CuttlefishConfig::serial_number() const { |
| 137 | return (*dictionary_)[kSerialNumber].asString(); |
| 138 | } |
| 139 | void CuttlefishConfig::set_serial_number(const std::string& serial_number) { |
| 140 | (*dictionary_)[kSerialNumber] = serial_number; |
| 141 | } |
| 142 | |
| 143 | int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); } |
| 144 | void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; } |
| 145 | |
| 146 | int CuttlefishConfig::memory_mb() const { |
| 147 | return (*dictionary_)[kMemoryMb].asInt(); |
| 148 | } |
| 149 | void CuttlefishConfig::set_memory_mb(int memory_mb) { |
| 150 | (*dictionary_)[kMemoryMb] = memory_mb; |
| 151 | } |
| 152 | |
| 153 | int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); } |
| 154 | void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; } |
| 155 | |
| 156 | int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); } |
| 157 | void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; } |
| 158 | |
| 159 | int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); } |
| 160 | void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; } |
| 161 | |
| 162 | int CuttlefishConfig::refresh_rate_hz() const { |
| 163 | return (*dictionary_)[kRefreshRateHz].asInt(); |
| 164 | } |
| 165 | void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) { |
| 166 | (*dictionary_)[kRefreshRateHz] = refresh_rate_hz; |
| 167 | } |
| 168 | |
| 169 | std::string CuttlefishConfig::kernel_image_path() const { |
| 170 | return (*dictionary_)[kKernelImagePath].asString(); |
| 171 | } |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 172 | |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 173 | void CuttlefishConfig::SetPath(const std::string& key, |
| 174 | const std::string& path) { |
| 175 | if (!path.empty()) { |
| 176 | (*dictionary_)[key] = cvd::AbsolutePath(path); |
| 177 | } |
| 178 | } |
| 179 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 180 | void CuttlefishConfig::set_kernel_image_path( |
| 181 | const std::string& kernel_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 182 | SetPath(kKernelImagePath, kernel_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 185 | std::string CuttlefishConfig::gdb_flag() const { |
| 186 | return (*dictionary_)[kGdbFlag].asString(); |
| 187 | } |
| 188 | |
| 189 | void CuttlefishConfig::set_gdb_flag( |
| 190 | const std::string& device) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 191 | SetPath(kGdbFlag, device); |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 194 | std::string CuttlefishConfig::kernel_args() const { |
| 195 | return (*dictionary_)[kKernelArgs].asString(); |
| 196 | } |
| 197 | void CuttlefishConfig::set_kernel_args(const std::string& kernel_args) { |
| 198 | (*dictionary_)[kKernelArgs] = kernel_args; |
| 199 | } |
| 200 | |
| 201 | std::string CuttlefishConfig::ramdisk_image_path() const { |
| 202 | return (*dictionary_)[kRamdiskImagePath].asString(); |
| 203 | } |
| 204 | void CuttlefishConfig::set_ramdisk_image_path( |
| 205 | const std::string& ramdisk_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 206 | SetPath(kRamdiskImagePath, ramdisk_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | std::string CuttlefishConfig::system_image_path() const { |
| 210 | return (*dictionary_)[kSystemImagePath].asString(); |
| 211 | } |
| 212 | void CuttlefishConfig::set_system_image_path( |
| 213 | const std::string& system_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 214 | SetPath(kSystemImagePath, system_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | std::string CuttlefishConfig::cache_image_path() const { |
| 218 | return (*dictionary_)[kCacheImagePath].asString(); |
| 219 | } |
| 220 | void CuttlefishConfig::set_cache_image_path( |
| 221 | const std::string& cache_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 222 | SetPath(kCacheImagePath, cache_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | std::string CuttlefishConfig::data_image_path() const { |
| 226 | return (*dictionary_)[kDataImagePath].asString(); |
| 227 | } |
| 228 | void CuttlefishConfig::set_data_image_path(const std::string& data_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 229 | SetPath(kDataImagePath, data_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | std::string CuttlefishConfig::vendor_image_path() const { |
| 233 | return (*dictionary_)[kVendorImagePath].asString(); |
| 234 | } |
| 235 | void CuttlefishConfig::set_vendor_image_path( |
| 236 | const std::string& vendor_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 237 | SetPath(kVendorImagePath, vendor_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | std::string CuttlefishConfig::dtb_path() const { |
| 241 | return (*dictionary_)[kDtbPath].asString(); |
| 242 | } |
| 243 | void CuttlefishConfig::set_dtb_path(const std::string& dtb_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 244 | SetPath(kDtbPath, dtb_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | std::string CuttlefishConfig::mempath() const { |
| 248 | return (*dictionary_)[kMempath].asString(); |
| 249 | } |
| 250 | void CuttlefishConfig::set_mempath(const std::string& mempath) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 251 | SetPath(kMempath, mempath); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | std::string CuttlefishConfig::ivshmem_qemu_socket_path() const { |
| 255 | return (*dictionary_)[kIvshmemQemuSocketPath].asString(); |
| 256 | } |
| 257 | void CuttlefishConfig::set_ivshmem_qemu_socket_path( |
| 258 | const std::string& ivshmem_qemu_socket_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 259 | SetPath(kIvshmemQemuSocketPath, ivshmem_qemu_socket_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | std::string CuttlefishConfig::ivshmem_client_socket_path() const { |
| 263 | return (*dictionary_)[kIvshmemClientSocketPath].asString(); |
| 264 | } |
| 265 | void CuttlefishConfig::set_ivshmem_client_socket_path( |
| 266 | const std::string& ivshmem_client_socket_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 267 | SetPath(kIvshmemClientSocketPath, ivshmem_client_socket_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int CuttlefishConfig::ivshmem_vector_count() const { |
| 271 | return (*dictionary_)[kIvshmemVectorCount].asInt(); |
| 272 | } |
| 273 | void CuttlefishConfig::set_ivshmem_vector_count(int ivshmem_vector_count) { |
| 274 | (*dictionary_)[kIvshmemVectorCount] = ivshmem_vector_count; |
| 275 | } |
| 276 | |
| 277 | std::string CuttlefishConfig::usb_v1_socket_name() const { |
| 278 | return (*dictionary_)[kUsbV1SocketName].asString(); |
| 279 | } |
| 280 | void CuttlefishConfig::set_usb_v1_socket_name( |
| 281 | const std::string& usb_v1_socket_name) { |
| 282 | (*dictionary_)[kUsbV1SocketName] = usb_v1_socket_name; |
| 283 | } |
| 284 | |
| 285 | int CuttlefishConfig::vhci_port() const { |
| 286 | return (*dictionary_)[kVhciPort].asInt(); |
| 287 | } |
| 288 | void CuttlefishConfig::set_vhci_port(int vhci_port) { |
| 289 | (*dictionary_)[kVhciPort] = vhci_port; |
| 290 | } |
| 291 | |
| 292 | std::string CuttlefishConfig::usb_ip_socket_name() const { |
| 293 | return (*dictionary_)[kUsbIpSocketName].asString(); |
| 294 | } |
| 295 | void CuttlefishConfig::set_usb_ip_socket_name( |
| 296 | const std::string& usb_ip_socket_name) { |
| 297 | (*dictionary_)[kUsbIpSocketName] = usb_ip_socket_name; |
| 298 | } |
| 299 | |
| 300 | std::string CuttlefishConfig::kernel_log_socket_name() const { |
| 301 | return (*dictionary_)[kKernelLogSocketName].asString(); |
| 302 | } |
| 303 | void CuttlefishConfig::set_kernel_log_socket_name( |
| 304 | const std::string& kernel_log_socket_name) { |
| 305 | (*dictionary_)[kKernelLogSocketName] = kernel_log_socket_name; |
| 306 | } |
| 307 | |
| 308 | std::string CuttlefishConfig::console_path() const { |
| 309 | return (*dictionary_)[kConsolePath].asString(); |
| 310 | } |
| 311 | void CuttlefishConfig::set_console_path(const std::string& console_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 312 | SetPath(kConsolePath, console_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | std::string CuttlefishConfig::logcat_path() const { |
| 316 | return (*dictionary_)[kLogcatPath].asString(); |
| 317 | } |
| 318 | void CuttlefishConfig::set_logcat_path(const std::string& logcat_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 319 | SetPath(kLogcatPath, logcat_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 322 | std::string CuttlefishConfig::launcher_monitor_socket_path() const { |
| 323 | return (*dictionary_)[kLauncherMonitorPath].asString(); |
| 324 | } |
| 325 | void CuttlefishConfig::set_launcher_monitor_socket_path( |
Jorge E. Moreira | be7049a | 2018-07-26 18:12:13 -0700 | [diff] [blame^] | 326 | const std::string& launcher_monitor_path) { |
| 327 | SetPath(kLauncherMonitorPath, launcher_monitor_path); |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Jorge E. Moreira | 66f6ec8 | 2018-07-16 16:43:15 -0700 | [diff] [blame] | 330 | std::string CuttlefishConfig::launcher_log_path() const { |
| 331 | return (*dictionary_)[kLauncherLogPath].asString(); |
| 332 | } |
| 333 | void CuttlefishConfig::set_launcher_log_path( |
| 334 | const std::string& launcher_log_path) { |
| 335 | (*dictionary_)[kLauncherLogPath] = launcher_log_path; |
| 336 | } |
| 337 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 338 | std::string CuttlefishConfig::mobile_bridge_name() const { |
| 339 | return (*dictionary_)[kMobileBridgeName].asString(); |
| 340 | } |
| 341 | void CuttlefishConfig::set_mobile_bridge_name( |
| 342 | const std::string& mobile_bridge_name) { |
| 343 | (*dictionary_)[kMobileBridgeName] = mobile_bridge_name; |
| 344 | } |
| 345 | |
Cody Schuffelen | 975175a | 2018-06-14 18:20:02 -0700 | [diff] [blame] | 346 | std::string CuttlefishConfig::wifi_bridge_name() const { |
| 347 | return (*dictionary_)[kWifiBridgeName].asString(); |
| 348 | } |
| 349 | void CuttlefishConfig::set_wifi_bridge_name( |
| 350 | const std::string& wifi_bridge_name) { |
| 351 | (*dictionary_)[kWifiBridgeName] = wifi_bridge_name; |
| 352 | } |
| 353 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 354 | std::string CuttlefishConfig::wifi_guest_mac_addr() const { |
| 355 | return (*dictionary_)[kWifiGuestMacAddr].asString(); |
| 356 | } |
| 357 | void CuttlefishConfig::set_wifi_guest_mac_addr( |
| 358 | const std::string& wifi_guest_mac_addr) { |
| 359 | (*dictionary_)[kWifiGuestMacAddr] = wifi_guest_mac_addr; |
| 360 | } |
| 361 | |
| 362 | std::string CuttlefishConfig::wifi_host_mac_addr() const { |
| 363 | return (*dictionary_)[kWifiHostMacAddr].asString(); |
| 364 | } |
| 365 | void CuttlefishConfig::set_wifi_host_mac_addr( |
| 366 | const std::string& wifi_host_mac_addr) { |
| 367 | (*dictionary_)[kWifiHostMacAddr] = wifi_host_mac_addr; |
| 368 | } |
| 369 | |
| 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 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 384 | std::string CuttlefishConfig::entropy_source() const { |
| 385 | return (*dictionary_)[kEntropySource].asString(); |
| 386 | } |
| 387 | void CuttlefishConfig::set_entropy_source(const std::string& entropy_source) { |
| 388 | (*dictionary_)[kEntropySource] = entropy_source; |
| 389 | } |
| 390 | |
| 391 | std::string CuttlefishConfig::uuid() const { |
| 392 | return (*dictionary_)[kUuid].asString(); |
| 393 | } |
| 394 | void CuttlefishConfig::set_uuid(const std::string& uuid) { |
| 395 | (*dictionary_)[kUuid] = uuid; |
| 396 | } |
| 397 | |
| 398 | bool CuttlefishConfig::disable_dac_security() const { |
| 399 | return (*dictionary_)[kDisableDacSecurity].asBool(); |
| 400 | } |
| 401 | void CuttlefishConfig::set_disable_dac_security(bool disable_dac_security) { |
| 402 | (*dictionary_)[kDisableDacSecurity] = disable_dac_security; |
| 403 | } |
| 404 | |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 405 | void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 406 | SetPath(kCuttlefishEnvPath, path); |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 407 | } |
| 408 | std::string CuttlefishConfig::cuttlefish_env_path() const { |
| 409 | return (*dictionary_)[kCuttlefishEnvPath].asString(); |
| 410 | } |
| 411 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 412 | bool CuttlefishConfig::disable_app_armor_security() const { |
| 413 | return (*dictionary_)[kDisableAppArmorSecurity].asBool(); |
| 414 | } |
| 415 | void CuttlefishConfig::set_disable_app_armor_security( |
| 416 | bool disable_app_armor_security) { |
| 417 | (*dictionary_)[kDisableAppArmorSecurity] = disable_app_armor_security; |
| 418 | } |
| 419 | |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 420 | std::string CuttlefishConfig::adb_mode() const { |
| 421 | return (*dictionary_)[kAdbMode].asString(); |
| 422 | } |
| 423 | |
| 424 | void CuttlefishConfig::set_adb_mode(const std::string& mode) { |
| 425 | (*dictionary_)[kAdbMode] = mode; |
| 426 | } |
| 427 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 428 | // Creates the (initially empty) config object and populates it with values from |
| 429 | // the config file if the --config_file command line argument is present. |
| 430 | // Returns nullptr if there was an error loading from file |
| 431 | /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() { |
| 432 | auto ret = new CuttlefishConfig(); |
| 433 | if (ret && !FLAGS_config_file.empty()) { |
| 434 | auto loaded = ret->LoadFromFile(FLAGS_config_file.c_str()); |
| 435 | if (!loaded) { |
| 436 | return nullptr; |
| 437 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 438 | } |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 439 | return ret; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 442 | /*static*/ CuttlefishConfig* CuttlefishConfig::Get() { |
| 443 | static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl()); |
| 444 | return config.get(); |
| 445 | } |
| 446 | |
| 447 | CuttlefishConfig::CuttlefishConfig() : dictionary_(new Json::Value()) {} |
| 448 | // Can't use '= default' on the header because the compiler complains of |
| 449 | // Json::Value being an incomplete type |
| 450 | CuttlefishConfig::~CuttlefishConfig() {} |
| 451 | |
| 452 | bool CuttlefishConfig::LoadFromFile(const char* file) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 453 | auto real_file_path = cvd::AbsolutePath(file); |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 454 | if (real_file_path.empty()) { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 455 | LOG(ERROR) << "Could not get real path for file " << file; |
| 456 | return false; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 457 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 458 | Json::Reader reader; |
| 459 | std::ifstream ifs(real_file_path); |
| 460 | if (!reader.parse(ifs, *dictionary_)) { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 461 | LOG(ERROR) << "Could not read config file " << file << ": " |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 462 | << reader.getFormattedErrorMessages(); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 463 | return false; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 464 | } |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 465 | return true; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 466 | } |
| 467 | bool CuttlefishConfig::SaveToFile(const std::string& file) const { |
| 468 | std::ofstream ofs(file); |
| 469 | if (!ofs.is_open()) { |
| 470 | LOG(ERROR) << "Unable to write to file " << file; |
| 471 | return false; |
| 472 | } |
| 473 | ofs << *dictionary_; |
| 474 | return !ofs.fail(); |
| 475 | } |
| 476 | |
| 477 | std::string CuttlefishConfig::PerInstancePath(const char* file_name) const { |
| 478 | return (instance_dir() + "/") + file_name; |
| 479 | } |
| 480 | |
| 481 | std::string CuttlefishConfig::instance_name() const { |
| 482 | return GetPerInstanceDefault("cvd-"); |
| 483 | } |
| 484 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 485 | int GetInstance() { |
| 486 | static int instance_id = InstanceFromEnvironment(); |
| 487 | return instance_id; |
| 488 | } |
| 489 | |
Jorge E. Moreira | 015c000 | 2018-07-19 11:01:16 -0700 | [diff] [blame] | 490 | std::string GetGlobalConfigFileLink() { |
| 491 | return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json"; |
| 492 | } |
| 493 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 494 | std::string GetDomain() { |
| 495 | return CuttlefishConfig::Get()->ivshmem_client_socket_path(); |
| 496 | } |
| 497 | |
| 498 | std::string GetPerInstanceDefault(const char* prefix) { |
| 499 | std::ostringstream stream; |
| 500 | stream << prefix << std::setfill('0') << std::setw(2) << GetInstance(); |
| 501 | return stream.str(); |
| 502 | } |
| 503 | int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; } |
| 504 | |
| 505 | std::string GetDefaultPerInstanceDir() { |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 506 | std::ostringstream stream; |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 507 | if (HostSupportsQemuCli()) { |
Jorge E. Moreira | 285b48b | 2018-06-27 10:33:24 -0700 | [diff] [blame] | 508 | stream << std::getenv("HOME") << "/cuttlefish_runtime"; |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 509 | } else { |
| 510 | stream << "/var/run/libvirt-" << kDefaultUuidPrefix << std::setfill('0') |
| 511 | << std::setw(2) << GetInstance(); |
| 512 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 513 | return stream.str(); |
| 514 | } |
| 515 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 516 | std::string GetDefaultMempath() { |
| 517 | return GetPerInstanceDefault("/var/run/shm/cvd-"); |
| 518 | } |
| 519 | |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 520 | std::string DefaultHostArtifactsPath(const std::string& file_name) { |
| 521 | return (cvd::StringFromEnv("ANDROID_HOST_OUT", |
| 522 | cvd::StringFromEnv("HOME", ".")) + |
| 523 | "/") + |
| 524 | file_name; |
| 525 | } |
| 526 | |
| 527 | std::string DefaultGuestImagePath(const std::string& file_name) { |
| 528 | return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT", |
| 529 | cvd::StringFromEnv("HOME", ".")) + |
| 530 | "/") + |
| 531 | file_name; |
| 532 | } |
| 533 | |
| 534 | bool HostSupportsQemuCli() { |
| 535 | static bool supported = |
| 536 | std::system( |
| 537 | "/usr/lib/cuttlefish-common/bin/capability_query.py qemu_cli") == 0; |
| 538 | return supported; |
| 539 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 540 | } // namespace vsoc |