Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 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 | // |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 16 | |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 17 | #ifndef SHILL_DAEMON_TASK_H_ |
| 18 | #define SHILL_DAEMON_TASK_H_ |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Peter Qiu | 29875ff | 2015-08-27 21:24:56 -0700 | [diff] [blame] | 24 | #include <base/callback.h> |
| 25 | |
Samuel Tan | 646c7e8 | 2015-12-28 15:43:46 -0800 | [diff] [blame] | 26 | #include "shill/event_dispatcher.h" |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 27 | #if !defined(DISABLE_WIFI) |
| 28 | #include "shill/wifi/callback80211_metrics.h" |
| 29 | #endif // DISABLE_WIFI |
| 30 | |
| 31 | namespace shill { |
| 32 | |
| 33 | class Config; |
| 34 | class ControlInterface; |
| 35 | class DHCPProvider; |
| 36 | class Error; |
| 37 | class Manager; |
| 38 | class Metrics; |
Peter Qiu | dde57ef | 2015-08-14 09:58:51 -0700 | [diff] [blame] | 39 | class ProcessManager; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 40 | class RoutingTable; |
| 41 | class RTNLHandler; |
| 42 | |
| 43 | #if !defined(DISABLE_WIFI) |
| 44 | class NetlinkManager; |
| 45 | #endif // DISABLE_WIFI |
| 46 | |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 47 | // DaemonTask contains most of the logic used in ShillDaemon (e.g. |
| 48 | // init/shutdown, start/stop). This class is kept separate from ShillDaemon to |
| 49 | // ensure that it does not inherit brillo::Daemon. This is necessary for |
| 50 | // DaemonTask unit tests to run, since the base::ExitManager inherited from |
Samuel Tan | 60ea15d | 2016-01-19 15:31:26 -0800 | [diff] [blame] | 51 | // brillo::Daemon cannot coexist with the base::ExitManager used by shill's |
| 52 | // test_runner.cc. |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 53 | class DaemonTask { |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 54 | public: |
| 55 | // Run-time settings retrieved from command line. |
| 56 | struct Settings { |
| 57 | Settings() |
| 58 | : ignore_unknown_ethernet(false), |
| 59 | minimum_mtu(0), |
| 60 | passive_mode(false), |
| 61 | use_portal_list(false) {} |
| 62 | std::string accept_hostname_from; |
| 63 | std::string default_technology_order; |
| 64 | std::vector<std::string> device_blacklist; |
Christopher Grant | c1d4473 | 2016-01-15 10:54:45 -0500 | [diff] [blame] | 65 | std::vector<std::string> device_whitelist; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 66 | std::vector<std::string> dhcpv6_enabled_devices; |
| 67 | bool ignore_unknown_ethernet; |
| 68 | int minimum_mtu; |
| 69 | bool passive_mode; |
| 70 | std::string portal_list; |
| 71 | std::string prepend_dns_servers; |
| 72 | bool use_portal_list; |
| 73 | }; |
| 74 | |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 75 | DaemonTask(const Settings& settings, Config* config); |
| 76 | virtual ~DaemonTask(); |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 77 | |
mukesh agrawal | e6b4192 | 2015-09-24 16:52:14 -0700 | [diff] [blame] | 78 | // Starts the termination actions in the manager. Returns true if |
| 79 | // termination actions have completed synchronously, and false |
| 80 | // otherwise. Arranges for |completion_callback| to be invoked after |
| 81 | // all asynchronous work completes, but ignores |
| 82 | // |completion_callback| if no asynchronous work is required. |
| 83 | virtual bool Quit(const base::Closure& completion_callback); |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 84 | |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 85 | // Break the termination loop started in DaemonTask::OnShutdown. Invoked |
Samuel Tan | 60ea15d | 2016-01-19 15:31:26 -0800 | [diff] [blame] | 86 | // after shill completes its termination tasks during shutdown. |
| 87 | void BreakTerminationLoop(); |
| 88 | |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 89 | protected: |
Samuel Tan | 60ea15d | 2016-01-19 15:31:26 -0800 | [diff] [blame] | 90 | void Init(); |
Samuel Tan | 646c7e8 | 2015-12-28 15:43:46 -0800 | [diff] [blame] | 91 | |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 92 | private: |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 93 | friend class DaemonTaskTest; |
| 94 | friend class DaemonTaskForTest; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 95 | |
Samuel Tan | 60ea15d | 2016-01-19 15:31:26 -0800 | [diff] [blame] | 96 | void Start(); |
| 97 | |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 98 | // Apply run-time settings to the manager. |
Peter Qiu | dde57ef | 2015-08-14 09:58:51 -0700 | [diff] [blame] | 99 | void ApplySettings(); |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 100 | |
| 101 | // Called when the termination actions are completed. |
| 102 | void TerminationActionsCompleted(const Error& error); |
| 103 | |
| 104 | // Calls Stop() and then causes the dispatcher message loop to terminate and |
| 105 | // return to the main function which started the daemon. |
| 106 | void StopAndReturnToMain(); |
| 107 | |
| 108 | void Stop(); |
| 109 | |
Peter Qiu | dde57ef | 2015-08-14 09:58:51 -0700 | [diff] [blame] | 110 | Settings settings_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 111 | Config* config_; |
Samuel Tan | 646c7e8 | 2015-12-28 15:43:46 -0800 | [diff] [blame] | 112 | std::unique_ptr<EventDispatcher> dispatcher_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 113 | std::unique_ptr<ControlInterface> control_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 114 | std::unique_ptr<Metrics> metrics_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 115 | RTNLHandler* rtnl_handler_; |
| 116 | RoutingTable* routing_table_; |
| 117 | DHCPProvider* dhcp_provider_; |
Peter Qiu | dde57ef | 2015-08-14 09:58:51 -0700 | [diff] [blame] | 118 | ProcessManager* process_manager_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 119 | #if !defined(DISABLE_WIFI) |
| 120 | NetlinkManager* netlink_manager_; |
Peter Qiu | dde57ef | 2015-08-14 09:58:51 -0700 | [diff] [blame] | 121 | std::unique_ptr<Callback80211Metrics> callback80211_metrics_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 122 | #endif // DISABLE_WIFI |
| 123 | std::unique_ptr<Manager> manager_; |
Peter Qiu | 5988a85 | 2015-08-25 16:45:35 -0700 | [diff] [blame] | 124 | base::Closure termination_completed_callback_; |
Peter Qiu | 24b34a0 | 2015-06-30 10:22:26 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace shill |
| 128 | |
Samuel Tan | 59b397b | 2016-01-20 11:25:21 -0800 | [diff] [blame] | 129 | #endif // SHILL_DAEMON_TASK_H_ |