blob: 04a513416af638acc0c52d1981253dd272695b08 [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>
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080018#include <sysexits.h>
Alex Vakulenkobc648492015-09-08 15:39:18 -070019
Christopher Wiley357deca2015-02-07 18:29:32 -080020#include <base/files/file_path.h>
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080021#include <binderwrapper/binder_wrapper.h>
22#include <brillo/binder_watcher.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070023#include <brillo/daemons/dbus_daemon.h>
24#include <brillo/dbus/async_event_sequencer.h>
25#include <brillo/dbus/exported_object_manager.h>
26#include <brillo/flag_helper.h>
27#include <brillo/strings/string_utils.h>
28#include <brillo/syslog_logging.h>
Chris Sosa5bac9492014-03-24 11:18:54 -070029
Vitaly Bukabecd4612015-08-16 23:31:55 -070030#include "buffet/buffet_config.h"
Alex Vakulenko94858962014-12-01 17:53:27 -080031#include "buffet/dbus_constants.h"
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070032#include "buffet/manager.h"
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080033#include "common/binder_constants.h"
Chris Sosa5bac9492014-03-24 11:18:54 -070034
Alex Vakulenko41705852015-10-13 10:12:06 -070035using brillo::dbus_utils::AsyncEventSequencer;
36using brillo::DBusServiceDaemon;
Robert Gindacf92c662015-08-20 09:30:11 -070037using buffet::dbus_constants::kServiceName;
38using buffet::dbus_constants::kRootServicePath;
Christopher Wiley54028f92014-04-01 17:33:29 -070039
Alex Vakulenko79e6a282014-09-08 17:07:19 -070040namespace buffet {
Chris Sosa5bac9492014-03-24 11:18:54 -070041
Alex Vakulenko1e3a66b2015-05-22 15:48:53 -070042class Daemon final : public DBusServiceDaemon {
Alex Vakulenko79e6a282014-09-08 17:07:19 -070043 public:
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070044 explicit Daemon(const Manager::Options& options)
45 : DBusServiceDaemon(kServiceName, kRootServicePath), options_{options} {}
Chris Sosaea6456d2014-04-09 15:42:01 -070046
Alex Vakulenko79e6a282014-09-08 17:07:19 -070047 protected:
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080048 int OnInit() override {
49 android::BinderWrapper::Create();
50 if (!binder_watcher_.Init())
51 return EX_OSERR;
52
53 return brillo::DBusServiceDaemon::OnInit();
54 }
55
Alex Vakulenko79e6a282014-09-08 17:07:19 -070056 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080057 manager_ = new Manager{options_, bus_};
58 android::BinderWrapper::Get()->RegisterService(
59 weaved::binder::kWeaveServiceName,
60 android::IInterface::asBinder(manager_));
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070061 manager_->Start(sequencer);
Chris Sosa5bac9492014-03-24 11:18:54 -070062 }
Chris Sosa5bac9492014-03-24 11:18:54 -070063
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070064 void OnShutdown(int* return_code) override { manager_->Stop(); }
Chris Sosa5bac9492014-03-24 11:18:54 -070065
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070066 private:
Alex Vakulenko0022b752015-10-02 11:09:59 -070067 Manager::Options options_;
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080068 brillo::BinderWatcher binder_watcher_;
69 android::sp<buffet::Manager> manager_;
Vitaly Buka58a288b2015-07-31 00:33:31 -070070
Alex Vakulenko79e6a282014-09-08 17:07:19 -070071 DISALLOW_COPY_AND_ASSIGN(Daemon);
72};
Chris Sosa5bac9492014-03-24 11:18:54 -070073
Alex Vakulenko79e6a282014-09-08 17:07:19 -070074} // namespace buffet
Chris Sosa5bac9492014-03-24 11:18:54 -070075
Christopher Wiley357deca2015-02-07 18:29:32 -080076namespace {
77
Alex Vakulenko8e346362015-08-21 10:01:57 -070078const char kDefaultConfigFilePath[] = "/etc/weaved/weaved.conf";
Alex Vakulenkod42e09d2015-08-21 18:23:32 -070079const char kDefaultStateFilePath[] = "/data/misc/weaved/device_reg_info";
Christopher Wiley357deca2015-02-07 18:29:32 -080080
81} // namespace
82
Chris Sosa5bac9492014-03-24 11:18:54 -070083int main(int argc, char* argv[]) {
Vitaly Bukab12e9952015-03-13 23:54:21 -070084 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wiley357deca2015-02-07 18:29:32 -080085 DEFINE_string(config_path, kDefaultConfigFilePath,
86 "Path to file containing config information.");
87 DEFINE_string(state_path, kDefaultStateFilePath,
88 "Path to file containing state information.");
Christopher Wiley1d35ecc2015-04-07 11:11:18 -070089 DEFINE_bool(enable_xmpp, true,
90 "Connect to GCD via a persistent XMPP connection.");
Alex Vakulenko49c36ee2015-08-19 11:06:04 -070091 DEFINE_bool(disable_privet, false, "disable Privet protocol");
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070092 DEFINE_bool(enable_ping, false, "enable test HTTP handler at /privet/ping");
93 DEFINE_string(device_whitelist, "",
94 "Comma separated list of network interfaces to monitor for "
95 "connectivity (an empty list enables all interfaces).");
Vitaly Buka8b511e92015-07-18 20:43:58 -070096
97 DEFINE_bool(disable_security, false,
98 "disable Privet security for tests. For test only.");
99 DEFINE_string(test_privet_ssid, "",
100 "Fixed SSID for WiFi bootstrapping. For test only.");
101 DEFINE_string(test_definitions_path, "",
102 "Path to directory containing additional command "
103 "and state definitions. For test only.");
104
Alex Vakulenko41705852015-10-13 10:12:06 -0700105 brillo::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wiley357deca2015-02-07 18:29:32 -0800106 if (FLAGS_config_path.empty())
107 FLAGS_config_path = kDefaultConfigFilePath;
108 if (FLAGS_state_path.empty())
109 FLAGS_state_path = kDefaultStateFilePath;
Alex Vakulenko41705852015-10-13 10:12:06 -0700110 int flags = brillo::kLogToSyslog | brillo::kLogHeader;
Vitaly Bukab12e9952015-03-13 23:54:21 -0700111 if (FLAGS_log_to_stderr)
Alex Vakulenko41705852015-10-13 10:12:06 -0700112 flags |= brillo::kLogToStderr;
113 brillo::InitLog(flags);
Vitaly Bukab12e9952015-03-13 23:54:21 -0700114
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700115 auto device_whitelist =
Alex Vakulenko41705852015-10-13 10:12:06 -0700116 brillo::string_utils::Split(FLAGS_device_whitelist, ",", true, true);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700117
Alex Vakulenkobc648492015-09-08 15:39:18 -0700118 // We are handling write errors on closed sockets correctly and not relying on
119 // (nor handling) SIGPIPE signal, which just kills the process.
120 // Mark it to be ignored.
121 signal(SIGPIPE, SIG_IGN);
122
Alex Vakulenko0022b752015-10-02 11:09:59 -0700123 buffet::Manager::Options options;
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700124 options.xmpp_enabled = FLAGS_enable_xmpp;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700125 options.disable_privet = FLAGS_disable_privet;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700126 options.enable_ping = FLAGS_enable_ping;
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700127 options.device_whitelist = {device_whitelist.begin(), device_whitelist.end()};
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700128
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700129 options.config_options.defaults = base::FilePath{FLAGS_config_path};
130 options.config_options.settings = base::FilePath{FLAGS_state_path};
131 options.config_options.definitions = base::FilePath{"/etc/weaved"};
132 options.config_options.test_definitions =
133 base::FilePath{FLAGS_test_definitions_path};
134 options.config_options.disable_security = FLAGS_disable_security;
135 options.config_options.test_privet_ssid = FLAGS_test_privet_ssid;
136
137 buffet::Daemon daemon{options};
Alex Vakulenko79e6a282014-09-08 17:07:19 -0700138 return daemon.Run();
Chris Sosa5bac9492014-03-24 11:18:54 -0700139}