Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 1 | // Copyright 2018 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 "ml/daemon.h" |
| 6 | |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 7 | #include <sysexits.h> |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/bind.h> |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 13 | #include <base/files/file_util.h> |
| 14 | #include <base/memory/ptr_util.h> |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 15 | #include <base/memory/ref_counted.h> |
| 16 | #include <chromeos/dbus/service_constants.h> |
| 17 | #include <dbus/bus.h> |
| 18 | #include <dbus/message.h> |
Qijiang Fan | 7da812d | 2019-11-26 10:48:46 +0900 | [diff] [blame] | 19 | #include <mojo/core/embedder/embedder.h> |
Qijiang Fan | 3b917c7 | 2019-12-09 15:24:15 +0900 | [diff] [blame] | 20 | #include <mojo/public/cpp/system/invitation.h> |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 21 | |
| 22 | #include "ml/machine_learning_service_impl.h" |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 23 | |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 24 | namespace ml { |
| 25 | |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 26 | Daemon::Daemon() : weak_ptr_factory_(this) {} |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 27 | |
| 28 | Daemon::~Daemon() {} |
| 29 | |
| 30 | int Daemon::OnInit() { |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 31 | int exit_code = DBusDaemon::OnInit(); |
| 32 | if (exit_code != EX_OK) |
| 33 | return exit_code; |
| 34 | |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 35 | metrics_.StartCollectingProcessMetrics(); |
Qijiang Fan | 7da812d | 2019-11-26 10:48:46 +0900 | [diff] [blame] | 36 | mojo::core::Init(); |
| 37 | ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>( |
| 38 | base::ThreadTaskRunnerHandle::Get(), |
| 39 | mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST); |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 40 | InitDBus(); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 41 | |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | void Daemon::InitDBus() { |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 46 | // Get or create the ExportedObject for the ML service. |
| 47 | dbus::ExportedObject* const ml_service_exported_object = |
Andrew Moylan | 49f50b3 | 2018-07-27 08:48:11 +1000 | [diff] [blame] | 48 | bus_->GetExportedObject(dbus::ObjectPath(kMachineLearningServicePath)); |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 49 | CHECK(ml_service_exported_object); |
| 50 | |
| 51 | // Register a handler of the BootstrapMojoConnection method. |
| 52 | CHECK(ml_service_exported_object->ExportMethodAndBlock( |
Andrew Moylan | 49f50b3 | 2018-07-27 08:48:11 +1000 | [diff] [blame] | 53 | kMachineLearningInterfaceName, kBootstrapMojoConnectionMethod, |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 54 | base::Bind(&Daemon::BootstrapMojoConnection, |
| 55 | weak_ptr_factory_.GetWeakPtr()))); |
| 56 | |
| 57 | // Take ownership of the ML service. |
Andrew Moylan | 49f50b3 | 2018-07-27 08:48:11 +1000 | [diff] [blame] | 58 | CHECK(bus_->RequestOwnershipAndBlock(kMachineLearningServiceName, |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 59 | dbus::Bus::REQUIRE_PRIMARY)); |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void Daemon::BootstrapMojoConnection( |
| 63 | dbus::MethodCall* method_call, |
| 64 | dbus::ExportedObject::ResponseSender response_sender) { |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 65 | metrics_.RecordMojoConnectionEvent( |
| 66 | Metrics::MojoConnectionEvent::kBootstrapRequested); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 67 | if (machine_learning_service_) { |
| 68 | LOG(ERROR) << "MachineLearningService already instantiated"; |
| 69 | response_sender.Run(dbus::ErrorResponse::FromMethodCall( |
| 70 | method_call, DBUS_ERROR_FAILED, "Bootstrap already completed")); |
| 71 | return; |
| 72 | } |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 73 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 74 | base::ScopedFD file_handle; |
| 75 | dbus::MessageReader reader(method_call); |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 76 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 77 | if (!reader.PopFileDescriptor(&file_handle)) { |
| 78 | LOG(ERROR) << "Couldn't extract file descriptor from D-Bus call"; |
| 79 | response_sender.Run(dbus::ErrorResponse::FromMethodCall( |
| 80 | method_call, DBUS_ERROR_INVALID_ARGS, "Expected file descriptor")); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if (!file_handle.is_valid()) { |
| 85 | LOG(ERROR) << "ScopedFD extracted from D-Bus call was invalid (i.e. empty)"; |
| 86 | response_sender.Run(dbus::ErrorResponse::FromMethodCall( |
| 87 | method_call, DBUS_ERROR_INVALID_ARGS, |
| 88 | "Invalid (empty) file descriptor")); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | if (!base::SetCloseOnExec(file_handle.get())) { |
| 93 | PLOG(ERROR) << "Failed setting FD_CLOEXEC on file descriptor"; |
| 94 | response_sender.Run(dbus::ErrorResponse::FromMethodCall( |
| 95 | method_call, DBUS_ERROR_FAILED, |
| 96 | "Failed setting FD_CLOEXEC on file descriptor")); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Connect to mojo in the requesting process. |
Qijiang Fan | 3b917c7 | 2019-12-09 15:24:15 +0900 | [diff] [blame] | 101 | mojo::IncomingInvitation invitation = |
| 102 | mojo::IncomingInvitation::Accept(mojo::PlatformChannelEndpoint( |
| 103 | mojo::PlatformHandle(std::move(file_handle)))); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 104 | |
| 105 | // Bind primordial message pipe to a MachineLearningService implementation. |
| 106 | machine_learning_service_ = base::MakeUnique<MachineLearningServiceImpl>( |
Qijiang Fan | 3b917c7 | 2019-12-09 15:24:15 +0900 | [diff] [blame] | 107 | invitation.ExtractMessagePipe(kBootstrapMojoConnectionChannelToken), |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 108 | base::Bind(&Daemon::OnConnectionError, base::Unretained(this))); |
| 109 | |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 110 | metrics_.RecordMojoConnectionEvent( |
| 111 | Metrics::MojoConnectionEvent::kBootstrapSucceeded); |
| 112 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 113 | // Send success response. |
| 114 | response_sender.Run(dbus::Response::FromMethodCall(method_call)); |
| 115 | } |
| 116 | |
| 117 | void Daemon::OnConnectionError() { |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 118 | metrics_.RecordMojoConnectionEvent( |
| 119 | Metrics::MojoConnectionEvent::kConnectionError); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 120 | // Die upon Mojo error. Reconnection can occur when the daemon is restarted. |
| 121 | // (A future Mojo API may enable Mojo re-bootstrap without a process restart.) |
| 122 | Quit(); |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } // namespace ml |