1 /*
2  * Copyright (C) 2021 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/commands/run_cvd/validate.h"
18 
19 #include <sys/utsname.h>
20 
21 #include <android-base/logging.h>
22 #include <fruit/fruit.h>
23 
24 #include "common/libs/utils/network.h"
25 #include "common/libs/utils/result.h"
26 #include "host/libs/config/cuttlefish_config.h"
27 #include "host/libs/vm_manager/host_configuration.h"
28 
29 namespace cuttlefish {
30 
TestTapDevices(const CuttlefishConfig::InstanceSpecific & instance)31 static Result<void> TestTapDevices(
32     const CuttlefishConfig::InstanceSpecific& instance) {
33 #ifdef __linux__
34   auto taps = TapInterfacesInUse();
35   auto wifi = instance.wifi_tap_name();
36   CF_EXPECTF(taps.count(wifi) == 0, "Device \"{}\" in use", wifi);
37   auto mobile = instance.mobile_tap_name();
38   CF_EXPECTF(taps.count(mobile) == 0, "Device \"{}\" in use", mobile);
39   auto eth = instance.ethernet_tap_name();
40   CF_EXPECTF(taps.count(eth) == 0, "Device \"{}\" in use", eth);
41 #else
42   (void)instance;
43 #endif
44   return {};
45 }
46 
ValidateTapDevices(const CuttlefishConfig::InstanceSpecific & instance)47 Result<void> ValidateTapDevices(
48     const CuttlefishConfig::InstanceSpecific& instance) {
49   CF_EXPECT(TestTapDevices(instance),
50             "There appears to be another cuttlefish device"
51             " already running, using the requested host "
52             "resources. Try `cvd reset` or `pkill run_cvd` "
53             "and `pkill crosvm`");
54   return {};
55 }
56 
ValidateHostConfiguration()57 Result<void> ValidateHostConfiguration() {
58 #ifdef __ANDROID__
59   std::vector<std::string> config_commands;
60   CF_EXPECTF(vm_manager::ValidateHostConfiguration(&config_commands),
61              "Validation of user configuration failed.\n"
62              "Execute the following to correctly configure: \n[{}]\n",
63              "You may need to logout for the changes to take effect.\n",
64              fmt::join(config_commands, "\n"));
65 #endif
66   return {};
67 }
68 
ValidateHostKernel()69 Result<void> ValidateHostKernel() {
70   struct utsname uname_data;
71   CF_EXPECT_EQ(uname(&uname_data), 0, "uname failed: " << strerror(errno));
72   LOG(DEBUG) << "uts.sysname = \"" << uname_data.sysname << "\"";
73   LOG(DEBUG) << "uts.nodename = \"" << uname_data.nodename << "\"";
74   LOG(DEBUG) << "uts.release = \"" << uname_data.release << "\"";
75   LOG(DEBUG) << "uts.version = \"" << uname_data.version << "\"";
76   LOG(DEBUG) << "uts.machine = \"" << uname_data.machine << "\"";
77 #ifdef _GNU_SOURCE
78   LOG(DEBUG) << "uts.domainname = \"" << uname_data.domainname << "\"";
79 #endif
80   return {};
81 }
82 
83 }  // namespace cuttlefish
84