blob: 3b8e6acd7c93ecc19889f3454df0c1a628e14bc3 [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 Guthrie64b4c142012-08-20 15:21:01 -070015#include "shill/callback80211_object.h"
16#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
Gary Moraina9fb3252012-05-31 12:05:31 -070033// TODO(gmorain): 3 seconds may or may not be enough. Add an UMA stat to see
34// how often the timeout occurs. crosbug.com/31475.
Wade Guthrie0d438532012-05-18 14:18:50 -070035const int Daemon::kTerminationActionsTimeout = 3000; // ms
Gary Moraina9fb3252012-05-31 12:05:31 -070036
Darin Petkova7b89492011-07-27 12:48:17 -070037Daemon::Daemon(Config *config, ControlInterface *control)
38 : config_(config),
39 control_(control),
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020040 nss_(NSS::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080041 proxy_factory_(ProxyFactory::GetInstance()),
42 rtnl_handler_(RTNLHandler::GetInstance()),
43 routing_table_(RoutingTable::GetInstance()),
44 dhcp_provider_(DHCPProvider::GetInstance()),
Wade Guthrie0d438532012-05-18 14:18:50 -070045 config80211_(Config80211::GetInstance()),
46 callback80211_(Callback80211Object::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080047 manager_(new Manager(control_,
48 &dispatcher_,
49 &metrics_,
50 &glib_,
51 config->GetRunDirectory(),
52 config->GetStorageDirectory(),
53 config->GetUserStorageDirectoryFormat())) {
Chris Masone2ae797d2011-08-23 20:41:00 -070054}
Wade Guthrie0d438532012-05-18 14:18:50 -070055
Paul Stewart75897df2011-04-27 09:05:53 -070056Daemon::~Daemon() {}
57
mukesh agrawal8f317b62011-07-15 11:53:23 -070058void Daemon::AddDeviceToBlackList(const string &device_name) {
Thieu Lefb46caf2012-03-08 11:57:15 -080059 manager_->AddDeviceToBlackList(device_name);
mukesh agrawal8f317b62011-07-15 11:53:23 -070060}
61
Paul Stewart10e9e4e2012-04-26 19:46:28 -070062void Daemon::SetStartupPortalList(const string &portal_list) {
63 manager_->SetStartupPortalList(portal_list);
64}
65
Gaurav Shah71354762011-11-28 19:22:49 -080066void Daemon::SetStartupProfiles(const vector<string> &profile_name_list) {
67 Error error;
Thieu Lefb46caf2012-03-08 11:57:15 -080068 manager_->set_startup_profiles(profile_name_list);
Thieu Le1271d682011-11-02 22:48:19 +000069}
70
Paul Stewart75897df2011-04-27 09:05:53 -070071void Daemon::Run() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070072 Start();
Ben Chanfad4a0b2012-04-18 15:49:59 -070073 SLOG(Daemon, 1) << "Running main loop.";
Chris Masonec5b392e2011-05-14 16:31:01 -070074 dispatcher_.DispatchForever();
Ben Chanfad4a0b2012-04-18 15:49:59 -070075 SLOG(Daemon, 1) << "Exited main loop.";
Thieu Le1271d682011-11-02 22:48:19 +000076}
77
78void Daemon::Quit() {
Gary Moraina9fb3252012-05-31 12:05:31 -070079 SLOG(Daemon, 1) << "Starting termination actions.";
80 // Stop() prevents autoconnect from attempting to immediately connect to
81 // services after they have been disconnected.
82 Stop();
83 manager_->RunTerminationActions(kTerminationActionsTimeout,
84 Bind(&Daemon::TerminationActionsCompleted,
85 Unretained(this)));
86}
87
88void Daemon::TerminationActionsCompleted(const Error & error) {
89 SLOG(Daemon, 1) << "Finished termination actions. Result: " << error;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050090 dispatcher_.PostTask(MessageLoop::QuitClosure());
Paul Stewart75897df2011-04-27 09:05:53 -070091}
92
Gaurav Shah71354762011-11-28 19:22:49 -080093void Daemon::Start() {
94 glib_.TypeInit();
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020095 nss_->Init(&glib_);
Thieu Lefb46caf2012-03-08 11:57:15 -080096 proxy_factory_->Init();
97 rtnl_handler_->Start(&dispatcher_, &sockets_);
98 routing_table_->Start();
99 dhcp_provider_->Init(control_, &dispatcher_, &glib_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700100
101 if (config80211_) {
102 config80211_->Init(&dispatcher_);
103 // Subscribe to all the events in which we're interested.
104 static const Config80211::EventType kEvents[] = {
105 Config80211::kEventTypeConfig,
106 Config80211::kEventTypeScan,
107 Config80211::kEventTypeRegulatory,
108 Config80211::kEventTypeMlme };
109
110 // Install |callback80211_| in the Config80211 singleton.
Wade Guthried4977f22012-08-22 12:37:54 -0700111 callback80211_->set_metrics(&metrics_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700112 callback80211_->set_config80211(config80211_);
113 callback80211_->InstallAsCallback();
114
115 for (size_t i = 0; i < arraysize(kEvents); i++) {
116 config80211_->SubscribeToEvents(kEvents[i]);
117 }
118 }
119
Thieu Lefb46caf2012-03-08 11:57:15 -0800120 manager_->Start();
Gaurav Shah71354762011-11-28 19:22:49 -0800121}
122
123void Daemon::Stop() {
Thieu Lefb46caf2012-03-08 11:57:15 -0800124 manager_->Stop();
Gaurav Shah71354762011-11-28 19:22:49 -0800125}
Paul Stewart75897df2011-04-27 09:05:53 -0700126
127} // namespace shill