blob: 0c64e5f9cd9950c221c1f52bddb36a29dc8a04e3 [file] [log] [blame]
mukesh agrawal43500062012-02-09 18:25:15 -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
mukesh agrawal7eb02892012-05-29 11:22:37 -07005#include <errno.h>
Ben Chan03ed8182012-04-19 20:39:27 -07006#include <glib-unix.h>
mukesh agrawal7eb02892012-05-29 11:22:37 -07007#include <stdio.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <time.h>
Gaurav Shahb893bed2011-11-28 19:37:16 -08009#include <unistd.h>
10
Paul Stewart75897df2011-04-27 09:05:53 -070011#include <string>
mukesh agrawal8f317b62011-07-15 11:53:23 -070012#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070013
Chris Masone0e1d1042011-05-09 18:07:03 -070014#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -070015#include <base/command_line.h>
Ben Chana0ddf462014-02-06 11:32:42 -080016#include <base/files/file_path.h>
Garret Kelly782cdce2015-04-01 16:39:16 -040017#include <base/strings/string_number_conversions.h>
Ben Chana0ddf462014-02-06 11:32:42 -080018#include <base/strings/string_split.h>
Utkarsh Sanghi83bd64b2014-07-29 16:01:43 -070019#include <chromeos/minijail/minijail.h>
Chris Masoneee929b72011-05-10 10:02:18 -070020#include <chromeos/syslog_logging.h>
21
Paul Stewart75897df2011-04-27 09:05:53 -070022#include "shill/dbus_control.h"
Jason Simmons5a2097c2015-02-10 18:03:08 -080023#include "shill/error.h"
Peter Qiu40a01482014-10-31 11:54:23 -070024#include "shill/glib_io_handler_factory.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070025#include "shill/logging.h"
Peter Qiu40a01482014-10-31 11:54:23 -070026#include "shill/net/io_handler_factory_container.h"
Paul Stewart8dc5e7b2014-12-11 19:24:50 -080027#include "shill/shared_dbus_connection.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070028#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070029#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070030
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080031using base::FilePath;
Paul Stewart75897df2011-04-27 09:05:53 -070032using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070033using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070034
Chris Masoneee929b72011-05-10 10:02:18 -070035namespace switches {
36
37// Don't daemon()ize; run in foreground.
38static const char kForeground[] = "foreground";
mukesh agrawal8f317b62011-07-15 11:53:23 -070039// Don't attempt to manage these devices.
40static const char kDeviceBlackList[] = "device-black-list";
Paul Stewart83d62562015-02-09 19:57:47 -080041// Ignore Ethernet-like devices that don't have any driver information.
42static const char kIgnoreUnknownEthernet[] = "ignore-unknown-ethernet";
Paul Stewart10e9e4e2012-04-26 19:46:28 -070043// Technologies to enable for portal check at startup.
44static const char kPortalList[] = "portal-list";
Peter Qiu1d499ed2015-01-30 16:01:27 -080045// When in passive mode, Shill will not manage any devices by default.
46// Remote service can instruct Shill to manage/unmanage devices through
47// org.chromium.flimflam.Manager's ClaimInterface/ReleaseInterface APIs.
48static const char kPassiveMode[] = "passive-mode";
Jason Simmons5a2097c2015-02-10 18:03:08 -080049// Default priority order of the technologies.
Paul Stewart1ce231c2015-06-12 19:44:22 -070050static const char kTechnologyOrder[] = "default-technology-order";
Garret Kellyc5f89d12015-02-18 14:39:36 -050051// Comma-separated list of DNS servers to prepend to the resolver list.
52static const char kPrependDNSServers[] = "prepend-dns-servers";
Garret Kelly782cdce2015-04-01 16:39:16 -040053// The minimum MTU value that will be respected in DHCP responses.
54static const char kMinimumMTU[] = "minimum-mtu";
Matthew Wein08add482015-04-20 13:26:48 -070055// Accept hostname from the DHCP server for the specified devices.
56// eg. eth0 or eth*
57static const char kAcceptHostnameFrom[] = "accept-hostname-from";
Peter Qiud48fa0c2015-06-10 12:20:48 -070058#ifndef DISABLE_DHCPV6
59// List of devices to enable DHCPv6.
60static const char kDhcpv6EnabledDevices[] = "dhcpv6-enabled-devices";
61#endif // DISABLE_DHCPV6
Chris Masoneee929b72011-05-10 10:02:18 -070062// Flag that causes shill to show the help message and exit.
63static const char kHelp[] = "help";
64
65// The help message shown if help flag is passed to the program.
66static const char kHelpMessage[] = "\n"
67 "Available Switches: \n"
68 " --foreground\n"
69 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070070 " --device-black-list=device1,device2\n"
71 " Do not manage devices named device1 or device2\n"
Paul Stewart83d62562015-02-09 19:57:47 -080072 " --ignore-unknown-ethernet\n"
73 " Ignore Ethernet-like devices that do not report a driver\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070074 " --log-level=N\n"
Ben Chan156e73f2012-04-20 12:02:10 -070075 " Logging level:\n"
76 " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n"
77 " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n"
78 " --log-scopes=\"*scope1+scope2\".\n"
79 " Scopes to enable for SLOG()-based logging.\n"
Paul Stewart10e9e4e2012-04-26 19:46:28 -070080 " --portal-list=technology1,technology2\n"
Peter Qiu1d499ed2015-01-30 16:01:27 -080081 " Specify technologies to perform portal detection on at startup.\n"
82 " --passive-mode\n"
Jason Simmons5a2097c2015-02-10 18:03:08 -080083 " Do not manage any devices by default\n"
84 " --default-technology-order=technology1,technology2\n"
Garret Kellyc5f89d12015-02-18 14:39:36 -050085 " Specify the default priority order of the technologies.\n"
86 " --prepend-dns-servers=server1,server2,...\n"
Garret Kelly782cdce2015-04-01 16:39:16 -040087 " Prepend the provided DNS servers to the resolver list.\n"
Matthew Wein08add482015-04-20 13:26:48 -070088 " --accept-hostname-from=eth0 or --accept-hostname-from=eth*\n"
89 " Accept a hostname from the DHCP server for the matching devices.\n"
Peter Qiud48fa0c2015-06-10 12:20:48 -070090#ifndef DISABLE_DHCPV6
91 " --dhcpv6-enabled-devices=device1,device2\n"
92 " Enable DHCPv6 for devices named device1 and device2\n"
93#endif // DISABLE_DHCPV6
Garret Kelly782cdce2015-04-01 16:39:16 -040094 " --minimum-mtu=mtu\n"
95 " Set the minimum value to respect as the MTU from DHCP responses.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070096} // namespace switches
97
mukesh agrawal7eb02892012-05-29 11:22:37 -070098namespace {
99
Paul Stewart1a212a62015-06-16 13:13:10 -0700100const char* kLoggerCommand = "/usr/bin/logger";
101const char* kLoggerUser = "syslog";
102const char* kDefaultTechnologyOrder = "vpn,ethernet,wifi,wimax,cellular";
mukesh agrawal7eb02892012-05-29 11:22:37 -0700103
104} // namespace
105
Chris Masoneee929b72011-05-10 10:02:18 -0700106// Always logs to the syslog and logs to stderr if
107// we are running in the foreground.
Paul Stewart1a212a62015-06-16 13:13:10 -0700108void SetupLogging(bool foreground, char* daemon_name) {
Chris Masoneee929b72011-05-10 10:02:18 -0700109 int log_flags = 0;
110 log_flags |= chromeos::kLogToSyslog;
mukesh agrawal43500062012-02-09 18:25:15 -0800111 log_flags |= chromeos::kLogHeader;
Chris Masoneee929b72011-05-10 10:02:18 -0700112 if (foreground) {
113 log_flags |= chromeos::kLogToStderr;
114 }
115 chromeos::InitLog(log_flags);
mukesh agrawal7eb02892012-05-29 11:22:37 -0700116
117 if (!foreground) {
Paul Stewart1a212a62015-06-16 13:13:10 -0700118 vector<char*> logger_command_line;
mukesh agrawal7eb02892012-05-29 11:22:37 -0700119 int logger_stdin_fd;
Paul Stewart1a212a62015-06-16 13:13:10 -0700120 logger_command_line.push_back(const_cast<char*>(kLoggerCommand));
121 logger_command_line.push_back(const_cast<char*>("--priority"));
122 logger_command_line.push_back(const_cast<char*>("daemon.err"));
123 logger_command_line.push_back(const_cast<char*>("--tag"));
mukesh agrawal7eb02892012-05-29 11:22:37 -0700124 logger_command_line.push_back(daemon_name);
Ben Chancc225ef2014-09-30 13:26:51 -0700125 logger_command_line.push_back(nullptr);
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -0700126
Paul Stewart1a212a62015-06-16 13:13:10 -0700127 chromeos::Minijail* minijail = chromeos::Minijail::GetInstance();
128 struct minijail* jail = minijail->New();
Utkarsh Sanghie4c6aff2014-07-30 14:49:03 -0700129 minijail->DropRoot(jail, kLoggerUser, kLoggerUser);
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -0700130
Ben Chancc225ef2014-09-30 13:26:51 -0700131 if (!minijail->RunPipeAndDestroy(jail, logger_command_line, nullptr,
132 &logger_stdin_fd)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700133 LOG(ERROR) << "Unable to spawn logger. "
134 << "Writes to stderr will be discarded.";
135 return;
136 }
137
138 // Note that we don't set O_CLOEXEC here. This means that stderr
139 // from any child processes will, by default, be logged to syslog.
140 if (dup2(logger_stdin_fd, fileno(stderr)) != fileno(stderr)) {
Alex Deymoaf198702015-04-09 10:06:31 -0700141 PLOG(ERROR) << "Failed to redirect stderr to syslog";
mukesh agrawal7eb02892012-05-29 11:22:37 -0700142 }
143 close(logger_stdin_fd);
144 }
Chris Masoneee929b72011-05-10 10:02:18 -0700145}
146
Thieu Le1271d682011-11-02 22:48:19 +0000147gboolean ExitSigHandler(gpointer data) {
Paul Stewart63864b62012-11-07 15:10:55 -0800148 LOG(INFO) << "Shutting down due to received signal.";
Paul Stewart8dc5e7b2014-12-11 19:24:50 -0800149
Thieu Le1271d682011-11-02 22:48:19 +0000150 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
151 daemon->Quit();
152 return TRUE;
153}
154
Chris Masoneee929b72011-05-10 10:02:18 -0700155
156int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700157 base::AtExitManager exit_manager;
Alex Vakulenko127f2562015-04-02 14:31:10 -0700158 base::CommandLine::Init(argc, argv);
159 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
Chris Masoneee929b72011-05-10 10:02:18 -0700160
Chris Masoneee929b72011-05-10 10:02:18 -0700161 if (cl->HasSwitch(switches::kHelp)) {
162 LOG(INFO) << switches::kHelpMessage;
163 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700164 }
Paul Stewartb7163252011-12-09 16:21:30 -0800165
166 const int nochdir = 0, noclose = 0;
167 if (!cl->HasSwitch(switches::kForeground))
Wade Guthriebb5d4a62013-06-07 09:41:51 -0700168 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1) << "Failed to daemonize";
Paul Stewartb7163252011-12-09 16:21:30 -0800169
mukesh agrawal7eb02892012-05-29 11:22:37 -0700170 SetupLogging(cl->HasSwitch(switches::kForeground), argv[0]);
Prathmesh Prabhuf002cdb2014-09-17 22:41:05 -0700171 shill::SetLogLevelFromCommandLine(cl);
Ben Chan156e73f2012-04-20 12:02:10 -0700172
Peter Qiu40a01482014-10-31 11:54:23 -0700173 // Overwrite default IOHandlerFactory with the glib version of it. This needs
174 // to be placed before any reference to the IOHandlerFactory.
175 shill::IOHandlerFactoryContainer::GetInstance()->SetIOHandlerFactory(
176 new shill::GlibIOHandlerFactory());
177
Darin Petkovaceede32011-07-18 15:32:38 -0700178 // TODO(pstew): This should be chosen based on config
Paul Stewart8dc5e7b2014-12-11 19:24:50 -0800179 shill::SharedDBusConnection::GetInstance()->Init();
Thieu Leb52d6ff2011-11-02 18:21:51 +0000180 shill::DBusControl* dbus_control = new shill::DBusControl();
Darin Petkovaceede32011-07-18 15:32:38 -0700181 dbus_control->Init();
182
Paul Stewart1ce231c2015-06-12 19:44:22 -0700183 shill::Daemon::Settings settings;
184 if (cl->HasSwitch(switches::kTechnologyOrder)) {
Jason Simmons5a2097c2015-02-10 18:03:08 -0800185 shill::Error error;
186 string order_flag = cl->GetSwitchValueASCII(
Paul Stewart1ce231c2015-06-12 19:44:22 -0700187 switches::kTechnologyOrder);
188 vector<shill::Technology::Identifier> test_order_vector;
189 if (shill::Technology::GetTechnologyVectorFromString(
190 order_flag, &test_order_vector, &error)) {
191 settings.default_technology_order = order_flag;
192 } else {
Jason Simmons5a2097c2015-02-10 18:03:08 -0800193 LOG(ERROR) << "Invalid default technology order: [" << order_flag
194 << "] Error: " << error.message();
195 }
196 }
Paul Stewart1ce231c2015-06-12 19:44:22 -0700197 if (settings.default_technology_order.empty()) {
198 settings.default_technology_order = kDefaultTechnologyOrder;
Jason Simmons5a2097c2015-02-10 18:03:08 -0800199 }
200
mukesh agrawal8f317b62011-07-15 11:53:23 -0700201 if (cl->HasSwitch(switches::kDeviceBlackList)) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700202 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
Paul Stewart1ce231c2015-06-12 19:44:22 -0700203 ',', &settings.device_blacklist);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700204 }
Thieu Le1271d682011-11-02 22:48:19 +0000205
Paul Stewart1ce231c2015-06-12 19:44:22 -0700206 settings.ignore_unknown_ethernet =
207 cl->HasSwitch(switches::kIgnoreUnknownEthernet);
Paul Stewart83d62562015-02-09 19:57:47 -0800208
Paul Stewart10e9e4e2012-04-26 19:46:28 -0700209 if (cl->HasSwitch(switches::kPortalList)) {
Paul Stewart1ce231c2015-06-12 19:44:22 -0700210 settings.use_portal_list = true;
211 settings.portal_list = cl->GetSwitchValueASCII(switches::kPortalList);
Paul Stewart10e9e4e2012-04-26 19:46:28 -0700212 }
213
Paul Stewart1ce231c2015-06-12 19:44:22 -0700214 settings.passive_mode = cl->HasSwitch(switches::kPassiveMode);
Peter Qiu1d499ed2015-01-30 16:01:27 -0800215
Garret Kellyc5f89d12015-02-18 14:39:36 -0500216 if (cl->HasSwitch(switches::kPrependDNSServers)) {
Paul Stewart1ce231c2015-06-12 19:44:22 -0700217 settings.prepend_dns_servers =
218 cl->GetSwitchValueASCII(switches::kPrependDNSServers);
Garret Kellyc5f89d12015-02-18 14:39:36 -0500219 }
220
Garret Kelly782cdce2015-04-01 16:39:16 -0400221 if (cl->HasSwitch(switches::kMinimumMTU)) {
222 int mtu;
223 std::string value = cl->GetSwitchValueASCII(switches::kMinimumMTU);
224 if (!base::StringToInt(value, &mtu)) {
225 LOG(FATAL) << "Could not convert '" << value << "' to integer.";
226 }
Paul Stewart1ce231c2015-06-12 19:44:22 -0700227 settings.minimum_mtu = mtu;
Garret Kelly782cdce2015-04-01 16:39:16 -0400228 }
229
Matthew Wein08add482015-04-20 13:26:48 -0700230 if (cl->HasSwitch(switches::kAcceptHostnameFrom)) {
Paul Stewart1ce231c2015-06-12 19:44:22 -0700231 settings.accept_hostname_from =
232 cl->GetSwitchValueASCII(switches::kAcceptHostnameFrom);
Matthew Wein08add482015-04-20 13:26:48 -0700233 }
234
Peter Qiud48fa0c2015-06-10 12:20:48 -0700235#ifndef DISABLE_DHCPV6
236 if (cl->HasSwitch(switches::kDhcpv6EnabledDevices)) {
Peter Qiud48fa0c2015-06-10 12:20:48 -0700237 base::SplitString(cl->GetSwitchValueASCII(switches::kDhcpv6EnabledDevices),
Paul Stewart1ce231c2015-06-12 19:44:22 -0700238 ',', &settings.dhcpv6_enabled_devices);
Peter Qiud48fa0c2015-06-10 12:20:48 -0700239 }
240#endif // DISABLE_DHCPV6
241
Paul Stewart1ce231c2015-06-12 19:44:22 -0700242 shill::Config config;
243
244 // Passes ownership of dbus_control.
245 shill::Daemon daemon(&config, dbus_control);
246 daemon.ApplySettings(settings);
247
Thieu Le1271d682011-11-02 22:48:19 +0000248 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
249 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
250
Paul Stewart8dc5e7b2014-12-11 19:24:50 -0800251 // Catch but ignore SIGPIPE signals we receive if we write to the logger
252 // process after it exits. GLib cannot handle this signal number, so use
253 // signal() directly.
254 signal(SIGPIPE, SIG_IGN);
255
Paul Stewart75897df2011-04-27 09:05:53 -0700256 daemon.Run();
257
Paul Stewart8dc5e7b2014-12-11 19:24:50 -0800258 LOG(INFO) << "Process exiting.";
259
Paul Stewart75897df2011-04-27 09:05:53 -0700260 return 0;
261}