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