Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 <string> |
| 6 | |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 7 | #include <base/files/file_path.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 8 | #include <chromeos/dbus/async_event_sequencer.h> |
| 9 | #include <chromeos/dbus/exported_object_manager.h> |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 10 | #include <chromeos/daemons/dbus_daemon.h> |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 11 | #include <chromeos/flag_helper.h> |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 12 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 13 | #include <chromeos/syslog_logging.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 15 | #include "buffet/dbus_constants.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 16 | #include "buffet/manager.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 17 | |
Christopher Wiley | 2d2d92b | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 18 | using chromeos::dbus_utils::AsyncEventSequencer; |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 19 | using chromeos::DBusServiceDaemon; |
| 20 | using buffet::dbus_constants::kServiceName; |
| 21 | using buffet::dbus_constants::kRootServicePath; |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 22 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 23 | namespace buffet { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 24 | |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 25 | class Daemon final : public DBusServiceDaemon { |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 26 | public: |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 27 | explicit Daemon(const buffet::Manager::Options& options) |
| 28 | : DBusServiceDaemon(kServiceName, kRootServicePath), options_{options} {} |
Chris Sosa | ababc5c | 2014-04-09 15:42:01 -0700 | [diff] [blame] | 29 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 30 | protected: |
| 31 | void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override { |
| 32 | manager_.reset(new buffet::Manager(object_manager_->AsWeakPtr())); |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 33 | manager_->Start(options_, sequencer); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 34 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 35 | |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 36 | void OnShutdown(int* return_code) override { manager_->Stop(); } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 37 | |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 38 | private: |
| 39 | buffet::Manager::Options options_; |
| 40 | std::unique_ptr<buffet::Manager> manager_; |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 41 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 42 | }; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 43 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 44 | } // namespace buffet |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 45 | |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | const char kDefaultConfigFilePath[] = "/etc/buffet/buffet.conf"; |
| 49 | const char kDefaultStateFilePath[] = "/var/lib/buffet/device_reg_info"; |
| 50 | |
| 51 | } // namespace |
| 52 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 53 | int main(int argc, char* argv[]) { |
Vitaly Buka | c5e9b4d | 2015-03-13 23:54:21 -0700 | [diff] [blame] | 54 | DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well"); |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 55 | DEFINE_string(config_path, kDefaultConfigFilePath, |
| 56 | "Path to file containing config information."); |
| 57 | DEFINE_string(state_path, kDefaultStateFilePath, |
| 58 | "Path to file containing state information."); |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 59 | DEFINE_string(test_definitions_path, "", |
| 60 | "Path to directory containing additional command " |
| 61 | "and state definitions. For use in test only."); |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 62 | DEFINE_bool(enable_xmpp, true, |
| 63 | "Connect to GCD via a persistent XMPP connection."); |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 64 | DEFINE_bool(disable_privet, false, "disable Privet protocol"); |
| 65 | DEFINE_bool(disable_security, false, "disable Privet security for tests"); |
| 66 | DEFINE_bool(enable_ping, false, "enable test HTTP handler at /privet/ping"); |
| 67 | DEFINE_string(device_whitelist, "", |
| 68 | "Comma separated list of network interfaces to monitor for " |
| 69 | "connectivity (an empty list enables all interfaces)."); |
Aaron Kemp | 292cc84 | 2015-02-11 08:00:06 -0800 | [diff] [blame] | 70 | chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon"); |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 71 | if (FLAGS_config_path.empty()) |
| 72 | FLAGS_config_path = kDefaultConfigFilePath; |
| 73 | if (FLAGS_state_path.empty()) |
| 74 | FLAGS_state_path = kDefaultStateFilePath; |
Vitaly Buka | c5e9b4d | 2015-03-13 23:54:21 -0700 | [diff] [blame] | 75 | int flags = chromeos::kLogToSyslog | chromeos::kLogHeader; |
| 76 | if (FLAGS_log_to_stderr) |
| 77 | flags |= chromeos::kLogToStderr; |
| 78 | chromeos::InitLog(flags); |
| 79 | |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 80 | auto device_whitelist = |
| 81 | chromeos::string_utils::Split(FLAGS_device_whitelist, ",", true, true); |
| 82 | |
| 83 | buffet::Manager::Options options; |
| 84 | options.config_path = base::FilePath{FLAGS_config_path}; |
| 85 | options.state_path = base::FilePath{FLAGS_state_path}; |
| 86 | options.test_definitions_path = base::FilePath{FLAGS_test_definitions_path}; |
| 87 | options.xmpp_enabled = FLAGS_enable_xmpp; |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 88 | options.device_whitelist.insert(device_whitelist.begin(), |
| 89 | device_whitelist.end()); |
Vitaly Buka | 3d2cd66 | 2015-06-16 16:23:03 -0700 | [diff] [blame] | 90 | options.privet.config_path = base::FilePath{FLAGS_config_path}; |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 91 | options.privet.disable_privet = FLAGS_disable_privet; |
| 92 | options.privet.disable_security = FLAGS_disable_security; |
| 93 | options.privet.enable_ping = FLAGS_enable_ping; |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 94 | |
| 95 | buffet::Daemon daemon{options}; |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 96 | return daemon.Run(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 97 | } |