blob: ac7f407232f6fe76312758fdb18ebc4c380d63e5 [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>
6#include <glib.h>
Ben Chan03ed8182012-04-19 20:39:27 -07007#include <glib-unix.h>
mukesh agrawal7eb02892012-05-29 11:22:37 -07008#include <stdio.h>
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <time.h>
Gaurav Shahb893bed2011-11-28 19:37:16 -080010#include <unistd.h>
11
Paul Stewart75897df2011-04-27 09:05:53 -070012#include <string>
mukesh agrawal8f317b62011-07-15 11:53:23 -070013#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070014
Chris Masone0e1d1042011-05-09 18:07:03 -070015#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -070016#include <base/command_line.h>
17#include <base/file_path.h>
18#include <base/logging.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070019#include <base/string_number_conversions.h>
mukesh agrawal8f317b62011-07-15 11:53:23 -070020#include <base/string_split.h>
Chris Masoneee929b72011-05-10 10:02:18 -070021#include <chromeos/syslog_logging.h>
22
Paul Stewart75897df2011-04-27 09:05:53 -070023#include "shill/dbus_control.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070024#include "shill/scope_logger.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070025#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070026#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070027
28using 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";
35// Directory to read confguration settings.
36static const char kConfigDir[] = "config-dir";
37// Directory to read default configuration settings (Read Only).
38static const char kDefaultConfigDir[] = "default-config-dir";
mukesh agrawal8f317b62011-07-15 11:53:23 -070039// Don't attempt to manage these devices.
40static const char kDeviceBlackList[] = "device-black-list";
Paul Stewart10e9e4e2012-04-26 19:46:28 -070041// Technologies to enable for portal check at startup.
42static const char kPortalList[] = "portal-list";
Gaurav Shah71354762011-11-28 19:22:49 -080043// Flag to specify specific profiles to be pushed.
44static const char kPushProfiles[] = "push";
Chris Masoneee929b72011-05-10 10:02:18 -070045// Flag that causes shill to show the help message and exit.
46static const char kHelp[] = "help";
Ben Chan156e73f2012-04-20 12:02:10 -070047// Logging level:
48// 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),
49// -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.
Chris Masone7ccc8192011-05-24 14:54:49 -070050static const char kLogLevel[] = "log-level";
Ben Chan156e73f2012-04-20 12:02:10 -070051// Scopes to enable for SLOG()-based logging.
52static const char kLogScopes[] = "log-scopes";
Darin Petkov4f5e5492012-04-18 14:05:55 +020053// Use the same directories flimflam uses (profiles, run dir...)
54static const char kUseFlimflamDirs[] = "use-flimflam-dirs";
Chris Masoneee929b72011-05-10 10:02:18 -070055
56// The help message shown if help flag is passed to the program.
57static const char kHelpMessage[] = "\n"
58 "Available Switches: \n"
59 " --foreground\n"
60 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070061 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070062 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070063 " --default-config-dir\n"
64 " Directory to read default configuration settings (Read Only).\n"
65 " --device-black-list=device1,device2\n"
66 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070067 " --log-level=N\n"
Ben Chan156e73f2012-04-20 12:02:10 -070068 " Logging level:\n"
69 " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n"
70 " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n"
71 " --log-scopes=\"*scope1+scope2\".\n"
72 " Scopes to enable for SLOG()-based logging.\n"
Paul Stewart10e9e4e2012-04-26 19:46:28 -070073 " --portal-list=technology1,technology2\n"
74 " Specify technologies to perform portal detection on at startup.\n"
Gaurav Shah71354762011-11-28 19:22:49 -080075 " --push=profile1,profile2\n"
76 " Specify profiles to push on startup.\n"
Darin Petkov4f5e5492012-04-18 14:05:55 +020077 " --use-flimflam-dirs\n"
Ben Chan156e73f2012-04-20 12:02:10 -070078 " Use the same directories flimflam uses (profiles, run dir...).\n";
Chris Masoneee929b72011-05-10 10:02:18 -070079} // namespace switches
80
mukesh agrawal7eb02892012-05-29 11:22:37 -070081namespace {
82
83const char *kLoggerCommand = "/usr/bin/logger";
84
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);
107 if (!g_spawn_async_with_pipes(NULL,
108 logger_command_line.data(),
109 NULL,
110 G_SPAWN_STDERR_TO_DEV_NULL,
111 NULL,
112 NULL,
113 NULL,
114 &logger_stdin_fd,
115 NULL,
116 NULL,
117 NULL)) {
118 LOG(ERROR) << "Unable to spawn logger. "
119 << "Writes to stderr will be discarded.";
120 return;
121 }
122
123 // Note that we don't set O_CLOEXEC here. This means that stderr
124 // from any child processes will, by default, be logged to syslog.
125 if (dup2(logger_stdin_fd, fileno(stderr)) != fileno(stderr)) {
126 LOG(ERROR) << "Failed to redirect stderr to syslog: "
127 << strerror(errno);
128 }
129 close(logger_stdin_fd);
130 }
Chris Masoneee929b72011-05-10 10:02:18 -0700131}
132
Thieu Leb52d6ff2011-11-02 18:21:51 +0000133void DeleteDBusControl(void* param) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700134 SLOG(DBus, 2) << __func__;
Thieu Leb52d6ff2011-11-02 18:21:51 +0000135 shill::DBusControl* dbus_control =
136 reinterpret_cast<shill::DBusControl*>(param);
137 delete dbus_control;
138}
139
Thieu Le1271d682011-11-02 22:48:19 +0000140gboolean ExitSigHandler(gpointer data) {
141 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
142 daemon->Quit();
143 return TRUE;
144}
145
Chris Masoneee929b72011-05-10 10:02:18 -0700146
147int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700148 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -0700149 CommandLine::Init(argc, argv);
150 CommandLine* cl = CommandLine::ForCurrentProcess();
151
Chris Masoneee929b72011-05-10 10:02:18 -0700152 if (cl->HasSwitch(switches::kHelp)) {
153 LOG(INFO) << switches::kHelpMessage;
154 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700155 }
Paul Stewartb7163252011-12-09 16:21:30 -0800156
157 const int nochdir = 0, noclose = 0;
158 if (!cl->HasSwitch(switches::kForeground))
159 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize";
160
mukesh agrawal7eb02892012-05-29 11:22:37 -0700161 SetupLogging(cl->HasSwitch(switches::kForeground), argv[0]);
Chris Masone7ccc8192011-05-24 14:54:49 -0700162 if (cl->HasSwitch(switches::kLogLevel)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700163 string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
Chris Masone7ccc8192011-05-24 14:54:49 -0700164 int level = 0;
165 if (base::StringToInt(log_level, &level) &&
Ben Chan156e73f2012-04-20 12:02:10 -0700166 level < logging::LOG_NUM_SEVERITIES) {
Chris Masone7ccc8192011-05-24 14:54:49 -0700167 logging::SetMinLogLevel(level);
Ben Chan156e73f2012-04-20 12:02:10 -0700168 // Like VLOG, SLOG uses negative verbose level.
169 shill::ScopeLogger::GetInstance()->set_verbose_level(-level);
Chris Masone7ccc8192011-05-24 14:54:49 -0700170 } else {
171 LOG(WARNING) << "Bad log level: " << log_level;
172 }
173 }
Paul Stewart75897df2011-04-27 09:05:53 -0700174
Ben Chan156e73f2012-04-20 12:02:10 -0700175 if (cl->HasSwitch(switches::kLogScopes)) {
mukesh agrawal7eb02892012-05-29 11:22:37 -0700176 string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes);
Ben Chan156e73f2012-04-20 12:02:10 -0700177 shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes);
178 }
179
Chris Masoneee929b72011-05-10 10:02:18 -0700180 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
181 FilePath default_config_dir(
182 !cl->HasSwitch(switches::kDefaultConfigDir) ?
183 shill::Config::kShillDefaultPrefsDir :
184 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700185
Paul Stewart75897df2011-04-27 09:05:53 -0700186 shill::Config config; /* (config_dir, default_config_dir) */
Darin Petkov4f5e5492012-04-18 14:05:55 +0200187 if (cl->HasSwitch(switches::kUseFlimflamDirs))
188 config.UseFlimflamDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700189
Darin Petkovaceede32011-07-18 15:32:38 -0700190 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000191 // Make sure we delete the DBusControl object AFTER the LazyInstances
192 // since some LazyInstances destructors rely on D-Bus being around.
193 shill::DBusControl* dbus_control = new shill::DBusControl();
194 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700195 dbus_control->Init();
196
Thieu Leb52d6ff2011-11-02 18:21:51 +0000197 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700198
Gaurav Shah71354762011-11-28 19:22:49 -0800199 if (cl->HasSwitch(switches::kPushProfiles)) {
200 vector<string> profile_list;
201 base::SplitString(cl->GetSwitchValueASCII(switches::kPushProfiles),
202 ',', &profile_list);
203 daemon.SetStartupProfiles(profile_list);
204 }
205
mukesh agrawal8f317b62011-07-15 11:53:23 -0700206 if (cl->HasSwitch(switches::kDeviceBlackList)) {
207 vector<string> device_list;
208 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
209 ',', &device_list);
210
211 vector<string>::iterator i;
212 for (i = device_list.begin(); i != device_list.end(); ++i) {
213 daemon.AddDeviceToBlackList(*i);
214 }
215 }
Thieu Le1271d682011-11-02 22:48:19 +0000216
Paul Stewart10e9e4e2012-04-26 19:46:28 -0700217 if (cl->HasSwitch(switches::kPortalList)) {
218 daemon.SetStartupPortalList(cl->GetSwitchValueASCII(switches::kPortalList));
219 }
220
Thieu Le1271d682011-11-02 22:48:19 +0000221 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
222 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
223
Paul Stewart75897df2011-04-27 09:05:53 -0700224 daemon.Run();
225
226 return 0;
227}