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