blob: dca6ae24eb22dab4c55ff352f390fea24e93ae70 [file] [log] [blame]
Ken Turneredb93932018-02-14 05:27:31 +11001// 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 Moylanef116f92018-04-11 15:12:38 +10007#include <sysexits.h>
8
9#include <memory>
10#include <utility>
11
12#include <base/bind.h>
Qijiang Fan713061e2021-03-08 15:45:12 +090013#include <base/check.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100014#include <base/files/file_util.h>
Andrew Moylanef116f92018-04-11 15:12:38 +100015#include <base/memory/ref_counted.h>
16#include <chromeos/dbus/service_constants.h>
17#include <dbus/bus.h>
18#include <dbus/message.h>
Qijiang Fan7da812d2019-11-26 10:48:46 +090019#include <mojo/core/embedder/embedder.h>
Qijiang Fan3b917c72019-12-09 15:24:15 +090020#include <mojo/public/cpp/system/invitation.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100021
Charles Zhaod4fb7b62020-08-25 17:21:58 +100022#include "ml/dlcservice_client.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100023#include "ml/machine_learning_service_impl.h"
Andrew Moylanef116f92018-04-11 15:12:38 +100024
Ken Turneredb93932018-02-14 05:27:31 +110025namespace ml {
26
Andrew Moylanef116f92018-04-11 15:12:38 +100027Daemon::Daemon() : weak_ptr_factory_(this) {}
Ken Turneredb93932018-02-14 05:27:31 +110028
29Daemon::~Daemon() {}
30
31int Daemon::OnInit() {
Andrew Moylanef116f92018-04-11 15:12:38 +100032 int exit_code = DBusDaemon::OnInit();
33 if (exit_code != EX_OK)
34 return exit_code;
35
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100036 metrics_.StartCollectingProcessMetrics();
Qijiang Fan7da812d2019-11-26 10:48:46 +090037 mojo::core::Init();
38 ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
39 base::ThreadTaskRunnerHandle::Get(),
40 mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
Ken Turneredb93932018-02-14 05:27:31 +110041 InitDBus();
Andrew Moylanff6be512018-07-03 11:05:01 +100042
Ken Turneredb93932018-02-14 05:27:31 +110043 return 0;
44}
45
46void Daemon::InitDBus() {
Andrew Moylanef116f92018-04-11 15:12:38 +100047 // Get or create the ExportedObject for the ML service.
48 dbus::ExportedObject* const ml_service_exported_object =
Andrew Moylan49f50b32018-07-27 08:48:11 +100049 bus_->GetExportedObject(dbus::ObjectPath(kMachineLearningServicePath));
Andrew Moylanef116f92018-04-11 15:12:38 +100050 CHECK(ml_service_exported_object);
51
52 // Register a handler of the BootstrapMojoConnection method.
53 CHECK(ml_service_exported_object->ExportMethodAndBlock(
Andrew Moylan49f50b32018-07-27 08:48:11 +100054 kMachineLearningInterfaceName, kBootstrapMojoConnectionMethod,
Andrew Moylanef116f92018-04-11 15:12:38 +100055 base::Bind(&Daemon::BootstrapMojoConnection,
56 weak_ptr_factory_.GetWeakPtr())));
57
58 // Take ownership of the ML service.
Andrew Moylan49f50b32018-07-27 08:48:11 +100059 CHECK(bus_->RequestOwnershipAndBlock(kMachineLearningServiceName,
Andrew Moylanef116f92018-04-11 15:12:38 +100060 dbus::Bus::REQUIRE_PRIMARY));
Ken Turneredb93932018-02-14 05:27:31 +110061}
62
63void Daemon::BootstrapMojoConnection(
64 dbus::MethodCall* method_call,
65 dbus::ExportedObject::ResponseSender response_sender) {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100066 metrics_.RecordMojoConnectionEvent(
67 Metrics::MojoConnectionEvent::kBootstrapRequested);
Andrew Moylanff6be512018-07-03 11:05:01 +100068 if (machine_learning_service_) {
69 LOG(ERROR) << "MachineLearningService already instantiated";
Qijiang Fan795b81f2020-07-06 12:36:18 +090070 std::move(response_sender)
71 .Run(dbus::ErrorResponse::FromMethodCall(
72 method_call, DBUS_ERROR_FAILED, "Bootstrap already completed"));
Andrew Moylanff6be512018-07-03 11:05:01 +100073 return;
74 }
Andrew Moylanef116f92018-04-11 15:12:38 +100075
Andrew Moylanff6be512018-07-03 11:05:01 +100076 base::ScopedFD file_handle;
77 dbus::MessageReader reader(method_call);
Andrew Moylanef116f92018-04-11 15:12:38 +100078
Andrew Moylanff6be512018-07-03 11:05:01 +100079 if (!reader.PopFileDescriptor(&file_handle)) {
80 LOG(ERROR) << "Couldn't extract file descriptor from D-Bus call";
Qijiang Fan795b81f2020-07-06 12:36:18 +090081 std::move(response_sender)
82 .Run(dbus::ErrorResponse::FromMethodCall(
83 method_call, DBUS_ERROR_INVALID_ARGS, "Expected file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +100084 return;
85 }
86
87 if (!file_handle.is_valid()) {
88 LOG(ERROR) << "ScopedFD extracted from D-Bus call was invalid (i.e. empty)";
Qijiang Fan795b81f2020-07-06 12:36:18 +090089 std::move(response_sender)
90 .Run(dbus::ErrorResponse::FromMethodCall(
91 method_call, DBUS_ERROR_INVALID_ARGS,
92 "Invalid (empty) file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +100093 return;
94 }
95
96 if (!base::SetCloseOnExec(file_handle.get())) {
97 PLOG(ERROR) << "Failed setting FD_CLOEXEC on file descriptor";
Qijiang Fan795b81f2020-07-06 12:36:18 +090098 std::move(response_sender)
99 .Run(dbus::ErrorResponse::FromMethodCall(
100 method_call, DBUS_ERROR_FAILED,
101 "Failed setting FD_CLOEXEC on file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +1000102 return;
103 }
104
105 // Connect to mojo in the requesting process.
Qijiang Fan3b917c72019-12-09 15:24:15 +0900106 mojo::IncomingInvitation invitation =
107 mojo::IncomingInvitation::Accept(mojo::PlatformChannelEndpoint(
108 mojo::PlatformHandle(std::move(file_handle))));
Andrew Moylanff6be512018-07-03 11:05:01 +1000109
110 // Bind primordial message pipe to a MachineLearningService implementation.
hscham3f45fcc2019-12-25 16:54:54 +0900111 machine_learning_service_ = std::make_unique<MachineLearningServiceImpl>(
Qijiang Fan3b917c72019-12-09 15:24:15 +0900112 invitation.ExtractMessagePipe(kBootstrapMojoConnectionChannelToken),
Charles Zhaod4fb7b62020-08-25 17:21:58 +1000113 base::Bind(&Daemon::OnMojoDisconnection, base::Unretained(this)),
114 bus_.get());
Andrew Moylanff6be512018-07-03 11:05:01 +1000115
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000116 metrics_.RecordMojoConnectionEvent(
117 Metrics::MojoConnectionEvent::kBootstrapSucceeded);
118
Andrew Moylanff6be512018-07-03 11:05:01 +1000119 // Send success response.
Qijiang Fan795b81f2020-07-06 12:36:18 +0900120 std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call));
Andrew Moylanff6be512018-07-03 11:05:01 +1000121}
122
Andrew Moylanb481af72020-07-09 15:22:00 +1000123void Daemon::OnMojoDisconnection() {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000124 metrics_.RecordMojoConnectionEvent(
Andrew Moylanb481af72020-07-09 15:22:00 +1000125 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 Moylanff6be512018-07-03 11:05:01 +1000129 Quit();
Ken Turneredb93932018-02-14 05:27:31 +1100130}
131
132} // namespace ml