Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Prashant Malani | 0655855 | 2017-08-21 13:45:38 -0700 | [diff] [blame] | 5 | #include "midis/daemon.h" |
| 6 | |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | |
Ben Chan | d496e61 | 2017-09-29 00:20:50 -0700 | [diff] [blame] | 9 | #include <memory> |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 13 | #include <base/check.h> |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 14 | #include <base/files/file_util.h> |
Prashant Malani | 0655855 | 2017-08-21 13:45:38 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 16 | #include <dbus/bus.h> |
| 17 | #include <dbus/message.h> |
Prashant Malani | d91f489 | 2017-03-22 14:06:44 -0700 | [diff] [blame] | 18 | |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 19 | #include "midis/client_tracker.h" |
| 20 | #include "midis/device_tracker.h" |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 21 | |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 22 | namespace midis { |
| 23 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 24 | Daemon::Daemon() |
Ben Chan | d496e61 | 2017-09-29 00:20:50 -0700 | [diff] [blame] | 25 | : device_tracker_(std::make_unique<DeviceTracker>()), |
| 26 | client_tracker_(std::make_unique<ClientTracker>()), |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 27 | weak_factory_(this) {} |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 28 | |
| 29 | Daemon::~Daemon() {} |
| 30 | |
| 31 | int Daemon::OnInit() { |
| 32 | if (!device_tracker_->InitDeviceTracker()) { |
| 33 | return -1; |
| 34 | } |
| 35 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 36 | // It's OK to set |device_tracker_| here because the D-Bus connection |
| 37 | // has not yet been initialized, so it is impossible for clients to be |
| 38 | // registered. |
| 39 | client_tracker_->SetDeviceTracker(device_tracker_.get()); |
| 40 | client_tracker_->InitClientTracker(); |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 41 | InitDBus(); |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 45 | void Daemon::InitDBus() { |
| 46 | dbus::Bus::Options options; |
| 47 | options.bus_type = dbus::Bus::SYSTEM; |
| 48 | scoped_refptr<dbus::Bus> bus(new dbus::Bus(options)); |
| 49 | CHECK(bus->Connect()); |
| 50 | dbus::ExportedObject* exported_object = |
| 51 | bus->GetExportedObject(dbus::ObjectPath(kMidisServicePath)); |
| 52 | |
| 53 | CHECK(exported_object); |
| 54 | CHECK(exported_object->ExportMethodAndBlock( |
Tom Hughes | aebcdce | 2020-08-27 15:18:39 -0700 | [diff] [blame] | 55 | kMidisInterfaceName, kBootstrapMojoConnectionMethod, |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 56 | base::Bind(&Daemon::BootstrapMojoConnection, |
| 57 | weak_factory_.GetWeakPtr()))); |
| 58 | CHECK(bus->RequestOwnershipAndBlock(kMidisServiceName, |
| 59 | dbus::Bus::REQUIRE_PRIMARY)); |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 60 | VLOG(1) << "D-Bus Registration succeeded"; |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void Daemon::BootstrapMojoConnection( |
| 64 | dbus::MethodCall* method_call, |
| 65 | dbus::ExportedObject::ResponseSender response_sender) { |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 66 | LOG(INFO) << "Successfully received call from D-Bus client."; |
| 67 | if (client_tracker_->IsProxyConnected()) { |
| 68 | LOG(WARNING) << "Midis can only instantiate one Mojo Proxy instance."; |
| 69 | return; |
| 70 | } |
| 71 | |
Eric Caruso | 92f668e | 2018-03-30 15:12:41 -0700 | [diff] [blame] | 72 | base::ScopedFD file_handle; |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 73 | dbus::MessageReader reader(method_call); |
| 74 | |
| 75 | if (!reader.PopFileDescriptor(&file_handle)) { |
| 76 | LOG(ERROR) << "Couldn't extract Mojo IPC handle."; |
| 77 | return; |
| 78 | } |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 79 | |
Eric Caruso | 92f668e | 2018-03-30 15:12:41 -0700 | [diff] [blame] | 80 | if (!file_handle.is_valid()) { |
| 81 | LOG(ERROR) << "Couldn't get file handle sent over D-Bus."; |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
Eric Caruso | 92f668e | 2018-03-30 15:12:41 -0700 | [diff] [blame] | 85 | if (!base::SetCloseOnExec(file_handle.get())) { |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 86 | PLOG(ERROR) << "Failed setting FD_CLOEXEC on fd."; |
| 87 | return; |
| 88 | } |
| 89 | |
Eric Caruso | 92f668e | 2018-03-30 15:12:41 -0700 | [diff] [blame] | 90 | client_tracker_->AcceptProxyConnection(std::move(file_handle)); |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame] | 91 | LOG(INFO) << "MojoBridger connection established."; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 92 | std::unique_ptr<dbus::Response> response = |
| 93 | dbus::Response::FromMethodCall(method_call); |
Qijiang Fan | affa580 | 2020-08-03 13:57:01 +0900 | [diff] [blame] | 94 | std::move(response_sender).Run(std::move(response)); |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 97 | } // namespace midis |