blob: 07dde744477e2a452f63fe39e0ecc93afdf6f102 [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>
mukesh agrawal8f317b62011-07-15 11:53:23 -07007#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -07008
Chris Masone0e1d1042011-05-09 18:07:03 -07009#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -070010#include <base/command_line.h>
11#include <base/file_path.h>
12#include <base/logging.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070013#include <base/string_number_conversions.h>
mukesh agrawal8f317b62011-07-15 11:53:23 -070014#include <base/string_split.h>
Chris Masoneee929b72011-05-10 10:02:18 -070015#include <chromeos/syslog_logging.h>
16
Paul Stewart75897df2011-04-27 09:05:53 -070017#include "shill/dbus_control.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070018#include "shill/dhcp_provider.h"
Darin Petkovf7897bc2011-06-08 17:13:36 -070019#include "shill/glib.h"
Darin Petkovc90fe522011-07-15 13:59:47 -070020#include "shill/proxy_factory.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070021#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070022#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023
24using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070025using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070026
Chris Masoneee929b72011-05-10 10:02:18 -070027namespace switches {
28
29// Don't daemon()ize; run in foreground.
30static const char kForeground[] = "foreground";
31// Directory to read confguration settings.
32static const char kConfigDir[] = "config-dir";
33// Directory to read default configuration settings (Read Only).
34static const char kDefaultConfigDir[] = "default-config-dir";
mukesh agrawal8f317b62011-07-15 11:53:23 -070035// Don't attempt to manage these devices.
36static const char kDeviceBlackList[] = "device-black-list";
Chris Masoneee929b72011-05-10 10:02:18 -070037// Flag that causes shill to show the help message and exit.
38static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070039// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
40static const char kLogLevel[] = "log-level";
Chris Masoneee929b72011-05-10 10:02:18 -070041
42// The help message shown if help flag is passed to the program.
43static const char kHelpMessage[] = "\n"
44 "Available Switches: \n"
45 " --foreground\n"
46 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070047 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070048 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070049 " --default-config-dir\n"
50 " Directory to read default configuration settings (Read Only).\n"
51 " --device-black-list=device1,device2\n"
52 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070053 " --log-level=N\n"
54 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
55 " --v=N\n"
56 " Enables VLOG(N) and below.\n"
57 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
58 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070059} // namespace switches
60
61// Always logs to the syslog and logs to stderr if
62// we are running in the foreground.
63void SetupLogging(bool foreground) {
64 int log_flags = 0;
65 log_flags |= chromeos::kLogToSyslog;
66 if (foreground) {
67 log_flags |= chromeos::kLogToStderr;
68 }
69 chromeos::InitLog(log_flags);
70}
71
72
73int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070074 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070075 CommandLine::Init(argc, argv);
76 CommandLine* cl = CommandLine::ForCurrentProcess();
77
78 // If the help flag is set, force log in foreground.
79 SetupLogging(cl->HasSwitch(switches::kForeground) ||
80 cl->HasSwitch(switches::kHelp));
81 if (cl->HasSwitch(switches::kHelp)) {
82 LOG(INFO) << switches::kHelpMessage;
83 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -070084 }
Chris Masone7ccc8192011-05-24 14:54:49 -070085 if (cl->HasSwitch(switches::kLogLevel)) {
86 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
87 int level = 0;
88 if (base::StringToInt(log_level, &level) &&
89 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
90 logging::SetMinLogLevel(level);
91 } else {
92 LOG(WARNING) << "Bad log level: " << log_level;
93 }
94 }
Paul Stewart75897df2011-04-27 09:05:53 -070095
Chris Masoneee929b72011-05-10 10:02:18 -070096 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
97 FilePath default_config_dir(
98 !cl->HasSwitch(switches::kDefaultConfigDir) ?
99 shill::Config::kShillDefaultPrefsDir :
100 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700101
Paul Stewart75897df2011-04-27 09:05:53 -0700102 shill::Config config; /* (config_dir, default_config_dir) */
103
Darin Petkovaceede32011-07-18 15:32:38 -0700104 // TODO(pstew): This should be chosen based on config
105 scoped_ptr<shill::DBusControl> dbus_control(new shill::DBusControl());
106 dbus_control->Init();
107
Darin Petkovc90fe522011-07-15 13:59:47 -0700108 shill::ProxyFactory proxy_factory;
Darin Petkovaceede32011-07-18 15:32:38 -0700109 proxy_factory.Init();
Darin Petkovc90fe522011-07-15 13:59:47 -0700110 shill::ProxyFactory::set_factory(&proxy_factory);
111
Darin Petkovf7897bc2011-06-08 17:13:36 -0700112 shill::GLib glib;
Darin Petkov887f2982011-07-14 16:10:17 -0700113 glib.TypeInit();
Paul Stewart75897df2011-04-27 09:05:53 -0700114
Darin Petkovaceede32011-07-18 15:32:38 -0700115 shill::DHCPProvider::GetInstance()->Init(&glib);
116
117 shill::Daemon daemon(&config, dbus_control.get(), &glib);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700118
119 if (cl->HasSwitch(switches::kDeviceBlackList)) {
120 vector<string> device_list;
121 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
122 ',', &device_list);
123
124 vector<string>::iterator i;
125 for (i = device_list.begin(); i != device_list.end(); ++i) {
126 daemon.AddDeviceToBlackList(*i);
127 }
128 }
Paul Stewart75897df2011-04-27 09:05:53 -0700129 daemon.Run();
130
131 return 0;
132}