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> |
| 12 | #include <chromeos/syslog_logging.h> |
| 13 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | #include "shill/shill_daemon.h" |
| 15 | #include "shill/dbus_control.h" |
| 16 | |
| 17 | using std::string; |
| 18 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 19 | namespace switches { |
| 20 | |
| 21 | // Don't daemon()ize; run in foreground. |
| 22 | static const char kForeground[] = "foreground"; |
| 23 | // Directory to read confguration settings. |
| 24 | static const char kConfigDir[] = "config-dir"; |
| 25 | // Directory to read default configuration settings (Read Only). |
| 26 | static const char kDefaultConfigDir[] = "default-config-dir"; |
| 27 | // Flag that causes shill to show the help message and exit. |
| 28 | static const char kHelp[] = "help"; |
| 29 | |
| 30 | // The help message shown if help flag is passed to the program. |
| 31 | static const char kHelpMessage[] = "\n" |
| 32 | "Available Switches: \n" |
| 33 | " --foreground\n" |
| 34 | " Don\'t daemon()ize; run in foreground.\n" |
| 35 | " --config_dir\n" |
| 36 | " Directory to read confguration settings.\n" |
| 37 | " --default_config_dir\n" |
| 38 | " Directory to read default configuration settings (Read Only)."; |
| 39 | } // namespace switches |
| 40 | |
| 41 | // Always logs to the syslog and logs to stderr if |
| 42 | // we are running in the foreground. |
| 43 | void SetupLogging(bool foreground) { |
| 44 | int log_flags = 0; |
| 45 | log_flags |= chromeos::kLogToSyslog; |
| 46 | if (foreground) { |
| 47 | log_flags |= chromeos::kLogToStderr; |
| 48 | } |
| 49 | chromeos::InitLog(log_flags); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 54 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 55 | CommandLine::Init(argc, argv); |
| 56 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 57 | |
| 58 | // If the help flag is set, force log in foreground. |
| 59 | SetupLogging(cl->HasSwitch(switches::kForeground) || |
| 60 | cl->HasSwitch(switches::kHelp)); |
| 61 | if (cl->HasSwitch(switches::kHelp)) { |
| 62 | LOG(INFO) << switches::kHelpMessage; |
| 63 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 66 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 67 | FilePath default_config_dir( |
| 68 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 69 | shill::Config::kShillDefaultPrefsDir : |
| 70 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 71 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 72 | shill::Config config; /* (config_dir, default_config_dir) */ |
| 73 | |
| 74 | // TODO(pstew): This should be chosen based on config |
| 75 | shill::ControlInterface *control_interface = new shill::DBusControl(); |
| 76 | |
| 77 | shill::Daemon daemon(&config, control_interface); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 78 | daemon.Run(); |
| 79 | |
| 80 | return 0; |
| 81 | } |