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 | |
| 5 | #include <time.h> |
Gaurav Shah | b893bed | 2011-11-28 19:37:16 -0800 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <string> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 9 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 10 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 11 | #include <base/at_exit.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 12 | #include <base/command_line.h> |
| 13 | #include <base/file_path.h> |
| 14 | #include <base/logging.h> |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 15 | #include <base/string_number_conversions.h> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 16 | #include <base/string_split.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 17 | #include <chromeos/syslog_logging.h> |
| 18 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 19 | extern "C" { |
| 20 | #include <glib-unix.h> |
| 21 | } |
| 22 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | #include "shill/dbus_control.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 | |
| 27 | using std::string; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 28 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 29 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 30 | namespace switches { |
| 31 | |
| 32 | // Don't daemon()ize; run in foreground. |
| 33 | static const char kForeground[] = "foreground"; |
| 34 | // Directory to read confguration settings. |
| 35 | static const char kConfigDir[] = "config-dir"; |
| 36 | // Directory to read default configuration settings (Read Only). |
| 37 | static const char kDefaultConfigDir[] = "default-config-dir"; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 38 | // Don't attempt to manage these devices. |
| 39 | static const char kDeviceBlackList[] = "device-black-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"; |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 44 | // LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR. |
| 45 | static const char kLogLevel[] = "log-level"; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 46 | // Use the same directories flimflam uses for global, user profiles.. |
| 47 | static const char kUseFlimflamProfiles[] = "use-flimflam-profiles"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 48 | |
| 49 | // The help message shown if help flag is passed to the program. |
| 50 | static const char kHelpMessage[] = "\n" |
| 51 | "Available Switches: \n" |
| 52 | " --foreground\n" |
| 53 | " Don\'t daemon()ize; run in foreground.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 54 | " --config-dir\n" |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 55 | " Directory to read confguration settings.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 56 | " --default-config-dir\n" |
| 57 | " Directory to read default configuration settings (Read Only).\n" |
| 58 | " --device-black-list=device1,device2\n" |
| 59 | " Do not manage devices named device1 or device2\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 60 | " --log-level=N\n" |
| 61 | " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n" |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 62 | " --push=profile1,profile2\n" |
| 63 | " Specify profiles to push on startup.\n" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 64 | " --use-flimflam-profiles\n" |
| 65 | " Use the same directories flimflam uses for global, user profiles.\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 66 | " --v=N\n" |
| 67 | " Enables VLOG(N) and below.\n" |
| 68 | " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n" |
| 69 | " Enable VLOG() at different levels in different files/modules.\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 70 | } // namespace switches |
| 71 | |
| 72 | // Always logs to the syslog and logs to stderr if |
| 73 | // we are running in the foreground. |
| 74 | void SetupLogging(bool foreground) { |
| 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); |
| 82 | } |
| 83 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 84 | void DeleteDBusControl(void* param) { |
| 85 | VLOG(2) << __func__; |
| 86 | shill::DBusControl* dbus_control = |
| 87 | reinterpret_cast<shill::DBusControl*>(param); |
| 88 | delete dbus_control; |
| 89 | } |
| 90 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 91 | gboolean ExitSigHandler(gpointer data) { |
| 92 | shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data); |
| 93 | daemon->Quit(); |
| 94 | return TRUE; |
| 95 | } |
| 96 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 97 | |
| 98 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 99 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 100 | CommandLine::Init(argc, argv); |
| 101 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 102 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 103 | if (cl->HasSwitch(switches::kHelp)) { |
| 104 | LOG(INFO) << switches::kHelpMessage; |
| 105 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 106 | } |
Paul Stewart | b716325 | 2011-12-09 16:21:30 -0800 | [diff] [blame] | 107 | |
| 108 | const int nochdir = 0, noclose = 0; |
| 109 | if (!cl->HasSwitch(switches::kForeground)) |
| 110 | PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize"; |
| 111 | |
| 112 | SetupLogging(cl->HasSwitch(switches::kForeground)); |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 113 | if (cl->HasSwitch(switches::kLogLevel)) { |
| 114 | std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
| 115 | int level = 0; |
| 116 | if (base::StringToInt(log_level, &level) && |
| 117 | level >= 0 && level < logging::LOG_NUM_SEVERITIES) { |
| 118 | logging::SetMinLogLevel(level); |
| 119 | } else { |
| 120 | LOG(WARNING) << "Bad log level: " << log_level; |
| 121 | } |
| 122 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 123 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 124 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 125 | FilePath default_config_dir( |
| 126 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 127 | shill::Config::kShillDefaultPrefsDir : |
| 128 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 129 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 130 | shill::Config config; /* (config_dir, default_config_dir) */ |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 131 | if (cl->HasSwitch(switches::kUseFlimflamProfiles)) |
| 132 | config.UseFlimflamStorageDirs(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 133 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 134 | // TODO(pstew): This should be chosen based on config |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 135 | // Make sure we delete the DBusControl object AFTER the LazyInstances |
| 136 | // since some LazyInstances destructors rely on D-Bus being around. |
| 137 | shill::DBusControl* dbus_control = new shill::DBusControl(); |
| 138 | exit_manager.RegisterCallback(DeleteDBusControl, dbus_control); |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 139 | dbus_control->Init(); |
| 140 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame] | 141 | shill::Daemon daemon(&config, dbus_control); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 142 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 143 | if (cl->HasSwitch(switches::kPushProfiles)) { |
| 144 | vector<string> profile_list; |
| 145 | base::SplitString(cl->GetSwitchValueASCII(switches::kPushProfiles), |
| 146 | ',', &profile_list); |
| 147 | daemon.SetStartupProfiles(profile_list); |
| 148 | } |
| 149 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 150 | if (cl->HasSwitch(switches::kDeviceBlackList)) { |
| 151 | vector<string> device_list; |
| 152 | base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList), |
| 153 | ',', &device_list); |
| 154 | |
| 155 | vector<string>::iterator i; |
| 156 | for (i = device_list.begin(); i != device_list.end(); ++i) { |
| 157 | daemon.AddDeviceToBlackList(*i); |
| 158 | } |
| 159 | } |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 160 | |
| 161 | g_unix_signal_add(SIGINT, ExitSigHandler, &daemon); |
| 162 | g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon); |
| 163 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 164 | daemon.Run(); |
| 165 | |
| 166 | return 0; |
| 167 | } |