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 <android-base/logging.h> 18 #include <fruit/fruit.h> 19 #include <iostream> 20 21 #include "common/libs/utils/network.h" 22 #include "common/libs/utils/result.h" 23 #include "host/libs/config/cuttlefish_config.h" 24 #include "host/libs/config/feature.h" 25 #include "host/libs/vm_manager/host_configuration.h" 26 27 namespace cuttlefish { 28 namespace { 29 30 using vm_manager::ValidateHostConfiguration; 31 32 class ValidateTapDevices : public SetupFeature { 33 public: INJECT(ValidateTapDevices (const CuttlefishConfig::InstanceSpecific & instance))34 INJECT(ValidateTapDevices(const CuttlefishConfig::InstanceSpecific& instance)) 35 : instance_(instance) {} 36 Name() const37 std::string Name() const override { return "ValidateTapDevices"; } Enabled() const38 bool Enabled() const override { return true; } 39 40 private: Dependencies() const41 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; } ResultSetup()42 Result<void> ResultSetup() override { 43 auto taps = TapInterfacesInUse(); 44 auto wifi = instance_.wifi_tap_name(); 45 CF_EXPECT(taps.count(wifi) == 0, "Device \"" << wifi << "\" in use"); 46 auto mobile = instance_.mobile_tap_name(); 47 CF_EXPECT(taps.count(mobile) == 0, "Device \"" << mobile << "\" in use"); 48 auto eth = instance_.ethernet_tap_name(); 49 CF_EXPECT(taps.count(eth) == 0, "Device \"" << eth << "\" in use"); 50 return {}; 51 } 52 53 private: 54 const CuttlefishConfig::InstanceSpecific& instance_; 55 }; 56 57 class ValidateHostConfigurationFeature : public SetupFeature { 58 public: INJECT(ValidateHostConfigurationFeature ())59 INJECT(ValidateHostConfigurationFeature()) {} 60 Enabled() const61 bool Enabled() const override { 62 #ifndef __ANDROID__ 63 return true; 64 #else 65 return false; 66 #endif 67 } Name() const68 std::string Name() const override { return "ValidateHostConfiguration"; } 69 70 private: Dependencies() const71 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; } Setup()72 bool Setup() override { 73 // Check host configuration 74 std::vector<std::string> config_commands; 75 if (!ValidateHostConfiguration(&config_commands)) { 76 LOG(ERROR) << "Validation of user configuration failed"; 77 std::cout << "Execute the following to correctly configure:" << std::endl; 78 for (auto& command : config_commands) { 79 std::cout << " " << command << std::endl; 80 } 81 std::cout << "You may need to logout for the changes to take effect" 82 << std::endl; 83 return false; 84 } 85 return true; 86 } 87 }; 88 89 } // namespace 90 91 fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>> validationComponent()92validationComponent() { 93 return fruit::createComponent() 94 .addMultibinding<SetupFeature, ValidateHostConfigurationFeature>() 95 .addMultibinding<SetupFeature, ValidateTapDevices>(); 96 } 97 98 } // namespace cuttlefish 99