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 | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 38 | |
| 39 | // The help message shown if help flag is passed to the program. |
| 40 | static const char kHelpMessage[] = "\n" |
| 41 | "Available Switches: \n" |
| 42 | " --foreground\n" |
| 43 | " Don\'t daemon()ize; run in foreground.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 44 | " --config-dir\n" |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 45 | " Directory to read confguration settings.\n" |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 46 | " --default-config-dir\n" |
| 47 | " Directory to read default configuration settings (Read Only).\n" |
| 48 | " --device-black-list=device1,device2\n" |
| 49 | " Do not manage devices named device1 or device2\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 50 | " --log-level=N\n" |
| 51 | " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n" |
| 52 | " --v=N\n" |
| 53 | " Enables VLOG(N) and below.\n" |
| 54 | " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n" |
| 55 | " Enable VLOG() at different levels in different files/modules.\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 56 | } // namespace switches |
| 57 | |
| 58 | // Always logs to the syslog and logs to stderr if |
| 59 | // we are running in the foreground. |
| 60 | void SetupLogging(bool foreground) { |
| 61 | int log_flags = 0; |
| 62 | log_flags |= chromeos::kLogToSyslog; |
| 63 | if (foreground) { |
| 64 | log_flags |= chromeos::kLogToStderr; |
| 65 | } |
| 66 | chromeos::InitLog(log_flags); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 71 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 72 | CommandLine::Init(argc, argv); |
| 73 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 74 | |
| 75 | // If the help flag is set, force log in foreground. |
| 76 | SetupLogging(cl->HasSwitch(switches::kForeground) || |
| 77 | cl->HasSwitch(switches::kHelp)); |
| 78 | if (cl->HasSwitch(switches::kHelp)) { |
| 79 | LOG(INFO) << switches::kHelpMessage; |
| 80 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 81 | } |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 82 | if (cl->HasSwitch(switches::kLogLevel)) { |
| 83 | std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
| 84 | int level = 0; |
| 85 | if (base::StringToInt(log_level, &level) && |
| 86 | level >= 0 && level < logging::LOG_NUM_SEVERITIES) { |
| 87 | logging::SetMinLogLevel(level); |
| 88 | } else { |
| 89 | LOG(WARNING) << "Bad log level: " << log_level; |
| 90 | } |
| 91 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 92 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 93 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 94 | FilePath default_config_dir( |
| 95 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 96 | shill::Config::kShillDefaultPrefsDir : |
| 97 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 98 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 99 | shill::Config config; /* (config_dir, default_config_dir) */ |
| 100 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 101 | // TODO(pstew): This should be chosen based on config |
| 102 | scoped_ptr<shill::DBusControl> dbus_control(new shill::DBusControl()); |
| 103 | dbus_control->Init(); |
| 104 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 105 | shill::Daemon daemon(&config, dbus_control.get()); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 106 | |
| 107 | if (cl->HasSwitch(switches::kDeviceBlackList)) { |
| 108 | vector<string> device_list; |
| 109 | base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList), |
| 110 | ',', &device_list); |
| 111 | |
| 112 | vector<string>::iterator i; |
| 113 | for (i = device_list.begin(); i != device_list.end(); ++i) { |
| 114 | daemon.AddDeviceToBlackList(*i); |
| 115 | } |
| 116 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 117 | daemon.Run(); |
| 118 | |
| 119 | return 0; |
| 120 | } |