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"; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 35 | // Don't attempt to manage these devices. |
| 36 | static const char kDeviceBlackList[] = "device-black-list"; |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 37 | // Technologies to enable for portal check at startup. |
| 38 | static const char kPortalList[] = "portal-list"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 39 | // Flag that causes shill to show the help message and exit. |
| 40 | static const char kHelp[] = "help"; |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 41 | // Logging level: |
| 42 | // 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR), |
| 43 | // -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc. |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 44 | static const char kLogLevel[] = "log-level"; |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 45 | // Scopes to enable for SLOG()-based logging. |
| 46 | static const char kLogScopes[] = "log-scopes"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 47 | |
| 48 | // The help message shown if help flag is passed to the program. |
| 49 | static const char kHelpMessage[] = "\n" |
| 50 | "Available Switches: \n" |
| 51 | " --foreground\n" |
| 52 | " Don\'t daemon()ize; run in foreground.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 53 | " --device-black-list=device1,device2\n" |
| 54 | " Do not manage devices named device1 or device2\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 55 | " --log-level=N\n" |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 56 | " 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 Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 61 | " --portal-list=technology1,technology2\n" |
Wade Guthrie | bb5d4a6 | 2013-06-07 09:41:51 -0700 | [diff] [blame] | 62 | " Specify technologies to perform portal detection on at startup.\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 63 | } // namespace switches |
| 64 | |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 65 | namespace { |
| 66 | |
| 67 | const char *kLoggerCommand = "/usr/bin/logger"; |
Jorge Lucangeli Obes | 8c1706f | 2012-08-30 15:30:48 -0700 | [diff] [blame] | 68 | const char *kLoggerUser = "syslog"; |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 69 | |
| 70 | } // namespace |
| 71 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 72 | // Always logs to the syslog and logs to stderr if |
| 73 | // we are running in the foreground. |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 74 | void SetupLogging(bool foreground, char *daemon_name) { |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 75 | int log_flags = 0; |
| 76 | log_flags |= chromeos::kLogToSyslog; |
mukesh agrawal | 4350006 | 2012-02-09 18:25:15 -0800 | [diff] [blame] | 77 | log_flags |= chromeos::kLogHeader; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 78 | if (foreground) { |
| 79 | log_flags |= chromeos::kLogToStderr; |
| 80 | } |
| 81 | chromeos::InitLog(log_flags); |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 82 | |
| 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 Obes | 8c1706f | 2012-08-30 15:30:48 -0700 | [diff] [blame] | 92 | |
| 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 agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 99 | 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 Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 114 | void DeleteDBusControl(void* param) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 115 | SLOG(DBus, 2) << __func__; |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 116 | shill::DBusControl* dbus_control = |
| 117 | reinterpret_cast<shill::DBusControl*>(param); |
| 118 | delete dbus_control; |
| 119 | } |
| 120 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 121 | gboolean ExitSigHandler(gpointer data) { |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 122 | LOG(INFO) << "Shutting down due to received signal."; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 123 | shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data); |
| 124 | daemon->Quit(); |
| 125 | return TRUE; |
| 126 | } |
| 127 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 128 | |
| 129 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 130 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 131 | CommandLine::Init(argc, argv); |
| 132 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 133 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 134 | if (cl->HasSwitch(switches::kHelp)) { |
| 135 | LOG(INFO) << switches::kHelpMessage; |
| 136 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 137 | } |
Paul Stewart | b716325 | 2011-12-09 16:21:30 -0800 | [diff] [blame] | 138 | |
| 139 | const int nochdir = 0, noclose = 0; |
| 140 | if (!cl->HasSwitch(switches::kForeground)) |
Wade Guthrie | bb5d4a6 | 2013-06-07 09:41:51 -0700 | [diff] [blame] | 141 | PLOG_IF(FATAL, daemon(nochdir, noclose) == -1) << "Failed to daemonize"; |
Paul Stewart | b716325 | 2011-12-09 16:21:30 -0800 | [diff] [blame] | 142 | |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 143 | SetupLogging(cl->HasSwitch(switches::kForeground), argv[0]); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 144 | if (cl->HasSwitch(switches::kLogLevel)) { |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 145 | string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 146 | int level = 0; |
| 147 | if (base::StringToInt(log_level, &level) && |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 148 | level < logging::LOG_NUM_SEVERITIES) { |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 149 | logging::SetMinLogLevel(level); |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 150 | // Like VLOG, SLOG uses negative verbose level. |
| 151 | shill::ScopeLogger::GetInstance()->set_verbose_level(-level); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 152 | } else { |
| 153 | LOG(WARNING) << "Bad log level: " << log_level; |
| 154 | } |
| 155 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 156 | |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 157 | if (cl->HasSwitch(switches::kLogScopes)) { |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 158 | string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes); |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 159 | shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes); |
| 160 | } |
| 161 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 162 | // TODO(pstew): This should be chosen based on config |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 163 | // 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 Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 167 | dbus_control->Init(); |
| 168 | |
Wade Guthrie | bb5d4a6 | 2013-06-07 09:41:51 -0700 | [diff] [blame] | 169 | shill::Config config; |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 170 | shill::Daemon daemon(&config, dbus_control); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 171 | |
| 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 Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 182 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 183 | if (cl->HasSwitch(switches::kPortalList)) { |
| 184 | daemon.SetStartupPortalList(cl->GetSwitchValueASCII(switches::kPortalList)); |
| 185 | } |
| 186 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 187 | g_unix_signal_add(SIGINT, ExitSigHandler, &daemon); |
| 188 | g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon); |
| 189 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 190 | daemon.Run(); |
| 191 | |
| 192 | return 0; |
| 193 | } |