• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 
17 #ifndef SHILL_DAEMON_TASK_H_
18 #define SHILL_DAEMON_TASK_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <base/callback.h>
25 
26 #include "shill/event_dispatcher.h"
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;
39 class ProcessManager;
40 class RoutingTable;
41 class RTNLHandler;
42 
43 #if !defined(DISABLE_WIFI)
44 class NetlinkManager;
45 #endif  // DISABLE_WIFI
46 
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
51 // brillo::Daemon cannot coexist with the base::ExitManager used by shill's
52 // test_runner.cc.
53 class DaemonTask {
54  public:
55   // Run-time settings retrieved from command line.
56   struct Settings {
SettingsSettings57     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;
65     std::vector<std::string> device_whitelist;
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 
75   DaemonTask(const Settings& settings, Config* config);
76   virtual ~DaemonTask();
77 
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);
84 
85   // Break the termination loop started in DaemonTask::OnShutdown. Invoked
86   // after shill completes its termination tasks during shutdown.
87   void BreakTerminationLoop();
88 
89  protected:
90   void Init();
91 
92  private:
93   friend class DaemonTaskTest;
94   friend class DaemonTaskForTest;
95 
96   void Start();
97 
98   // Apply run-time settings to the manager.
99   void ApplySettings();
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 
110   Settings settings_;
111   Config* config_;
112   std::unique_ptr<EventDispatcher> dispatcher_;
113   std::unique_ptr<ControlInterface> control_;
114   std::unique_ptr<Metrics> metrics_;
115   RTNLHandler* rtnl_handler_;
116   RoutingTable* routing_table_;
117   DHCPProvider* dhcp_provider_;
118   ProcessManager* process_manager_;
119 #if !defined(DISABLE_WIFI)
120   NetlinkManager* netlink_manager_;
121   std::unique_ptr<Callback80211Metrics> callback80211_metrics_;
122 #endif  // DISABLE_WIFI
123   std::unique_ptr<Manager> manager_;
124   base::Closure termination_completed_callback_;
125 };
126 
127 }  // namespace shill
128 
129 #endif  // SHILL_DAEMON_TASK_H_
130