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" |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 16 | #include "shill/config80211.h" |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 17 | #include "shill/dhcp_provider.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" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 20 | #include "shill/nss.h" |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 21 | #include "shill/proxy_factory.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 22 | #include "shill/routing_table.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 23 | #include "shill/rtnl_handler.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 24 | #include "shill/shill_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 26 | using base::Bind; |
| 27 | using base::Unretained; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 28 | using std::string; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 29 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 30 | |
| 31 | namespace shill { |
| 32 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 33 | Daemon::Daemon(Config *config, ControlInterface *control) |
| 34 | : config_(config), |
| 35 | control_(control), |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 36 | nss_(NSS::GetInstance()), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 37 | proxy_factory_(ProxyFactory::GetInstance()), |
| 38 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 39 | routing_table_(RoutingTable::GetInstance()), |
| 40 | dhcp_provider_(DHCPProvider::GetInstance()), |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 41 | config80211_(Config80211::GetInstance()), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 42 | manager_(new Manager(control_, |
| 43 | &dispatcher_, |
| 44 | &metrics_, |
| 45 | &glib_, |
| 46 | config->GetRunDirectory(), |
| 47 | config->GetStorageDirectory(), |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 48 | config->GetUserStorageDirectoryFormat())), |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 49 | callback80211_output_(config80211_), |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 50 | callback80211_metrics_(config80211_, &metrics_) { |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 51 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 52 | |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 53 | Daemon::~Daemon() { } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 54 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 55 | void Daemon::AddDeviceToBlackList(const string &device_name) { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 56 | manager_->AddDeviceToBlackList(device_name); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 59 | void Daemon::SetStartupPortalList(const string &portal_list) { |
| 60 | manager_->SetStartupPortalList(portal_list); |
| 61 | } |
| 62 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 63 | void Daemon::SetStartupProfiles(const vector<string> &profile_name_list) { |
| 64 | Error error; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 65 | manager_->set_startup_profiles(profile_name_list); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 68 | void Daemon::Run() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 69 | Start(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 70 | SLOG(Daemon, 1) << "Running main loop."; |
Chris Masone | c5b392e | 2011-05-14 16:31:01 -0700 | [diff] [blame] | 71 | dispatcher_.DispatchForever(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 72 | SLOG(Daemon, 1) << "Exited main loop."; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Daemon::Quit() { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 76 | SLOG(Daemon, 1) << "Starting termination actions."; |
| 77 | // Stop() prevents autoconnect from attempting to immediately connect to |
| 78 | // services after they have been disconnected. |
| 79 | Stop(); |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 80 | if (!manager_->RunTerminationActionsAndNotifyMetrics( |
| 81 | Bind(&Daemon::TerminationActionsCompleted, Unretained(this)), |
| 82 | Metrics::kTerminationActionReasonTerminate)) { |
| 83 | SLOG(Daemon, 1) << "No termination actions were run"; |
| 84 | dispatcher_.PostTask(MessageLoop::QuitClosure()); |
| 85 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 88 | void Daemon::TerminationActionsCompleted(const Error &error) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 89 | SLOG(Daemon, 1) << "Finished termination actions. Result: " << error; |
Arman Uguray | ab22c16 | 2012-10-08 19:08:38 -0700 | [diff] [blame] | 90 | metrics_.NotifyTerminationActionsCompleted( |
| 91 | Metrics::kTerminationActionReasonTerminate, error.IsSuccess()); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 92 | dispatcher_.PostTask(MessageLoop::QuitClosure()); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 95 | void Daemon::Start() { |
| 96 | glib_.TypeInit(); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 97 | nss_->Init(&glib_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 98 | proxy_factory_->Init(); |
| 99 | rtnl_handler_->Start(&dispatcher_, &sockets_); |
| 100 | routing_table_->Start(); |
| 101 | dhcp_provider_->Init(control_, &dispatcher_, &glib_); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 102 | |
| 103 | if (config80211_) { |
| 104 | config80211_->Init(&dispatcher_); |
| 105 | // Subscribe to all the events in which we're interested. |
| 106 | static const Config80211::EventType kEvents[] = { |
| 107 | Config80211::kEventTypeConfig, |
| 108 | Config80211::kEventTypeScan, |
| 109 | Config80211::kEventTypeRegulatory, |
| 110 | Config80211::kEventTypeMlme }; |
| 111 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 112 | // Install callbacks in the Config80211 singleton. |
| 113 | callback80211_output_.InstallAsBroadcastCallback(); |
| 114 | callback80211_metrics_.InstallAsBroadcastCallback(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 115 | |
| 116 | for (size_t i = 0; i < arraysize(kEvents); i++) { |
| 117 | config80211_->SubscribeToEvents(kEvents[i]); |
| 118 | } |
| 119 | } |
| 120 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 121 | manager_->Start(); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void Daemon::Stop() { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 125 | manager_->Stop(); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 126 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 127 | |
| 128 | } // namespace shill |