blob: 7c63b88ff26bb1ceb5c2ff5f2c0f049ee80c2380 [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"
Wade Guthrie64b4c142012-08-20 15:21:01 -070016#include "shill/config80211.h"
Darin Petkova7b89492011-07-27 12:48:17 -070017#include "shill/dhcp_provider.h"
Darin Petkovf0136cd2012-11-07 16:18:02 +010018#include "shill/diagnostics_reporter.h"
Gaurav Shah71354762011-11-28 19:22:49 -080019#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070020#include "shill/logging.h"
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020021#include "shill/nss.h"
Darin Petkovab565bb2011-10-06 02:55:51 -070022#include "shill/proxy_factory.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080023#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070024#include "shill/rtnl_handler.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070025#include "shill/shill_config.h"
Paul Stewart75897df2011-04-27 09:05:53 -070026
Gary Moraina9fb3252012-05-31 12:05:31 -070027using base::Bind;
28using base::Unretained;
Paul Stewart75897df2011-04-27 09:05:53 -070029using std::string;
Gaurav Shah71354762011-11-28 19:22:49 -080030using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070031
32namespace shill {
33
Darin Petkova7b89492011-07-27 12:48:17 -070034Daemon::Daemon(Config *config, ControlInterface *control)
35 : config_(config),
36 control_(control),
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020037 nss_(NSS::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080038 proxy_factory_(ProxyFactory::GetInstance()),
39 rtnl_handler_(RTNLHandler::GetInstance()),
40 routing_table_(RoutingTable::GetInstance()),
41 dhcp_provider_(DHCPProvider::GetInstance()),
Wade Guthrie0d438532012-05-18 14:18:50 -070042 config80211_(Config80211::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080043 manager_(new Manager(control_,
44 &dispatcher_,
45 &metrics_,
46 &glib_,
47 config->GetRunDirectory(),
48 config->GetStorageDirectory(),
Wade Guthrie5020b572012-10-12 15:51:14 -070049 config->GetUserStorageDirectoryFormat())),
Christopher Wileyfe34be02012-11-12 16:02:46 -080050 callback80211_output_(),
51 callback80211_metrics_(&metrics_) {
Chris Masone2ae797d2011-08-23 20:41:00 -070052}
Wade Guthrie0d438532012-05-18 14:18:50 -070053
Wade Guthrie5020b572012-10-12 15:51:14 -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
Gaurav Shah71354762011-11-28 19:22:49 -080064void Daemon::SetStartupProfiles(const vector<string> &profile_name_list) {
65 Error error;
Thieu Lefb46caf2012-03-08 11:57:15 -080066 manager_->set_startup_profiles(profile_name_list);
Thieu Le1271d682011-11-02 22:48:19 +000067}
68
Paul Stewart75897df2011-04-27 09:05:53 -070069void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070070 Start();
Ben Chanfad4a0b2012-04-18 15:49:59 -070071 SLOG(Daemon, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070072 dispatcher_.DispatchForever();
Ben Chanfad4a0b2012-04-18 15:49:59 -070073 SLOG(Daemon, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000074}
75
76void Daemon::Quit() {
Gary Moraina9fb3252012-05-31 12:05:31 -070077 SLOG(Daemon, 1) << "Starting termination actions.";
78 // Stop() prevents autoconnect from attempting to immediately connect to
79 // services after they have been disconnected.
80 Stop();
Arman Ugurayab22c162012-10-08 19:08:38 -070081 if (!manager_->RunTerminationActionsAndNotifyMetrics(
82 Bind(&Daemon::TerminationActionsCompleted, Unretained(this)),
83 Metrics::kTerminationActionReasonTerminate)) {
84 SLOG(Daemon, 1) << "No termination actions were run";
85 dispatcher_.PostTask(MessageLoop::QuitClosure());
86 }
Gary Moraina9fb3252012-05-31 12:05:31 -070087}
88
Arman Ugurayab22c162012-10-08 19:08:38 -070089void Daemon::TerminationActionsCompleted(const Error &error) {
Gary Moraina9fb3252012-05-31 12:05:31 -070090 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Arman Ugurayab22c162012-10-08 19:08:38 -070091 metrics_.NotifyTerminationActionsCompleted(
92 Metrics::kTerminationActionReasonTerminate, error.IsSuccess());
Eric Shienbrood3e20a232012-02-16 11:35:56 -050093 dispatcher_.PostTask(MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -070094}
95
Gaurav Shah71354762011-11-28 19:22:49 -080096void Daemon::Start() {
97 glib_.TypeInit();
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020098 nss_->Init(&glib_);
Thieu Lefb46caf2012-03-08 11:57:15 -080099 proxy_factory_->Init();
100 rtnl_handler_->Start(&dispatcher_, &sockets_);
101 routing_table_->Start();
102 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
Darin Petkovf0136cd2012-11-07 16:18:02 +0100103 DiagnosticsReporter::GetInstance()->Init(&dispatcher_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700104
105 if (config80211_) {
106 config80211_->Init(&dispatcher_);
107 // Subscribe to all the events in which we're interested.
108 static const Config80211::EventType kEvents[] = {
109 Config80211::kEventTypeConfig,
110 Config80211::kEventTypeScan,
111 Config80211::kEventTypeRegulatory,
112 Config80211::kEventTypeMlme };
113
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700114 // Install callbacks in the Config80211 singleton.
115 callback80211_output_.InstallAsBroadcastCallback();
116 callback80211_metrics_.InstallAsBroadcastCallback();
Wade Guthrie0d438532012-05-18 14:18:50 -0700117
118 for (size_t i = 0; i < arraysize(kEvents); i++) {
119 config80211_->SubscribeToEvents(kEvents[i]);
120 }
121 }
122
Thieu Lefb46caf2012-03-08 11:57:15 -0800123 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800124}
125
126void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800127 manager_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800128}
Paul Stewart75897df2011-04-27 09:05:53 -0700129
130} // namespace shill