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