blob: f4e06dc87b3db317a5b932ceb7774ec92612d2b8 [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"
Darin Petkov633ac6f2011-07-08 13:56:13 -070021#include "shill/shill_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070022#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023
24using std::string;
mukesh agrawal8f317b62011-07-15 11:53:23 -070025using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070026
Chris Masoneee929b72011-05-10 10:02:18 -070027namespace switches {
28
29// Don't daemon()ize; run in foreground.
30static const char kForeground[] = "foreground";
31// Directory to read confguration settings.
32static const char kConfigDir[] = "config-dir";
33// Directory to read default configuration settings (Read Only).
34static const char kDefaultConfigDir[] = "default-config-dir";
mukesh agrawal8f317b62011-07-15 11:53:23 -070035// Don't attempt to manage these devices.
36static const char kDeviceBlackList[] = "device-black-list";
Gaurav Shah71354762011-11-28 19:22:49 -080037// Flag to specify specific profiles to be pushed.
38static const char kPushProfiles[] = "push";
Chris Masoneee929b72011-05-10 10:02:18 -070039// Flag that causes shill to show the help message and exit.
40static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070041// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
42static const char kLogLevel[] = "log-level";
Darin Petkov4f5e5492012-04-18 14:05:55 +020043// Use the same directories flimflam uses (profiles, run dir...)
44static const char kUseFlimflamDirs[] = "use-flimflam-dirs";
Chris Masoneee929b72011-05-10 10:02:18 -070045
46// The help message shown if help flag is passed to the program.
47static const char kHelpMessage[] = "\n"
48 "Available Switches: \n"
49 " --foreground\n"
50 " Don\'t daemon()ize; run in foreground.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070051 " --config-dir\n"
Chris Masoneee929b72011-05-10 10:02:18 -070052 " Directory to read confguration settings.\n"
mukesh agrawal8f317b62011-07-15 11:53:23 -070053 " --default-config-dir\n"
54 " Directory to read default configuration settings (Read Only).\n"
55 " --device-black-list=device1,device2\n"
56 " Do not manage devices named device1 or device2\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070057 " --log-level=N\n"
58 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
Gaurav Shah71354762011-11-28 19:22:49 -080059 " --push=profile1,profile2\n"
60 " Specify profiles to push on startup.\n"
Darin Petkov4f5e5492012-04-18 14:05:55 +020061 " --use-flimflam-dirs\n"
62 " Use the same directories flimflam uses (profiles, run dir...).\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070063 " --v=N\n"
64 " Enables VLOG(N) and below.\n"
65 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
66 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070067} // namespace switches
68
69// Always logs to the syslog and logs to stderr if
70// we are running in the foreground.
71void SetupLogging(bool foreground) {
72 int log_flags = 0;
73 log_flags |= chromeos::kLogToSyslog;
mukesh agrawal43500062012-02-09 18:25:15 -080074 log_flags |= chromeos::kLogHeader;
Chris Masoneee929b72011-05-10 10:02:18 -070075 if (foreground) {
76 log_flags |= chromeos::kLogToStderr;
77 }
78 chromeos::InitLog(log_flags);
79}
80
Thieu Leb52d6ff2011-11-02 18:21:51 +000081void DeleteDBusControl(void* param) {
82 VLOG(2) << __func__;
83 shill::DBusControl* dbus_control =
84 reinterpret_cast<shill::DBusControl*>(param);
85 delete dbus_control;
86}
87
Thieu Le1271d682011-11-02 22:48:19 +000088gboolean ExitSigHandler(gpointer data) {
89 shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
90 daemon->Quit();
91 return TRUE;
92}
93
Chris Masoneee929b72011-05-10 10:02:18 -070094
95int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070096 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070097 CommandLine::Init(argc, argv);
98 CommandLine* cl = CommandLine::ForCurrentProcess();
99
Chris Masoneee929b72011-05-10 10:02:18 -0700100 if (cl->HasSwitch(switches::kHelp)) {
101 LOG(INFO) << switches::kHelpMessage;
102 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -0700103 }
Paul Stewartb7163252011-12-09 16:21:30 -0800104
105 const int nochdir = 0, noclose = 0;
106 if (!cl->HasSwitch(switches::kForeground))
107 PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize";
108
109 SetupLogging(cl->HasSwitch(switches::kForeground));
Chris Masone7ccc8192011-05-24 14:54:49 -0700110 if (cl->HasSwitch(switches::kLogLevel)) {
111 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
112 int level = 0;
113 if (base::StringToInt(log_level, &level) &&
114 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
115 logging::SetMinLogLevel(level);
116 } else {
117 LOG(WARNING) << "Bad log level: " << log_level;
118 }
119 }
Paul Stewart75897df2011-04-27 09:05:53 -0700120
Chris Masoneee929b72011-05-10 10:02:18 -0700121 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
122 FilePath default_config_dir(
123 !cl->HasSwitch(switches::kDefaultConfigDir) ?
124 shill::Config::kShillDefaultPrefsDir :
125 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -0700126
Paul Stewart75897df2011-04-27 09:05:53 -0700127 shill::Config config; /* (config_dir, default_config_dir) */
Darin Petkov4f5e5492012-04-18 14:05:55 +0200128 if (cl->HasSwitch(switches::kUseFlimflamDirs))
129 config.UseFlimflamDirs();
Paul Stewart75897df2011-04-27 09:05:53 -0700130
Darin Petkovaceede32011-07-18 15:32:38 -0700131 // TODO(pstew): This should be chosen based on config
Thieu Leb52d6ff2011-11-02 18:21:51 +0000132 // Make sure we delete the DBusControl object AFTER the LazyInstances
133 // since some LazyInstances destructors rely on D-Bus being around.
134 shill::DBusControl* dbus_control = new shill::DBusControl();
135 exit_manager.RegisterCallback(DeleteDBusControl, dbus_control);
Darin Petkovaceede32011-07-18 15:32:38 -0700136 dbus_control->Init();
137
Thieu Leb52d6ff2011-11-02 18:21:51 +0000138 shill::Daemon daemon(&config, dbus_control);
mukesh agrawal8f317b62011-07-15 11:53:23 -0700139
Gaurav Shah71354762011-11-28 19:22:49 -0800140 if (cl->HasSwitch(switches::kPushProfiles)) {
141 vector<string> profile_list;
142 base::SplitString(cl->GetSwitchValueASCII(switches::kPushProfiles),
143 ',', &profile_list);
144 daemon.SetStartupProfiles(profile_list);
145 }
146
mukesh agrawal8f317b62011-07-15 11:53:23 -0700147 if (cl->HasSwitch(switches::kDeviceBlackList)) {
148 vector<string> device_list;
149 base::SplitString(cl->GetSwitchValueASCII(switches::kDeviceBlackList),
150 ',', &device_list);
151
152 vector<string>::iterator i;
153 for (i = device_list.begin(); i != device_list.end(); ++i) {
154 daemon.AddDeviceToBlackList(*i);
155 }
156 }
Thieu Le1271d682011-11-02 22:48:19 +0000157
158 g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
159 g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
160
Paul Stewart75897df2011-04-27 09:05:53 -0700161 daemon.Run();
162
163 return 0;
164}