1 // 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.
14
15 #include <string>
16
17 #include <signal.h>
18 #include <sysexits.h>
19
20 #include <base/files/file_path.h>
21 #include <binderwrapper/binder_wrapper.h>
22 #include <brillo/binder_watcher.h>
23 #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>
29
30 #include "buffet/buffet_config.h"
31 #include "buffet/dbus_constants.h"
32 #include "buffet/manager.h"
33 #include "common/binder_constants.h"
34
35 using brillo::dbus_utils::AsyncEventSequencer;
36 using brillo::DBusServiceDaemon;
37 using buffet::dbus_constants::kServiceName;
38 using buffet::dbus_constants::kRootServicePath;
39
40 namespace buffet {
41
42 class Daemon final : public DBusServiceDaemon {
43 public:
Daemon(const Manager::Options & options)44 explicit Daemon(const Manager::Options& options)
45 : DBusServiceDaemon(kServiceName, kRootServicePath), options_{options} {}
46
47 protected:
OnInit()48 int OnInit() override {
49 android::BinderWrapper::Create();
50 if (!binder_watcher_.Init())
51 return EX_OSERR;
52
53 return brillo::DBusServiceDaemon::OnInit();
54 }
55
RegisterDBusObjectsAsync(AsyncEventSequencer * sequencer)56 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
57 manager_ = new Manager{options_, bus_};
58 android::BinderWrapper::Get()->RegisterService(
59 weaved::binder::kWeaveServiceName,
60 android::IInterface::asBinder(manager_));
61 manager_->Start(sequencer);
62 }
63
OnShutdown(int * return_code)64 void OnShutdown(int* return_code) override { manager_->Stop(); }
65
66 private:
67 Manager::Options options_;
68 brillo::BinderWatcher binder_watcher_;
69 android::sp<buffet::Manager> manager_;
70
71 DISALLOW_COPY_AND_ASSIGN(Daemon);
72 };
73
74 } // namespace buffet
75
76 namespace {
77
78 const char kDefaultConfigFilePath[] = "/etc/weaved/weaved.conf";
79 const char kDefaultStateFilePath[] = "/data/misc/weaved/device_reg_info";
80
81 } // namespace
82
main(int argc,char * argv[])83 int main(int argc, char* argv[]) {
84 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
85 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.");
89 DEFINE_bool(enable_xmpp, true,
90 "Connect to GCD via a persistent XMPP connection.");
91 DEFINE_bool(disable_privet, false, "disable Privet protocol");
92 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).");
96
97 DEFINE_string(test_privet_ssid, "",
98 "Fixed SSID for WiFi bootstrapping. For test only.");
99 DEFINE_string(test_definitions_path, "",
100 "Path to directory containing additional command "
101 "and state definitions. For test only.");
102
103 brillo::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
104 if (FLAGS_config_path.empty())
105 FLAGS_config_path = kDefaultConfigFilePath;
106 if (FLAGS_state_path.empty())
107 FLAGS_state_path = kDefaultStateFilePath;
108 int flags = brillo::kLogToSyslog | brillo::kLogHeader;
109 if (FLAGS_log_to_stderr)
110 flags |= brillo::kLogToStderr;
111 brillo::InitLog(flags);
112
113 auto device_whitelist =
114 brillo::string_utils::Split(FLAGS_device_whitelist, ",", true, true);
115
116 // We are handling write errors on closed sockets correctly and not relying on
117 // (nor handling) SIGPIPE signal, which just kills the process.
118 // Mark it to be ignored.
119 signal(SIGPIPE, SIG_IGN);
120
121 buffet::Manager::Options options;
122 options.xmpp_enabled = FLAGS_enable_xmpp;
123 options.disable_privet = FLAGS_disable_privet;
124 options.enable_ping = FLAGS_enable_ping;
125 options.device_whitelist = {device_whitelist.begin(), device_whitelist.end()};
126
127 options.config_options.defaults = base::FilePath{FLAGS_config_path};
128 options.config_options.settings = base::FilePath{FLAGS_state_path};
129 options.config_options.definitions = base::FilePath{"/etc/weaved"};
130 options.config_options.test_definitions =
131 base::FilePath{FLAGS_test_definitions_path};
132 options.config_options.test_privet_ssid = FLAGS_test_privet_ssid;
133
134 buffet::Daemon daemon{options};
135 return daemon.Run();
136 }
137