Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 5 | #include "shill/shill_daemon.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <string> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 10 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 11 | #include "shill/callback80211_metrics.h" |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 12 | #include "shill/dhcp_provider.h" |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 13 | #include "shill/diagnostics_reporter.h" |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 14 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 15 | #include "shill/logging.h" |
Peter Qiu | 02e3dc3 | 2014-10-31 10:15:00 -0700 | [diff] [blame] | 16 | #include "shill/net/netlink_manager.h" |
| 17 | #include "shill/net/nl80211_message.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 18 | #include "shill/net/rtnl_handler.h" |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 19 | #include "shill/proxy_factory.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 20 | #include "shill/routing_table.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 21 | #include "shill/shill_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 23 | using base::Bind; |
| 24 | using base::Unretained; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | using std::string; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | |
| 27 | namespace shill { |
| 28 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 29 | namespace Logging { |
| 30 | static auto kModuleLogScope = ScopeLogger::kDaemon; |
| 31 | static string ObjectID(Daemon *d) { return "(shill_daemon)"; } |
| 32 | } |
| 33 | |
| 34 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 35 | Daemon::Daemon(Config *config, ControlInterface *control) |
| 36 | : config_(config), |
| 37 | control_(control), |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 38 | metrics_(new Metrics(&dispatcher_)), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 39 | proxy_factory_(ProxyFactory::GetInstance()), |
| 40 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 41 | routing_table_(RoutingTable::GetInstance()), |
| 42 | dhcp_provider_(DHCPProvider::GetInstance()), |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 43 | netlink_manager_(NetlinkManager::GetInstance()), |
Paul Stewart | ca32474 | 2014-08-29 14:41:59 -0700 | [diff] [blame] | 44 | manager_(new Manager(control_.get(), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 45 | &dispatcher_, |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 46 | metrics_.get(), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 47 | &glib_, |
| 48 | config->GetRunDirectory(), |
| 49 | config->GetStorageDirectory(), |
mukesh agrawal | 0a59a5a | 2014-04-24 19:10:46 -0700 | [diff] [blame] | 50 | config->GetUserStorageDirectory())), |
Wade Guthrie | c272896 | 2013-07-10 09:32:16 -0700 | [diff] [blame] | 51 | callback80211_metrics_(metrics_.get()) { |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 52 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 53 | |
Ben Chan | b7bf216 | 2014-06-12 02:12:28 -0700 | [diff] [blame] | 54 | Daemon::~Daemon() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 55 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 56 | void Daemon::AddDeviceToBlackList(const string &device_name) { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 57 | manager_->AddDeviceToBlackList(device_name); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 60 | void Daemon::SetStartupPortalList(const string &portal_list) { |
| 61 | manager_->SetStartupPortalList(portal_list); |
| 62 | } |
| 63 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 64 | void Daemon::Run() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 65 | Start(); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 66 | SLOG(this, 1) << "Running main loop."; |
Chris Masone | c5b392e | 2011-05-14 16:31:01 -0700 | [diff] [blame] | 67 | dispatcher_.DispatchForever(); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 68 | SLOG(this, 1) << "Exited main loop."; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void Daemon::Quit() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 72 | SLOG(this, 1) << "Starting termination actions."; |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 73 | if (!manager_->RunTerminationActionsAndNotifyMetrics( |
Samuel Tan | fbe8d2b | 2014-09-15 20:23:59 -0700 | [diff] [blame] | 74 | Bind(&Daemon::TerminationActionsCompleted, Unretained(this)))) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 75 | SLOG(this, 1) << "No termination actions were run"; |
Ben Chan | a3ac096 | 2014-06-12 01:46:18 -0700 | [diff] [blame] | 76 | StopAndReturnToMain(); |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 77 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 80 | void Daemon::TerminationActionsCompleted(const Error &error) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 81 | SLOG(this, 1) << "Finished termination actions. Result: " << error; |
Samuel Tan | fbe8d2b | 2014-09-15 20:23:59 -0700 | [diff] [blame] | 82 | metrics_->NotifyTerminationActionsCompleted(error.IsSuccess()); |
Ben Chan | a3ac096 | 2014-06-12 01:46:18 -0700 | [diff] [blame] | 83 | |
| 84 | // Daemon::TerminationActionsCompleted() should not directly call |
| 85 | // Daemon::Stop(). Otherwise, it could lead to the call sequence below. That |
| 86 | // is not safe as the HookTable's start callback only holds a weak pointer to |
| 87 | // the Cellular object, which is destroyed in midst of the |
| 88 | // Cellular::OnTerminationCompleted() call. We schedule the |
| 89 | // Daemon::StopAndReturnToMain() call through the message loop instead. |
| 90 | // |
| 91 | // Daemon::Quit |
| 92 | // -> Manager::RunTerminationActionsAndNotifyMetrics |
| 93 | // -> Manager::RunTerminationActions |
| 94 | // -> HookTable::Run |
| 95 | // ... |
| 96 | // -> Cellular::OnTerminationCompleted |
| 97 | // -> Manager::TerminationActionComplete |
| 98 | // -> HookTable::ActionComplete |
| 99 | // -> Daemon::TerminationActionsCompleted |
| 100 | // -> Daemon::Stop |
| 101 | // -> Manager::Stop |
| 102 | // -> DeviceInfo::Stop |
| 103 | // -> Cellular::~Cellular |
| 104 | // -> Manager::RemoveTerminationAction |
| 105 | dispatcher_.PostTask(Bind(&Daemon::StopAndReturnToMain, Unretained(this))); |
| 106 | } |
| 107 | |
| 108 | void Daemon::StopAndReturnToMain() { |
Ben Chan | 13804a1 | 2013-10-08 15:32:51 -0700 | [diff] [blame] | 109 | Stop(); |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 110 | dispatcher_.PostTask(base::MessageLoop::QuitClosure()); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 113 | void Daemon::Start() { |
| 114 | glib_.TypeInit(); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 115 | proxy_factory_->Init(); |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 116 | metrics_->Start(); |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 117 | rtnl_handler_->Start(&sockets_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 118 | routing_table_->Start(); |
Paul Stewart | ca32474 | 2014-08-29 14:41:59 -0700 | [diff] [blame] | 119 | dhcp_provider_->Init(control_.get(), &dispatcher_, &glib_, metrics_.get()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 120 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 121 | if (netlink_manager_) { |
| 122 | netlink_manager_->Init(); |
| 123 | uint16_t nl80211_family_id = netlink_manager_->GetFamily( |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 124 | Nl80211Message::kMessageTypeString, |
| 125 | Bind(&Nl80211Message::CreateMessage)); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 126 | if (nl80211_family_id == NetlinkMessage::kIllegalMessageType) { |
| 127 | LOG(FATAL) << "Didn't get a legal message type for 'nl80211' messages."; |
| 128 | } |
| 129 | Nl80211Message::SetMessageType(nl80211_family_id); |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 130 | netlink_manager_->Start(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 131 | |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 132 | // Install handlers for NetlinkMessages that don't have specific handlers |
| 133 | // (which are registered by message sequence number). |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 134 | netlink_manager_->AddBroadcastHandler(Bind( |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 135 | &Callback80211Metrics::CollectDisconnectStatistics, |
| 136 | callback80211_metrics_.AsWeakPtr())); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 139 | manager_->Start(); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void Daemon::Stop() { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 143 | manager_->Stop(); |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 144 | metrics_->Stop(); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 145 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 146 | |
| 147 | } // namespace shill |