blob: 6c5475d0efab9eb640e518f15feabde83b1883c4 [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
27using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070028using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070029
Chris Masoneee929b72011-05-10 10:02:18 -070030namespace switches {
31
32// Don't daemon()ize; run in foreground.
33static const char kForeground[] = "foreground";
34// Directory to read confguration settings.
35static const char kConfigDir[] = "config-dir";
36// Directory to read default configuration settings (Read Only).
37static const char kDefaultConfigDir[] = "default-config-dir";
mukesh agrawal8f317b62011-07-15 11:53:23 -070038// Don't attempt to manage these devices.
39static const char kDeviceBlackList[] = "device-black-list";
Paul Stewart10e9e4e2012-04-26 19:46:28 -070040// Technologies to enable for portal check at startup.
41static const char kPortalList[] = "portal-list";
Gaurav Shah71354762011-11-28 19:22:49 -080042// Flag to specify specific profiles to be pushed.
43static const char kPushProfiles[] = "push";
Chris Masoneee929b72011-05-10 10:02:18 -070044// Flag that causes shill to show the help message and exit.
45static const char kHelp[] = "help";
Ben Chan156e73f2012-04-20 12:02:10 -070046// Logging level:
47// 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),
48// -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.
Chris Masone7ccc8192011-05-24 14:54:49 -070049static const char kLogLevel[] = "log-level";
Ben Chan156e73f2012-04-20 12:02:10 -070050// Scopes to enable for SLOG()-based logging.
51static const char kLogScopes[] = "log-scopes";
Darin Petkov4f5e5492012-04-18 14:05:55 +020052// Use the same directories flimflam uses (profiles, run dir...)
53static const char kUseFlimflamDirs[] = "use-flimflam-dirs";
Chris Masoneee929b72011-05-10 10:02:18 -070054
55// The help message shown if help flag is passed to the program.
56static const char kHelpMessage[] = "\n"
57 "Available Switches: \n"
58 " --foreground\n"
59 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070060 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070061 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070062 " --default-config-dir\n"
63 " Directory to read default configuration settings (Read Only).\n"
64 " --device-black-list=device1,device2\n"
65 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070066 " --log-level=N\n"
Ben Chan156e73f2012-04-20 12:02:10 -070067 " Logging level:\n"
68 " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n"
69 " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n"
70 " --log-scopes=\"*scope1+scope2\".\n"
71 " Scopes to enable for SLOG()-based logging.\n"
Paul Stewart10e9e4e2012-04-26 19:46:28 -070072 " --portal-list=technology1,technology2\n"
73 " Specify technologies to perform portal detection on at startup.\n"
Gaurav Shah71354762011-11-28 19:22:49 -080074 " --push=profile1,profile2\n"
75 " Specify profiles to push on startup.\n"
Darin Petkov4f5e5492012-04-18 14:05:55 +020076 " --use-flimflam-dirs\n"
Ben Chan156e73f2012-04-20 12:02:10 -070077 " Use the same directories flimflam uses (profiles, run dir...).\n";
Chris Masoneee929b72011-05-10 10:02:18 -070078} // namespace switches
79
mukesh agrawal7eb02892012-05-29 11:22:37 -070080namespace {
81
82const char *kLoggerCommand = "/usr/bin/logger";
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -070083const char *kLoggerUser = "syslog";
mukesh agrawal7eb02892012-05-29 11:22:37 -070084
85} // namespace
86
Chris Masoneee929b72011-05-10 10:02:18 -070087// Always logs to the syslog and logs to stderr if
88// we are running in the foreground.
mukesh agrawal7eb02892012-05-29 11:22:37 -070089void SetupLogging(bool foreground, char *daemon_name) {
Chris Masoneee929b72011-05-10 10:02:18 -070090 int log_flags = 0;
91 log_flags |= chromeos::kLogToSyslog;
mukesh agrawal43500062012-02-09 18:25:15 -080092 log_flags |= chromeos::kLogHeader;
Chris Masoneee929b72011-05-10 10:02:18 -070093 if (foreground) {
94 log_flags |= chromeos::kLogToStderr;
95 }
96 chromeos::InitLog(log_flags);
mukesh agrawal7eb02892012-05-29 11:22:37 -070097
98 if (!foreground) {
99 vector<char *> logger_command_line;
100 int logger_stdin_fd;
101 logger_command_line.push_back(const_cast<char *>(kLoggerCommand));
102 logger_command_line.push_back(const_cast<char *>("--priority"));
103 logger_command_line.push_back(const_cast<char *>("daemon.err"));
104 logger_command_line.push_back(const_cast<char *>("--tag"));
105 logger_command_line.push_back(daemon_name);
106 logger_command_line.push_back(NULL);
Jorge Lucangeli Obes8c1706f2012-08-30 15:30:48 -0700107
108 shill::Minijail *minijail = shill::Minijail::GetInstance();
109 struct minijail *jail = minijail->New();
110 minijail->DropRoot(jail, kLoggerUser);
111
112 if (!minijail->RunPipeAndDestroy(jail, logger_command_line,
113 NULL, &logger_stdin_fd)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700114 LOG(ERROR) << "Unable to spawn logger. "
115 << "Writes to stderr will be discarded.";
116 return;
117 }
118
119 // Note that we don't set O_CLOEXEC here. This means that stderr
120 // from any child processes will, by default, be logged to syslog.
121 if (dup2(logger_stdin_fd, fileno(stderr)) != fileno(stderr)) {
122 LOG(ERROR) << "Failed to redirect stderr to syslog: "
123 << strerror(errno);
124 }
125 close(logger_stdin_fd);
126 }
Chris Masoneee929b72011-05-10 10:02:18 -0700127}
128
Thieu Leb52d6ff2011-11-02 18:21:51 +0000129void DeleteDBusControl(void* param) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700130 SLOG(DBus, 2) << __func__;
Thieu Leb52d6ff2011-11-02 18:21:51 +0000131 shill::DBusControl* dbus_control =
132 reinterpret_cast<shill::DBusControl*>(param);
133 delete dbus_control;
134}
135
Thieu Le1271d682011-11-02 22:48:19 +0000136gboolean ExitSigHandler(gpointer data) {
137 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
138 daemon->Quit();
139 return TRUE;
140}
141
Chris Masoneee929b72011-05-10 10:02:18 -0700142
143int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700144 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -0700145 CommandLine::Init(argc, argv);
146 CommandLine* cl = CommandLine::ForCurrentProcess();
147
Chris Masoneee929b72011-05-10 10:02:18 -0700148 if (cl->HasSwitch(switches::kHelp)) {
149 LOG(INFO) << switches::kHelpMessage;
150 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700151 }
Paul Stewartb7163252011-12-09 16:21:30 -0800152
153 const int nochdir = 0, noclose = 0;
154 if (!cl->HasSwitch(switches::kForeground))
155 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize";
156
mukesh agrawal7eb02892012-05-29 11:22:37 -0700157 SetupLogging(cl->HasSwitch(switches::kForeground), argv[0]);
Chris Masone7ccc8192011-05-24 14:54:49 -0700158 if (cl->HasSwitch(switches::kLogLevel)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700159 string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
Chris Masone7ccc8192011-05-24 14:54:49 -0700160 int level = 0;
161 if (base::StringToInt(log_level, &level) &&
Ben Chan156e73f2012-04-20 12:02:10 -0700162 level < logging::LOG_NUM_SEVERITIES) {
Chris Masone7ccc8192011-05-24 14:54:49 -0700163 logging::SetMinLogLevel(level);
Ben Chan156e73f2012-04-20 12:02:10 -0700164 // Like VLOG, SLOG uses negative verbose level.
165 shill::ScopeLogger::GetInstance()->set_verbose_level(-level);
Chris Masone7ccc8192011-05-24 14:54:49 -0700166 } else {
167 LOG(WARNING) << "Bad log level: " << log_level;
168 }
169 }
Paul Stewart75897df2011-04-27 09:05:53 -0700170
Ben Chan156e73f2012-04-20 12:02:10 -0700171 if (cl->HasSwitch(switches::kLogScopes)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700172 string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes);
Ben Chan156e73f2012-04-20 12:02:10 -0700173 shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes);
174 }
175
Chris Masoneee929b72011-05-10 10:02:18 -0700176 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
177 FilePath default_config_dir(
178 !cl->HasSwitch(switches::kDefaultConfigDir) ?
179 shill::Config::kShillDefaultPrefsDir :
180 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700181
Paul Stewart75897df2011-04-27 09:05:53 -0700182 shill::Config config; /* (config_dir, default_config_dir) */
Darin Petkov4f5e5492012-04-18 14:05:55 +0200183 if (cl->HasSwitch(switches::kUseFlimflamDirs))
184 config.UseFlimflamDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700185
Darin Petkovaceede32011-07-18 15:32:38 -0700186 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000187 // Make sure we delete the DBusControl object AFTER the LazyInstances
188 // since some LazyInstances destructors rely on D-Bus being around.
189 shill::DBusControl* dbus_control = new shill::DBusControl();
190 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700191 dbus_control->Init();
192
Thieu Leb52d6ff2011-11-02 18:21:51 +0000193 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700194
Gaurav Shah71354762011-11-28 19:22:49 -0800195 if (cl->HasSwitch(switches::kPushProfiles)) {
196 vector<string> profile_list;
197 base::SplitString(cl->GetSwitchValueASCII(switches::kPushProfiles),
198 ',', &profile_list);
199 daemon.SetStartupProfiles(profile_list);
200 }
201
mukesh agrawal8f317b62011-07-15 11:53:23 -0700202 if (cl->HasSwitch(switches::kDeviceBlackList)) {
203 vector<string> device_list;
204 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
205 ',', &device_list);
206
207 vector<string>::iterator i;
208 for (i = device_list.begin(); i != device_list.end(); ++i) {
209 daemon.AddDeviceToBlackList(*i);
210 }
211 }
Thieu Le1271d682011-11-02 22:48:19 +0000212
Paul Stewart10e9e4e2012-04-26 19:46:28 -0700213 if (cl->HasSwitch(switches::kPortalList)) {
214 daemon.SetStartupPortalList(cl->GetSwitchValueASCII(switches::kPortalList));
215 }
216
Thieu Le1271d682011-11-02 22:48:19 +0000217 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
218 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
219
Paul Stewart75897df2011-04-27 09:05:53 -0700220 daemon.Run();
221
222 return 0;
223}