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> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 8 | #include <base/at_exit.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | #include <base/command_line.h> |
| 10 | #include <base/file_path.h> |
| 11 | #include <base/logging.h> |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 12 | #include <base/string_number_conversions.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 13 | #include <chromeos/syslog_logging.h> |
| 14 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 15 | #include "shill/dbus_control.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 16 | #include "shill/dhcp_provider.h" |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 17 | #include "shill/glib.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; |
| 22 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 23 | namespace switches { |
| 24 | |
| 25 | // Don't daemon()ize; run in foreground. |
| 26 | static const char kForeground[] = "foreground"; |
| 27 | // Directory to read confguration settings. |
| 28 | static const char kConfigDir[] = "config-dir"; |
| 29 | // Directory to read default configuration settings (Read Only). |
| 30 | static const char kDefaultConfigDir[] = "default-config-dir"; |
| 31 | // Flag that causes shill to show the help message and exit. |
| 32 | static const char kHelp[] = "help"; |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 33 | // LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR. |
| 34 | static const char kLogLevel[] = "log-level"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 35 | |
| 36 | // The help message shown if help flag is passed to the program. |
| 37 | static const char kHelpMessage[] = "\n" |
| 38 | "Available Switches: \n" |
| 39 | " --foreground\n" |
| 40 | " Don\'t daemon()ize; run in foreground.\n" |
| 41 | " --config_dir\n" |
| 42 | " Directory to read confguration settings.\n" |
| 43 | " --default_config_dir\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 44 | " Directory to read default configuration settings (Read Only)." |
| 45 | " --log-level=N\n" |
| 46 | " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n" |
| 47 | " --v=N\n" |
| 48 | " Enables VLOG(N) and below.\n" |
| 49 | " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n" |
| 50 | " Enable VLOG() at different levels in different files/modules.\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 51 | } // namespace switches |
| 52 | |
| 53 | // Always logs to the syslog and logs to stderr if |
| 54 | // we are running in the foreground. |
| 55 | void SetupLogging(bool foreground) { |
| 56 | int log_flags = 0; |
| 57 | log_flags |= chromeos::kLogToSyslog; |
| 58 | if (foreground) { |
| 59 | log_flags |= chromeos::kLogToStderr; |
| 60 | } |
| 61 | chromeos::InitLog(log_flags); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 66 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 67 | CommandLine::Init(argc, argv); |
| 68 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 69 | |
| 70 | // If the help flag is set, force log in foreground. |
| 71 | SetupLogging(cl->HasSwitch(switches::kForeground) || |
| 72 | cl->HasSwitch(switches::kHelp)); |
| 73 | if (cl->HasSwitch(switches::kHelp)) { |
| 74 | LOG(INFO) << switches::kHelpMessage; |
| 75 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 76 | } |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 77 | if (cl->HasSwitch(switches::kLogLevel)) { |
| 78 | std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
| 79 | int level = 0; |
| 80 | if (base::StringToInt(log_level, &level) && |
| 81 | level >= 0 && level < logging::LOG_NUM_SEVERITIES) { |
| 82 | logging::SetMinLogLevel(level); |
| 83 | } else { |
| 84 | LOG(WARNING) << "Bad log level: " << log_level; |
| 85 | } |
| 86 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 87 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 88 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 89 | FilePath default_config_dir( |
| 90 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 91 | shill::Config::kShillDefaultPrefsDir : |
| 92 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 93 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 94 | shill::Config config; /* (config_dir, default_config_dir) */ |
| 95 | |
| 96 | // TODO(pstew): This should be chosen based on config |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 97 | shill::DBusControl *dbus_control = new shill::DBusControl(); |
| 98 | dbus_control->Init(); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 99 | shill::GLib glib; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 100 | glib.TypeInit(); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 101 | shill::DHCPProvider::GetInstance()->Init(dbus_control->connection(), &glib); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 102 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 103 | shill::Daemon daemon(&config, dbus_control, &glib); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 104 | daemon.Run(); |
| 105 | |
| 106 | return 0; |
| 107 | } |