blob: b447b747e2e6cac58efbc30a6ad6d586d7e6d958 [file] [log] [blame]
Chris Sosa5bac9492014-03-24 11:18:54 -07001// 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 Vakulenkobc648492015-09-08 15:39:18 -07007#include <signal.h>
8
Christopher Wiley357deca2015-02-07 18:29:32 -08009#include <base/files/file_path.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070010#include <brillo/daemons/dbus_daemon.h>
11#include <brillo/dbus/async_event_sequencer.h>
12#include <brillo/dbus/exported_object_manager.h>
13#include <brillo/flag_helper.h>
14#include <brillo/strings/string_utils.h>
15#include <brillo/syslog_logging.h>
Chris Sosa5bac9492014-03-24 11:18:54 -070016
Vitaly Bukabecd4612015-08-16 23:31:55 -070017#include "buffet/buffet_config.h"
Alex Vakulenko94858962014-12-01 17:53:27 -080018#include "buffet/dbus_constants.h"
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070019#include "buffet/manager.h"
Chris Sosa5bac9492014-03-24 11:18:54 -070020
Alex Vakulenko41705852015-10-13 10:12:06 -070021using brillo::dbus_utils::AsyncEventSequencer;
22using brillo::DBusServiceDaemon;
Robert Gindacf92c662015-08-20 09:30:11 -070023using buffet::dbus_constants::kServiceName;
24using buffet::dbus_constants::kRootServicePath;
Christopher Wiley54028f92014-04-01 17:33:29 -070025
Alex Vakulenko79e6a282014-09-08 17:07:19 -070026namespace buffet {
Chris Sosa5bac9492014-03-24 11:18:54 -070027
Alex Vakulenko1e3a66b2015-05-22 15:48:53 -070028class Daemon final : public DBusServiceDaemon {
Alex Vakulenko79e6a282014-09-08 17:07:19 -070029 public:
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070030 explicit Daemon(const Manager::Options& options)
31 : DBusServiceDaemon(kServiceName, kRootServicePath), options_{options} {}
Chris Sosaea6456d2014-04-09 15:42:01 -070032
Alex Vakulenko79e6a282014-09-08 17:07:19 -070033 protected:
34 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070035 manager_.reset(new Manager(options_, object_manager_->AsWeakPtr()));
36 manager_->Start(sequencer);
Chris Sosa5bac9492014-03-24 11:18:54 -070037 }
Chris Sosa5bac9492014-03-24 11:18:54 -070038
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070039 void OnShutdown(int* return_code) override { manager_->Stop(); }
Chris Sosa5bac9492014-03-24 11:18:54 -070040
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070041 private:
Alex Vakulenko0022b752015-10-02 11:09:59 -070042 Manager::Options options_;
Vitaly Buka58a288b2015-07-31 00:33:31 -070043
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070044 std::unique_ptr<buffet::Manager> manager_;
Alex Vakulenko79e6a282014-09-08 17:07:19 -070045 DISALLOW_COPY_AND_ASSIGN(Daemon);
46};
Chris Sosa5bac9492014-03-24 11:18:54 -070047
Alex Vakulenko79e6a282014-09-08 17:07:19 -070048} // namespace buffet
Chris Sosa5bac9492014-03-24 11:18:54 -070049
Christopher Wiley357deca2015-02-07 18:29:32 -080050namespace {
51
Alex Vakulenko8e346362015-08-21 10:01:57 -070052const char kDefaultConfigFilePath[] = "/etc/weaved/weaved.conf";
Alex Vakulenkod42e09d2015-08-21 18:23:32 -070053const char kDefaultStateFilePath[] = "/data/misc/weaved/device_reg_info";
Christopher Wiley357deca2015-02-07 18:29:32 -080054
55} // namespace
56
Chris Sosa5bac9492014-03-24 11:18:54 -070057int main(int argc, char* argv[]) {
Vitaly Bukab12e9952015-03-13 23:54:21 -070058 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wiley357deca2015-02-07 18:29:32 -080059 DEFINE_string(config_path, kDefaultConfigFilePath,
60 "Path to file containing config information.");
61 DEFINE_string(state_path, kDefaultStateFilePath,
62 "Path to file containing state information.");
Christopher Wiley1d35ecc2015-04-07 11:11:18 -070063 DEFINE_bool(enable_xmpp, true,
64 "Connect to GCD via a persistent XMPP connection.");
Alex Vakulenko49c36ee2015-08-19 11:06:04 -070065 DEFINE_bool(disable_privet, false, "disable Privet protocol");
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070066 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).");
Vitaly Buka8b511e92015-07-18 20:43:58 -070070
71 DEFINE_bool(disable_security, false,
72 "disable Privet security for tests. For test only.");
73 DEFINE_string(test_privet_ssid, "",
74 "Fixed SSID for WiFi bootstrapping. For test only.");
75 DEFINE_string(test_definitions_path, "",
76 "Path to directory containing additional command "
77 "and state definitions. For test only.");
78
Alex Vakulenko41705852015-10-13 10:12:06 -070079 brillo::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wiley357deca2015-02-07 18:29:32 -080080 if (FLAGS_config_path.empty())
81 FLAGS_config_path = kDefaultConfigFilePath;
82 if (FLAGS_state_path.empty())
83 FLAGS_state_path = kDefaultStateFilePath;
Alex Vakulenko41705852015-10-13 10:12:06 -070084 int flags = brillo::kLogToSyslog | brillo::kLogHeader;
Vitaly Bukab12e9952015-03-13 23:54:21 -070085 if (FLAGS_log_to_stderr)
Alex Vakulenko41705852015-10-13 10:12:06 -070086 flags |= brillo::kLogToStderr;
87 brillo::InitLog(flags);
Vitaly Bukab12e9952015-03-13 23:54:21 -070088
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070089 auto device_whitelist =
Alex Vakulenko41705852015-10-13 10:12:06 -070090 brillo::string_utils::Split(FLAGS_device_whitelist, ",", true, true);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070091
Alex Vakulenkobc648492015-09-08 15:39:18 -070092 // We are handling write errors on closed sockets correctly and not relying on
93 // (nor handling) SIGPIPE signal, which just kills the process.
94 // Mark it to be ignored.
95 signal(SIGPIPE, SIG_IGN);
96
Alex Vakulenko0022b752015-10-02 11:09:59 -070097 buffet::Manager::Options options;
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070098 options.xmpp_enabled = FLAGS_enable_xmpp;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070099 options.disable_privet = FLAGS_disable_privet;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700100 options.enable_ping = FLAGS_enable_ping;
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700101 options.device_whitelist = {device_whitelist.begin(), device_whitelist.end()};
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700102
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700103 options.config_options.defaults = base::FilePath{FLAGS_config_path};
104 options.config_options.settings = base::FilePath{FLAGS_state_path};
105 options.config_options.definitions = base::FilePath{"/etc/weaved"};
106 options.config_options.test_definitions =
107 base::FilePath{FLAGS_test_definitions_path};
108 options.config_options.disable_security = FLAGS_disable_security;
109 options.config_options.test_privet_ssid = FLAGS_test_privet_ssid;
110
111 buffet::Daemon daemon{options};
Alex Vakulenko79e6a282014-09-08 17:07:19 -0700112 return daemon.Run();
Chris Sosa5bac9492014-03-24 11:18:54 -0700113}