blob: 727d6f61e3f7934299835ff5f465ac8663cc75fc [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
Darin Petkova7b89492011-07-27 12:48:17 -070015#include "shill/dhcp_provider.h"
Gaurav Shah71354762011-11-28 19:22:49 -080016#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070017#include "shill/logging.h"
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020018#include "shill/nss.h"
Darin Petkovab565bb2011-10-06 02:55:51 -070019#include "shill/proxy_factory.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080020#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070021#include "shill/rtnl_handler.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070022#include "shill/shill_config.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023
Gary Moraina9fb3252012-05-31 12:05:31 -070024using base::Bind;
25using base::Unretained;
Paul Stewart75897df2011-04-27 09:05:53 -070026using std::string;
Gaurav Shah71354762011-11-28 19:22:49 -080027using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070028
29namespace shill {
30
Gary Moraina9fb3252012-05-31 12:05:31 -070031// TODO(gmorain): 3 seconds may or may not be enough. Add an UMA stat to see
32// how often the timeout occurs. crosbug.com/31475.
Wade Guthrie0d438532012-05-18 14:18:50 -070033const int Daemon::kTerminationActionsTimeout = 3000; // ms
Gary Moraina9fb3252012-05-31 12:05:31 -070034
Darin Petkova7b89492011-07-27 12:48:17 -070035Daemon::Daemon(Config *config, ControlInterface *control)
36 : config_(config),
37 control_(control),
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()),
44 callback80211_(Callback80211Object::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080045 manager_(new Manager(control_,
46 &dispatcher_,
47 &metrics_,
48 &glib_,
49 config->GetRunDirectory(),
50 config->GetStorageDirectory(),
51 config->GetUserStorageDirectoryFormat())) {
Chris Masone2ae797d2011-08-23 20:41:00 -070052}
Wade Guthrie0d438532012-05-18 14:18:50 -070053
Paul Stewart75897df2011-04-27 09:05:53 -070054Daemon::~Daemon() {}
55
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();
81 manager_->RunTerminationActions(kTerminationActionsTimeout,
82 Bind(&Daemon::TerminationActionsCompleted,
83 Unretained(this)));
84}
85
86void Daemon::TerminationActionsCompleted(const Error & error) {
87 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050088 dispatcher_.PostTask(MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -070089}
90
Gaurav Shah71354762011-11-28 19:22:49 -080091void Daemon::Start() {
92 glib_.TypeInit();
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020093 nss_->Init(&glib_);
Thieu Lefb46caf2012-03-08 11:57:15 -080094 proxy_factory_->Init();
95 rtnl_handler_->Start(&dispatcher_, &sockets_);
96 routing_table_->Start();
97 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
Wade Guthrie0d438532012-05-18 14:18:50 -070098
99 if (config80211_) {
100 config80211_->Init(&dispatcher_);
101 // Subscribe to all the events in which we're interested.
102 static const Config80211::EventType kEvents[] = {
103 Config80211::kEventTypeConfig,
104 Config80211::kEventTypeScan,
105 Config80211::kEventTypeRegulatory,
106 Config80211::kEventTypeMlme };
107
108 // Install |callback80211_| in the Config80211 singleton.
109 callback80211_->set_config80211(config80211_);
110 callback80211_->InstallAsCallback();
111
112 for (size_t i = 0; i < arraysize(kEvents); i++) {
113 config80211_->SubscribeToEvents(kEvents[i]);
114 }
115 }
116
Thieu Lefb46caf2012-03-08 11:57:15 -0800117 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800118}
119
120void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800121 manager_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800122}
Paul Stewart75897df2011-04-27 09:05:53 -0700123
124} // namespace shill