Chris Sosa | 5bac949 | 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 | |
Alex Vakulenko | bc64849 | 2015-09-08 15:39:18 -0700 | [diff] [blame] | 7 | #include <signal.h> |
| 8 | |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 9 | #include <base/files/file_path.h> |
Vitaly Buka | cd478bb | 2015-08-16 01:29:30 -0700 | [diff] [blame] | 10 | #include <chromeos/daemons/dbus_daemon.h> |
Alex Vakulenko | 24e5f5d | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/async_event_sequencer.h> |
| 12 | #include <chromeos/dbus/exported_object_manager.h> |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 13 | #include <chromeos/flag_helper.h> |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 14 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 15 | #include <chromeos/syslog_logging.h> |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 17 | #include "buffet/buffet_config.h" |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 18 | #include "buffet/dbus_constants.h" |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 19 | #include "buffet/manager.h" |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 20 | |
Christopher Wiley | 68c07cc | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 21 | using chromeos::dbus_utils::AsyncEventSequencer; |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 22 | using chromeos::DBusServiceDaemon; |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 23 | using buffet::dbus_constants::kServiceName; |
| 24 | using buffet::dbus_constants::kRootServicePath; |
Christopher Wiley | 54028f9 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 25 | |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 26 | namespace buffet { |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 27 | |
Alex Vakulenko | 1e3a66b | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 28 | class Daemon final : public DBusServiceDaemon { |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 29 | public: |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 30 | Daemon(const Manager::Options& options, |
| 31 | const BuffetConfig::Options& config_options, |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 32 | const std::set<std::string>& device_whitelist) |
| 33 | : DBusServiceDaemon(kServiceName, kRootServicePath), |
| 34 | options_{options}, |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 35 | config_options_{config_options}, |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 36 | device_whitelist_{device_whitelist} {} |
Chris Sosa | ea6456d | 2014-04-09 15:42:01 -0700 | [diff] [blame] | 37 | |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 38 | protected: |
| 39 | void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override { |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 40 | manager_.reset(new Manager(object_manager_->AsWeakPtr())); |
| 41 | manager_->Start(options_, config_options_, device_whitelist_, sequencer); |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 42 | } |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 43 | |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 44 | void OnShutdown(int* return_code) override { manager_->Stop(); } |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 45 | |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 46 | private: |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 47 | Manager::Options options_; |
| 48 | BuffetConfig::Options config_options_; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 49 | std::set<std::string> device_whitelist_; |
| 50 | |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 51 | std::unique_ptr<buffet::Manager> manager_; |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 53 | }; |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 54 | |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 55 | } // namespace buffet |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 56 | |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 57 | namespace { |
| 58 | |
Alex Vakulenko | 8e34636 | 2015-08-21 10:01:57 -0700 | [diff] [blame] | 59 | const char kDefaultConfigFilePath[] = "/etc/weaved/weaved.conf"; |
Alex Vakulenko | d42e09d | 2015-08-21 18:23:32 -0700 | [diff] [blame] | 60 | const char kDefaultStateFilePath[] = "/data/misc/weaved/device_reg_info"; |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 61 | |
| 62 | } // namespace |
| 63 | |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 64 | int main(int argc, char* argv[]) { |
Vitaly Buka | b12e995 | 2015-03-13 23:54:21 -0700 | [diff] [blame] | 65 | DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well"); |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 66 | DEFINE_string(config_path, kDefaultConfigFilePath, |
| 67 | "Path to file containing config information."); |
| 68 | DEFINE_string(state_path, kDefaultStateFilePath, |
| 69 | "Path to file containing state information."); |
Christopher Wiley | 1d35ecc | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 70 | DEFINE_bool(enable_xmpp, true, |
| 71 | "Connect to GCD via a persistent XMPP connection."); |
Alex Vakulenko | 49c36ee | 2015-08-19 11:06:04 -0700 | [diff] [blame] | 72 | DEFINE_bool(disable_privet, false, "disable Privet protocol"); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 73 | DEFINE_bool(enable_ping, false, "enable test HTTP handler at /privet/ping"); |
| 74 | DEFINE_string(device_whitelist, "", |
| 75 | "Comma separated list of network interfaces to monitor for " |
| 76 | "connectivity (an empty list enables all interfaces)."); |
Vitaly Buka | 8b511e9 | 2015-07-18 20:43:58 -0700 | [diff] [blame] | 77 | |
| 78 | DEFINE_bool(disable_security, false, |
| 79 | "disable Privet security for tests. For test only."); |
| 80 | DEFINE_string(test_privet_ssid, "", |
| 81 | "Fixed SSID for WiFi bootstrapping. For test only."); |
| 82 | DEFINE_string(test_definitions_path, "", |
| 83 | "Path to directory containing additional command " |
| 84 | "and state definitions. For test only."); |
| 85 | |
Aaron Kemp | 4a4be07 | 2015-02-11 08:00:06 -0800 | [diff] [blame] | 86 | chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon"); |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 87 | if (FLAGS_config_path.empty()) |
| 88 | FLAGS_config_path = kDefaultConfigFilePath; |
| 89 | if (FLAGS_state_path.empty()) |
| 90 | FLAGS_state_path = kDefaultStateFilePath; |
Vitaly Buka | b12e995 | 2015-03-13 23:54:21 -0700 | [diff] [blame] | 91 | int flags = chromeos::kLogToSyslog | chromeos::kLogHeader; |
| 92 | if (FLAGS_log_to_stderr) |
| 93 | flags |= chromeos::kLogToStderr; |
| 94 | chromeos::InitLog(flags); |
| 95 | |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 96 | auto device_whitelist = |
| 97 | chromeos::string_utils::Split(FLAGS_device_whitelist, ",", true, true); |
| 98 | |
Alex Vakulenko | bc64849 | 2015-09-08 15:39:18 -0700 | [diff] [blame] | 99 | // We are handling write errors on closed sockets correctly and not relying on |
| 100 | // (nor handling) SIGPIPE signal, which just kills the process. |
| 101 | // Mark it to be ignored. |
| 102 | signal(SIGPIPE, SIG_IGN); |
| 103 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 104 | buffet::BuffetConfig::Options config_options; |
| 105 | config_options.defaults = base::FilePath{FLAGS_config_path}; |
| 106 | config_options.settings = base::FilePath{FLAGS_state_path}; |
| 107 | config_options.definitions = base::FilePath{"/etc/weaved"}; |
| 108 | config_options.test_definitions = base::FilePath{FLAGS_test_definitions_path}; |
| 109 | config_options.disable_security = FLAGS_disable_security; |
| 110 | config_options.test_privet_ssid = FLAGS_test_privet_ssid; |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 111 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 112 | buffet::Manager::Options options; |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 113 | options.xmpp_enabled = FLAGS_enable_xmpp; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 114 | options.disable_privet = FLAGS_disable_privet; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 115 | options.enable_ping = FLAGS_enable_ping; |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 116 | |
Vitaly Buka | 7b9ebee | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 117 | buffet::Daemon daemon{ |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame^] | 118 | options, config_options, |
Vitaly Buka | 7b9ebee | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 119 | std::set<std::string>{device_whitelist.begin(), device_whitelist.end()}}; |
Alex Vakulenko | 79e6a28 | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 120 | return daemon.Run(); |
Chris Sosa | 5bac949 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 121 | } |