blob: be8290312c3942bab4b1792394f41d22d175368e [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"
Gaurav Shah71354762011-11-28 19:22:49 -080018#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070019#include "shill/logging.h"
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020020#include "shill/nss.h"
Darin Petkovab565bb2011-10-06 02:55:51 -070021#include "shill/proxy_factory.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080022#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070023#include "shill/rtnl_handler.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070024#include "shill/shill_config.h"
Paul Stewart75897df2011-04-27 09:05:53 -070025
Gary Moraina9fb3252012-05-31 12:05:31 -070026using base::Bind;
27using base::Unretained;
Paul Stewart75897df2011-04-27 09:05:53 -070028using std::string;
Gaurav Shah71354762011-11-28 19:22:49 -080029using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070030
31namespace shill {
32
Darin Petkova7b89492011-07-27 12:48:17 -070033Daemon::Daemon(Config *config, ControlInterface *control)
34 : config_(config),
35 control_(control),
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020036 nss_(NSS::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080037 proxy_factory_(ProxyFactory::GetInstance()),
38 rtnl_handler_(RTNLHandler::GetInstance()),
39 routing_table_(RoutingTable::GetInstance()),
40 dhcp_provider_(DHCPProvider::GetInstance()),
Wade Guthrie0d438532012-05-18 14:18:50 -070041 config80211_(Config80211::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080042 manager_(new Manager(control_,
43 &dispatcher_,
44 &metrics_,
45 &glib_,
46 config->GetRunDirectory(),
47 config->GetStorageDirectory(),
Wade Guthrie5020b572012-10-12 15:51:14 -070048 config->GetUserStorageDirectoryFormat())),
Wade Guthrieb1ec8602012-10-18 17:26:14 -070049 callback80211_output_(config80211_),
Wade Guthrie5020b572012-10-12 15:51:14 -070050 callback80211_metrics_(config80211_, &metrics_) {
Chris Masone2ae797d2011-08-23 20:41:00 -070051}
Wade Guthrie0d438532012-05-18 14:18:50 -070052
Wade Guthrie5020b572012-10-12 15:51:14 -070053Daemon::~Daemon() { }
Paul Stewart75897df2011-04-27 09:05:53 -070054
mukesh agrawal8f317b62011-07-15 11:53:23 -070055void Daemon::AddDeviceToBlackList(const string &device_name) {
Thieu Lefb46caf2012-03-08 11:57:15 -080056 manager_->AddDeviceToBlackList(device_name);
mukesh agrawal8f317b62011-07-15 11:53:23 -070057}
58
Paul Stewart10e9e4e2012-04-26 19:46:28 -070059void Daemon::SetStartupPortalList(const string &portal_list) {
60 manager_->SetStartupPortalList(portal_list);
61}
62
Gaurav Shah71354762011-11-28 19:22:49 -080063void Daemon::SetStartupProfiles(const vector<string> &profile_name_list) {
64 Error error;
Thieu Lefb46caf2012-03-08 11:57:15 -080065 manager_->set_startup_profiles(profile_name_list);
Thieu Le1271d682011-11-02 22:48:19 +000066}
67
Paul Stewart75897df2011-04-27 09:05:53 -070068void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070069 Start();
Ben Chanfad4a0b2012-04-18 15:49:59 -070070 SLOG(Daemon, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070071 dispatcher_.DispatchForever();
Ben Chanfad4a0b2012-04-18 15:49:59 -070072 SLOG(Daemon, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000073}
74
75void Daemon::Quit() {
Gary Moraina9fb3252012-05-31 12:05:31 -070076 SLOG(Daemon, 1) << "Starting termination actions.";
77 // Stop() prevents autoconnect from attempting to immediately connect to
78 // services after they have been disconnected.
79 Stop();
Arman Ugurayab22c162012-10-08 19:08:38 -070080 if (!manager_->RunTerminationActionsAndNotifyMetrics(
81 Bind(&Daemon::TerminationActionsCompleted, Unretained(this)),
82 Metrics::kTerminationActionReasonTerminate)) {
83 SLOG(Daemon, 1) << "No termination actions were run";
84 dispatcher_.PostTask(MessageLoop::QuitClosure());
85 }
Gary Moraina9fb3252012-05-31 12:05:31 -070086}
87
Arman Ugurayab22c162012-10-08 19:08:38 -070088void Daemon::TerminationActionsCompleted(const Error &error) {
Gary Moraina9fb3252012-05-31 12:05:31 -070089 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Arman Ugurayab22c162012-10-08 19:08:38 -070090 metrics_.NotifyTerminationActionsCompleted(
91 Metrics::kTerminationActionReasonTerminate, error.IsSuccess());
Eric Shienbrood3e20a232012-02-16 11:35:56 -050092 dispatcher_.PostTask(MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -070093}
94
Gaurav Shah71354762011-11-28 19:22:49 -080095void Daemon::Start() {
96 glib_.TypeInit();
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020097 nss_->Init(&glib_);
Thieu Lefb46caf2012-03-08 11:57:15 -080098 proxy_factory_->Init();
99 rtnl_handler_->Start(&dispatcher_, &sockets_);
100 routing_table_->Start();
101 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700102
103 if (config80211_) {
104 config80211_->Init(&dispatcher_);
105 // Subscribe to all the events in which we're interested.
106 static const Config80211::EventType kEvents[] = {
107 Config80211::kEventTypeConfig,
108 Config80211::kEventTypeScan,
109 Config80211::kEventTypeRegulatory,
110 Config80211::kEventTypeMlme };
111
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700112 // Install callbacks in the Config80211 singleton.
113 callback80211_output_.InstallAsBroadcastCallback();
114 callback80211_metrics_.InstallAsBroadcastCallback();
Wade Guthrie0d438532012-05-18 14:18:50 -0700115
116 for (size_t i = 0; i < arraysize(kEvents); i++) {
117 config80211_->SubscribeToEvents(kEvents[i]);
118 }
119 }
120
Thieu Lefb46caf2012-03-08 11:57:15 -0800121 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800122}
123
124void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800125 manager_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800126}
Paul Stewart75897df2011-04-27 09:05:53 -0700127
128} // namespace shill