blob: 769669d830e4b9d03d23573b53d645e94ef21966 [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 Petkov633ac6f2011-07-08 13:56:13 -070018#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070019#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070020
21using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070022using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070023
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";
mukesh agrawal8f317b62011-07-15 11:53:23 -070032// Don't attempt to manage these devices.
33static const char kDeviceBlackList[] = "device-black-list";
Chris Masoneee929b72011-05-10 10:02:18 -070034// Flag that causes shill to show the help message and exit.
35static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070036// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
37static const char kLogLevel[] = "log-level";
Chris Masoneee929b72011-05-10 10:02:18 -070038
39// The help message shown if help flag is passed to the program.
40static const char kHelpMessage[] = "\n"
41 "Available Switches: \n"
42 " --foreground\n"
43 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070044 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070045 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070046 " --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 Masone7ccc8192011-05-24 14:54:49 -070050 " --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 Masoneee929b72011-05-10 10:02:18 -070056} // namespace switches
57
58// Always logs to the syslog and logs to stderr if
59// we are running in the foreground.
60void 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
70int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070071 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070072 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 Stewart75897df2011-04-27 09:05:53 -070081 }
Chris Masone7ccc8192011-05-24 14:54:49 -070082 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 Stewart75897df2011-04-27 09:05:53 -070092
Chris Masoneee929b72011-05-10 10:02:18 -070093 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 Stewart75897df2011-04-27 09:05:53 -070098
Paul Stewart75897df2011-04-27 09:05:53 -070099 shill::Config config; /* (config_dir, default_config_dir) */
100
Darin Petkovaceede32011-07-18 15:32:38 -0700101 // 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 Petkova7b89492011-07-27 12:48:17 -0700105 shill::Daemon daemon(&config, dbus_control.get());
mukesh agrawal8f317b62011-07-15 11:53:23 -0700106
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 Stewart75897df2011-04-27 09:05:53 -0700117 daemon.Run();
118
119 return 0;
120}