blob: 2213466420b1e14b23690a9d107791d631dd1893 [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 Masone2ae797d2011-08-23 20:41:00 -070038// Use the same directories flimflam uses for global, user profiles..
39static const char kUseFlimflamProfiles[] = "use-flimflam-profiles";
Chris Masoneee929b72011-05-10 10:02:18 -070040
41// The help message shown if help flag is passed to the program.
42static const char kHelpMessage[] = "\n"
43 "Available Switches: \n"
44 " --foreground\n"
45 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070046 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070047 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070048 " --default-config-dir\n"
49 " Directory to read default configuration settings (Read Only).\n"
50 " --device-black-list=device1,device2\n"
51 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070052 " --log-level=N\n"
53 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
Chris Masone2ae797d2011-08-23 20:41:00 -070054 " --use-flimflam-profiles\n"
55 " Use the same directories flimflam uses for global, user profiles.\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070056 " --v=N\n"
57 " Enables VLOG(N) and below.\n"
58 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
59 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070060} // namespace switches
61
62// Always logs to the syslog and logs to stderr if
63// we are running in the foreground.
64void SetupLogging(bool foreground) {
65 int log_flags = 0;
66 log_flags |= chromeos::kLogToSyslog;
67 if (foreground) {
68 log_flags |= chromeos::kLogToStderr;
69 }
70 chromeos::InitLog(log_flags);
71}
72
Thieu Leb52d6ff2011-11-02 18:21:51 +000073void DeleteDBusControl(void* param) {
74 VLOG(2) << __func__;
75 shill::DBusControl* dbus_control =
76 reinterpret_cast<shill::DBusControl*>(param);
77 delete dbus_control;
78}
79
Chris Masoneee929b72011-05-10 10:02:18 -070080
81int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070082 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070083 CommandLine::Init(argc, argv);
84 CommandLine* cl = CommandLine::ForCurrentProcess();
85
86 // If the help flag is set, force log in foreground.
87 SetupLogging(cl->HasSwitch(switches::kForeground) ||
88 cl->HasSwitch(switches::kHelp));
89 if (cl->HasSwitch(switches::kHelp)) {
90 LOG(INFO) << switches::kHelpMessage;
91 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -070092 }
Chris Masone7ccc8192011-05-24 14:54:49 -070093 if (cl->HasSwitch(switches::kLogLevel)) {
94 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
95 int level = 0;
96 if (base::StringToInt(log_level, &level) &&
97 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
98 logging::SetMinLogLevel(level);
99 } else {
100 LOG(WARNING) << "Bad log level: " << log_level;
101 }
102 }
Paul Stewart75897df2011-04-27 09:05:53 -0700103
Chris Masoneee929b72011-05-10 10:02:18 -0700104 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
105 FilePath default_config_dir(
106 !cl->HasSwitch(switches::kDefaultConfigDir) ?
107 shill::Config::kShillDefaultPrefsDir :
108 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700109
Paul Stewart75897df2011-04-27 09:05:53 -0700110 shill::Config config; /* (config_dir, default_config_dir) */
Chris Masone2ae797d2011-08-23 20:41:00 -0700111 if (cl->HasSwitch(switches::kUseFlimflamProfiles))
112 config.UseFlimflamStorageDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700113
Darin Petkovaceede32011-07-18 15:32:38 -0700114 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000115 // Make sure we delete the DBusControl object AFTER the LazyInstances
116 // since some LazyInstances destructors rely on D-Bus being around.
117 shill::DBusControl* dbus_control = new shill::DBusControl();
118 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700119 dbus_control->Init();
120
Thieu Leb52d6ff2011-11-02 18:21:51 +0000121 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700122
123 if (cl->HasSwitch(switches::kDeviceBlackList)) {
124 vector<string> device_list;
125 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
126 ',', &device_list);
127
128 vector<string>::iterator i;
129 for (i = device_list.begin(); i != device_list.end(); ++i) {
130 daemon.AddDeviceToBlackList(*i);
131 }
132 }
Paul Stewart75897df2011-04-27 09:05:53 -0700133 daemon.Run();
134
135 return 0;
136}