blob: 71ea27184f3586567d78a92f846e05af4689b57a [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>
Gaurav Shahb893bed2011-11-28 19:37:16 -08006#include <unistd.h>
7
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <string>
mukesh agrawal8f317b62011-07-15 11:53:23 -07009#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070010
Chris Masone0e1d1042011-05-09 18:07:03 -070011#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -070012#include <base/command_line.h>
13#include <base/file_path.h>
14#include <base/logging.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070015#include <base/string_number_conversions.h>
mukesh agrawal8f317b62011-07-15 11:53:23 -070016#include <base/string_split.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017#include <chromeos/syslog_logging.h>
18
Thieu Le1271d682011-11-02 22:48:19 +000019extern "C" {
20#include <glib-unix.h>
21}
22
Paul Stewart75897df2011-04-27 09:05:53 -070023#include "shill/dbus_control.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070024#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070025#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070026
27using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070028using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070029
Chris Masoneee929b72011-05-10 10:02:18 -070030namespace switches {
31
32// Don't daemon()ize; run in foreground.
33static const char kForeground[] = "foreground";
34// Directory to read confguration settings.
35static const char kConfigDir[] = "config-dir";
36// Directory to read default configuration settings (Read Only).
37static const char kDefaultConfigDir[] = "default-config-dir";
mukesh agrawal8f317b62011-07-15 11:53:23 -070038// Don't attempt to manage these devices.
39static const char kDeviceBlackList[] = "device-black-list";
Chris Masoneee929b72011-05-10 10:02:18 -070040// Flag that causes shill to show the help message and exit.
41static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070042// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
43static const char kLogLevel[] = "log-level";
Chris Masone2ae797d2011-08-23 20:41:00 -070044// Use the same directories flimflam uses for global, user profiles..
45static const char kUseFlimflamProfiles[] = "use-flimflam-profiles";
Chris Masoneee929b72011-05-10 10:02:18 -070046
47// The help message shown if help flag is passed to the program.
48static const char kHelpMessage[] = "\n"
49 "Available Switches: \n"
50 " --foreground\n"
51 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070052 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070053 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070054 " --default-config-dir\n"
55 " Directory to read default configuration settings (Read Only).\n"
56 " --device-black-list=device1,device2\n"
57 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070058 " --log-level=N\n"
59 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
Chris Masone2ae797d2011-08-23 20:41:00 -070060 " --use-flimflam-profiles\n"
61 " Use the same directories flimflam uses for global, user profiles.\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070062 " --v=N\n"
63 " Enables VLOG(N) and below.\n"
64 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
65 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070066} // namespace switches
67
68// Always logs to the syslog and logs to stderr if
69// we are running in the foreground.
70void SetupLogging(bool foreground) {
71 int log_flags = 0;
72 log_flags |= chromeos::kLogToSyslog;
73 if (foreground) {
74 log_flags |= chromeos::kLogToStderr;
75 }
76 chromeos::InitLog(log_flags);
77}
78
Thieu Leb52d6ff2011-11-02 18:21:51 +000079void DeleteDBusControl(void* param) {
80 VLOG(2) << __func__;
81 shill::DBusControl* dbus_control =
82 reinterpret_cast<shill::DBusControl*>(param);
83 delete dbus_control;
84}
85
Thieu Le1271d682011-11-02 22:48:19 +000086gboolean ExitSigHandler(gpointer data) {
87 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
88 daemon->Quit();
89 return TRUE;
90}
91
Chris Masoneee929b72011-05-10 10:02:18 -070092
93int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070094 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070095 CommandLine::Init(argc, argv);
96 CommandLine* cl = CommandLine::ForCurrentProcess();
97
Gaurav Shahb893bed2011-11-28 19:37:16 -080098 const int nochdir = 0, noclose = 0;
99 if (!cl->HasSwitch(switches::kForeground))
100 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize";
101
Chris Masoneee929b72011-05-10 10:02:18 -0700102 // If the help flag is set, force log in foreground.
103 SetupLogging(cl->HasSwitch(switches::kForeground) ||
104 cl->HasSwitch(switches::kHelp));
105 if (cl->HasSwitch(switches::kHelp)) {
106 LOG(INFO) << switches::kHelpMessage;
107 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700108 }
Chris Masone7ccc8192011-05-24 14:54:49 -0700109 if (cl->HasSwitch(switches::kLogLevel)) {
110 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
111 int level = 0;
112 if (base::StringToInt(log_level, &level) &&
113 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
114 logging::SetMinLogLevel(level);
115 } else {
116 LOG(WARNING) << "Bad log level: " << log_level;
117 }
118 }
Paul Stewart75897df2011-04-27 09:05:53 -0700119
Chris Masoneee929b72011-05-10 10:02:18 -0700120 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
121 FilePath default_config_dir(
122 !cl->HasSwitch(switches::kDefaultConfigDir) ?
123 shill::Config::kShillDefaultPrefsDir :
124 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700125
Paul Stewart75897df2011-04-27 09:05:53 -0700126 shill::Config config; /* (config_dir, default_config_dir) */
Chris Masone2ae797d2011-08-23 20:41:00 -0700127 if (cl->HasSwitch(switches::kUseFlimflamProfiles))
128 config.UseFlimflamStorageDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700129
Darin Petkovaceede32011-07-18 15:32:38 -0700130 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000131 // Make sure we delete the DBusControl object AFTER the LazyInstances
132 // since some LazyInstances destructors rely on D-Bus being around.
133 shill::DBusControl* dbus_control = new shill::DBusControl();
134 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700135 dbus_control->Init();
136
Thieu Leb52d6ff2011-11-02 18:21:51 +0000137 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700138
139 if (cl->HasSwitch(switches::kDeviceBlackList)) {
140 vector<string> device_list;
141 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
142 ',', &device_list);
143
144 vector<string>::iterator i;
145 for (i = device_list.begin(); i != device_list.end(); ++i) {
146 daemon.AddDeviceToBlackList(*i);
147 }
148 }
Thieu Le1271d682011-11-02 22:48:19 +0000149
150 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
151 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
152
Paul Stewart75897df2011-04-27 09:05:53 -0700153 daemon.Run();
154
155 return 0;
156}