• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/config_utils.h"
18 
19 #include <string.h>
20 
21 #include <iomanip>
22 #include <sstream>
23 
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26 
27 #include "common/libs/utils/architecture.h"
28 #include "common/libs/utils/contains.h"
29 #include "common/libs/utils/environment.h"
30 #include "common/libs/utils/in_sandbox.h"
31 #include "common/libs/utils/subprocess.h"
32 #include "host/libs/config/config_constants.h"
33 
34 namespace cuttlefish {
35 
InstanceFromString(std::string instance_str)36 int InstanceFromString(std::string instance_str) {
37   if (android::base::StartsWith(instance_str, kVsocUserPrefix)) {
38     instance_str = instance_str.substr(std::string(kVsocUserPrefix).size());
39   } else if (android::base::StartsWith(instance_str, kCvdNamePrefix)) {
40     instance_str = instance_str.substr(std::string(kCvdNamePrefix).size());
41   }
42 
43   int instance = std::stoi(instance_str);
44   if (instance <= 0) {
45     LOG(INFO) << "Failed to interpret \"" << instance_str << "\" as an id, "
46               << "using instance id " << kDefaultInstance;
47     return kDefaultInstance;
48   }
49   return instance;
50 }
51 
InstanceFromEnvironment()52 int InstanceFromEnvironment() {
53   std::string instance_str = StringFromEnv(kCuttlefishInstanceEnvVarName, "");
54   if (instance_str.empty()) {
55     // Try to get it from the user instead
56     instance_str = StringFromEnv("USER", "");
57 
58     if (instance_str.empty()) {
59       LOG(DEBUG) << kCuttlefishInstanceEnvVarName
60                  << " and USER unset, using instance id " << kDefaultInstance;
61       return kDefaultInstance;
62     }
63     if (!android::base::StartsWith(instance_str, kVsocUserPrefix)) {
64       // No user or we don't recognize this user
65       LOG(DEBUG) << "Non-vsoc user, using instance id " << kDefaultInstance;
66       return kDefaultInstance;
67     }
68   }
69   return InstanceFromString(instance_str);
70 }
71 
GetInstance()72 int GetInstance() {
73   static int instance_id = InstanceFromEnvironment();
74   return instance_id;
75 }
76 
GetDefaultVsockCid()77 int GetDefaultVsockCid() {
78   // we assume that this function is used to configure CuttlefishConfig once
79   static const int default_vsock_cid = 3 + GetInstance() - 1;
80   return default_vsock_cid;
81 }
82 
GetVsockServerPort(const int base,const int vsock_guest_cid)83 int GetVsockServerPort(const int base,
84                        const int vsock_guest_cid /**< per instance guest cid */) {
85     return base + (vsock_guest_cid - 3);
86 }
87 
GetGlobalConfigFileLink()88 std::string GetGlobalConfigFileLink() {
89   return StringFromEnv("HOME", ".") + "/.cuttlefish_config.json";
90 }
91 
ForCurrentInstance(const char * prefix)92 std::string ForCurrentInstance(const char* prefix) {
93   std::ostringstream stream;
94   stream << prefix << std::setfill('0') << std::setw(2) << GetInstance();
95   return stream.str();
96 }
97 
RandomSerialNumber(const std::string & prefix)98 std::string RandomSerialNumber(const std::string& prefix) {
99   const char hex_characters[] = "0123456789ABCDEF";
100   std::srand(time(0));
101   char str[10];
102   for(int i=0; i<10; i++){
103     str[i] = hex_characters[rand() % strlen(hex_characters)];
104   }
105   return prefix + str;
106 }
107 
DefaultHostArtifactsPath(const std::string & file_name)108 std::string DefaultHostArtifactsPath(const std::string& file_name) {
109   return (StringFromEnv("ANDROID_HOST_OUT", StringFromEnv("HOME", ".")) + "/") +
110          file_name;
111 }
112 
HostBinaryDir()113 std::string HostBinaryDir() {
114   return DefaultHostArtifactsPath("bin");
115 }
116 
UseQemuPrebuilt()117 bool UseQemuPrebuilt() {
118   const std::string target_prod_str = StringFromEnv("TARGET_PRODUCT", "");
119   if (!Contains(target_prod_str, "arm")) {
120     return true;
121   }
122   return false;
123 }
124 
DefaultQemuBinaryDir()125 std::string DefaultQemuBinaryDir() {
126   if (UseQemuPrebuilt()) {
127     return HostBinaryDir() + "/" + HostArchStr() + "-linux-gnu/qemu";
128   }
129   return "/usr/bin";
130 }
131 
HostBinaryPath(const std::string & binary_name)132 std::string HostBinaryPath(const std::string& binary_name) {
133 #ifdef __ANDROID__
134   return binary_name;
135 #else
136   return HostBinaryDir() + "/" + binary_name;
137 #endif
138 }
139 
HostUsrSharePath(const std::string & binary_name)140 std::string HostUsrSharePath(const std::string& binary_name) {
141   return DefaultHostArtifactsPath("usr/share/" + binary_name);
142 }
143 
HostQemuBiosPath()144 std::string HostQemuBiosPath() {
145   if (UseQemuPrebuilt()) {
146     return DefaultHostArtifactsPath(
147         "usr/share/qemu/" + HostArchStr() + "-linux-gnu");
148   }
149   return "/usr/share/qemu";
150 }
151 
DefaultGuestImagePath(const std::string & file_name)152 std::string DefaultGuestImagePath(const std::string& file_name) {
153   return (StringFromEnv("ANDROID_PRODUCT_OUT", StringFromEnv("HOME", "."))) +
154          file_name;
155 }
156 
157 // In practice this is mostly validating that the `cuttlefish-base` debian
158 // package is installed, which implies that more things are present like the
159 // predefined network setup.
HostSupportsQemuCli()160 bool HostSupportsQemuCli() {
161   static bool supported =
162 #ifdef __linux__
163       InSandbox() ||
164       RunWithManagedStdio(
165           Command("/usr/lib/cuttlefish-common/bin/capability_query.py")
166               .AddParameter("qemu_cli"),
167           nullptr, nullptr, nullptr) == 0;
168 #else
169       true;
170 #endif
171   return supported;
172 }
173 
174 }
175