blob: 9a5555609461d34b90cb0eacde34d69cf5fc46aa [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkova7b89492011-07-27 12:48:17 -07005#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -07006
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <string>
Paul Stewart75897df2011-04-27 09:05:53 -07008
Gary Moraina9fb3252012-05-31 12:05:31 -07009#include <base/bind.h>
Chris Masoneee929b72011-05-10 10:02:18 -070010
Wade Guthrie5020b572012-10-12 15:51:14 -070011#include "shill/callback80211_metrics.h"
Darin Petkova7b89492011-07-27 12:48:17 -070012#include "shill/dhcp_provider.h"
Darin Petkovf0136cd2012-11-07 16:18:02 +010013#include "shill/diagnostics_reporter.h"
Gaurav Shah71354762011-11-28 19:22:49 -080014#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070015#include "shill/logging.h"
Wade Guthriebb9fca22013-04-10 17:21:42 -070016#include "shill/netlink_manager.h"
Wade Guthriebee87c22013-03-06 11:00:46 -080017#include "shill/nl80211_message.h"
Darin Petkovab565bb2011-10-06 02:55:51 -070018#include "shill/proxy_factory.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080019#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070020#include "shill/rtnl_handler.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070021#include "shill/shill_config.h"
Paul Stewart75897df2011-04-27 09:05:53 -070022
Gary Moraina9fb3252012-05-31 12:05:31 -070023using base::Bind;
24using base::Unretained;
Paul Stewart75897df2011-04-27 09:05:53 -070025using std::string;
Paul Stewart75897df2011-04-27 09:05:53 -070026
27namespace shill {
28
Darin Petkova7b89492011-07-27 12:48:17 -070029Daemon::Daemon(Config *config, ControlInterface *control)
30 : config_(config),
31 control_(control),
Thieu Le6c1e3bb2013-02-06 15:20:35 -080032 metrics_(new Metrics(&dispatcher_)),
Thieu Lefb46caf2012-03-08 11:57:15 -080033 proxy_factory_(ProxyFactory::GetInstance()),
34 rtnl_handler_(RTNLHandler::GetInstance()),
35 routing_table_(RoutingTable::GetInstance()),
36 dhcp_provider_(DHCPProvider::GetInstance()),
Wade Guthriebb9fca22013-04-10 17:21:42 -070037 netlink_manager_(NetlinkManager::GetInstance()),
Paul Stewartca324742014-08-29 14:41:59 -070038 manager_(new Manager(control_.get(),
Thieu Lefb46caf2012-03-08 11:57:15 -080039 &dispatcher_,
Thieu Le6c1e3bb2013-02-06 15:20:35 -080040 metrics_.get(),
Thieu Lefb46caf2012-03-08 11:57:15 -080041 &glib_,
42 config->GetRunDirectory(),
43 config->GetStorageDirectory(),
mukesh agrawal0a59a5a2014-04-24 19:10:46 -070044 config->GetUserStorageDirectory())),
Wade Guthriec2728962013-07-10 09:32:16 -070045 callback80211_metrics_(metrics_.get()) {
Chris Masone2ae797d2011-08-23 20:41:00 -070046}
Wade Guthrie0d438532012-05-18 14:18:50 -070047
Ben Chanb7bf2162014-06-12 02:12:28 -070048Daemon::~Daemon() {}
Paul Stewart75897df2011-04-27 09:05:53 -070049
mukesh agrawal8f317b62011-07-15 11:53:23 -070050void Daemon::AddDeviceToBlackList(const string &device_name) {
Thieu Lefb46caf2012-03-08 11:57:15 -080051 manager_->AddDeviceToBlackList(device_name);
mukesh agrawal8f317b62011-07-15 11:53:23 -070052}
53
Paul Stewart10e9e4e2012-04-26 19:46:28 -070054void Daemon::SetStartupPortalList(const string &portal_list) {
55 manager_->SetStartupPortalList(portal_list);
56}
57
Paul Stewart75897df2011-04-27 09:05:53 -070058void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070059 Start();
Ben Chanfad4a0b2012-04-18 15:49:59 -070060 SLOG(Daemon, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070061 dispatcher_.DispatchForever();
Ben Chanfad4a0b2012-04-18 15:49:59 -070062 SLOG(Daemon, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000063}
64
65void Daemon::Quit() {
Gary Moraina9fb3252012-05-31 12:05:31 -070066 SLOG(Daemon, 1) << "Starting termination actions.";
Arman Ugurayab22c162012-10-08 19:08:38 -070067 if (!manager_->RunTerminationActionsAndNotifyMetrics(
Ben Chanb7bf2162014-06-12 02:12:28 -070068 Bind(&Daemon::TerminationActionsCompleted, Unretained(this)),
69 Metrics::kTerminationActionReasonTerminate)) {
Arman Ugurayab22c162012-10-08 19:08:38 -070070 SLOG(Daemon, 1) << "No termination actions were run";
Ben Chana3ac0962014-06-12 01:46:18 -070071 StopAndReturnToMain();
Arman Ugurayab22c162012-10-08 19:08:38 -070072 }
Gary Moraina9fb3252012-05-31 12:05:31 -070073}
74
Arman Ugurayab22c162012-10-08 19:08:38 -070075void Daemon::TerminationActionsCompleted(const Error &error) {
Gary Moraina9fb3252012-05-31 12:05:31 -070076 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Thieu Le6c1e3bb2013-02-06 15:20:35 -080077 metrics_->NotifyTerminationActionsCompleted(
Arman Ugurayab22c162012-10-08 19:08:38 -070078 Metrics::kTerminationActionReasonTerminate, error.IsSuccess());
Ben Chana3ac0962014-06-12 01:46:18 -070079
80 // Daemon::TerminationActionsCompleted() should not directly call
81 // Daemon::Stop(). Otherwise, it could lead to the call sequence below. That
82 // is not safe as the HookTable's start callback only holds a weak pointer to
83 // the Cellular object, which is destroyed in midst of the
84 // Cellular::OnTerminationCompleted() call. We schedule the
85 // Daemon::StopAndReturnToMain() call through the message loop instead.
86 //
87 // Daemon::Quit
88 // -> Manager::RunTerminationActionsAndNotifyMetrics
89 // -> Manager::RunTerminationActions
90 // -> HookTable::Run
91 // ...
92 // -> Cellular::OnTerminationCompleted
93 // -> Manager::TerminationActionComplete
94 // -> HookTable::ActionComplete
95 // -> Daemon::TerminationActionsCompleted
96 // -> Daemon::Stop
97 // -> Manager::Stop
98 // -> DeviceInfo::Stop
99 // -> Cellular::~Cellular
100 // -> Manager::RemoveTerminationAction
101 dispatcher_.PostTask(Bind(&Daemon::StopAndReturnToMain, Unretained(this)));
102}
103
104void Daemon::StopAndReturnToMain() {
Ben Chan13804a12013-10-08 15:32:51 -0700105 Stop();
Ben Chana0ddf462014-02-06 11:32:42 -0800106 dispatcher_.PostTask(base::MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -0700107}
108
Gaurav Shah71354762011-11-28 19:22:49 -0800109void Daemon::Start() {
110 glib_.TypeInit();
Thieu Lefb46caf2012-03-08 11:57:15 -0800111 proxy_factory_->Init();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800112 metrics_->Start();
Thieu Lefb46caf2012-03-08 11:57:15 -0800113 rtnl_handler_->Start(&dispatcher_, &sockets_);
114 routing_table_->Start();
Paul Stewartca324742014-08-29 14:41:59 -0700115 dhcp_provider_->Init(control_.get(), &dispatcher_, &glib_, metrics_.get());
Wade Guthrie0d438532012-05-18 14:18:50 -0700116
Wade Guthriebb9fca22013-04-10 17:21:42 -0700117 if (netlink_manager_) {
118 netlink_manager_->Init();
119 uint16_t nl80211_family_id = netlink_manager_->GetFamily(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700120 Nl80211Message::kMessageTypeString,
121 Bind(&Nl80211Message::CreateMessage));
Wade Guthriebee87c22013-03-06 11:00:46 -0800122 if (nl80211_family_id == NetlinkMessage::kIllegalMessageType) {
123 LOG(FATAL) << "Didn't get a legal message type for 'nl80211' messages.";
124 }
125 Nl80211Message::SetMessageType(nl80211_family_id);
Wade Guthriebb9fca22013-04-10 17:21:42 -0700126 netlink_manager_->Start(&dispatcher_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700127
Wade Guthriec6c81962013-03-06 15:47:13 -0800128 // Install handlers for NetlinkMessages that don't have specific handlers
129 // (which are registered by message sequence number).
Wade Guthriebb9fca22013-04-10 17:21:42 -0700130 netlink_manager_->AddBroadcastHandler(Bind(
Wade Guthriec6c81962013-03-06 15:47:13 -0800131 &Callback80211Metrics::CollectDisconnectStatistics,
132 callback80211_metrics_.AsWeakPtr()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700133 }
134
Thieu Lefb46caf2012-03-08 11:57:15 -0800135 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800136}
137
138void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800139 manager_->Stop();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800140 metrics_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800141}
Paul Stewart75897df2011-04-27 09:05:53 -0700142
143} // namespace shill