blob: 9dee73351c57836cf0185baaa9159aba48309e55 [file] [log] [blame]
Jorge E. Moreira577383b2018-05-24 14:17:51 -07001/*
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. Moreira2a777f62018-06-13 17:28:10 -070031#include "common/libs/utils/environment.h"
Ryan Haining10e42312018-07-17 12:11:52 -070032#include "common/libs/utils/files.h"
Jorge E. Moreira2a777f62018-06-13 17:28:10 -070033
Jorge E. Moreira577383b2018-05-24 14:17:51 -070034DEFINE_string(config_file,
Jorge E. Moreira015c0002018-07-19 11:01:16 -070035 vsoc::GetGlobalConfigFileLink(),
Jorge E. Moreira577383b2018-05-24 14:17:51 -070036 "A file from where to load the config values. This flag is "
37 "ignored by the launcher");
38
39namespace {
40
Jorge E. Moreira577383b2018-05-24 14:17:51 -070041int 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 Haining10e42312018-07-17 12:11:52 -070068
Jorge E. Moreira577383b2018-05-24 14:17:51 -070069const char* kSerialNumber = "serial_number";
70const char* kInstanceDir = "instance_dir";
Jorge E. Moreira88322832018-07-22 16:41:01 -070071const char* kVmManager = "vm_manager";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070072
73const char* kCpus = "cpus";
74const char* kMemoryMb = "memory_mb";
75const char* kDpi = "dpi";
76const char* kXRes = "x_res";
77const char* kYRes = "y_res";
78const char* kRefreshRateHz = "refresh_rate_hz";
79
80const char* kKernelImagePath = "kernel_image_path";
Greg Hartman91f81422018-07-09 16:04:49 -070081const char* kGdbFlag = "gdb_flag";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070082const char* kKernelArgs = "kernel_args";
83const char* kRamdiskImagePath = "ramdisk_image_path";
84
85const char* kSystemImagePath = "system_image_path";
86const char* kCacheImagePath = "cache_image_path";
87const char* kDataImagePath = "data_image_path";
88const char* kVendorImagePath = "vendor_image_path";
89const char* kUsbV1SocketName = "usb_v1_socket_name";
90const char* kVhciPort = "vhci_port";
91const char* kUsbIpSocketName = "usb_ip_socket_name";
92const char* kKernelLogSocketName = "kernel_log_socket_name";
93const char* kConsolePath = "console_path";
94const char* kLogcatPath = "logcat_path";
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -070095const char* kLauncherLogPath = "launcher_log_path";
Jorge E. Moreira50a07752018-07-18 18:49:04 -070096const char* kLauncherMonitorPath = "launcher_monitor_socket";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070097const char* kDtbPath = "dtb_path";
98
99const char* kMempath = "mempath";
100const char* kIvshmemQemuSocketPath = "ivshmem_qemu_socket_path";
101const char* kIvshmemClientSocketPath = "ivshmem_client_socket_path";
102const char* kIvshmemVectorCount = "ivshmem_vector_count";
103
104const char* kMobileBridgeName = "mobile_bridge_name";
105const char* kMobileTapName = "mobile_tap_name";
Cody Schuffelen975175a2018-06-14 18:20:02 -0700106const char* kWifiBridgeName = "wifi_bridge_name";
107const char* kWifiTapName = "wifi_tap_name";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700108const char* kWifiGuestMacAddr = "wifi_guest_mac_addr";
109const char* kWifiHostMacAddr = "wifi_host_mac_addr";
110const char* kEntropySource = "entropy_source";
111
112const char* kUuid = "uuid";
113const char* kDisableDacSecurity = "disable_dac_security";
114const char* kDisableAppArmorSecurity = "disable_app_armor_security";
Greg Hartman6abdbb92018-05-24 09:49:00 -0700115const char* kCuttlefishEnvPath = "cuttlefish_env_path";
Ryan Haining4a1abea2018-07-10 16:20:19 -0700116
117const char* kAdbMode = "adb_mode";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700118} // namespace
119
120namespace vsoc {
121
122std::string CuttlefishConfig::instance_dir() const {
123 return (*dictionary_)[kInstanceDir].asString();
124}
125void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) {
126 (*dictionary_)[kInstanceDir] = instance_dir;
127}
128
Jorge E. Moreira88322832018-07-22 16:41:01 -0700129std::string CuttlefishConfig::vm_manager() const {
130 return (*dictionary_)[kVmManager].asString();
131}
132void CuttlefishConfig::set_vm_manager(const std::string& name) {
133 (*dictionary_)[kVmManager] = name;
134}
135
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700136std::string CuttlefishConfig::serial_number() const {
137 return (*dictionary_)[kSerialNumber].asString();
138}
139void CuttlefishConfig::set_serial_number(const std::string& serial_number) {
140 (*dictionary_)[kSerialNumber] = serial_number;
141}
142
143int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); }
144void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; }
145
146int CuttlefishConfig::memory_mb() const {
147 return (*dictionary_)[kMemoryMb].asInt();
148}
149void CuttlefishConfig::set_memory_mb(int memory_mb) {
150 (*dictionary_)[kMemoryMb] = memory_mb;
151}
152
153int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); }
154void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; }
155
156int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); }
157void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; }
158
159int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); }
160void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; }
161
162int CuttlefishConfig::refresh_rate_hz() const {
163 return (*dictionary_)[kRefreshRateHz].asInt();
164}
165void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) {
166 (*dictionary_)[kRefreshRateHz] = refresh_rate_hz;
167}
168
169std::string CuttlefishConfig::kernel_image_path() const {
170 return (*dictionary_)[kKernelImagePath].asString();
171}
Greg Hartman91f81422018-07-09 16:04:49 -0700172
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700173void 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. Moreira577383b2018-05-24 14:17:51 -0700180void CuttlefishConfig::set_kernel_image_path(
181 const std::string& kernel_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700182 SetPath(kKernelImagePath, kernel_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700183}
184
Greg Hartman91f81422018-07-09 16:04:49 -0700185std::string CuttlefishConfig::gdb_flag() const {
186 return (*dictionary_)[kGdbFlag].asString();
187}
188
189void CuttlefishConfig::set_gdb_flag(
190 const std::string& device) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700191 SetPath(kGdbFlag, device);
Greg Hartman91f81422018-07-09 16:04:49 -0700192}
193
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700194std::string CuttlefishConfig::kernel_args() const {
195 return (*dictionary_)[kKernelArgs].asString();
196}
197void CuttlefishConfig::set_kernel_args(const std::string& kernel_args) {
198 (*dictionary_)[kKernelArgs] = kernel_args;
199}
200
201std::string CuttlefishConfig::ramdisk_image_path() const {
202 return (*dictionary_)[kRamdiskImagePath].asString();
203}
204void CuttlefishConfig::set_ramdisk_image_path(
205 const std::string& ramdisk_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700206 SetPath(kRamdiskImagePath, ramdisk_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700207}
208
209std::string CuttlefishConfig::system_image_path() const {
210 return (*dictionary_)[kSystemImagePath].asString();
211}
212void CuttlefishConfig::set_system_image_path(
213 const std::string& system_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700214 SetPath(kSystemImagePath, system_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700215}
216
217std::string CuttlefishConfig::cache_image_path() const {
218 return (*dictionary_)[kCacheImagePath].asString();
219}
220void CuttlefishConfig::set_cache_image_path(
221 const std::string& cache_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700222 SetPath(kCacheImagePath, cache_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700223}
224
225std::string CuttlefishConfig::data_image_path() const {
226 return (*dictionary_)[kDataImagePath].asString();
227}
228void CuttlefishConfig::set_data_image_path(const std::string& data_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700229 SetPath(kDataImagePath, data_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700230}
231
232std::string CuttlefishConfig::vendor_image_path() const {
233 return (*dictionary_)[kVendorImagePath].asString();
234}
235void CuttlefishConfig::set_vendor_image_path(
236 const std::string& vendor_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700237 SetPath(kVendorImagePath, vendor_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700238}
239
240std::string CuttlefishConfig::dtb_path() const {
241 return (*dictionary_)[kDtbPath].asString();
242}
243void CuttlefishConfig::set_dtb_path(const std::string& dtb_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700244 SetPath(kDtbPath, dtb_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700245}
246
247std::string CuttlefishConfig::mempath() const {
248 return (*dictionary_)[kMempath].asString();
249}
250void CuttlefishConfig::set_mempath(const std::string& mempath) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700251 SetPath(kMempath, mempath);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700252}
253
254std::string CuttlefishConfig::ivshmem_qemu_socket_path() const {
255 return (*dictionary_)[kIvshmemQemuSocketPath].asString();
256}
257void CuttlefishConfig::set_ivshmem_qemu_socket_path(
258 const std::string& ivshmem_qemu_socket_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700259 SetPath(kIvshmemQemuSocketPath, ivshmem_qemu_socket_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700260}
261
262std::string CuttlefishConfig::ivshmem_client_socket_path() const {
263 return (*dictionary_)[kIvshmemClientSocketPath].asString();
264}
265void CuttlefishConfig::set_ivshmem_client_socket_path(
266 const std::string& ivshmem_client_socket_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700267 SetPath(kIvshmemClientSocketPath, ivshmem_client_socket_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700268}
269
270int CuttlefishConfig::ivshmem_vector_count() const {
271 return (*dictionary_)[kIvshmemVectorCount].asInt();
272}
273void CuttlefishConfig::set_ivshmem_vector_count(int ivshmem_vector_count) {
274 (*dictionary_)[kIvshmemVectorCount] = ivshmem_vector_count;
275}
276
277std::string CuttlefishConfig::usb_v1_socket_name() const {
278 return (*dictionary_)[kUsbV1SocketName].asString();
279}
280void CuttlefishConfig::set_usb_v1_socket_name(
281 const std::string& usb_v1_socket_name) {
282 (*dictionary_)[kUsbV1SocketName] = usb_v1_socket_name;
283}
284
285int CuttlefishConfig::vhci_port() const {
286 return (*dictionary_)[kVhciPort].asInt();
287}
288void CuttlefishConfig::set_vhci_port(int vhci_port) {
289 (*dictionary_)[kVhciPort] = vhci_port;
290}
291
292std::string CuttlefishConfig::usb_ip_socket_name() const {
293 return (*dictionary_)[kUsbIpSocketName].asString();
294}
295void CuttlefishConfig::set_usb_ip_socket_name(
296 const std::string& usb_ip_socket_name) {
297 (*dictionary_)[kUsbIpSocketName] = usb_ip_socket_name;
298}
299
300std::string CuttlefishConfig::kernel_log_socket_name() const {
301 return (*dictionary_)[kKernelLogSocketName].asString();
302}
303void CuttlefishConfig::set_kernel_log_socket_name(
304 const std::string& kernel_log_socket_name) {
305 (*dictionary_)[kKernelLogSocketName] = kernel_log_socket_name;
306}
307
308std::string CuttlefishConfig::console_path() const {
309 return (*dictionary_)[kConsolePath].asString();
310}
311void CuttlefishConfig::set_console_path(const std::string& console_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700312 SetPath(kConsolePath, console_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700313}
314
315std::string CuttlefishConfig::logcat_path() const {
316 return (*dictionary_)[kLogcatPath].asString();
317}
318void CuttlefishConfig::set_logcat_path(const std::string& logcat_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700319 SetPath(kLogcatPath, logcat_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700320}
321
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700322std::string CuttlefishConfig::launcher_monitor_socket_path() const {
323 return (*dictionary_)[kLauncherMonitorPath].asString();
324}
325void CuttlefishConfig::set_launcher_monitor_socket_path(
Jorge E. Moreirabe7049a2018-07-26 18:12:13 -0700326 const std::string& launcher_monitor_path) {
327 SetPath(kLauncherMonitorPath, launcher_monitor_path);
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700328}
329
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700330std::string CuttlefishConfig::launcher_log_path() const {
331 return (*dictionary_)[kLauncherLogPath].asString();
332}
333void CuttlefishConfig::set_launcher_log_path(
334 const std::string& launcher_log_path) {
335 (*dictionary_)[kLauncherLogPath] = launcher_log_path;
336}
337
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700338std::string CuttlefishConfig::mobile_bridge_name() const {
339 return (*dictionary_)[kMobileBridgeName].asString();
340}
341void CuttlefishConfig::set_mobile_bridge_name(
342 const std::string& mobile_bridge_name) {
343 (*dictionary_)[kMobileBridgeName] = mobile_bridge_name;
344}
345
Cody Schuffelen975175a2018-06-14 18:20:02 -0700346std::string CuttlefishConfig::wifi_bridge_name() const {
347 return (*dictionary_)[kWifiBridgeName].asString();
348}
349void CuttlefishConfig::set_wifi_bridge_name(
350 const std::string& wifi_bridge_name) {
351 (*dictionary_)[kWifiBridgeName] = wifi_bridge_name;
352}
353
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700354std::string CuttlefishConfig::wifi_guest_mac_addr() const {
355 return (*dictionary_)[kWifiGuestMacAddr].asString();
356}
357void CuttlefishConfig::set_wifi_guest_mac_addr(
358 const std::string& wifi_guest_mac_addr) {
359 (*dictionary_)[kWifiGuestMacAddr] = wifi_guest_mac_addr;
360}
361
362std::string CuttlefishConfig::wifi_host_mac_addr() const {
363 return (*dictionary_)[kWifiHostMacAddr].asString();
364}
365void CuttlefishConfig::set_wifi_host_mac_addr(
366 const std::string& wifi_host_mac_addr) {
367 (*dictionary_)[kWifiHostMacAddr] = wifi_host_mac_addr;
368}
369
370std::string CuttlefishConfig::mobile_tap_name() const {
371 return (*dictionary_)[kMobileTapName].asString();
372}
373void CuttlefishConfig::set_mobile_tap_name(const std::string& mobile_tap_name) {
374 (*dictionary_)[kMobileTapName] = mobile_tap_name;
375}
376
Cody Schuffelen975175a2018-06-14 18:20:02 -0700377std::string CuttlefishConfig::wifi_tap_name() const {
378 return (*dictionary_)[kWifiTapName].asString();
379}
380void CuttlefishConfig::set_wifi_tap_name(const std::string& wifi_tap_name) {
381 (*dictionary_)[kWifiTapName] = wifi_tap_name;
382}
383
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700384std::string CuttlefishConfig::entropy_source() const {
385 return (*dictionary_)[kEntropySource].asString();
386}
387void CuttlefishConfig::set_entropy_source(const std::string& entropy_source) {
388 (*dictionary_)[kEntropySource] = entropy_source;
389}
390
391std::string CuttlefishConfig::uuid() const {
392 return (*dictionary_)[kUuid].asString();
393}
394void CuttlefishConfig::set_uuid(const std::string& uuid) {
395 (*dictionary_)[kUuid] = uuid;
396}
397
398bool CuttlefishConfig::disable_dac_security() const {
399 return (*dictionary_)[kDisableDacSecurity].asBool();
400}
401void CuttlefishConfig::set_disable_dac_security(bool disable_dac_security) {
402 (*dictionary_)[kDisableDacSecurity] = disable_dac_security;
403}
404
Greg Hartman6abdbb92018-05-24 09:49:00 -0700405void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700406 SetPath(kCuttlefishEnvPath, path);
Greg Hartman6abdbb92018-05-24 09:49:00 -0700407}
408std::string CuttlefishConfig::cuttlefish_env_path() const {
409 return (*dictionary_)[kCuttlefishEnvPath].asString();
410}
411
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700412bool CuttlefishConfig::disable_app_armor_security() const {
413 return (*dictionary_)[kDisableAppArmorSecurity].asBool();
414}
415void CuttlefishConfig::set_disable_app_armor_security(
416 bool disable_app_armor_security) {
417 (*dictionary_)[kDisableAppArmorSecurity] = disable_app_armor_security;
418}
419
Ryan Haining4a1abea2018-07-10 16:20:19 -0700420std::string CuttlefishConfig::adb_mode() const {
421 return (*dictionary_)[kAdbMode].asString();
422}
423
424void CuttlefishConfig::set_adb_mode(const std::string& mode) {
425 (*dictionary_)[kAdbMode] = mode;
426}
427
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700428// 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. Moreira577383b2018-05-24 14:17:51 -0700438 }
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700439 return ret;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700440}
441
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700442/*static*/ CuttlefishConfig* CuttlefishConfig::Get() {
443 static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl());
444 return config.get();
445}
446
447CuttlefishConfig::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
450CuttlefishConfig::~CuttlefishConfig() {}
451
452bool CuttlefishConfig::LoadFromFile(const char* file) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700453 auto real_file_path = cvd::AbsolutePath(file);
Ryan Haining10e42312018-07-17 12:11:52 -0700454 if (real_file_path.empty()) {
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700455 LOG(ERROR) << "Could not get real path for file " << file;
456 return false;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700457 }
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700458 Json::Reader reader;
459 std::ifstream ifs(real_file_path);
460 if (!reader.parse(ifs, *dictionary_)) {
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700461 LOG(ERROR) << "Could not read config file " << file << ": "
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700462 << reader.getFormattedErrorMessages();
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700463 return false;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700464 }
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700465 return true;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700466}
467bool 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
477std::string CuttlefishConfig::PerInstancePath(const char* file_name) const {
478 return (instance_dir() + "/") + file_name;
479}
480
481std::string CuttlefishConfig::instance_name() const {
482 return GetPerInstanceDefault("cvd-");
483}
484
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700485int GetInstance() {
486 static int instance_id = InstanceFromEnvironment();
487 return instance_id;
488}
489
Jorge E. Moreira015c0002018-07-19 11:01:16 -0700490std::string GetGlobalConfigFileLink() {
491 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json";
492}
493
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700494std::string GetDomain() {
495 return CuttlefishConfig::Get()->ivshmem_client_socket_path();
496}
497
498std::string GetPerInstanceDefault(const char* prefix) {
499 std::ostringstream stream;
500 stream << prefix << std::setfill('0') << std::setw(2) << GetInstance();
501 return stream.str();
502}
503int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; }
504
505std::string GetDefaultPerInstanceDir() {
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700506 std::ostringstream stream;
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -0700507 if (HostSupportsQemuCli()) {
Jorge E. Moreira285b48b2018-06-27 10:33:24 -0700508 stream << std::getenv("HOME") << "/cuttlefish_runtime";
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -0700509 } else {
510 stream << "/var/run/libvirt-" << kDefaultUuidPrefix << std::setfill('0')
511 << std::setw(2) << GetInstance();
512 }
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700513 return stream.str();
514}
515
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700516std::string GetDefaultMempath() {
517 return GetPerInstanceDefault("/var/run/shm/cvd-");
518}
519
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -0700520std::string DefaultHostArtifactsPath(const std::string& file_name) {
521 return (cvd::StringFromEnv("ANDROID_HOST_OUT",
522 cvd::StringFromEnv("HOME", ".")) +
523 "/") +
524 file_name;
525}
526
527std::string DefaultGuestImagePath(const std::string& file_name) {
528 return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT",
529 cvd::StringFromEnv("HOME", ".")) +
530 "/") +
531 file_name;
532}
533
534bool 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. Moreira577383b2018-05-24 14:17:51 -0700540} // namespace vsoc