Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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> |
| 6 | #include <string> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 7 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 9 | #include <base/at_exit.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 10 | #include <base/command_line.h> |
| 11 | #include <base/file_path.h> |
| 12 | #include <base/logging.h> |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 13 | #include <base/string_number_conversions.h> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 14 | #include <base/string_split.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | #include <chromeos/syslog_logging.h> |
| 16 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 17 | #include "shill/dbus_control.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 18 | #include "shill/shill_config.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 19 | #include "shill/shill_daemon.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | |
| 21 | using std::string; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 22 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 24 | namespace switches { |
| 25 | |
| 26 | // Don't daemon()ize; run in foreground. |
| 27 | static const char kForeground[] = "foreground"; |
| 28 | // Directory to read confguration settings. |
| 29 | static const char kConfigDir[] = "config-dir"; |
| 30 | // Directory to read default configuration settings (Read Only). |
| 31 | static const char kDefaultConfigDir[] = "default-config-dir"; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 32 | // Don't attempt to manage these devices. |
| 33 | static const char kDeviceBlackList[] = "device-black-list"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 34 | // Flag that causes shill to show the help message and exit. |
| 35 | static const char kHelp[] = "help"; |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 36 | // LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR. |
| 37 | static const char kLogLevel[] = "log-level"; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 38 | // Use the same directories flimflam uses for global, user profiles.. |
| 39 | static const char kUseFlimflamProfiles[] = "use-flimflam-profiles"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 40 | |
| 41 | // The help message shown if help flag is passed to the program. |
| 42 | static const char kHelpMessage[] = "\n" |
| 43 | "Available Switches: \n" |
| 44 | " --foreground\n" |
| 45 | " Don\'t daemon()ize; run in foreground.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 46 | " --config-dir\n" |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 47 | " Directory to read confguration settings.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 48 | " --default-config-dir\n" |
| 49 | " Directory to read default configuration settings (Read Only).\n" |
| 50 | " --device-black-list=device1,device2\n" |
| 51 | " Do not manage devices named device1 or device2\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 52 | " --log-level=N\n" |
| 53 | " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 54 | " --use-flimflam-profiles\n" |
| 55 | " Use the same directories flimflam uses for global, user profiles.\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 56 | " --v=N\n" |
| 57 | " Enables VLOG(N) and below.\n" |
| 58 | " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n" |
| 59 | " Enable VLOG() at different levels in different files/modules.\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 60 | } // namespace switches |
| 61 | |
| 62 | // Always logs to the syslog and logs to stderr if |
| 63 | // we are running in the foreground. |
| 64 | void SetupLogging(bool foreground) { |
| 65 | int log_flags = 0; |
| 66 | log_flags |= chromeos::kLogToSyslog; |
| 67 | if (foreground) { |
| 68 | log_flags |= chromeos::kLogToStderr; |
| 69 | } |
| 70 | chromeos::InitLog(log_flags); |
| 71 | } |
| 72 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame^] | 73 | void DeleteDBusControl(void* param) { |
| 74 | VLOG(2) << __func__; |
| 75 | shill::DBusControl* dbus_control = |
| 76 | reinterpret_cast<shill::DBusControl*>(param); |
| 77 | delete dbus_control; |
| 78 | } |
| 79 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 80 | |
| 81 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 82 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 83 | CommandLine::Init(argc, argv); |
| 84 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 85 | |
| 86 | // If the help flag is set, force log in foreground. |
| 87 | SetupLogging(cl->HasSwitch(switches::kForeground) || |
| 88 | cl->HasSwitch(switches::kHelp)); |
| 89 | if (cl->HasSwitch(switches::kHelp)) { |
| 90 | LOG(INFO) << switches::kHelpMessage; |
| 91 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 92 | } |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 93 | if (cl->HasSwitch(switches::kLogLevel)) { |
| 94 | std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
| 95 | int level = 0; |
| 96 | if (base::StringToInt(log_level, &level) && |
| 97 | level >= 0 && level < logging::LOG_NUM_SEVERITIES) { |
| 98 | logging::SetMinLogLevel(level); |
| 99 | } else { |
| 100 | LOG(WARNING) << "Bad log level: " << log_level; |
| 101 | } |
| 102 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 103 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 104 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 105 | FilePath default_config_dir( |
| 106 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 107 | shill::Config::kShillDefaultPrefsDir : |
| 108 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 109 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 110 | shill::Config config; /* (config_dir, default_config_dir) */ |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 111 | if (cl->HasSwitch(switches::kUseFlimflamProfiles)) |
| 112 | config.UseFlimflamStorageDirs(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 113 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 114 | // TODO(pstew): This should be chosen based on config |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame^] | 115 | // Make sure we delete the DBusControl object AFTER the LazyInstances |
| 116 | // since some LazyInstances destructors rely on D-Bus being around. |
| 117 | shill::DBusControl* dbus_control = new shill::DBusControl(); |
| 118 | exit_manager.RegisterCallback(DeleteDBusControl, dbus_control); |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 119 | dbus_control->Init(); |
| 120 | |
Thieu Le | b52d6ff | 2011-11-02 18:21:51 +0000 | [diff] [blame^] | 121 | shill::Daemon daemon(&config, dbus_control); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 122 | |
| 123 | if (cl->HasSwitch(switches::kDeviceBlackList)) { |
| 124 | vector<string> device_list; |
| 125 | base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList), |
| 126 | ',', &device_list); |
| 127 | |
| 128 | vector<string>::iterator i; |
| 129 | for (i = device_list.begin(); i != device_list.end(); ++i) { |
| 130 | daemon.AddDeviceToBlackList(*i); |
| 131 | } |
| 132 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 133 | daemon.Run(); |
| 134 | |
| 135 | return 0; |
| 136 | } |