blob: 9c7899360f0f9d8f1c40249fa33d9d9720e5a4f5 [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
Darin Petkova7b89492011-07-27 12:48:17 -070011#include "shill/dhcp_provider.h"
Darin Petkovf0136cd2012-11-07 16:18:02 +010012#include "shill/diagnostics_reporter.h"
Gaurav Shah71354762011-11-28 19:22:49 -080013#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070014#include "shill/logging.h"
Peter Qiu02e3dc32014-10-31 10:15:00 -070015#include "shill/net/netlink_manager.h"
16#include "shill/net/nl80211_message.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/rtnl_handler.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"
Chris Masone2ae797d2011-08-23 20:41:00 -070020#include "shill/shill_config.h"
Ben Chand6a8b512014-11-18 10:45:15 -080021#include "shill/wifi/callback80211_metrics.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
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070029namespace Logging {
30static auto kModuleLogScope = ScopeLogger::kDaemon;
31static string ObjectID(Daemon *d) { return "(shill_daemon)"; }
32}
33
34
Darin Petkova7b89492011-07-27 12:48:17 -070035Daemon::Daemon(Config *config, ControlInterface *control)
36 : config_(config),
37 control_(control),
Thieu Le6c1e3bb2013-02-06 15:20:35 -080038 metrics_(new Metrics(&dispatcher_)),
Thieu Lefb46caf2012-03-08 11:57:15 -080039 proxy_factory_(ProxyFactory::GetInstance()),
40 rtnl_handler_(RTNLHandler::GetInstance()),
41 routing_table_(RoutingTable::GetInstance()),
42 dhcp_provider_(DHCPProvider::GetInstance()),
Wade Guthriebb9fca22013-04-10 17:21:42 -070043 netlink_manager_(NetlinkManager::GetInstance()),
Paul Stewartca324742014-08-29 14:41:59 -070044 manager_(new Manager(control_.get(),
Thieu Lefb46caf2012-03-08 11:57:15 -080045 &dispatcher_,
Thieu Le6c1e3bb2013-02-06 15:20:35 -080046 metrics_.get(),
Thieu Lefb46caf2012-03-08 11:57:15 -080047 &glib_,
48 config->GetRunDirectory(),
49 config->GetStorageDirectory(),
mukesh agrawal0a59a5a2014-04-24 19:10:46 -070050 config->GetUserStorageDirectory())),
Wade Guthriec2728962013-07-10 09:32:16 -070051 callback80211_metrics_(metrics_.get()) {
Chris Masone2ae797d2011-08-23 20:41:00 -070052}
Wade Guthrie0d438532012-05-18 14:18:50 -070053
Ben Chanb7bf2162014-06-12 02:12:28 -070054Daemon::~Daemon() {}
Paul Stewart75897df2011-04-27 09:05:53 -070055
mukesh agrawal8f317b62011-07-15 11:53:23 -070056void Daemon::AddDeviceToBlackList(const string &device_name) {
Thieu Lefb46caf2012-03-08 11:57:15 -080057 manager_->AddDeviceToBlackList(device_name);
mukesh agrawal8f317b62011-07-15 11:53:23 -070058}
59
Paul Stewart10e9e4e2012-04-26 19:46:28 -070060void Daemon::SetStartupPortalList(const string &portal_list) {
61 manager_->SetStartupPortalList(portal_list);
62}
63
Paul Stewart75897df2011-04-27 09:05:53 -070064void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070065 Start();
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070066 SLOG(this, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070067 dispatcher_.DispatchForever();
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070068 SLOG(this, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000069}
70
71void Daemon::Quit() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070072 SLOG(this, 1) << "Starting termination actions.";
Arman Ugurayab22c162012-10-08 19:08:38 -070073 if (!manager_->RunTerminationActionsAndNotifyMetrics(
Samuel Tanfbe8d2b2014-09-15 20:23:59 -070074 Bind(&Daemon::TerminationActionsCompleted, Unretained(this)))) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070075 SLOG(this, 1) << "No termination actions were run";
Ben Chana3ac0962014-06-12 01:46:18 -070076 StopAndReturnToMain();
Arman Ugurayab22c162012-10-08 19:08:38 -070077 }
Gary Moraina9fb3252012-05-31 12:05:31 -070078}
79
Arman Ugurayab22c162012-10-08 19:08:38 -070080void Daemon::TerminationActionsCompleted(const Error &error) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070081 SLOG(this, 1) << "Finished termination actions. Result: " << error;
Samuel Tanfbe8d2b2014-09-15 20:23:59 -070082 metrics_->NotifyTerminationActionsCompleted(error.IsSuccess());
Ben Chana3ac0962014-06-12 01:46:18 -070083
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
108void Daemon::StopAndReturnToMain() {
Ben Chan13804a12013-10-08 15:32:51 -0700109 Stop();
Ben Chana0ddf462014-02-06 11:32:42 -0800110 dispatcher_.PostTask(base::MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -0700111}
112
Gaurav Shah71354762011-11-28 19:22:49 -0800113void Daemon::Start() {
114 glib_.TypeInit();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800115 metrics_->Start();
Peter Qiu6d856a62015-01-14 14:32:53 -0800116 rtnl_handler_->Start();
Thieu Lefb46caf2012-03-08 11:57:15 -0800117 routing_table_->Start();
Paul Stewartca324742014-08-29 14:41:59 -0700118 dhcp_provider_->Init(control_.get(), &dispatcher_, &glib_, metrics_.get());
Wade Guthrie0d438532012-05-18 14:18:50 -0700119
Wade Guthriebb9fca22013-04-10 17:21:42 -0700120 if (netlink_manager_) {
121 netlink_manager_->Init();
122 uint16_t nl80211_family_id = netlink_manager_->GetFamily(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700123 Nl80211Message::kMessageTypeString,
124 Bind(&Nl80211Message::CreateMessage));
Wade Guthriebee87c22013-03-06 11:00:46 -0800125 if (nl80211_family_id == NetlinkMessage::kIllegalMessageType) {
126 LOG(FATAL) << "Didn't get a legal message type for 'nl80211' messages.";
127 }
128 Nl80211Message::SetMessageType(nl80211_family_id);
Peter Qiu37a2b602014-10-27 10:00:43 -0700129 netlink_manager_->Start();
Wade Guthrie0d438532012-05-18 14:18:50 -0700130
Wade Guthriec6c81962013-03-06 15:47:13 -0800131 // Install handlers for NetlinkMessages that don't have specific handlers
132 // (which are registered by message sequence number).
Wade Guthriebb9fca22013-04-10 17:21:42 -0700133 netlink_manager_->AddBroadcastHandler(Bind(
Wade Guthriec6c81962013-03-06 15:47:13 -0800134 &Callback80211Metrics::CollectDisconnectStatistics,
135 callback80211_metrics_.AsWeakPtr()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700136 }
137
Thieu Lefb46caf2012-03-08 11:57:15 -0800138 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800139}
140
141void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800142 manager_->Stop();
Paul Stewart8dc5e7b2014-12-11 19:24:50 -0800143 manager_ = nullptr; // Release manager resources, including DBus adaptor.
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800144 metrics_->Stop();
Paul Stewart8dc5e7b2014-12-11 19:24:50 -0800145 dhcp_provider_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800146}
Paul Stewart75897df2011-04-27 09:05:53 -0700147
148} // namespace shill