mukesh agrawal | 4350006 | 2012-02-09 18:25:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 5 | #include <errno.h> |
Ben Chan | 03ed818 | 2012-04-19 20:39:27 -0700 | [diff] [blame] | 6 | #include <glib-unix.h> |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <time.h> |
Gaurav Shah | b893bed | 2011-11-28 19:37:16 -0800 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 11 | #include <string> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 12 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 13 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 14 | #include <base/at_exit.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | #include <base/command_line.h> |
| 16 | #include <base/file_path.h> |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 17 | #include <base/string_number_conversions.h> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 18 | #include <base/string_split.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 19 | #include <chromeos/syslog_logging.h> |
| 20 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 21 | #include "shill/dbus_control.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 22 | #include "shill/logging.h" |
Jorge Lucangeli Obes | 8c1706f | 2012-08-30 15:30:48 -0700 | [diff] [blame] | 23 | #include "shill/minijail.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 24 | #include "shill/shill_config.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 25 | #include "shill/shill_daemon.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 27 | using base::FilePath; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 28 | using std::string; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 29 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 30 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 31 | namespace switches { |
| 32 | |
| 33 | // Don't daemon()ize; run in foreground. |
| 34 | static const char kForeground[] = "foreground"; |
| 35 | // Directory to read confguration settings. |
| 36 | static const char kConfigDir[] = "config-dir"; |
| 37 | // Directory to read default configuration settings (Read Only). |
| 38 | static const char kDefaultConfigDir[] = "default-config-dir"; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 39 | // Don't attempt to manage these devices. |
| 40 | static const char kDeviceBlackList[] = "device-black-list"; |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 41 | // Technologies to enable for portal check at startup. |
| 42 | static const char kPortalList[] = "portal-list"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 43 | // Flag that causes shill to show the help message and exit. |
| 44 | static const char kHelp[] = "help"; |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 45 | // Logging level: |
| 46 | // 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR), |
| 47 | // -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc. |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 48 | static const char kLogLevel[] = "log-level"; |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 49 | // Scopes to enable for SLOG()-based logging. |
| 50 | static const char kLogScopes[] = "log-scopes"; |
Darin Petkov | 4f5e549 | 2012-04-18 14:05:55 +0200 | [diff] [blame] | 51 | // Use the same directories flimflam uses (profiles, run dir...) |
| 52 | static const char kUseFlimflamDirs[] = "use-flimflam-dirs"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 53 | |
| 54 | // The help message shown if help flag is passed to the program. |
| 55 | static const char kHelpMessage[] = "\n" |
| 56 | "Available Switches: \n" |
| 57 | " --foreground\n" |
| 58 | " Don\'t daemon()ize; run in foreground.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 59 | " --config-dir\n" |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 60 | " Directory to read confguration settings.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 61 | " --default-config-dir\n" |
| 62 | " Directory to read default configuration settings (Read Only).\n" |
| 63 | " --device-black-list=device1,device2\n" |
| 64 | " Do not manage devices named device1 or device2\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 65 | " --log-level=N\n" |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 66 | " Logging level:\n" |
| 67 | " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n" |
| 68 | " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n" |
| 69 | " --log-scopes=\"*scope1+scope2\".\n" |
| 70 | " Scopes to enable for SLOG()-based logging.\n" |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 71 | " --portal-list=technology1,technology2\n" |
| 72 | " Specify technologies to perform portal detection on at startup.\n" |
Darin Petkov | 4f5e549 | 2012-04-18 14:05:55 +0200 | [diff] [blame] | 73 | " --use-flimflam-dirs\n" |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 74 | " Use the same directories flimflam uses (profiles, run dir...).\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 75 | } // namespace switches |
| 76 | |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 77 | namespace { |
| 78 | |
| 79 | const char *kLoggerCommand = "/usr/bin/logger"; |
Jorge Lucangeli Obes | 8c1706f | 2012-08-30 15:30:48 -0700 | [diff] [blame] | 80 | const char *kLoggerUser = "syslog"; |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 81 | |
| 82 | } // namespace |
| 83 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 84 | // Always logs to the syslog and logs to stderr if |
| 85 | // we are running in the foreground. |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 86 | void SetupLogging(bool foreground, char *daemon_name) { |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 87 | int log_flags = 0; |
| 88 | log_flags |= chromeos::kLogToSyslog; |
mukesh agrawal | 4350006 | 2012-02-09 18:25:15 -0800 | [diff] [blame] | 89 | log_flags |= chromeos::kLogHeader; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 90 | if (foreground) { |
| 91 | log_flags |= chromeos::kLogToStderr; |
| 92 | } |
| 93 | chromeos::InitLog(log_flags); |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 94 | |
| 95 | if (!foreground) { |
| 96 | vector<char *> logger_command_line; |
| 97 | int logger_stdin_fd; |
| 98 | logger_command_line.push_back(const_cast<char *>(kLoggerCommand)); |
| 99 | logger_command_line.push_back(const_cast<char *>("--priority")); |
| 100 | logger_command_line.push_back(const_cast<char *>("daemon.err")); |
| 101 | logger_command_line.push_back(const_cast<char *>("--tag")); |
| 102 | logger_command_line.push_back(daemon_name); |
| 103 | logger_command_line.push_back(NULL); |
Jorge Lucangeli Obes | 8c1706f | 2012-08-30 15:30:48 -0700 | [diff] [blame] | 104 | |
| 105 | shill::Minijail *minijail = shill::Minijail::GetInstance(); |
| 106 | struct minijail *jail = minijail->New(); |
| 107 | minijail->DropRoot(jail, kLoggerUser); |
| 108 | |
| 109 | if (!minijail->RunPipeAndDestroy(jail, logger_command_line, |
| 110 | NULL, &logger_stdin_fd)) { |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 111 | LOG(ERROR) << "Unable to spawn logger. " |
| 112 | << "Writes to stderr will be discarded."; |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Note that we don't set O_CLOEXEC here. This means that stderr |
| 117 | // from any child processes will, by default, be logged to syslog. |
| 118 | if (dup2(logger_stdin_fd, fileno(stderr)) != fileno(stderr)) { |
| 119 | LOG(ERROR) << "Failed to redirect stderr to syslog: " |
| 120 | << strerror(errno); |
| 121 | } |
| 122 | close(logger_stdin_fd); |
| 123 | } |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 126 | void DeleteDBusControl(void* param) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 127 | SLOG(DBus, 2) << __func__; |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 128 | shill::DBusControl* dbus_control = |
| 129 | reinterpret_cast<shill::DBusControl*>(param); |
| 130 | delete dbus_control; |
| 131 | } |
| 132 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 133 | gboolean ExitSigHandler(gpointer data) { |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 134 | LOG(INFO) << "Shutting down due to received signal."; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 135 | shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data); |
| 136 | daemon->Quit(); |
| 137 | return TRUE; |
| 138 | } |
| 139 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 140 | |
| 141 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 142 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 143 | CommandLine::Init(argc, argv); |
| 144 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 145 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 146 | if (cl->HasSwitch(switches::kHelp)) { |
| 147 | LOG(INFO) << switches::kHelpMessage; |
| 148 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 149 | } |
Paul Stewart | b716325 | 2011-12-09 16:21:30 -0800 | [diff] [blame] | 150 | |
| 151 | const int nochdir = 0, noclose = 0; |
| 152 | if (!cl->HasSwitch(switches::kForeground)) |
| 153 | PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize"; |
| 154 | |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 155 | SetupLogging(cl->HasSwitch(switches::kForeground), argv[0]); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 156 | if (cl->HasSwitch(switches::kLogLevel)) { |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 157 | string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 158 | int level = 0; |
| 159 | if (base::StringToInt(log_level, &level) && |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 160 | level < logging::LOG_NUM_SEVERITIES) { |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 161 | logging::SetMinLogLevel(level); |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 162 | // Like VLOG, SLOG uses negative verbose level. |
| 163 | shill::ScopeLogger::GetInstance()->set_verbose_level(-level); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 164 | } else { |
| 165 | LOG(WARNING) << "Bad log level: " << log_level; |
| 166 | } |
| 167 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 168 | |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 169 | if (cl->HasSwitch(switches::kLogScopes)) { |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 170 | string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes); |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 171 | shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes); |
| 172 | } |
| 173 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 174 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 175 | FilePath default_config_dir( |
| 176 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 177 | shill::Config::kShillDefaultPrefsDir : |
| 178 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 179 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 180 | shill::Config config; /* (config_dir, default_config_dir) */ |
Darin Petkov | 4f5e549 | 2012-04-18 14:05:55 +0200 | [diff] [blame] | 181 | if (cl->HasSwitch(switches::kUseFlimflamDirs)) |
| 182 | config.UseFlimflamDirs(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 183 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 184 | // TODO(pstew): This should be chosen based on config |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 185 | // Make sure we delete the DBusControl object AFTER the LazyInstances |
| 186 | // since some LazyInstances destructors rely on D-Bus being around. |
| 187 | shill::DBusControl* dbus_control = new shill::DBusControl(); |
| 188 | exit_manager.RegisterCallback(DeleteDBusControl, dbus_control); |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 189 | dbus_control->Init(); |
| 190 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 191 | shill::Daemon daemon(&config, dbus_control); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 192 | |
| 193 | if (cl->HasSwitch(switches::kDeviceBlackList)) { |
| 194 | vector<string> device_list; |
| 195 | base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList), |
| 196 | ',', &device_list); |
| 197 | |
| 198 | vector<string>::iterator i; |
| 199 | for (i = device_list.begin(); i != device_list.end(); ++i) { |
| 200 | daemon.AddDeviceToBlackList(*i); |
| 201 | } |
| 202 | } |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 203 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 204 | if (cl->HasSwitch(switches::kPortalList)) { |
| 205 | daemon.SetStartupPortalList(cl->GetSwitchValueASCII(switches::kPortalList)); |
| 206 | } |
| 207 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 208 | g_unix_signal_add(SIGINT, ExitSigHandler, &daemon); |
| 209 | g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon); |
| 210 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 211 | daemon.Run(); |
| 212 | |
| 213 | return 0; |
| 214 | } |