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 | |
Ben Chan | 03ed818 | 2012-04-19 20:39:27 -0700 | [diff] [blame] | 5 | #include <glib-unix.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 6 | #include <time.h> |
Gaurav Shah | b893bed | 2011-11-28 19:37:16 -0800 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <string> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 10 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 11 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 12 | #include <base/at_exit.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 13 | #include <base/command_line.h> |
| 14 | #include <base/file_path.h> |
| 15 | #include <base/logging.h> |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 16 | #include <base/string_number_conversions.h> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 17 | #include <base/string_split.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 18 | #include <chromeos/syslog_logging.h> |
| 19 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | #include "shill/dbus_control.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 21 | #include "shill/scope_logger.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 22 | #include "shill/shill_config.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 23 | #include "shill/shill_daemon.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | |
| 25 | using std::string; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 26 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 27 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 28 | namespace switches { |
| 29 | |
| 30 | // Don't daemon()ize; run in foreground. |
| 31 | static const char kForeground[] = "foreground"; |
| 32 | // Directory to read confguration settings. |
| 33 | static const char kConfigDir[] = "config-dir"; |
| 34 | // Directory to read default configuration settings (Read Only). |
| 35 | static const char kDefaultConfigDir[] = "default-config-dir"; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 36 | // Don't attempt to manage these devices. |
| 37 | static const char kDeviceBlackList[] = "device-black-list"; |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 38 | // Technologies to enable for portal check at startup. |
| 39 | static const char kPortalList[] = "portal-list"; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 40 | // Flag to specify specific profiles to be pushed. |
| 41 | static const char kPushProfiles[] = "push"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 42 | // Flag that causes shill to show the help message and exit. |
| 43 | static const char kHelp[] = "help"; |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 44 | // Logging level: |
| 45 | // 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR), |
| 46 | // -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc. |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 47 | static const char kLogLevel[] = "log-level"; |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 48 | // Scopes to enable for SLOG()-based logging. |
| 49 | static const char kLogScopes[] = "log-scopes"; |
Darin Petkov | 4f5e549 | 2012-04-18 14:05:55 +0200 | [diff] [blame] | 50 | // Use the same directories flimflam uses (profiles, run dir...) |
| 51 | static const char kUseFlimflamDirs[] = "use-flimflam-dirs"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 52 | |
| 53 | // The help message shown if help flag is passed to the program. |
| 54 | static const char kHelpMessage[] = "\n" |
| 55 | "Available Switches: \n" |
| 56 | " --foreground\n" |
| 57 | " Don\'t daemon()ize; run in foreground.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 58 | " --config-dir\n" |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 59 | " Directory to read confguration settings.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 60 | " --default-config-dir\n" |
| 61 | " Directory to read default configuration settings (Read Only).\n" |
| 62 | " --device-black-list=device1,device2\n" |
| 63 | " Do not manage devices named device1 or device2\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 64 | " --log-level=N\n" |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 65 | " Logging level:\n" |
| 66 | " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n" |
| 67 | " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n" |
| 68 | " --log-scopes=\"*scope1+scope2\".\n" |
| 69 | " Scopes to enable for SLOG()-based logging.\n" |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 70 | " --portal-list=technology1,technology2\n" |
| 71 | " Specify technologies to perform portal detection on at startup.\n" |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 72 | " --push=profile1,profile2\n" |
| 73 | " Specify profiles to push on startup.\n" |
Darin Petkov | 4f5e549 | 2012-04-18 14:05:55 +0200 | [diff] [blame] | 74 | " --use-flimflam-dirs\n" |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 75 | " Use the same directories flimflam uses (profiles, run dir...).\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 76 | } // namespace switches |
| 77 | |
| 78 | // Always logs to the syslog and logs to stderr if |
| 79 | // we are running in the foreground. |
| 80 | void SetupLogging(bool foreground) { |
| 81 | int log_flags = 0; |
| 82 | log_flags |= chromeos::kLogToSyslog; |
mukesh agrawal | 4350006 | 2012-02-09 18:25:15 -0800 | [diff] [blame] | 83 | log_flags |= chromeos::kLogHeader; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 84 | if (foreground) { |
| 85 | log_flags |= chromeos::kLogToStderr; |
| 86 | } |
| 87 | chromeos::InitLog(log_flags); |
| 88 | } |
| 89 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 90 | void DeleteDBusControl(void* param) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 91 | SLOG(DBus, 2) << __func__; |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 92 | shill::DBusControl* dbus_control = |
| 93 | reinterpret_cast<shill::DBusControl*>(param); |
| 94 | delete dbus_control; |
| 95 | } |
| 96 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 97 | gboolean ExitSigHandler(gpointer data) { |
| 98 | shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data); |
| 99 | daemon->Quit(); |
| 100 | return TRUE; |
| 101 | } |
| 102 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 103 | |
| 104 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 105 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 106 | CommandLine::Init(argc, argv); |
| 107 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 108 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 109 | if (cl->HasSwitch(switches::kHelp)) { |
| 110 | LOG(INFO) << switches::kHelpMessage; |
| 111 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 112 | } |
Paul Stewart | b716325 | 2011-12-09 16:21:30 -0800 | [diff] [blame] | 113 | |
| 114 | const int nochdir = 0, noclose = 0; |
| 115 | if (!cl->HasSwitch(switches::kForeground)) |
| 116 | PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize"; |
| 117 | |
| 118 | SetupLogging(cl->HasSwitch(switches::kForeground)); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 119 | if (cl->HasSwitch(switches::kLogLevel)) { |
| 120 | std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
| 121 | int level = 0; |
| 122 | if (base::StringToInt(log_level, &level) && |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 123 | level < logging::LOG_NUM_SEVERITIES) { |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 124 | logging::SetMinLogLevel(level); |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 125 | // Like VLOG, SLOG uses negative verbose level. |
| 126 | shill::ScopeLogger::GetInstance()->set_verbose_level(-level); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 127 | } else { |
| 128 | LOG(WARNING) << "Bad log level: " << log_level; |
| 129 | } |
| 130 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 131 | |
Ben Chan | 156e73f | 2012-04-20 12:02:10 -0700 | [diff] [blame] | 132 | if (cl->HasSwitch(switches::kLogScopes)) { |
| 133 | std::string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes); |
| 134 | shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes); |
| 135 | } |
| 136 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 137 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 138 | FilePath default_config_dir( |
| 139 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 140 | shill::Config::kShillDefaultPrefsDir : |
| 141 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 142 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 143 | shill::Config config; /* (config_dir, default_config_dir) */ |
Darin Petkov | 4f5e549 | 2012-04-18 14:05:55 +0200 | [diff] [blame] | 144 | if (cl->HasSwitch(switches::kUseFlimflamDirs)) |
| 145 | config.UseFlimflamDirs(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 146 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 147 | // TODO(pstew): This should be chosen based on config |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 148 | // Make sure we delete the DBusControl object AFTER the LazyInstances |
| 149 | // since some LazyInstances destructors rely on D-Bus being around. |
| 150 | shill::DBusControl* dbus_control = new shill::DBusControl(); |
| 151 | exit_manager.RegisterCallback(DeleteDBusControl, dbus_control); |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 152 | dbus_control->Init(); |
| 153 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 154 | shill::Daemon daemon(&config, dbus_control); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 155 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 156 | if (cl->HasSwitch(switches::kPushProfiles)) { |
| 157 | vector<string> profile_list; |
| 158 | base::SplitString(cl->GetSwitchValueASCII(switches::kPushProfiles), |
| 159 | ',', &profile_list); |
| 160 | daemon.SetStartupProfiles(profile_list); |
| 161 | } |
| 162 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 163 | if (cl->HasSwitch(switches::kDeviceBlackList)) { |
| 164 | vector<string> device_list; |
| 165 | base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList), |
| 166 | ',', &device_list); |
| 167 | |
| 168 | vector<string>::iterator i; |
| 169 | for (i = device_list.begin(); i != device_list.end(); ++i) { |
| 170 | daemon.AddDeviceToBlackList(*i); |
| 171 | } |
| 172 | } |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 173 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 174 | if (cl->HasSwitch(switches::kPortalList)) { |
| 175 | daemon.SetStartupPortalList(cl->GetSwitchValueASCII(switches::kPortalList)); |
| 176 | } |
| 177 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 178 | g_unix_signal_add(SIGINT, ExitSigHandler, &daemon); |
| 179 | g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon); |
| 180 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 181 | daemon.Run(); |
| 182 | |
| 183 | return 0; |
| 184 | } |