blob: da9dc41c91d1548db7098e8beb97291ae74e1000 [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>
Paul Stewart75897df2011-04-27 09:05:53 -07007
Chris Masone0e1d1042011-05-09 18:07:03 -07008#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009#include <base/command_line.h>
10#include <base/file_path.h>
11#include <base/logging.h>
12#include <chromeos/syslog_logging.h>
13
Paul Stewart75897df2011-04-27 09:05:53 -070014#include "shill/dbus_control.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070015#include "shill/dhcp_provider.h"
16#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070017
18using std::string;
19
Chris Masoneee929b72011-05-10 10:02:18 -070020namespace switches {
21
22// Don't daemon()ize; run in foreground.
23static const char kForeground[] = "foreground";
24// Directory to read confguration settings.
25static const char kConfigDir[] = "config-dir";
26// Directory to read default configuration settings (Read Only).
27static const char kDefaultConfigDir[] = "default-config-dir";
28// Flag that causes shill to show the help message and exit.
29static const char kHelp[] = "help";
30
31// The help message shown if help flag is passed to the program.
32static const char kHelpMessage[] = "\n"
33 "Available Switches: \n"
34 " --foreground\n"
35 " Don\'t daemon()ize; run in foreground.\n"
36 " --config_dir\n"
37 " Directory to read confguration settings.\n"
38 " --default_config_dir\n"
39 " Directory to read default configuration settings (Read Only).";
40} // namespace switches
41
42// Always logs to the syslog and logs to stderr if
43// we are running in the foreground.
44void SetupLogging(bool foreground) {
45 int log_flags = 0;
46 log_flags |= chromeos::kLogToSyslog;
47 if (foreground) {
48 log_flags |= chromeos::kLogToStderr;
49 }
50 chromeos::InitLog(log_flags);
51}
52
53
54int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070055 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070056 CommandLine::Init(argc, argv);
57 CommandLine* cl = CommandLine::ForCurrentProcess();
58
59 // If the help flag is set, force log in foreground.
60 SetupLogging(cl->HasSwitch(switches::kForeground) ||
61 cl->HasSwitch(switches::kHelp));
62 if (cl->HasSwitch(switches::kHelp)) {
63 LOG(INFO) << switches::kHelpMessage;
64 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -070065 }
66
Chris Masoneee929b72011-05-10 10:02:18 -070067 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
68 FilePath default_config_dir(
69 !cl->HasSwitch(switches::kDefaultConfigDir) ?
70 shill::Config::kShillDefaultPrefsDir :
71 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -070072
Paul Stewart75897df2011-04-27 09:05:53 -070073 shill::Config config; /* (config_dir, default_config_dir) */
74
75 // TODO(pstew): This should be chosen based on config
Darin Petkovd1b715b2011-06-02 21:21:22 -070076 shill::DBusControl *dbus_control = new shill::DBusControl();
77 dbus_control->Init();
78 shill::DHCPProvider::GetInstance()->Init(dbus_control->connection());
Paul Stewart75897df2011-04-27 09:05:53 -070079
Darin Petkovd1b715b2011-06-02 21:21:22 -070080 shill::Daemon daemon(&config, dbus_control);
Paul Stewart75897df2011-04-27 09:05:53 -070081 daemon.Run();
82
83 return 0;
84}