blob: a5e80b53e84d68fbf66f1b78abcffddb13415ca7 [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),
Thieu Le6c1e3bb2013-02-06 15:20:35 -080037 metrics_(new Metrics(&dispatcher_)),
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020038 nss_(NSS::GetInstance()),
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 Guthrie0d438532012-05-18 14:18:50 -070043 config80211_(Config80211::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080044 manager_(new Manager(control_,
45 &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(),
Wade Guthrie5020b572012-10-12 15:51:14 -070050 config->GetUserStorageDirectoryFormat())),
Christopher Wileyfe34be02012-11-12 16:02:46 -080051 callback80211_output_(),
Thieu Le6c1e3bb2013-02-06 15:20:35 -080052 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
Gaurav Shah71354762011-11-28 19:22:49 -080065void Daemon::SetStartupProfiles(const vector<string> &profile_name_list) {
66 Error error;
Thieu Lefb46caf2012-03-08 11:57:15 -080067 manager_->set_startup_profiles(profile_name_list);
Thieu Le1271d682011-11-02 22:48:19 +000068}
69
Paul Stewart75897df2011-04-27 09:05:53 -070070void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070071 Start();
Ben Chanfad4a0b2012-04-18 15:49:59 -070072 SLOG(Daemon, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070073 dispatcher_.DispatchForever();
Ben Chanfad4a0b2012-04-18 15:49:59 -070074 SLOG(Daemon, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000075}
76
77void Daemon::Quit() {
Gary Moraina9fb3252012-05-31 12:05:31 -070078 SLOG(Daemon, 1) << "Starting termination actions.";
79 // Stop() prevents autoconnect from attempting to immediately connect to
80 // services after they have been disconnected.
81 Stop();
Arman Ugurayab22c162012-10-08 19:08:38 -070082 if (!manager_->RunTerminationActionsAndNotifyMetrics(
83 Bind(&Daemon::TerminationActionsCompleted, Unretained(this)),
84 Metrics::kTerminationActionReasonTerminate)) {
85 SLOG(Daemon, 1) << "No termination actions were run";
86 dispatcher_.PostTask(MessageLoop::QuitClosure());
87 }
Gary Moraina9fb3252012-05-31 12:05:31 -070088}
89
Arman Ugurayab22c162012-10-08 19:08:38 -070090void Daemon::TerminationActionsCompleted(const Error &error) {
Gary Moraina9fb3252012-05-31 12:05:31 -070091 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Thieu Le6c1e3bb2013-02-06 15:20:35 -080092 metrics_->NotifyTerminationActionsCompleted(
Arman Ugurayab22c162012-10-08 19:08:38 -070093 Metrics::kTerminationActionReasonTerminate, error.IsSuccess());
Eric Shienbrood3e20a232012-02-16 11:35:56 -050094 dispatcher_.PostTask(MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -070095}
96
Gaurav Shah71354762011-11-28 19:22:49 -080097void Daemon::Start() {
98 glib_.TypeInit();
Thieu Lefb46caf2012-03-08 11:57:15 -080099 proxy_factory_->Init();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800100 metrics_->Start();
Thieu Lefb46caf2012-03-08 11:57:15 -0800101 rtnl_handler_->Start(&dispatcher_, &sockets_);
102 routing_table_->Start();
103 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
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.
Wade Guthrie71cb0a72013-02-27 10:27:18 -0800115 callback80211_output_.InstallAsBroadcastHandler();
116 callback80211_metrics_.InstallAsBroadcastHandler();
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();
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800128 metrics_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800129}
Paul Stewart75897df2011-04-27 09:05:53 -0700130
131} // namespace shill