blob: c3153a15c37eec602283516613cd1bbb94b9c9cc [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// 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 Stewart75897df2011-04-27 09:05:53 -07007
Chris Masone0e1d1042011-05-09 18:07:03 -07008#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009#include <base/command_line.h>
10#include <base/file_path.h>
11#include <base/logging.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070012#include <base/string_number_conversions.h>
Chris Masoneee929b72011-05-10 10:02:18 -070013#include <chromeos/syslog_logging.h>
14
Paul Stewart75897df2011-04-27 09:05:53 -070015#include "shill/dbus_control.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070016#include "shill/dhcp_provider.h"
Darin Petkovf7897bc2011-06-08 17:13:36 -070017#include "shill/glib.h"
Darin Petkovc90fe522011-07-15 13:59:47 -070018#include "shill/proxy_factory.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070019#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070020#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070021
22using std::string;
23
Chris Masoneee929b72011-05-10 10:02:18 -070024namespace switches {
25
26// Don't daemon()ize; run in foreground.
27static const char kForeground[] = "foreground";
28// Directory to read confguration settings.
29static const char kConfigDir[] = "config-dir";
30// Directory to read default configuration settings (Read Only).
31static const char kDefaultConfigDir[] = "default-config-dir";
32// Flag that causes shill to show the help message and exit.
33static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070034// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
35static const char kLogLevel[] = "log-level";
Chris Masoneee929b72011-05-10 10:02:18 -070036
37// The help message shown if help flag is passed to the program.
38static const char kHelpMessage[] = "\n"
39 "Available Switches: \n"
40 " --foreground\n"
41 " Don\'t daemon()ize; run in foreground.\n"
42 " --config_dir\n"
43 " Directory to read confguration settings.\n"
44 " --default_config_dir\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070045 " Directory to read default configuration settings (Read Only)."
46 " --log-level=N\n"
47 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
48 " --v=N\n"
49 " Enables VLOG(N) and below.\n"
50 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
51 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070052} // namespace switches
53
54// Always logs to the syslog and logs to stderr if
55// we are running in the foreground.
56void SetupLogging(bool foreground) {
57 int log_flags = 0;
58 log_flags |= chromeos::kLogToSyslog;
59 if (foreground) {
60 log_flags |= chromeos::kLogToStderr;
61 }
62 chromeos::InitLog(log_flags);
63}
64
65
66int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070067 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070068 CommandLine::Init(argc, argv);
69 CommandLine* cl = CommandLine::ForCurrentProcess();
70
71 // If the help flag is set, force log in foreground.
72 SetupLogging(cl->HasSwitch(switches::kForeground) ||
73 cl->HasSwitch(switches::kHelp));
74 if (cl->HasSwitch(switches::kHelp)) {
75 LOG(INFO) << switches::kHelpMessage;
76 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -070077 }
Chris Masone7ccc8192011-05-24 14:54:49 -070078 if (cl->HasSwitch(switches::kLogLevel)) {
79 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
80 int level = 0;
81 if (base::StringToInt(log_level, &level) &&
82 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
83 logging::SetMinLogLevel(level);
84 } else {
85 LOG(WARNING) << "Bad log level: " << log_level;
86 }
87 }
Paul Stewart75897df2011-04-27 09:05:53 -070088
Chris Masoneee929b72011-05-10 10:02:18 -070089 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
90 FilePath default_config_dir(
91 !cl->HasSwitch(switches::kDefaultConfigDir) ?
92 shill::Config::kShillDefaultPrefsDir :
93 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -070094
Paul Stewart75897df2011-04-27 09:05:53 -070095 shill::Config config; /* (config_dir, default_config_dir) */
96
Darin Petkovaceede32011-07-18 15:32:38 -070097 // TODO(pstew): This should be chosen based on config
98 scoped_ptr<shill::DBusControl> dbus_control(new shill::DBusControl());
99 dbus_control->Init();
100
Darin Petkovc90fe522011-07-15 13:59:47 -0700101 shill::ProxyFactory proxy_factory;
Darin Petkovaceede32011-07-18 15:32:38 -0700102 proxy_factory.Init();
Darin Petkovc90fe522011-07-15 13:59:47 -0700103 shill::ProxyFactory::set_factory(&proxy_factory);
104
Darin Petkovf7897bc2011-06-08 17:13:36 -0700105 shill::GLib glib;
Darin Petkov887f2982011-07-14 16:10:17 -0700106 glib.TypeInit();
Paul Stewart75897df2011-04-27 09:05:53 -0700107
Darin Petkovaceede32011-07-18 15:32:38 -0700108 shill::DHCPProvider::GetInstance()->Init(&glib);
109
110 shill::Daemon daemon(&config, dbus_control.get(), &glib);
Paul Stewart75897df2011-04-27 09:05:53 -0700111 daemon.Run();
112
113 return 0;
114}