blob: 0d855b8be54cb64419d4f1ed11ac31ee46f0f522 [file] [log] [blame]
mukesh agrawal43500062012-02-09 18:25:15 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan03ed8182012-04-19 20:39:27 -07005#include <glib-unix.h>
Paul Stewart75897df2011-04-27 09:05:53 -07006#include <time.h>
Gaurav Shahb893bed2011-11-28 19:37:16 -08007#include <unistd.h>
8
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <string>
mukesh agrawal8f317b62011-07-15 11:53:23 -070010#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070011
Chris Masone0e1d1042011-05-09 18:07:03 -070012#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -070013#include <base/command_line.h>
14#include <base/file_path.h>
15#include <base/logging.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070016#include <base/string_number_conversions.h>
mukesh agrawal8f317b62011-07-15 11:53:23 -070017#include <base/string_split.h>
Chris Masoneee929b72011-05-10 10:02:18 -070018#include <chromeos/syslog_logging.h>
19
Paul Stewart75897df2011-04-27 09:05:53 -070020#include "shill/dbus_control.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070021#include "shill/scope_logger.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";
Gaurav Shah71354762011-11-28 19:22:49 -080038// Flag to specify specific profiles to be pushed.
39static const char kPushProfiles[] = "push";
Chris Masoneee929b72011-05-10 10:02:18 -070040// Flag that causes shill to show the help message and exit.
41static const char kHelp[] = "help";
Ben Chan156e73f2012-04-20 12:02:10 -070042// Logging level:
43// 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),
44// -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.
Chris Masone7ccc8192011-05-24 14:54:49 -070045static const char kLogLevel[] = "log-level";
Ben Chan156e73f2012-04-20 12:02:10 -070046// Scopes to enable for SLOG()-based logging.
47static const char kLogScopes[] = "log-scopes";
Darin Petkov4f5e5492012-04-18 14:05:55 +020048// Use the same directories flimflam uses (profiles, run dir...)
49static const char kUseFlimflamDirs[] = "use-flimflam-dirs";
Chris Masoneee929b72011-05-10 10:02:18 -070050
51// The help message shown if help flag is passed to the program.
52static const char kHelpMessage[] = "\n"
53 "Available Switches: \n"
54 " --foreground\n"
55 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070056 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070057 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070058 " --default-config-dir\n"
59 " Directory to read default configuration settings (Read Only).\n"
60 " --device-black-list=device1,device2\n"
61 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070062 " --log-level=N\n"
Ben Chan156e73f2012-04-20 12:02:10 -070063 " Logging level:\n"
64 " 0 = LOG(INFO), 1 = LOG(WARNING), 2 = LOG(ERROR),\n"
65 " -1 = SLOG(..., 1), -2 = SLOG(..., 2), etc.\n"
66 " --log-scopes=\"*scope1+scope2\".\n"
67 " Scopes to enable for SLOG()-based logging.\n"
Gaurav Shah71354762011-11-28 19:22:49 -080068 " --push=profile1,profile2\n"
69 " Specify profiles to push on startup.\n"
Darin Petkov4f5e5492012-04-18 14:05:55 +020070 " --use-flimflam-dirs\n"
Ben Chan156e73f2012-04-20 12:02:10 -070071 " Use the same directories flimflam uses (profiles, run dir...).\n";
Chris Masoneee929b72011-05-10 10:02:18 -070072} // namespace switches
73
74// Always logs to the syslog and logs to stderr if
75// we are running in the foreground.
76void SetupLogging(bool foreground) {
77 int log_flags = 0;
78 log_flags |= chromeos::kLogToSyslog;
mukesh agrawal43500062012-02-09 18:25:15 -080079 log_flags |= chromeos::kLogHeader;
Chris Masoneee929b72011-05-10 10:02:18 -070080 if (foreground) {
81 log_flags |= chromeos::kLogToStderr;
82 }
83 chromeos::InitLog(log_flags);
84}
85
Thieu Leb52d6ff2011-11-02 18:21:51 +000086void DeleteDBusControl(void* param) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070087 SLOG(DBus, 2) << __func__;
Thieu Leb52d6ff2011-11-02 18:21:51 +000088 shill::DBusControl* dbus_control =
89 reinterpret_cast<shill::DBusControl*>(param);
90 delete dbus_control;
91}
92
Thieu Le1271d682011-11-02 22:48:19 +000093gboolean ExitSigHandler(gpointer data) {
94 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
95 daemon->Quit();
96 return TRUE;
97}
98
Chris Masoneee929b72011-05-10 10:02:18 -070099
100int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700101 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -0700102 CommandLine::Init(argc, argv);
103 CommandLine* cl = CommandLine::ForCurrentProcess();
104
Chris Masoneee929b72011-05-10 10:02:18 -0700105 if (cl->HasSwitch(switches::kHelp)) {
106 LOG(INFO) << switches::kHelpMessage;
107 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700108 }
Paul Stewartb7163252011-12-09 16:21:30 -0800109
110 const int nochdir = 0, noclose = 0;
111 if (!cl->HasSwitch(switches::kForeground))
112 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize";
113
114 SetupLogging(cl->HasSwitch(switches::kForeground));
Chris Masone7ccc8192011-05-24 14:54:49 -0700115 if (cl->HasSwitch(switches::kLogLevel)) {
116 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
117 int level = 0;
118 if (base::StringToInt(log_level, &level) &&
Ben Chan156e73f2012-04-20 12:02:10 -0700119 level < logging::LOG_NUM_SEVERITIES) {
Chris Masone7ccc8192011-05-24 14:54:49 -0700120 logging::SetMinLogLevel(level);
Ben Chan156e73f2012-04-20 12:02:10 -0700121 // Like VLOG, SLOG uses negative verbose level.
122 shill::ScopeLogger::GetInstance()->set_verbose_level(-level);
Chris Masone7ccc8192011-05-24 14:54:49 -0700123 } else {
124 LOG(WARNING) << "Bad log level: " << log_level;
125 }
126 }
Paul Stewart75897df2011-04-27 09:05:53 -0700127
Ben Chan156e73f2012-04-20 12:02:10 -0700128 if (cl->HasSwitch(switches::kLogScopes)) {
129 std::string log_scopes = cl->GetSwitchValueASCII(switches::kLogScopes);
130 shill::ScopeLogger::GetInstance()->EnableScopesByName(log_scopes);
131 }
132
Chris Masoneee929b72011-05-10 10:02:18 -0700133 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
134 FilePath default_config_dir(
135 !cl->HasSwitch(switches::kDefaultConfigDir) ?
136 shill::Config::kShillDefaultPrefsDir :
137 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700138
Paul Stewart75897df2011-04-27 09:05:53 -0700139 shill::Config config; /* (config_dir, default_config_dir) */
Darin Petkov4f5e5492012-04-18 14:05:55 +0200140 if (cl->HasSwitch(switches::kUseFlimflamDirs))
141 config.UseFlimflamDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700142
Darin Petkovaceede32011-07-18 15:32:38 -0700143 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000144 // Make sure we delete the DBusControl object AFTER the LazyInstances
145 // since some LazyInstances destructors rely on D-Bus being around.
146 shill::DBusControl* dbus_control = new shill::DBusControl();
147 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700148 dbus_control->Init();
149
Thieu Leb52d6ff2011-11-02 18:21:51 +0000150 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700151
Gaurav Shah71354762011-11-28 19:22:49 -0800152 if (cl->HasSwitch(switches::kPushProfiles)) {
153 vector<string> profile_list;
154 base::SplitString(cl->GetSwitchValueASCII(switches::kPushProfiles),
155 ',', &profile_list);
156 daemon.SetStartupProfiles(profile_list);
157 }
158
mukesh agrawal8f317b62011-07-15 11:53:23 -0700159 if (cl->HasSwitch(switches::kDeviceBlackList)) {
160 vector<string> device_list;
161 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
162 ',', &device_list);
163
164 vector<string>::iterator i;
165 for (i = device_list.begin(); i != device_list.end(); ++i) {
166 daemon.AddDeviceToBlackList(*i);
167 }
168 }
Thieu Le1271d682011-11-02 22:48:19 +0000169
170 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
171 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
172
Paul Stewart75897df2011-04-27 09:05:53 -0700173 daemon.Run();
174
175 return 0;
176}