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 #include <time.h>
29
30 #include <android-base/strings.h>
31 #include <android-base/logging.h>
32 #include <json/json.h>
33
34 #include "common/libs/utils/environment.h"
35 #include "common/libs/utils/files.h"
36 #include "host/libs/vm_manager/qemu_manager.h"
37
38
39 namespace {
40
InstanceFromEnvironment()41 int InstanceFromEnvironment() {
42 static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE";
43 static constexpr int kDefaultInstance = 1;
44
45 // CUTTLEFISH_INSTANCE environment variable
46 const char* instance_str = std::getenv(kInstanceEnvironmentVariable);
47 if (!instance_str) {
48 // Try to get it from the user instead
49 instance_str = std::getenv("USER");
50
51 if (!instance_str || std::strncmp(instance_str, cuttlefish::kVsocUserPrefix,
52 sizeof(cuttlefish::kVsocUserPrefix) - 1)) {
53 // No user or we don't recognize this user
54 LOG(DEBUG) << "No user or non-vsoc user, returning default config";
55 return kDefaultInstance;
56 }
57 instance_str += sizeof(cuttlefish::kVsocUserPrefix) - 1;
58
59 // Set the environment variable so that child processes see it
60 setenv(kInstanceEnvironmentVariable, instance_str, 0);
61 }
62
63 int instance = std::atoi(instance_str);
64 if (instance <= 0) {
65 instance = kDefaultInstance;
66 }
67
68 return instance;
69 }
70
71 const char* kInstances = "instances";
72 const char* kAssemblyDir = "assembly_dir";
73 const char* kVmManager = "vm_manager";
74 const char* const kGpuMode = "gpu_mode";
75
76 const char* kCpus = "cpus";
77 const char* kMemoryMb = "memory_mb";
78 const char* kDpi = "dpi";
79 const char* kXRes = "x_res";
80 const char* kYRes = "y_res";
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* kRamdiskImagePath = "ramdisk_image_path";
89 const char* kInitramfsPath = "initramfs_path";
90 const char* kFinalRamdiskPath = "final_ramdisk_path";
91 const char* kVendorRamdiskImagePath = "vendor_ramdisk_image_path";
92
93 const char* kDeprecatedBootCompleted = "deprecated_boot_completed";
94
95 const char* kCuttlefishEnvPath = "cuttlefish_env_path";
96
97 const char* kAdbMode = "adb_mode";
98 const char* kSetupWizardMode = "setupwizard_mode";
99 const char* kTpmDevice = "tpm_device";
100
101 const char* kQemuBinary = "qemu_binary";
102 const char* kCrosvmBinary = "crosvm_binary";
103 const char* kTpmBinary = "tpm_binary";
104 const char* kConsoleForwarderBinary = "console_forwarder_binary";
105 const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary";
106
107 const char* kEnableVncServer = "enable_vnc_server";
108 const char* kVncServerBinary = "vnc_server_binary";
109
110 const char* kEnableSandbox = "enable_sandbox";
111 const char* kSeccompPolicyDir = "seccomp_policy_dir";
112
113 const char* kEnableWebRTC = "enable_webrtc";
114 const char* kWebRTCBinary = "webrtc_binary";
115 const char* kWebRTCAssetsDir = "webrtc_assets_dir";
116 const char* kWebRTCEnableADBWebSocket = "webrtc_enable_adb_websocket";
117
118 const char* kRestartSubprocesses = "restart_subprocesses";
119 const char* kRunAdbConnector = "run_adb_connector";
120 const char* kAdbConnectorBinary = "adb_connector_binary";
121 const char* kSocketVsockProxyBinary = "socket_vsock_proxy_binary";
122
123 const char* kRunAsDaemon = "run_as_daemon";
124
125 const char* kDataPolicy = "data_policy";
126 const char* kBlankDataImageMb = "blank_data_image_mb";
127 const char* kBlankDataImageFmt = "blank_data_image_fmt";
128
129 const char* kLogcatReceiverBinary = "logcat_receiver_binary";
130 const char* kConfigServerBinary = "config_server_binary";
131
132 const char* kRunTombstoneReceiver = "enable_tombstone_logger";
133 const char* kTombstoneReceiverBinary = "tombstone_receiver_binary";
134
135 const char* kWebRTCCertsDir = "webrtc_certs_dir";
136 const char* kSigServerBinary = "webrtc_sig_server_binary";
137 const char* kSigServerPort = "webrtc_sig_server_port";
138 const char* kSigServerAddress = "webrtc_sig_server_addr";
139 const char* kSigServerPath = "webrtc_sig_server_path";
140 const char* kSigServerStrict = "webrtc_sig_server_strict";
141 const char* kWebrtcUdpPortRange = "webrtc_udp_port_range";
142 const char* kWebrtcTcpPortRange = "webrtc_tcp_port_range";
143
144 const char* kBootloader = "bootloader";
145 const char* kUseBootloader = "use_bootloader";
146
147 const char* kBootSlot = "boot_slot";
148
149 const char* kEnableMetrics = "enable_metrics";
150 const char* kMetricsBinary = "metrics_binary";
151
152 const char* kLoopMaxPart = "loop_max_part";
153 const char* kGuestEnforceSecurity = "guest_enforce_security";
154 const char* kGuestAuditSecurity = "guest_audit_security";
155 const char* kGuestForceNormalBoot = "guest_force_normal_boot";
156 const char* kBootImageKernelCmdline = "boot_image_kernel_cmdline";
157 const char* kExtraKernelCmdline = "extra_kernel_cmdline";
158 const char* kVmManagerKernelCmdline = "vm_manager_kernel_cmdline";
159
160 // modem simulator related
161 const char* kRunModemSimulator = "enable_modem_simulator";
162 const char* kModemSimulatorBinary = "modem_simulator_binary";
163 const char* kModemSimulatorInstanceNumber = "modem_simulator_instance_number";
164
165 const char* kRilDns = "ril_dns";
166
167 const char* kKgdb = "kgdb";
168 } // namespace
169
170 namespace cuttlefish {
171
172 const char* const kGpuModeGuestSwiftshader = "guest_swiftshader";
173 const char* const kGpuModeDrmVirgl = "drm_virgl";
174 const char* const kGpuModeGfxStream = "gfxstream";
175
DefaultEnvironmentPath(const char * environment_key,const char * default_value,const char * subpath)176 std::string DefaultEnvironmentPath(const char* environment_key,
177 const char* default_value,
178 const char* subpath) {
179 return cuttlefish::StringFromEnv(environment_key, default_value) + "/" + subpath;
180 }
181
assembly_dir() const182 std::string CuttlefishConfig::assembly_dir() const {
183 return (*dictionary_)[kAssemblyDir].asString();
184 }
set_assembly_dir(const std::string & assembly_dir)185 void CuttlefishConfig::set_assembly_dir(const std::string& assembly_dir) {
186 (*dictionary_)[kAssemblyDir] = assembly_dir;
187 }
188
vm_manager() const189 std::string CuttlefishConfig::vm_manager() const {
190 return (*dictionary_)[kVmManager].asString();
191 }
set_vm_manager(const std::string & name)192 void CuttlefishConfig::set_vm_manager(const std::string& name) {
193 (*dictionary_)[kVmManager] = name;
194 }
195
gpu_mode() const196 std::string CuttlefishConfig::gpu_mode() const {
197 return (*dictionary_)[kGpuMode].asString();
198 }
set_gpu_mode(const std::string & name)199 void CuttlefishConfig::set_gpu_mode(const std::string& name) {
200 (*dictionary_)[kGpuMode] = name;
201 }
202
cpus() const203 int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); }
set_cpus(int cpus)204 void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; }
205
memory_mb() const206 int CuttlefishConfig::memory_mb() const {
207 return (*dictionary_)[kMemoryMb].asInt();
208 }
set_memory_mb(int memory_mb)209 void CuttlefishConfig::set_memory_mb(int memory_mb) {
210 (*dictionary_)[kMemoryMb] = memory_mb;
211 }
212
dpi() const213 int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); }
set_dpi(int dpi)214 void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; }
215
x_res() const216 int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); }
set_x_res(int x_res)217 void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; }
218
y_res() const219 int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); }
set_y_res(int y_res)220 void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; }
221
refresh_rate_hz() const222 int CuttlefishConfig::refresh_rate_hz() const {
223 return (*dictionary_)[kRefreshRateHz].asInt();
224 }
set_refresh_rate_hz(int refresh_rate_hz)225 void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) {
226 (*dictionary_)[kRefreshRateHz] = refresh_rate_hz;
227 }
228
kernel_image_path() const229 std::string CuttlefishConfig::kernel_image_path() const {
230 return (*dictionary_)[kKernelImagePath].asString();
231 }
232
SetPath(const std::string & key,const std::string & path)233 void CuttlefishConfig::SetPath(const std::string& key,
234 const std::string& path) {
235 if (!path.empty()) {
236 (*dictionary_)[key] = cuttlefish::AbsolutePath(path);
237 }
238 }
239
set_kernel_image_path(const std::string & kernel_image_path)240 void CuttlefishConfig::set_kernel_image_path(
241 const std::string& kernel_image_path) {
242 SetPath(kKernelImagePath, kernel_image_path);
243 }
244
use_unpacked_kernel() const245 bool CuttlefishConfig::use_unpacked_kernel() const {
246 return (*dictionary_)[kUseUnpackedKernel].asBool();
247 }
248
set_use_unpacked_kernel(bool use_unpacked_kernel)249 void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) {
250 (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel;
251 }
252
decompress_kernel() const253 bool CuttlefishConfig::decompress_kernel() const {
254 return (*dictionary_)[kDecompressKernel].asBool();
255 }
set_decompress_kernel(bool decompress_kernel)256 void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) {
257 (*dictionary_)[kDecompressKernel] = decompress_kernel;
258 }
259
decompressed_kernel_image_path() const260 std::string CuttlefishConfig::decompressed_kernel_image_path() const {
261 return (*dictionary_)[kDecompressedKernelImagePath].asString();
262 }
set_decompressed_kernel_image_path(const std::string & path)263 void CuttlefishConfig::set_decompressed_kernel_image_path(
264 const std::string& path) {
265 SetPath(kDecompressedKernelImagePath, path);
266 }
267
gdb_flag() const268 std::string CuttlefishConfig::gdb_flag() const {
269 return (*dictionary_)[kGdbFlag].asString();
270 }
271
set_gdb_flag(const std::string & device)272 void CuttlefishConfig::set_gdb_flag(const std::string& device) {
273 (*dictionary_)[kGdbFlag] = device;
274 }
275
ramdisk_image_path() const276 std::string CuttlefishConfig::ramdisk_image_path() const {
277 return (*dictionary_)[kRamdiskImagePath].asString();
278 }
set_ramdisk_image_path(const std::string & ramdisk_image_path)279 void CuttlefishConfig::set_ramdisk_image_path(
280 const std::string& ramdisk_image_path) {
281 SetPath(kRamdiskImagePath, ramdisk_image_path);
282 }
283
initramfs_path() const284 std::string CuttlefishConfig::initramfs_path() const {
285 return (*dictionary_)[kInitramfsPath].asString();
286 }
set_initramfs_path(const std::string & initramfs_path)287 void CuttlefishConfig::set_initramfs_path(const std::string& initramfs_path) {
288 SetPath(kInitramfsPath, initramfs_path);
289 }
290
final_ramdisk_path() const291 std::string CuttlefishConfig::final_ramdisk_path() const {
292 return (*dictionary_)[kFinalRamdiskPath].asString();
293 }
set_final_ramdisk_path(const std::string & final_ramdisk_path)294 void CuttlefishConfig::set_final_ramdisk_path(
295 const std::string& final_ramdisk_path) {
296 SetPath(kFinalRamdiskPath, final_ramdisk_path);
297 }
298
vendor_ramdisk_image_path() const299 std::string CuttlefishConfig::vendor_ramdisk_image_path() const {
300 return (*dictionary_)[kVendorRamdiskImagePath].asString();
301 }
set_vendor_ramdisk_image_path(const std::string & vendor_ramdisk_image_path)302 void CuttlefishConfig::set_vendor_ramdisk_image_path(
303 const std::string& vendor_ramdisk_image_path) {
304 SetPath(kVendorRamdiskImagePath, vendor_ramdisk_image_path);
305 }
306
deprecated_boot_completed() const307 bool CuttlefishConfig::deprecated_boot_completed() const {
308 return (*dictionary_)[kDeprecatedBootCompleted].asBool();
309 }
set_deprecated_boot_completed(bool deprecated_boot_completed)310 void CuttlefishConfig::set_deprecated_boot_completed(
311 bool deprecated_boot_completed) {
312 (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed;
313 }
314
set_cuttlefish_env_path(const std::string & path)315 void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) {
316 SetPath(kCuttlefishEnvPath, path);
317 }
cuttlefish_env_path() const318 std::string CuttlefishConfig::cuttlefish_env_path() const {
319 return (*dictionary_)[kCuttlefishEnvPath].asString();
320 }
321
stringToAdbMode(std::string mode)322 static AdbMode stringToAdbMode(std::string mode) {
323 std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower);
324 if (mode == "vsock_tunnel") {
325 return AdbMode::VsockTunnel;
326 } else if (mode == "vsock_half_tunnel") {
327 return AdbMode::VsockHalfTunnel;
328 } else if (mode == "native_vsock") {
329 return AdbMode::NativeVsock;
330 } else {
331 return AdbMode::Unknown;
332 }
333 }
334
adb_mode() const335 std::set<AdbMode> CuttlefishConfig::adb_mode() const {
336 std::set<AdbMode> args_set;
337 for (auto& mode : (*dictionary_)[kAdbMode]) {
338 args_set.insert(stringToAdbMode(mode.asString()));
339 }
340 return args_set;
341 }
342
set_adb_mode(const std::set<std::string> & mode)343 void CuttlefishConfig::set_adb_mode(const std::set<std::string>& mode) {
344 Json::Value mode_json_obj(Json::arrayValue);
345 for (const auto& arg : mode) {
346 mode_json_obj.append(arg);
347 }
348 (*dictionary_)[kAdbMode] = mode_json_obj;
349 }
350
setupwizard_mode() const351 std::string CuttlefishConfig::setupwizard_mode() const {
352 return (*dictionary_)[kSetupWizardMode].asString();
353 }
354
set_setupwizard_mode(const std::string & mode)355 void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) {
356 (*dictionary_)[kSetupWizardMode] = mode;
357 }
358
qemu_binary() const359 std::string CuttlefishConfig::qemu_binary() const {
360 return (*dictionary_)[kQemuBinary].asString();
361 }
362
set_qemu_binary(const std::string & qemu_binary)363 void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) {
364 (*dictionary_)[kQemuBinary] = qemu_binary;
365 }
366
crosvm_binary() const367 std::string CuttlefishConfig::crosvm_binary() const {
368 return (*dictionary_)[kCrosvmBinary].asString();
369 }
370
set_crosvm_binary(const std::string & crosvm_binary)371 void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) {
372 (*dictionary_)[kCrosvmBinary] = crosvm_binary;
373 }
374
tpm_binary() const375 std::string CuttlefishConfig::tpm_binary() const {
376 return (*dictionary_)[kTpmBinary].asString();
377 }
378
set_tpm_binary(const std::string & tpm_binary)379 void CuttlefishConfig::set_tpm_binary(const std::string& tpm_binary) {
380 (*dictionary_)[kTpmBinary] = tpm_binary;
381 }
382
tpm_device() const383 std::string CuttlefishConfig::tpm_device() const {
384 return (*dictionary_)[kTpmDevice].asString();
385 }
386
set_tpm_device(const std::string & tpm_device)387 void CuttlefishConfig::set_tpm_device(const std::string& tpm_device) {
388 (*dictionary_)[kTpmDevice] = tpm_device;
389 }
390
console_forwarder_binary() const391 std::string CuttlefishConfig::console_forwarder_binary() const {
392 return (*dictionary_)[kConsoleForwarderBinary].asString();
393 }
394
set_console_forwarder_binary(const std::string & binary)395 void CuttlefishConfig::set_console_forwarder_binary(
396 const std::string& binary) {
397 (*dictionary_)[kConsoleForwarderBinary] = binary;
398 }
399
kernel_log_monitor_binary() const400 std::string CuttlefishConfig::kernel_log_monitor_binary() const {
401 return (*dictionary_)[kKernelLogMonitorBinary].asString();
402 }
403
set_kernel_log_monitor_binary(const std::string & kernel_log_monitor_binary)404 void CuttlefishConfig::set_kernel_log_monitor_binary(
405 const std::string& kernel_log_monitor_binary) {
406 (*dictionary_)[kKernelLogMonitorBinary] = kernel_log_monitor_binary;
407 }
408
enable_vnc_server() const409 bool CuttlefishConfig::enable_vnc_server() const {
410 return (*dictionary_)[kEnableVncServer].asBool();
411 }
412
set_enable_vnc_server(bool enable_vnc_server)413 void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) {
414 (*dictionary_)[kEnableVncServer] = enable_vnc_server;
415 }
416
vnc_server_binary() const417 std::string CuttlefishConfig::vnc_server_binary() const {
418 return (*dictionary_)[kVncServerBinary].asString();
419 }
420
set_vnc_server_binary(const std::string & vnc_server_binary)421 void CuttlefishConfig::set_vnc_server_binary(
422 const std::string& vnc_server_binary) {
423 (*dictionary_)[kVncServerBinary] = vnc_server_binary;
424 }
425
set_enable_sandbox(const bool enable_sandbox)426 void CuttlefishConfig::set_enable_sandbox(const bool enable_sandbox) {
427 (*dictionary_)[kEnableSandbox] = enable_sandbox;
428 }
429
enable_sandbox() const430 bool CuttlefishConfig::enable_sandbox() const {
431 return (*dictionary_)[kEnableSandbox].asBool();
432 }
433
set_seccomp_policy_dir(const std::string & seccomp_policy_dir)434 void CuttlefishConfig::set_seccomp_policy_dir(const std::string& seccomp_policy_dir) {
435 if (seccomp_policy_dir.empty()) {
436 (*dictionary_)[kSeccompPolicyDir] = seccomp_policy_dir;
437 return;
438 }
439 SetPath(kSeccompPolicyDir, seccomp_policy_dir);
440 }
441
seccomp_policy_dir() const442 std::string CuttlefishConfig::seccomp_policy_dir() const {
443 return (*dictionary_)[kSeccompPolicyDir].asString();
444 }
445
set_enable_webrtc(bool enable_webrtc)446 void CuttlefishConfig::set_enable_webrtc(bool enable_webrtc) {
447 (*dictionary_)[kEnableWebRTC] = enable_webrtc;
448 }
449
enable_webrtc() const450 bool CuttlefishConfig::enable_webrtc() const {
451 return (*dictionary_)[kEnableWebRTC].asBool();
452 }
453
set_webrtc_binary(const std::string & webrtc_binary)454 void CuttlefishConfig::set_webrtc_binary(const std::string& webrtc_binary) {
455 (*dictionary_)[kWebRTCBinary] = webrtc_binary;
456 }
457
webrtc_binary() const458 std::string CuttlefishConfig::webrtc_binary() const {
459 return (*dictionary_)[kWebRTCBinary].asString();
460 }
461
set_webrtc_assets_dir(const std::string & webrtc_assets_dir)462 void CuttlefishConfig::set_webrtc_assets_dir(const std::string& webrtc_assets_dir) {
463 (*dictionary_)[kWebRTCAssetsDir] = webrtc_assets_dir;
464 }
465
webrtc_assets_dir() const466 std::string CuttlefishConfig::webrtc_assets_dir() const {
467 return (*dictionary_)[kWebRTCAssetsDir].asString();
468 }
469
set_webrtc_enable_adb_websocket(bool enable)470 void CuttlefishConfig::set_webrtc_enable_adb_websocket(bool enable) {
471 (*dictionary_)[kWebRTCEnableADBWebSocket] = enable;
472 }
473
webrtc_enable_adb_websocket() const474 bool CuttlefishConfig::webrtc_enable_adb_websocket() const {
475 return (*dictionary_)[kWebRTCEnableADBWebSocket].asBool();
476 }
477
restart_subprocesses() const478 bool CuttlefishConfig::restart_subprocesses() const {
479 return (*dictionary_)[kRestartSubprocesses].asBool();
480 }
481
set_restart_subprocesses(bool restart_subprocesses)482 void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) {
483 (*dictionary_)[kRestartSubprocesses] = restart_subprocesses;
484 }
485
run_adb_connector() const486 bool CuttlefishConfig::run_adb_connector() const {
487 return (*dictionary_)[kRunAdbConnector].asBool();
488 }
489
set_run_adb_connector(bool run_adb_connector)490 void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) {
491 (*dictionary_)[kRunAdbConnector] = run_adb_connector;
492 }
493
adb_connector_binary() const494 std::string CuttlefishConfig::adb_connector_binary() const {
495 return (*dictionary_)[kAdbConnectorBinary].asString();
496 }
497
set_adb_connector_binary(const std::string & adb_connector_binary)498 void CuttlefishConfig::set_adb_connector_binary(
499 const std::string& adb_connector_binary) {
500 (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary;
501 }
502
socket_vsock_proxy_binary() const503 std::string CuttlefishConfig::socket_vsock_proxy_binary() const {
504 return (*dictionary_)[kSocketVsockProxyBinary].asString();
505 }
506
set_socket_vsock_proxy_binary(const std::string & socket_vsock_proxy_binary)507 void CuttlefishConfig::set_socket_vsock_proxy_binary(
508 const std::string& socket_vsock_proxy_binary) {
509 (*dictionary_)[kSocketVsockProxyBinary] = socket_vsock_proxy_binary;
510 }
511
run_as_daemon() const512 bool CuttlefishConfig::run_as_daemon() const {
513 return (*dictionary_)[kRunAsDaemon].asBool();
514 }
515
set_run_as_daemon(bool run_as_daemon)516 void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) {
517 (*dictionary_)[kRunAsDaemon] = run_as_daemon;
518 }
data_policy() const519 std::string CuttlefishConfig::data_policy() const {
520 return (*dictionary_)[kDataPolicy].asString();
521 }
522
set_data_policy(const std::string & data_policy)523 void CuttlefishConfig::set_data_policy(const std::string& data_policy) {
524 (*dictionary_)[kDataPolicy] = data_policy;
525 }
526
blank_data_image_mb() const527 int CuttlefishConfig::blank_data_image_mb() const {
528 return (*dictionary_)[kBlankDataImageMb].asInt();
529 }
530
set_blank_data_image_mb(int blank_data_image_mb)531 void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) {
532 (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb;
533 }
534
blank_data_image_fmt() const535 std::string CuttlefishConfig::blank_data_image_fmt() const {
536 return (*dictionary_)[kBlankDataImageFmt].asString();
537 }
538
set_blank_data_image_fmt(const std::string & blank_data_image_fmt)539 void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) {
540 (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt;
541 }
542
set_logcat_receiver_binary(const std::string & binary)543 void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) {
544 SetPath(kLogcatReceiverBinary, binary);
545 }
546
logcat_receiver_binary() const547 std::string CuttlefishConfig::logcat_receiver_binary() const {
548 return (*dictionary_)[kLogcatReceiverBinary].asString();
549 }
550
set_config_server_binary(const std::string & binary)551 void CuttlefishConfig::set_config_server_binary(const std::string& binary) {
552 SetPath(kConfigServerBinary, binary);
553 }
554
config_server_binary() const555 std::string CuttlefishConfig::config_server_binary() const {
556 return (*dictionary_)[kConfigServerBinary].asString();
557 }
558
enable_tombstone_receiver() const559 bool CuttlefishConfig::enable_tombstone_receiver() const {
560 return (*dictionary_)[kRunTombstoneReceiver].asBool();
561 }
562
set_enable_tombstone_receiver(bool enable_tombstone_receiver)563 void CuttlefishConfig::set_enable_tombstone_receiver(bool enable_tombstone_receiver) {
564 (*dictionary_)[kRunTombstoneReceiver] = enable_tombstone_receiver;
565 }
566
tombstone_receiver_binary() const567 std::string CuttlefishConfig::tombstone_receiver_binary() const {
568 return (*dictionary_)[kTombstoneReceiverBinary].asString();
569 }
570
set_tombstone_receiver_binary(const std::string & e2e_test_binary)571 void CuttlefishConfig::set_tombstone_receiver_binary(const std::string& e2e_test_binary) {
572 (*dictionary_)[kTombstoneReceiverBinary] = e2e_test_binary;
573 }
574
use_bootloader() const575 bool CuttlefishConfig::use_bootloader() const {
576 return (*dictionary_)[kUseBootloader].asBool();
577 }
578
set_use_bootloader(bool use_bootloader)579 void CuttlefishConfig::set_use_bootloader(bool use_bootloader) {
580 (*dictionary_)[kUseBootloader] = use_bootloader;
581 }
582
bootloader() const583 std::string CuttlefishConfig::bootloader() const {
584 return (*dictionary_)[kBootloader].asString();
585 }
586
set_bootloader(const std::string & bootloader)587 void CuttlefishConfig::set_bootloader(const std::string& bootloader) {
588 SetPath(kBootloader, bootloader);
589 }
590
set_boot_slot(const std::string & boot_slot)591 void CuttlefishConfig::set_boot_slot(const std::string& boot_slot) {
592 (*dictionary_)[kBootSlot] = boot_slot;
593 }
594
boot_slot() const595 std::string CuttlefishConfig::boot_slot() const {
596 return (*dictionary_)[kBootSlot].asString();
597 }
598
set_webrtc_certs_dir(const std::string & certs_dir)599 void CuttlefishConfig::set_webrtc_certs_dir(const std::string& certs_dir) {
600 (*dictionary_)[kWebRTCCertsDir] = certs_dir;
601 }
602
webrtc_certs_dir() const603 std::string CuttlefishConfig::webrtc_certs_dir() const {
604 return (*dictionary_)[kWebRTCCertsDir].asString();
605 }
606
set_sig_server_binary(const std::string & binary)607 void CuttlefishConfig::set_sig_server_binary(const std::string& binary) {
608 SetPath(kSigServerBinary, binary);
609 }
610
sig_server_binary() const611 std::string CuttlefishConfig::sig_server_binary() const {
612 return (*dictionary_)[kSigServerBinary].asString();
613 }
614
set_sig_server_port(int port)615 void CuttlefishConfig::set_sig_server_port(int port) {
616 (*dictionary_)[kSigServerPort] = port;
617 }
618
sig_server_port() const619 int CuttlefishConfig::sig_server_port() const {
620 return (*dictionary_)[kSigServerPort].asInt();
621 }
622
set_webrtc_udp_port_range(std::pair<uint16_t,uint16_t> range)623 void CuttlefishConfig::set_webrtc_udp_port_range(
624 std::pair<uint16_t, uint16_t> range) {
625 Json::Value arr(Json::ValueType::arrayValue);
626 arr[0] = range.first;
627 arr[1] = range.second;
628 (*dictionary_)[kWebrtcUdpPortRange] = arr;
629 }
630
webrtc_udp_port_range() const631 std::pair<uint16_t, uint16_t> CuttlefishConfig::webrtc_udp_port_range() const {
632 std::pair<uint16_t, uint16_t> ret;
633 ret.first = (*dictionary_)[kWebrtcUdpPortRange][0].asInt();
634 ret.second = (*dictionary_)[kWebrtcUdpPortRange][1].asInt();
635 return ret;
636 }
637
set_webrtc_tcp_port_range(std::pair<uint16_t,uint16_t> range)638 void CuttlefishConfig::set_webrtc_tcp_port_range(
639 std::pair<uint16_t, uint16_t> range) {
640 Json::Value arr(Json::ValueType::arrayValue);
641 arr[0] = range.first;
642 arr[1] = range.second;
643 (*dictionary_)[kWebrtcTcpPortRange] = arr;
644 }
645
webrtc_tcp_port_range() const646 std::pair<uint16_t, uint16_t> CuttlefishConfig::webrtc_tcp_port_range() const {
647 std::pair<uint16_t, uint16_t> ret;
648 ret.first = (*dictionary_)[kWebrtcTcpPortRange][0].asInt();
649 ret.second = (*dictionary_)[kWebrtcTcpPortRange][1].asInt();
650 return ret;
651 }
652
set_sig_server_address(const std::string & addr)653 void CuttlefishConfig::set_sig_server_address(const std::string& addr) {
654 (*dictionary_)[kSigServerAddress] = addr;
655 }
656
sig_server_address() const657 std::string CuttlefishConfig::sig_server_address() const {
658 return (*dictionary_)[kSigServerAddress].asString();
659 }
660
set_sig_server_path(const std::string & path)661 void CuttlefishConfig::set_sig_server_path(const std::string& path) {
662 // Don't use SetPath here, it's a URL path not a file system path
663 (*dictionary_)[kSigServerPath] = path;
664 }
665
sig_server_path() const666 std::string CuttlefishConfig::sig_server_path() const {
667 return (*dictionary_)[kSigServerPath].asString();
668 }
669
set_sig_server_strict(bool strict)670 void CuttlefishConfig::set_sig_server_strict(bool strict) {
671 (*dictionary_)[kSigServerStrict] = strict;
672 }
673
sig_server_strict() const674 bool CuttlefishConfig::sig_server_strict() const {
675 return (*dictionary_)[kSigServerStrict].asBool();
676 }
677
enable_modem_simulator() const678 bool CuttlefishConfig::enable_modem_simulator() const {
679 return (*dictionary_)[kRunModemSimulator].asBool();
680 }
681
set_enable_modem_simulator(bool enable_modem_simulator)682 void CuttlefishConfig::set_enable_modem_simulator(bool enable_modem_simulator) {
683 (*dictionary_)[kRunModemSimulator] = enable_modem_simulator;
684 }
685
modem_simulator_binary() const686 std::string CuttlefishConfig::modem_simulator_binary() const {
687 return (*dictionary_)[kModemSimulatorBinary].asString();
688 }
689
set_modem_simulator_binary(const std::string & binary)690 void CuttlefishConfig::set_modem_simulator_binary(const std::string& binary) {
691 (*dictionary_)[kModemSimulatorBinary] = binary;
692 }
693
set_modem_simulator_instance_number(int instance_number)694 void CuttlefishConfig::set_modem_simulator_instance_number(
695 int instance_number) {
696 (*dictionary_)[kModemSimulatorInstanceNumber] = instance_number;
697 }
698
modem_simulator_instance_number() const699 int CuttlefishConfig::modem_simulator_instance_number() const {
700 return (*dictionary_)[kModemSimulatorInstanceNumber].asInt();
701 }
702
set_loop_max_part(int loop_max_part)703 void CuttlefishConfig::set_loop_max_part(int loop_max_part) {
704 (*dictionary_)[kLoopMaxPart] = loop_max_part;
705 }
loop_max_part() const706 int CuttlefishConfig::loop_max_part() const {
707 return (*dictionary_)[kLoopMaxPart].asInt();
708 }
709
set_guest_enforce_security(bool guest_enforce_security)710 void CuttlefishConfig::set_guest_enforce_security(bool guest_enforce_security) {
711 (*dictionary_)[kGuestEnforceSecurity] = guest_enforce_security;
712 }
guest_enforce_security() const713 bool CuttlefishConfig::guest_enforce_security() const {
714 return (*dictionary_)[kGuestEnforceSecurity].asBool();
715 }
716
set_guest_audit_security(bool guest_audit_security)717 void CuttlefishConfig::set_guest_audit_security(bool guest_audit_security) {
718 (*dictionary_)[kGuestAuditSecurity] = guest_audit_security;
719 }
guest_audit_security() const720 bool CuttlefishConfig::guest_audit_security() const {
721 return (*dictionary_)[kGuestAuditSecurity].asBool();
722 }
723
set_guest_force_normal_boot(bool guest_force_normal_boot)724 void CuttlefishConfig::set_guest_force_normal_boot(bool guest_force_normal_boot) {
725 (*dictionary_)[kGuestForceNormalBoot] = guest_force_normal_boot;
726 }
guest_force_normal_boot() const727 bool CuttlefishConfig::guest_force_normal_boot() const {
728 return (*dictionary_)[kGuestForceNormalBoot].asBool();
729 }
730
set_enable_metrics(std::string enable_metrics)731 void CuttlefishConfig::set_enable_metrics(std::string enable_metrics) {
732 (*dictionary_)[kEnableMetrics] = kUnknown;
733 if (!enable_metrics.empty()) {
734 switch (enable_metrics.at(0)) {
735 case 'y':
736 case 'Y':
737 (*dictionary_)[kEnableMetrics] = kYes;
738 break;
739 case 'n':
740 case 'N':
741 (*dictionary_)[kEnableMetrics] = kNo;
742 break;
743 }
744 }
745 }
746
enable_metrics() const747 CuttlefishConfig::Answer CuttlefishConfig::enable_metrics() const {
748 return (CuttlefishConfig::Answer)(*dictionary_)[kEnableMetrics].asInt();
749 }
750
set_metrics_binary(const std::string & metrics_binary)751 void CuttlefishConfig::set_metrics_binary(const std::string& metrics_binary) {
752 (*dictionary_)[kMetricsBinary] = metrics_binary;
753 }
754
metrics_binary() const755 std::string CuttlefishConfig::metrics_binary() const {
756 return (*dictionary_)[kMetricsBinary].asString();
757 }
758
set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline)759 void CuttlefishConfig::set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline) {
760 Json::Value args_json_obj(Json::arrayValue);
761 for (const auto& arg : android::base::Split(boot_image_kernel_cmdline, " ")) {
762 args_json_obj.append(arg);
763 }
764 (*dictionary_)[kBootImageKernelCmdline] = args_json_obj;
765 }
boot_image_kernel_cmdline() const766 std::vector<std::string> CuttlefishConfig::boot_image_kernel_cmdline() const {
767 std::vector<std::string> cmdline;
768 for (const Json::Value& arg : (*dictionary_)[kBootImageKernelCmdline]) {
769 cmdline.push_back(arg.asString());
770 }
771 return cmdline;
772 }
773
set_extra_kernel_cmdline(std::string extra_cmdline)774 void CuttlefishConfig::set_extra_kernel_cmdline(std::string extra_cmdline) {
775 Json::Value args_json_obj(Json::arrayValue);
776 for (const auto& arg : android::base::Split(extra_cmdline, " ")) {
777 args_json_obj.append(arg);
778 }
779 (*dictionary_)[kExtraKernelCmdline] = args_json_obj;
780 }
extra_kernel_cmdline() const781 std::vector<std::string> CuttlefishConfig::extra_kernel_cmdline() const {
782 std::vector<std::string> cmdline;
783 for (const Json::Value& arg : (*dictionary_)[kExtraKernelCmdline]) {
784 cmdline.push_back(arg.asString());
785 }
786 return cmdline;
787 }
788
set_vm_manager_kernel_cmdline(std::string vm_manager_cmdline)789 void CuttlefishConfig::set_vm_manager_kernel_cmdline(std::string vm_manager_cmdline) {
790 Json::Value args_json_obj(Json::arrayValue);
791 for (const auto& arg : android::base::Split(vm_manager_cmdline, " ")) {
792 args_json_obj.append(arg);
793 }
794 (*dictionary_)[kVmManagerKernelCmdline] = args_json_obj;
795 }
vm_manager_kernel_cmdline() const796 std::vector<std::string> CuttlefishConfig::vm_manager_kernel_cmdline() const {
797 std::vector<std::string> cmdline;
798 for (const Json::Value& arg : (*dictionary_)[kVmManagerKernelCmdline]) {
799 cmdline.push_back(arg.asString());
800 }
801 return cmdline;
802 }
803
set_ril_dns(const std::string & ril_dns)804 void CuttlefishConfig::set_ril_dns(const std::string& ril_dns) {
805 (*dictionary_)[kRilDns] = ril_dns;
806 }
ril_dns() const807 std::string CuttlefishConfig::ril_dns() const {
808 return (*dictionary_)[kRilDns].asString();
809 }
810
set_kgdb(bool kgdb)811 void CuttlefishConfig::set_kgdb(bool kgdb) {
812 (*dictionary_)[kKgdb] = kgdb;
813 }
kgdb() const814 bool CuttlefishConfig::kgdb() const {
815 return (*dictionary_)[kKgdb].asBool();
816 }
817
818 // Creates the (initially empty) config object and populates it with values from
819 // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present.
820 // Returns nullptr if there was an error loading from file
BuildConfigImpl()821 /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() {
822 auto config_file_path = cuttlefish::StringFromEnv(kCuttlefishConfigEnvVarName,
823 cuttlefish::GetGlobalConfigFileLink());
824 auto ret = new CuttlefishConfig();
825 if (ret) {
826 auto loaded = ret->LoadFromFile(config_file_path.c_str());
827 if (!loaded) {
828 delete ret;
829 return nullptr;
830 }
831 }
832 return ret;
833 }
834
Get()835 /*static*/ const CuttlefishConfig* CuttlefishConfig::Get() {
836 static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl());
837 return config.get();
838 }
839
ConfigExists()840 /*static*/ bool CuttlefishConfig::ConfigExists() {
841 auto config_file_path = cuttlefish::StringFromEnv(kCuttlefishConfigEnvVarName,
842 cuttlefish::GetGlobalConfigFileLink());
843 auto real_file_path = cuttlefish::AbsolutePath(config_file_path.c_str());
844 return cuttlefish::FileExists(real_file_path);
845 }
846
CuttlefishConfig()847 CuttlefishConfig::CuttlefishConfig() : dictionary_(new Json::Value()) {}
848 // Can't use '= default' on the header because the compiler complains of
849 // Json::Value being an incomplete type
850 CuttlefishConfig::~CuttlefishConfig() = default;
851
852 CuttlefishConfig::CuttlefishConfig(CuttlefishConfig&&) = default;
853 CuttlefishConfig& CuttlefishConfig::operator=(CuttlefishConfig&&) = default;
854
LoadFromFile(const char * file)855 bool CuttlefishConfig::LoadFromFile(const char* file) {
856 auto real_file_path = cuttlefish::AbsolutePath(file);
857 if (real_file_path.empty()) {
858 LOG(ERROR) << "Could not get real path for file " << file;
859 return false;
860 }
861 Json::Reader reader;
862 std::ifstream ifs(real_file_path);
863 if (!reader.parse(ifs, *dictionary_)) {
864 LOG(ERROR) << "Could not read config file " << file << ": "
865 << reader.getFormattedErrorMessages();
866 return false;
867 }
868 return true;
869 }
SaveToFile(const std::string & file) const870 bool CuttlefishConfig::SaveToFile(const std::string& file) const {
871 std::ofstream ofs(file);
872 if (!ofs.is_open()) {
873 LOG(ERROR) << "Unable to write to file " << file;
874 return false;
875 }
876 ofs << *dictionary_;
877 return !ofs.fail();
878 }
879
AssemblyPath(const std::string & file_name) const880 std::string CuttlefishConfig::AssemblyPath(
881 const std::string& file_name) const {
882 return cuttlefish::AbsolutePath(assembly_dir() + "/" + file_name);
883 }
884
composite_disk_path() const885 std::string CuttlefishConfig::composite_disk_path() const {
886 return AssemblyPath("composite.img");
887 }
888
ForInstance(int num)889 CuttlefishConfig::MutableInstanceSpecific CuttlefishConfig::ForInstance(int num) {
890 return MutableInstanceSpecific(this, std::to_string(num));
891 }
892
ForInstance(int num) const893 CuttlefishConfig::InstanceSpecific CuttlefishConfig::ForInstance(int num) const {
894 return InstanceSpecific(this, std::to_string(num));
895 }
896
ForDefaultInstance() const897 CuttlefishConfig::InstanceSpecific CuttlefishConfig::ForDefaultInstance() const {
898 return InstanceSpecific(this, std::to_string(GetInstance()));
899 }
900
Instances() const901 std::vector<CuttlefishConfig::InstanceSpecific> CuttlefishConfig::Instances() const {
902 const auto& json = (*dictionary_)[kInstances];
903 std::vector<CuttlefishConfig::InstanceSpecific> instances;
904 for (const auto& name : json.getMemberNames()) {
905 instances.push_back(CuttlefishConfig::InstanceSpecific(this, name));
906 }
907 return instances;
908 }
909
GetInstance()910 int GetInstance() {
911 static int instance_id = InstanceFromEnvironment();
912 return instance_id;
913 }
914
GetGlobalConfigFileLink()915 std::string GetGlobalConfigFileLink() {
916 return cuttlefish::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json";
917 }
918
ForCurrentInstance(const char * prefix)919 std::string ForCurrentInstance(const char* prefix) {
920 std::ostringstream stream;
921 stream << prefix << std::setfill('0') << std::setw(2) << GetInstance();
922 return stream.str();
923 }
ForCurrentInstance(int base)924 int ForCurrentInstance(int base) { return base + GetInstance() - 1; }
925
RandomSerialNumber(const std::string & prefix)926 std::string RandomSerialNumber(const std::string& prefix) {
927 const char hex_characters[] = "0123456789ABCDEF";
928 std::srand(time(0));
929 char str[10];
930 for(int i=0; i<10; i++){
931 str[i] = hex_characters[rand() % strlen(hex_characters)];
932 }
933 return prefix + str;
934 }
935
GetDefaultPerInstanceVsockCid()936 int GetDefaultPerInstanceVsockCid() {
937 constexpr int kFirstGuestCid = 3;
938 return cuttlefish::HostSupportsVsock() ? ForCurrentInstance(kFirstGuestCid) : 0;
939 }
940
DefaultHostArtifactsPath(const std::string & file_name)941 std::string DefaultHostArtifactsPath(const std::string& file_name) {
942 return (cuttlefish::StringFromEnv("ANDROID_HOST_OUT",
943 cuttlefish::StringFromEnv("HOME", ".")) +
944 "/") +
945 file_name;
946 }
947
DefaultGuestImagePath(const std::string & file_name)948 std::string DefaultGuestImagePath(const std::string& file_name) {
949 return (cuttlefish::StringFromEnv("ANDROID_PRODUCT_OUT",
950 cuttlefish::StringFromEnv("HOME", "."))) +
951 file_name;
952 }
953
HostSupportsQemuCli()954 bool HostSupportsQemuCli() {
955 static bool supported =
956 std::system(
957 "/usr/lib/cuttlefish-common/bin/capability_query.py qemu_cli") == 0;
958 return supported;
959 }
960
HostSupportsVsock()961 bool HostSupportsVsock() {
962 static bool supported =
963 std::system(
964 "/usr/lib/cuttlefish-common/bin/capability_query.py vsock") == 0;
965 return supported;
966 }
967 } // namespace cuttlefish
968