blob: c3af22bef1792b5144d24e8c26d8b904b58f9ff9 [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
Thieu Le1271d682011-11-02 22:48:19 +000017extern "C" {
18#include <glib-unix.h>
19}
20
Paul Stewart75897df2011-04-27 09:05:53 -070021#include "shill/dbus_control.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070022#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070023#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070024
25using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070026using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070027
Chris Masoneee929b72011-05-10 10:02:18 -070028namespace switches {
29
30// Don't daemon()ize; run in foreground.
31static const char kForeground[] = "foreground";
32// Directory to read confguration settings.
33static const char kConfigDir[] = "config-dir";
34// Directory to read default configuration settings (Read Only).
35static const char kDefaultConfigDir[] = "default-config-dir";
mukesh agrawal8f317b62011-07-15 11:53:23 -070036// Don't attempt to manage these devices.
37static const char kDeviceBlackList[] = "device-black-list";
Chris Masoneee929b72011-05-10 10:02:18 -070038// Flag that causes shill to show the help message and exit.
39static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070040// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
41static const char kLogLevel[] = "log-level";
Chris Masone2ae797d2011-08-23 20:41:00 -070042// Use the same directories flimflam uses for global, user profiles..
43static const char kUseFlimflamProfiles[] = "use-flimflam-profiles";
Chris Masoneee929b72011-05-10 10:02:18 -070044
45// The help message shown if help flag is passed to the program.
46static const char kHelpMessage[] = "\n"
47 "Available Switches: \n"
48 " --foreground\n"
49 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070050 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070051 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070052 " --default-config-dir\n"
53 " Directory to read default configuration settings (Read Only).\n"
54 " --device-black-list=device1,device2\n"
55 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070056 " --log-level=N\n"
57 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
Chris Masone2ae797d2011-08-23 20:41:00 -070058 " --use-flimflam-profiles\n"
59 " Use the same directories flimflam uses for global, user profiles.\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070060 " --v=N\n"
61 " Enables VLOG(N) and below.\n"
62 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
63 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070064} // namespace switches
65
66// Always logs to the syslog and logs to stderr if
67// we are running in the foreground.
68void SetupLogging(bool foreground) {
69 int log_flags = 0;
70 log_flags |= chromeos::kLogToSyslog;
71 if (foreground) {
72 log_flags |= chromeos::kLogToStderr;
73 }
74 chromeos::InitLog(log_flags);
75}
76
Thieu Leb52d6ff2011-11-02 18:21:51 +000077void DeleteDBusControl(void* param) {
78 VLOG(2) << __func__;
79 shill::DBusControl* dbus_control =
80 reinterpret_cast<shill::DBusControl*>(param);
81 delete dbus_control;
82}
83
Thieu Le1271d682011-11-02 22:48:19 +000084gboolean ExitSigHandler(gpointer data) {
85 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
86 daemon->Quit();
87 return TRUE;
88}
89
Chris Masoneee929b72011-05-10 10:02:18 -070090
91int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070092 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070093 CommandLine::Init(argc, argv);
94 CommandLine* cl = CommandLine::ForCurrentProcess();
95
96 // If the help flag is set, force log in foreground.
97 SetupLogging(cl->HasSwitch(switches::kForeground) ||
98 cl->HasSwitch(switches::kHelp));
99 if (cl->HasSwitch(switches::kHelp)) {
100 LOG(INFO) << switches::kHelpMessage;
101 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700102 }
Chris Masone7ccc8192011-05-24 14:54:49 -0700103 if (cl->HasSwitch(switches::kLogLevel)) {
104 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
105 int level = 0;
106 if (base::StringToInt(log_level, &level) &&
107 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
108 logging::SetMinLogLevel(level);
109 } else {
110 LOG(WARNING) << "Bad log level: " << log_level;
111 }
112 }
Paul Stewart75897df2011-04-27 09:05:53 -0700113
Chris Masoneee929b72011-05-10 10:02:18 -0700114 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
115 FilePath default_config_dir(
116 !cl->HasSwitch(switches::kDefaultConfigDir) ?
117 shill::Config::kShillDefaultPrefsDir :
118 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700119
Paul Stewart75897df2011-04-27 09:05:53 -0700120 shill::Config config; /* (config_dir, default_config_dir) */
Chris Masone2ae797d2011-08-23 20:41:00 -0700121 if (cl->HasSwitch(switches::kUseFlimflamProfiles))
122 config.UseFlimflamStorageDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700123
Darin Petkovaceede32011-07-18 15:32:38 -0700124 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000125 // Make sure we delete the DBusControl object AFTER the LazyInstances
126 // since some LazyInstances destructors rely on D-Bus being around.
127 shill::DBusControl* dbus_control = new shill::DBusControl();
128 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700129 dbus_control->Init();
130
Thieu Leb52d6ff2011-11-02 18:21:51 +0000131 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700132
133 if (cl->HasSwitch(switches::kDeviceBlackList)) {
134 vector<string> device_list;
135 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
136 ',', &device_list);
137
138 vector<string>::iterator i;
139 for (i = device_list.begin(); i != device_list.end(); ++i) {
140 daemon.AddDeviceToBlackList(*i);
141 }
142 }
Thieu Le1271d682011-11-02 22:48:19 +0000143
144 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
145 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
146
Paul Stewart75897df2011-04-27 09:05:53 -0700147 daemon.Run();
148
149 return 0;
150}