blob: 9065ea559a8f3976d30bf378fb5b079b49deab9a [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>
16#include <base/file_path.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070017#include <base/string_number_conversions.h>
mukesh agrawal8f317b62011-07-15 11:53:23 -070018#include <base/string_split.h>
Chris Masoneee929b72011-05-10 10:02:18 -070019#include <chromeos/syslog_logging.h>
20
Paul Stewart75897df2011-04-27 09:05:53 -070021#include "shill/dbus_control.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070022#include "shill/logging.h"
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -070023#include "shill/minijail.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070024#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070025#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070026
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080027using base::FilePath;
Paul Stewart75897df2011-04-27 09:05:53 -070028using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070029using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070030
Chris Masoneee929b72011-05-10 10:02:18 -070031namespace switches {
32
33// Don't daemon()ize; run in foreground.
34static const char kForeground[] = "foreground";
mukesh agrawal8f317b62011-07-15 11:53:23 -070035// Don't attempt to manage these devices.
36static const char kDeviceBlackList[] = "device-black-list";
Paul Stewart10e9e4e2012-04-26 19:46:28 -070037// Technologies to enable for portal check at startup.
38static const char kPortalList[] = "portal-list";
Chris Masoneee929b72011-05-10 10:02:18 -070039// Flag that causes shill to show the help message and exit.
40static const char kHelp[] = "help";
Ben Chan156e73f2012-04-20 12:02:10 -070041// Logging level:
42// 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),
43// -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.
Chris Masone7ccc8192011-05-24 14:54:49 -070044static const char kLogLevel[] = "log-level";
Ben Chan156e73f2012-04-20 12:02:10 -070045// Scopes to enable for SLOG()-based logging.
46static const char kLogScopes[] = "log-scopes";
Chris Masoneee929b72011-05-10 10:02:18 -070047
48// The help message shown if help flag is passed to the program.
49static const char kHelpMessage[] = "\n"
50 "Available Switches: \n"
51 " --foreground\n"
52 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070053 " --device-black-list=device1,device2\n"
54 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070055 " --log-level=N\n"
Ben Chan156e73f2012-04-20 12:02:10 -070056 " Logging level:\n"
57 " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n"
58 " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n"
59 " --log-scopes=\"*scope1+scope2\".\n"
60 " Scopes to enable for SLOG()-based logging.\n"
Paul Stewart10e9e4e2012-04-26 19:46:28 -070061 " --portal-list=technology1,technology2\n"
Wade Guthriebb5d4a62013-06-07 09:41:51 -070062 " Specify technologies to perform portal detection on at startup.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070063} // namespace switches
64
mukesh agrawal7eb02892012-05-29 11:22:37 -070065namespace {
66
67const char *kLoggerCommand = "/usr/bin/logger";
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -070068const char *kLoggerUser = "syslog";
mukesh agrawal7eb02892012-05-29 11:22:37 -070069
70} // namespace
71
Chris Masoneee929b72011-05-10 10:02:18 -070072// Always logs to the syslog and logs to stderr if
73// we are running in the foreground.
mukesh agrawal7eb02892012-05-29 11:22:37 -070074void SetupLogging(bool foreground, char *daemon_name) {
Chris Masoneee929b72011-05-10 10:02:18 -070075 int log_flags = 0;
76 log_flags |= chromeos::kLogToSyslog;
mukesh agrawal43500062012-02-09 18:25:15 -080077 log_flags |= chromeos::kLogHeader;
Chris Masoneee929b72011-05-10 10:02:18 -070078 if (foreground) {
79 log_flags |= chromeos::kLogToStderr;
80 }
81 chromeos::InitLog(log_flags);
mukesh agrawal7eb02892012-05-29 11:22:37 -070082
83 if (!foreground) {
84 vector<char *> logger_command_line;
85 int logger_stdin_fd;
86 logger_command_line.push_back(const_cast<char *>(kLoggerCommand));
87 logger_command_line.push_back(const_cast<char *>("--priority"));
88 logger_command_line.push_back(const_cast<char *>("daemon.err"));
89 logger_command_line.push_back(const_cast<char *>("--tag"));
90 logger_command_line.push_back(daemon_name);
91 logger_command_line.push_back(NULL);
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -070092
93 shill::Minijail *minijail = shill::Minijail::GetInstance();
94 struct minijail *jail = minijail->New();
95 minijail->DropRoot(jail, kLoggerUser);
96
97 if (!minijail->RunPipeAndDestroy(jail, logger_command_line,
98 NULL, &logger_stdin_fd)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -070099 LOG(ERROR) << "Unable to spawn logger. "
100 << "Writes to stderr will be discarded.";
101 return;
102 }
103
104 // Note that we don't set O_CLOEXEC here. This means that stderr
105 // from any child processes will, by default, be logged to syslog.
106 if (dup2(logger_stdin_fd, fileno(stderr)) != fileno(stderr)) {
107 LOG(ERROR) << "Failed to redirect stderr to syslog: "
108 << strerror(errno);
109 }
110 close(logger_stdin_fd);
111 }
Chris Masoneee929b72011-05-10 10:02:18 -0700112}
113
Thieu Leb52d6ff2011-11-02 18:21:51 +0000114void DeleteDBusControl(void* param) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700115 SLOG(DBus, 2) << __func__;
Thieu Leb52d6ff2011-11-02 18:21:51 +0000116 shill::DBusControl* dbus_control =
117 reinterpret_cast<shill::DBusControl*>(param);
118 delete dbus_control;
119}
120
Thieu Le1271d682011-11-02 22:48:19 +0000121gboolean ExitSigHandler(gpointer data) {
Paul Stewart63864b62012-11-07 15:10:55 -0800122 LOG(INFO) << "Shutting down due to received signal.";
Thieu Le1271d682011-11-02 22:48:19 +0000123 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
124 daemon->Quit();
125 return TRUE;
126}
127
Chris Masoneee929b72011-05-10 10:02:18 -0700128
129int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700130 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -0700131 CommandLine::Init(argc, argv);
132 CommandLine* cl = CommandLine::ForCurrentProcess();
133
Chris Masoneee929b72011-05-10 10:02:18 -0700134 if (cl->HasSwitch(switches::kHelp)) {
135 LOG(INFO) << switches::kHelpMessage;
136 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700137 }
Paul Stewartb7163252011-12-09 16:21:30 -0800138
139 const int nochdir = 0, noclose = 0;
140 if (!cl->HasSwitch(switches::kForeground))
Wade Guthriebb5d4a62013-06-07 09:41:51 -0700141 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1) << "Failed to daemonize";
Paul Stewartb7163252011-12-09 16:21:30 -0800142
mukesh agrawal7eb02892012-05-29 11:22:37 -0700143 SetupLogging(cl->HasSwitch(switches::kForeground), argv[0]);
Chris Masone7ccc8192011-05-24 14:54:49 -0700144 if (cl->HasSwitch(switches::kLogLevel)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700145 string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
Chris Masone7ccc8192011-05-24 14:54:49 -0700146 int level = 0;
147 if (base::StringToInt(log_level, &level) &&
Ben Chan156e73f2012-04-20 12:02:10 -0700148 level < logging::LOG_NUM_SEVERITIES) {
Chris Masone7ccc8192011-05-24 14:54:49 -0700149 logging::SetMinLogLevel(level);
Ben Chan156e73f2012-04-20 12:02:10 -0700150 // Like VLOG, SLOG uses negative verbose level.
151 shill::ScopeLogger::GetInstance()->set_verbose_level(-level);
Chris Masone7ccc8192011-05-24 14:54:49 -0700152 } else {
153 LOG(WARNING) << "Bad log level: " << log_level;
154 }
155 }
Paul Stewart75897df2011-04-27 09:05:53 -0700156
Ben Chan156e73f2012-04-20 12:02:10 -0700157 if (cl->HasSwitch(switches::kLogScopes)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700158 string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes);
Ben Chan156e73f2012-04-20 12:02:10 -0700159 shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes);
160 }
161
Darin Petkovaceede32011-07-18 15:32:38 -0700162 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000163 // Make sure we delete the DBusControl object AFTER the LazyInstances
164 // since some LazyInstances destructors rely on D-Bus being around.
165 shill::DBusControl* dbus_control = new shill::DBusControl();
166 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700167 dbus_control->Init();
168
Wade Guthriebb5d4a62013-06-07 09:41:51 -0700169 shill::Config config;
Thieu Leb52d6ff2011-11-02 18:21:51 +0000170 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700171
172 if (cl->HasSwitch(switches::kDeviceBlackList)) {
173 vector<string> device_list;
174 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
175 ',', &device_list);
176
177 vector<string>::iterator i;
178 for (i = device_list.begin(); i != device_list.end(); ++i) {
179 daemon.AddDeviceToBlackList(*i);
180 }
181 }
Thieu Le1271d682011-11-02 22:48:19 +0000182
Paul Stewart10e9e4e2012-04-26 19:46:28 -0700183 if (cl->HasSwitch(switches::kPortalList)) {
184 daemon.SetStartupPortalList(cl->GetSwitchValueASCII(switches::kPortalList));
185 }
186
Thieu Le1271d682011-11-02 22:48:19 +0000187 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
188 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
189
Paul Stewart75897df2011-04-27 09:05:53 -0700190 daemon.Run();
191
192 return 0;
193}