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 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <string> |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 10 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 11 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 13 | #include <base/file_path.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 14 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 15 | #include "shill/callback80211_metrics.h" |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 16 | #include "shill/dhcp_provider.h" |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 17 | #include "shill/diagnostics_reporter.h" |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 18 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 19 | #include "shill/logging.h" |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 20 | #include "shill/netlink_manager.h" |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 21 | #include "shill/nl80211_message.h" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 22 | #include "shill/nss.h" |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 23 | #include "shill/proxy_factory.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 24 | #include "shill/routing_table.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 25 | #include "shill/rtnl_handler.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 26 | #include "shill/shill_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 27 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 28 | using base::Bind; |
| 29 | using base::Unretained; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 30 | using std::string; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 31 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 32 | |
| 33 | namespace shill { |
| 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_)), |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 39 | nss_(NSS::GetInstance()), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 40 | proxy_factory_(ProxyFactory::GetInstance()), |
| 41 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 42 | routing_table_(RoutingTable::GetInstance()), |
| 43 | dhcp_provider_(DHCPProvider::GetInstance()), |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 44 | netlink_manager_(NetlinkManager::GetInstance()), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 45 | manager_(new Manager(control_, |
| 46 | &dispatcher_, |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 47 | metrics_.get(), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 48 | &glib_, |
| 49 | config->GetRunDirectory(), |
| 50 | config->GetStorageDirectory(), |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 51 | config->GetUserStorageDirectoryFormat())), |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 52 | callback80211_metrics_(*netlink_manager_, metrics_.get()) { |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 53 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 54 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 55 | Daemon::~Daemon() { } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 56 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 57 | void Daemon::AddDeviceToBlackList(const string &device_name) { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 58 | manager_->AddDeviceToBlackList(device_name); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 61 | void Daemon::SetStartupPortalList(const string &portal_list) { |
| 62 | manager_->SetStartupPortalList(portal_list); |
| 63 | } |
| 64 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 65 | void Daemon::Run() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 66 | Start(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 67 | SLOG(Daemon, 1) << "Running main loop."; |
Chris Masone | c5b392e | 2011-05-14 16:31:01 -0700 | [diff] [blame] | 68 | dispatcher_.DispatchForever(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 69 | SLOG(Daemon, 1) << "Exited main loop."; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void Daemon::Quit() { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 73 | SLOG(Daemon, 1) << "Starting termination actions."; |
| 74 | // Stop() prevents autoconnect from attempting to immediately connect to |
| 75 | // services after they have been disconnected. |
| 76 | Stop(); |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 77 | if (!manager_->RunTerminationActionsAndNotifyMetrics( |
| 78 | Bind(&Daemon::TerminationActionsCompleted, Unretained(this)), |
| 79 | Metrics::kTerminationActionReasonTerminate)) { |
| 80 | SLOG(Daemon, 1) << "No termination actions were run"; |
| 81 | dispatcher_.PostTask(MessageLoop::QuitClosure()); |
| 82 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 85 | void Daemon::TerminationActionsCompleted(const Error &error) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 86 | SLOG(Daemon, 1) << "Finished termination actions. Result: " << error; |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 87 | metrics_->NotifyTerminationActionsCompleted( |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 88 | Metrics::kTerminationActionReasonTerminate, error.IsSuccess()); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 89 | dispatcher_.PostTask(MessageLoop::QuitClosure()); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 92 | void Daemon::Start() { |
| 93 | glib_.TypeInit(); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 94 | proxy_factory_->Init(); |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 95 | metrics_->Start(); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 96 | rtnl_handler_->Start(&dispatcher_, &sockets_); |
| 97 | routing_table_->Start(); |
| 98 | dhcp_provider_->Init(control_, &dispatcher_, &glib_); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 99 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 100 | if (netlink_manager_) { |
| 101 | netlink_manager_->Init(); |
| 102 | uint16_t nl80211_family_id = netlink_manager_->GetFamily( |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 103 | Nl80211Message::kMessageTypeString, |
| 104 | Bind(&Nl80211Message::CreateMessage)); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 105 | if (nl80211_family_id == NetlinkMessage::kIllegalMessageType) { |
| 106 | LOG(FATAL) << "Didn't get a legal message type for 'nl80211' messages."; |
| 107 | } |
| 108 | Nl80211Message::SetMessageType(nl80211_family_id); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 109 | netlink_manager_->Start(&dispatcher_); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 110 | |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 111 | // Install handlers for NetlinkMessages that don't have specific handlers |
| 112 | // (which are registered by message sequence number). |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 113 | netlink_manager_->AddBroadcastHandler(Bind( |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 114 | &Callback80211Metrics::CollectDisconnectStatistics, |
| 115 | callback80211_metrics_.AsWeakPtr())); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 118 | manager_->Start(); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void Daemon::Stop() { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 122 | manager_->Stop(); |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 123 | metrics_->Stop(); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 124 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 125 | |
| 126 | } // namespace shill |