blob: 4cd1bbb36b370f8ac1df6e48e8137819b554f9fb [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
Darin Petkova7b89492011-07-27 12:48:17 -07007#include <stdio.h>
8
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <string>
Gaurav Shah71354762011-11-28 19:22:49 -080010#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070011
Gary Moraina9fb3252012-05-31 12:05:31 -070012#include <base/bind.h>
Ben Chana0ddf462014-02-06 11:32:42 -080013#include <base/files/file_path.h>
Chris Masoneee929b72011-05-10 10:02:18 -070014
Wade Guthrie5020b572012-10-12 15:51:14 -070015#include "shill/callback80211_metrics.h"
Darin Petkova7b89492011-07-27 12:48:17 -070016#include "shill/dhcp_provider.h"
Darin Petkovf0136cd2012-11-07 16:18:02 +010017#include "shill/diagnostics_reporter.h"
Gaurav Shah71354762011-11-28 19:22:49 -080018#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070019#include "shill/logging.h"
Wade Guthriebb9fca22013-04-10 17:21:42 -070020#include "shill/netlink_manager.h"
Wade Guthriebee87c22013-03-06 11:00:46 -080021#include "shill/nl80211_message.h"
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020022#include "shill/nss.h"
Darin Petkovab565bb2011-10-06 02:55:51 -070023#include "shill/proxy_factory.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080024#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070025#include "shill/rtnl_handler.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070026#include "shill/shill_config.h"
Paul Stewart75897df2011-04-27 09:05:53 -070027
Gary Moraina9fb3252012-05-31 12:05:31 -070028using base::Bind;
29using base::Unretained;
Paul Stewart75897df2011-04-27 09:05:53 -070030using std::string;
Gaurav Shah71354762011-11-28 19:22:49 -080031using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070032
33namespace shill {
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_)),
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020039 nss_(NSS::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080040 proxy_factory_(ProxyFactory::GetInstance()),
41 rtnl_handler_(RTNLHandler::GetInstance()),
42 routing_table_(RoutingTable::GetInstance()),
43 dhcp_provider_(DHCPProvider::GetInstance()),
Wade Guthriebb9fca22013-04-10 17:21:42 -070044 netlink_manager_(NetlinkManager::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080045 manager_(new Manager(control_,
46 &dispatcher_,
Thieu Le6c1e3bb2013-02-06 15:20:35 -080047 metrics_.get(),
Thieu Lefb46caf2012-03-08 11:57:15 -080048 &glib_,
49 config->GetRunDirectory(),
50 config->GetStorageDirectory(),
mukesh agrawal0a59a5a2014-04-24 19:10:46 -070051 config->GetUserStorageDirectory())),
Wade Guthriec2728962013-07-10 09:32:16 -070052 callback80211_metrics_(metrics_.get()) {
Chris Masone2ae797d2011-08-23 20:41:00 -070053}
Wade Guthrie0d438532012-05-18 14:18:50 -070054
Wade Guthrie5020b572012-10-12 15:51:14 -070055Daemon::~Daemon() { }
Paul Stewart75897df2011-04-27 09:05:53 -070056
mukesh agrawal8f317b62011-07-15 11:53:23 -070057void Daemon::AddDeviceToBlackList(const string &device_name) {
Thieu Lefb46caf2012-03-08 11:57:15 -080058 manager_->AddDeviceToBlackList(device_name);
mukesh agrawal8f317b62011-07-15 11:53:23 -070059}
60
Paul Stewart10e9e4e2012-04-26 19:46:28 -070061void Daemon::SetStartupPortalList(const string &portal_list) {
62 manager_->SetStartupPortalList(portal_list);
63}
64
Paul Stewart75897df2011-04-27 09:05:53 -070065void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070066 Start();
Ben Chanfad4a0b2012-04-18 15:49:59 -070067 SLOG(Daemon, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070068 dispatcher_.DispatchForever();
Ben Chanfad4a0b2012-04-18 15:49:59 -070069 SLOG(Daemon, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000070}
71
72void Daemon::Quit() {
Gary Moraina9fb3252012-05-31 12:05:31 -070073 SLOG(Daemon, 1) << "Starting termination actions.";
Arman Ugurayab22c162012-10-08 19:08:38 -070074 if (!manager_->RunTerminationActionsAndNotifyMetrics(
75 Bind(&Daemon::TerminationActionsCompleted, Unretained(this)),
76 Metrics::kTerminationActionReasonTerminate)) {
77 SLOG(Daemon, 1) << "No termination actions were run";
Ben Chana3ac0962014-06-12 01:46:18 -070078 StopAndReturnToMain();
Arman Ugurayab22c162012-10-08 19:08:38 -070079 }
Gary Moraina9fb3252012-05-31 12:05:31 -070080}
81
Arman Ugurayab22c162012-10-08 19:08:38 -070082void Daemon::TerminationActionsCompleted(const Error &error) {
Gary Moraina9fb3252012-05-31 12:05:31 -070083 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Thieu Le6c1e3bb2013-02-06 15:20:35 -080084 metrics_->NotifyTerminationActionsCompleted(
Arman Ugurayab22c162012-10-08 19:08:38 -070085 Metrics::kTerminationActionReasonTerminate, error.IsSuccess());
Ben Chana3ac0962014-06-12 01:46:18 -070086
87 // Daemon::TerminationActionsCompleted() should not directly call
88 // Daemon::Stop(). Otherwise, it could lead to the call sequence below. That
89 // is not safe as the HookTable's start callback only holds a weak pointer to
90 // the Cellular object, which is destroyed in midst of the
91 // Cellular::OnTerminationCompleted() call. We schedule the
92 // Daemon::StopAndReturnToMain() call through the message loop instead.
93 //
94 // Daemon::Quit
95 // -> Manager::RunTerminationActionsAndNotifyMetrics
96 // -> Manager::RunTerminationActions
97 // -> HookTable::Run
98 // ...
99 // -> Cellular::OnTerminationCompleted
100 // -> Manager::TerminationActionComplete
101 // -> HookTable::ActionComplete
102 // -> Daemon::TerminationActionsCompleted
103 // -> Daemon::Stop
104 // -> Manager::Stop
105 // -> DeviceInfo::Stop
106 // -> Cellular::~Cellular
107 // -> Manager::RemoveTerminationAction
108 dispatcher_.PostTask(Bind(&Daemon::StopAndReturnToMain, Unretained(this)));
109}
110
111void Daemon::StopAndReturnToMain() {
Ben Chan13804a12013-10-08 15:32:51 -0700112 Stop();
Ben Chana0ddf462014-02-06 11:32:42 -0800113 dispatcher_.PostTask(base::MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -0700114}
115
Gaurav Shah71354762011-11-28 19:22:49 -0800116void Daemon::Start() {
117 glib_.TypeInit();
Thieu Lefb46caf2012-03-08 11:57:15 -0800118 proxy_factory_->Init();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800119 metrics_->Start();
Thieu Lefb46caf2012-03-08 11:57:15 -0800120 rtnl_handler_->Start(&dispatcher_, &sockets_);
121 routing_table_->Start();
122 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700123
Wade Guthriebb9fca22013-04-10 17:21:42 -0700124 if (netlink_manager_) {
125 netlink_manager_->Init();
126 uint16_t nl80211_family_id = netlink_manager_->GetFamily(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700127 Nl80211Message::kMessageTypeString,
128 Bind(&Nl80211Message::CreateMessage));
Wade Guthriebee87c22013-03-06 11:00:46 -0800129 if (nl80211_family_id == NetlinkMessage::kIllegalMessageType) {
130 LOG(FATAL) << "Didn't get a legal message type for 'nl80211' messages.";
131 }
132 Nl80211Message::SetMessageType(nl80211_family_id);
Wade Guthriebb9fca22013-04-10 17:21:42 -0700133 netlink_manager_->Start(&dispatcher_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700134
Wade Guthriec6c81962013-03-06 15:47:13 -0800135 // Install handlers for NetlinkMessages that don't have specific handlers
136 // (which are registered by message sequence number).
Wade Guthriebb9fca22013-04-10 17:21:42 -0700137 netlink_manager_->AddBroadcastHandler(Bind(
Wade Guthriec6c81962013-03-06 15:47:13 -0800138 &Callback80211Metrics::CollectDisconnectStatistics,
139 callback80211_metrics_.AsWeakPtr()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700140 }
141
Thieu Lefb46caf2012-03-08 11:57:15 -0800142 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800143}
144
145void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800146 manager_->Stop();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800147 metrics_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800148}
Paul Stewart75897df2011-04-27 09:05:53 -0700149
150} // namespace shill