blob: 58e24da47a2db18312c102781cc213c7d9a47f18 [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Chris Sosa5bac9492014-03-24 11:18:54 -070014
15#include <string>
16
Alex Vakulenkobc648492015-09-08 15:39:18 -070017#include <signal.h>
18
Christopher Wiley357deca2015-02-07 18:29:32 -080019#include <base/files/file_path.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070020#include <brillo/daemons/dbus_daemon.h>
21#include <brillo/dbus/async_event_sequencer.h>
22#include <brillo/dbus/exported_object_manager.h>
23#include <brillo/flag_helper.h>
24#include <brillo/strings/string_utils.h>
25#include <brillo/syslog_logging.h>
Chris Sosa5bac9492014-03-24 11:18:54 -070026
Vitaly Bukabecd4612015-08-16 23:31:55 -070027#include "buffet/buffet_config.h"
Alex Vakulenko94858962014-12-01 17:53:27 -080028#include "buffet/dbus_constants.h"
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070029#include "buffet/manager.h"
Chris Sosa5bac9492014-03-24 11:18:54 -070030
Alex Vakulenko41705852015-10-13 10:12:06 -070031using brillo::dbus_utils::AsyncEventSequencer;
32using brillo::DBusServiceDaemon;
Robert Gindacf92c662015-08-20 09:30:11 -070033using buffet::dbus_constants::kServiceName;
34using buffet::dbus_constants::kRootServicePath;
Christopher Wiley54028f92014-04-01 17:33:29 -070035
Alex Vakulenko79e6a282014-09-08 17:07:19 -070036namespace buffet {
Chris Sosa5bac9492014-03-24 11:18:54 -070037
Alex Vakulenko1e3a66b2015-05-22 15:48:53 -070038class Daemon final : public DBusServiceDaemon {
Alex Vakulenko79e6a282014-09-08 17:07:19 -070039 public:
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070040 explicit Daemon(const Manager::Options& options)
41 : DBusServiceDaemon(kServiceName, kRootServicePath), options_{options} {}
Chris Sosaea6456d2014-04-09 15:42:01 -070042
Alex Vakulenko79e6a282014-09-08 17:07:19 -070043 protected:
44 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070045 manager_.reset(new Manager(options_, object_manager_->AsWeakPtr()));
46 manager_->Start(sequencer);
Chris Sosa5bac9492014-03-24 11:18:54 -070047 }
Chris Sosa5bac9492014-03-24 11:18:54 -070048
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070049 void OnShutdown(int* return_code) override { manager_->Stop(); }
Chris Sosa5bac9492014-03-24 11:18:54 -070050
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070051 private:
Alex Vakulenko0022b752015-10-02 11:09:59 -070052 Manager::Options options_;
Vitaly Buka58a288b2015-07-31 00:33:31 -070053
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070054 std::unique_ptr<buffet::Manager> manager_;
Alex Vakulenko79e6a282014-09-08 17:07:19 -070055 DISALLOW_COPY_AND_ASSIGN(Daemon);
56};
Chris Sosa5bac9492014-03-24 11:18:54 -070057
Alex Vakulenko79e6a282014-09-08 17:07:19 -070058} // namespace buffet
Chris Sosa5bac9492014-03-24 11:18:54 -070059
Christopher Wiley357deca2015-02-07 18:29:32 -080060namespace {
61
Alex Vakulenko8e346362015-08-21 10:01:57 -070062const char kDefaultConfigFilePath[] = "/etc/weaved/weaved.conf";
Alex Vakulenkod42e09d2015-08-21 18:23:32 -070063const char kDefaultStateFilePath[] = "/data/misc/weaved/device_reg_info";
Christopher Wiley357deca2015-02-07 18:29:32 -080064
65} // namespace
66
Chris Sosa5bac9492014-03-24 11:18:54 -070067int main(int argc, char* argv[]) {
Vitaly Bukab12e9952015-03-13 23:54:21 -070068 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wiley357deca2015-02-07 18:29:32 -080069 DEFINE_string(config_path, kDefaultConfigFilePath,
70 "Path to file containing config information.");
71 DEFINE_string(state_path, kDefaultStateFilePath,
72 "Path to file containing state information.");
Christopher Wiley1d35ecc2015-04-07 11:11:18 -070073 DEFINE_bool(enable_xmpp, true,
74 "Connect to GCD via a persistent XMPP connection.");
Alex Vakulenko49c36ee2015-08-19 11:06:04 -070075 DEFINE_bool(disable_privet, false, "disable Privet protocol");
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070076 DEFINE_bool(enable_ping, false, "enable test HTTP handler at /privet/ping");
77 DEFINE_string(device_whitelist, "",
78 "Comma separated list of network interfaces to monitor for "
79 "connectivity (an empty list enables all interfaces).");
Vitaly Buka8b511e92015-07-18 20:43:58 -070080
81 DEFINE_bool(disable_security, false,
82 "disable Privet security for tests. For test only.");
83 DEFINE_string(test_privet_ssid, "",
84 "Fixed SSID for WiFi bootstrapping. For test only.");
85 DEFINE_string(test_definitions_path, "",
86 "Path to directory containing additional command "
87 "and state definitions. For test only.");
88
Alex Vakulenko41705852015-10-13 10:12:06 -070089 brillo::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wiley357deca2015-02-07 18:29:32 -080090 if (FLAGS_config_path.empty())
91 FLAGS_config_path = kDefaultConfigFilePath;
92 if (FLAGS_state_path.empty())
93 FLAGS_state_path = kDefaultStateFilePath;
Alex Vakulenko41705852015-10-13 10:12:06 -070094 int flags = brillo::kLogToSyslog | brillo::kLogHeader;
Vitaly Bukab12e9952015-03-13 23:54:21 -070095 if (FLAGS_log_to_stderr)
Alex Vakulenko41705852015-10-13 10:12:06 -070096 flags |= brillo::kLogToStderr;
97 brillo::InitLog(flags);
Vitaly Bukab12e9952015-03-13 23:54:21 -070098
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070099 auto device_whitelist =
Alex Vakulenko41705852015-10-13 10:12:06 -0700100 brillo::string_utils::Split(FLAGS_device_whitelist, ",", true, true);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700101
Alex Vakulenkobc648492015-09-08 15:39:18 -0700102 // We are handling write errors on closed sockets correctly and not relying on
103 // (nor handling) SIGPIPE signal, which just kills the process.
104 // Mark it to be ignored.
105 signal(SIGPIPE, SIG_IGN);
106
Alex Vakulenko0022b752015-10-02 11:09:59 -0700107 buffet::Manager::Options options;
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700108 options.xmpp_enabled = FLAGS_enable_xmpp;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700109 options.disable_privet = FLAGS_disable_privet;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700110 options.enable_ping = FLAGS_enable_ping;
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700111 options.device_whitelist = {device_whitelist.begin(), device_whitelist.end()};
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700112
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700113 options.config_options.defaults = base::FilePath{FLAGS_config_path};
114 options.config_options.settings = base::FilePath{FLAGS_state_path};
115 options.config_options.definitions = base::FilePath{"/etc/weaved"};
116 options.config_options.test_definitions =
117 base::FilePath{FLAGS_test_definitions_path};
118 options.config_options.disable_security = FLAGS_disable_security;
119 options.config_options.test_privet_ssid = FLAGS_test_privet_ssid;
120
121 buffet::Daemon daemon{options};
Alex Vakulenko79e6a282014-09-08 17:07:19 -0700122 return daemon.Run();
Chris Sosa5bac9492014-03-24 11:18:54 -0700123}