blob: 58379479068a1ba0af9b7cf2375c9ffd754ba75c [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
73
74int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070075 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070076 CommandLine::Init(argc, argv);
77 CommandLine* cl = CommandLine::ForCurrentProcess();
78
79 // If the help flag is set, force log in foreground.
80 SetupLogging(cl->HasSwitch(switches::kForeground) ||
81 cl->HasSwitch(switches::kHelp));
82 if (cl->HasSwitch(switches::kHelp)) {
83 LOG(INFO) << switches::kHelpMessage;
84 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -070085 }
Chris Masone7ccc8192011-05-24 14:54:49 -070086 if (cl->HasSwitch(switches::kLogLevel)) {
87 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
88 int level = 0;
89 if (base::StringToInt(log_level, &level) &&
90 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
91 logging::SetMinLogLevel(level);
92 } else {
93 LOG(WARNING) << "Bad log level: " << log_level;
94 }
95 }
Paul Stewart75897df2011-04-27 09:05:53 -070096
Chris Masoneee929b72011-05-10 10:02:18 -070097 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
98 FilePath default_config_dir(
99 !cl->HasSwitch(switches::kDefaultConfigDir) ?
100 shill::Config::kShillDefaultPrefsDir :
101 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700102
Paul Stewart75897df2011-04-27 09:05:53 -0700103 shill::Config config; /* (config_dir, default_config_dir) */
Chris Masone2ae797d2011-08-23 20:41:00 -0700104 if (cl->HasSwitch(switches::kUseFlimflamProfiles))
105 config.UseFlimflamStorageDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700106
Darin Petkovaceede32011-07-18 15:32:38 -0700107 // TODO(pstew): This should be chosen based on config
108 scoped_ptr<shill::DBusControl> dbus_control(new shill::DBusControl());
109 dbus_control->Init();
110
Darin Petkova7b89492011-07-27 12:48:17 -0700111 shill::Daemon daemon(&config, dbus_control.get());
mukesh agrawal8f317b62011-07-15 11:53:23 -0700112
113 if (cl->HasSwitch(switches::kDeviceBlackList)) {
114 vector<string> device_list;
115 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
116 ',', &device_list);
117
118 vector<string>::iterator i;
119 for (i = device_list.begin(); i != device_list.end(); ++i) {
120 daemon.AddDeviceToBlackList(*i);
121 }
122 }
Paul Stewart75897df2011-04-27 09:05:53 -0700123 daemon.Run();
124
125 return 0;
126}