blob: e0a81588478128b09b9e410b42e3a0780925cb5f [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>
Chris Masone2ae797d2011-08-23 20:41:00 -070013#include <base/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(),
Wade Guthrie5020b572012-10-12 15:51:14 -070051 config->GetUserStorageDirectoryFormat())),
Wade Guthriebb9fca22013-04-10 17:21:42 -070052 callback80211_metrics_(*netlink_manager_, 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.";
74 // Stop() prevents autoconnect from attempting to immediately connect to
75 // services after they have been disconnected.
76 Stop();
Arman Ugurayab22c162012-10-08 19:08:38 -070077 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 Moraina9fb3252012-05-31 12:05:31 -070083}
84
Arman Ugurayab22c162012-10-08 19:08:38 -070085void Daemon::TerminationActionsCompleted(const Error &error) {
Gary Moraina9fb3252012-05-31 12:05:31 -070086 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Thieu Le6c1e3bb2013-02-06 15:20:35 -080087 metrics_->NotifyTerminationActionsCompleted(
Arman Ugurayab22c162012-10-08 19:08:38 -070088 Metrics::kTerminationActionReasonTerminate, error.IsSuccess());
Eric Shienbrood3e20a232012-02-16 11:35:56 -050089 dispatcher_.PostTask(MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -070090}
91
Gaurav Shah71354762011-11-28 19:22:49 -080092void Daemon::Start() {
93 glib_.TypeInit();
Thieu Lefb46caf2012-03-08 11:57:15 -080094 proxy_factory_->Init();
Thieu Le6c1e3bb2013-02-06 15:20:35 -080095 metrics_->Start();
Thieu Lefb46caf2012-03-08 11:57:15 -080096 rtnl_handler_->Start(&dispatcher_, &sockets_);
97 routing_table_->Start();
98 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
Wade Guthrie0d438532012-05-18 14:18:50 -070099
Wade Guthriebb9fca22013-04-10 17:21:42 -0700100 if (netlink_manager_) {
101 netlink_manager_->Init();
102 uint16_t nl80211_family_id = netlink_manager_->GetFamily(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700103 Nl80211Message::kMessageTypeString,
104 Bind(&Nl80211Message::CreateMessage));
Wade Guthriebee87c22013-03-06 11:00:46 -0800105 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 Guthriebb9fca22013-04-10 17:21:42 -0700109 netlink_manager_->Start(&dispatcher_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700110
Wade Guthriec6c81962013-03-06 15:47:13 -0800111 // Install handlers for NetlinkMessages that don't have specific handlers
112 // (which are registered by message sequence number).
Wade Guthriebb9fca22013-04-10 17:21:42 -0700113 netlink_manager_->AddBroadcastHandler(Bind(
Wade Guthriec6c81962013-03-06 15:47:13 -0800114 &Callback80211Metrics::CollectDisconnectStatistics,
115 callback80211_metrics_.AsWeakPtr()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700116 }
117
Thieu Lefb46caf2012-03-08 11:57:15 -0800118 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800119}
120
121void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800122 manager_->Stop();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800123 metrics_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800124}
Paul Stewart75897df2011-04-27 09:05:53 -0700125
126} // namespace shill